add.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <template>
  2. <view class="content">
  3. <view class="title"> 新增项目 </view>
  4. <uni-forms
  5. :modelValue="formData"
  6. label-position="left"
  7. class="form"
  8. ref="form"
  9. :rules="rules"
  10. >
  11. <uni-forms-item required label="项目名称" name="project_name">
  12. <uni-easyinput
  13. type="text"
  14. v-model="formData.project_name"
  15. class="input"
  16. />
  17. </uni-forms-item>
  18. <uni-forms-item label="项目类别" name="type" required>
  19. <picker
  20. @change="selectChangeType"
  21. :range="typeList"
  22. :range-key="'name'"
  23. :value="
  24. formData.type &&
  25. typeList.findIndex((item) => item.id == formData.type.id)
  26. "
  27. >
  28. <view class="select">
  29. {{
  30. formData.type && `${formData.type.name}(${formData.type.code})`
  31. }}
  32. </view>
  33. </picker>
  34. </uni-forms-item>
  35. <uni-forms-item label="流程" name="form">
  36. <picker disabled>
  37. <view class="select">
  38. {{
  39. formData.type && (formData.type.id == 7 ? "研发立项" : "销售立项")
  40. }}
  41. </view>
  42. </picker>
  43. </uni-forms-item>
  44. <uni-forms-item
  45. label="行业名称"
  46. name="industry"
  47. v-show="!formData.type || (formData.type && formData.type.id != 7)"
  48. required
  49. >
  50. <picker
  51. @change="selectChangeIndustry"
  52. :range="industryList"
  53. :range-key="'name'"
  54. :value="
  55. formData.industry &&
  56. typeList.findIndex((item) => item.id == formData.industry.id)
  57. "
  58. >
  59. <view class="select">
  60. {{
  61. formData.industry &&
  62. `${formData.industry.name}(${formData.industry.code})`
  63. }}
  64. </view>
  65. </picker>
  66. </uni-forms-item>
  67. <uni-forms-item
  68. label="项目地区"
  69. name="location"
  70. v-show="!formData.type || (formData.type && formData.type.id != 7)"
  71. required
  72. >
  73. <picker
  74. mode="multiSelector"
  75. @columnchange="bindChangeLocationColumn"
  76. @change="selectChangeLocation"
  77. :range="locationList"
  78. :range-key="'name'"
  79. :value="locationIndex"
  80. >
  81. <view class="select">
  82. {{
  83. formData.location &&
  84. `${formData.location.name}(${formData.code.location})`
  85. }}
  86. </view>
  87. </picker>
  88. </uni-forms-item>
  89. <uni-forms-item
  90. label="项目简称"
  91. name="name"
  92. v-show="!formData.type || (formData.type && formData.type.id != 7)"
  93. required
  94. >
  95. <uni-easyinput
  96. type="text"
  97. v-model="formData.name"
  98. class="input"
  99. :clearable="false"
  100. @blur="onChangeName"
  101. />
  102. </uni-forms-item>
  103. <uni-forms-item
  104. label="项目期数"
  105. name="version"
  106. v-show="!formData.type || (formData.type && formData.type.id != 7)"
  107. required
  108. >
  109. <picker
  110. @change="selectChangeVersion"
  111. :range="versionList"
  112. :value="formData.version && formData.version - 1"
  113. >
  114. <view class="select">
  115. {{ versionList[this.formData.version - 1] }}
  116. </view>
  117. </picker>
  118. </uni-forms-item>
  119. <uni-forms-item
  120. label="项目编号"
  121. name="code"
  122. v-show="!formData.type || (formData.type && formData.type.id != 7)"
  123. >
  124. <view>
  125. {{
  126. `${formData.code.type}-${formData.code.industry}-${formData.code.location}-${formData.code.name}-${formData.code.version}`
  127. }}
  128. </view>
  129. </uni-forms-item>
  130. </uni-forms>
  131. <view class="group">
  132. <button @click="onHandleSubmit(0)" class="commit">取消</button>
  133. <button @click="onHandleSubmit(1)" type="primary" class="commit">
  134. 确定
  135. </button>
  136. </view>
  137. </view>
  138. </template>
  139. <script>
  140. import {
  141. queryProjectType,
  142. queryIndustry,
  143. queryFlow,
  144. createApproval,
  145. updateApproval,
  146. } from "@/services/project";
  147. import provinces from "./provinces";
  148. import { mapState } from "vuex";
  149. const versionList = ["一期", "二期", "三期", "四期", "五期"];
  150. export default {
  151. data() {
  152. return {
  153. formData: {
  154. project_name: null,
  155. type: null,
  156. flow: null,
  157. industry: null,
  158. location: null,
  159. name: null,
  160. version: null,
  161. code: {
  162. type: "***",
  163. industry: "***",
  164. location: "***",
  165. name: "***",
  166. version: "*",
  167. },
  168. },
  169. rules: {
  170. project_name: {
  171. rules: [
  172. {
  173. required: true,
  174. errorMessage: "请输入项目名称",
  175. },
  176. ],
  177. },
  178. type: {
  179. rules: [
  180. {
  181. required: true,
  182. errorMessage: "请选择项目类别",
  183. },
  184. ],
  185. },
  186. industry: {
  187. rules: [
  188. {
  189. required: false,
  190. errorMessage: "请选择行业名称",
  191. },
  192. ],
  193. },
  194. location: {
  195. rules: [
  196. {
  197. required: false,
  198. errorMessage: "请选择地区",
  199. },
  200. ],
  201. },
  202. name: {
  203. rules: [
  204. {
  205. required: false,
  206. errorMessage: "请输入项目简称",
  207. },
  208. {
  209. maxLength: 3,
  210. errorMessage: "项目简称不能超过3位",
  211. },
  212. {
  213. validateFunction: function (rule, value, data, callback) {
  214. if (value.match(/[^A-Za-z]/g)) {
  215. callback("项目简称只能是英文字符");
  216. } else {
  217. callback();
  218. }
  219. },
  220. },
  221. ],
  222. },
  223. version: {
  224. rules: [
  225. {
  226. required: false,
  227. errorMessage: "请选择期数",
  228. },
  229. ],
  230. },
  231. },
  232. project_id: 0,
  233. typeList: [],
  234. industryList: [],
  235. flowList: [],
  236. versionList,
  237. provinces,
  238. locationList: [provinces, []],
  239. locationIndex: [0, 0],
  240. };
  241. },
  242. computed: {
  243. ...mapState(["currentProject"]),
  244. },
  245. onLoad(options) {
  246. if (options.project_id) {
  247. this.project_id = options.project_id;
  248. }
  249. this.init(options);
  250. },
  251. onReady() {
  252. this.$refs.form.setRules(this.rules);
  253. },
  254. methods: {
  255. onHandleSubmit(value) {
  256. if (value) {
  257. this.$refs.form.validate(async (err, formData) => {
  258. if (!err) {
  259. console.log("success", formData);
  260. let payload = {
  261. project_name: this.formData.project_name,
  262. type_id: Number(this.formData.type.id),
  263. flow_id: Number(this.formData.flow.id),
  264. node_id: this.flowList.find(
  265. (item) => item.id == this.formData.flow.id
  266. ).Nodes[0].id,
  267. };
  268. if (formData.type != 7) {
  269. payload = {
  270. ...payload,
  271. industry_id: Number(this.formData.industry.id),
  272. location: this.formData.location.name,
  273. location_code: this.formData.location.code,
  274. name: this.formData.name,
  275. version: this.formData.version + "",
  276. project_full_code:
  277. this.formData.code.type +
  278. this.formData.code.industry +
  279. this.formData.code.location +
  280. this.formData.code.name +
  281. this.formData.code.version,
  282. };
  283. }
  284. if (this.project_id == 0) {
  285. await createApproval(payload);
  286. } else {
  287. payload.id = Number(this.project_id);
  288. await updateApproval(payload);
  289. }
  290. uni.navigateTo({
  291. url: "./list",
  292. });
  293. }
  294. });
  295. } else uni.navigateTo({ url: "./list" });
  296. },
  297. async init(options) {
  298. let res;
  299. res = await queryProjectType();
  300. this.typeList = res.data;
  301. res = await queryIndustry();
  302. this.industryList = res.data;
  303. res = await queryFlow();
  304. this.flowList = res.data;
  305. let form = this.formData;
  306. let code = this.formData.code;
  307. let current = this.currentProject;
  308. if (options.project_id) {
  309. form.project_name = current.project_name;
  310. form.type = this.typeList.find(
  311. (item) => item.id == this.currentProject.type_id
  312. );
  313. form.flow = this.flowList.find(
  314. (item) => item.id == this.currentProject.flow_id
  315. );
  316. if (current.type_id != 7) {
  317. form.industry = this.industryList.find(
  318. (item) => item.id == this.currentProject.industry_id
  319. );
  320. form.location = {
  321. name: current.location,
  322. code: current.location_code,
  323. };
  324. form.name = current.name;
  325. form.version = current.version;
  326. code.type = form.type.code;
  327. code.industry = form.industry.code;
  328. code.location = form.location.code;
  329. code.name = form.name;
  330. code.version = form.version;
  331. const searchLocation = (list, name) => {
  332. for (let i = 0; i < list.length; ++i) {
  333. if (list[i].children) {
  334. let childrenIndex = searchLocation(list[i].children, name);
  335. if (childrenIndex[0] != -1) return [i, childrenIndex[0]];
  336. } else {
  337. if (list[i].name == name) return [i, 0];
  338. }
  339. }
  340. return [-1, -1];
  341. };
  342. this.locationIndex = searchLocation(this.provinces, current.location);
  343. if (this.provinces[this.locationIndex[0]].children)
  344. this.locationList[1] =
  345. this.provinces[this.locationIndex[0]].children;
  346. }
  347. }
  348. },
  349. binddata(name, value) {
  350. this.$refs.form.setValue(name, value);
  351. },
  352. selectChangeType(e) {
  353. let form = this.formData;
  354. let code = this.formData.code;
  355. form.type = this.typeList[e.target.value];
  356. code.type = form.type.code;
  357. this.binddata("type", e.target.value);
  358. if (form.type.id == 7) {
  359. form.flow = this.flowList.find((item) => item.id == 4);
  360. form.industry = null;
  361. form.location = null;
  362. form.name = null;
  363. form.version = null;
  364. code.industry = "***";
  365. code.location = "***";
  366. code.name = "***";
  367. code.version = "*";
  368. } else form.flow = this.flowList.find((item) => item.id == 1);
  369. },
  370. selectChangeIndustry(e) {
  371. this.formData.industry = this.industryList[e.target.value];
  372. this.formData.code.industry = this.formData.industry.code;
  373. this.binddata("industry", e.target.value);
  374. },
  375. onChangeName(e) {
  376. let name = e.detail.value.toUpperCase();
  377. while (name.length < 3) name += "V";
  378. this.formData.name = name;
  379. this.formData.code.name = name;
  380. },
  381. selectChangeVersion(e) {
  382. this.formData.version = e.target.value + 1;
  383. this.formData.code.version = this.formData.version + "";
  384. this.binddata("version", e.target.value);
  385. },
  386. bindChangeLocationColumn(e) {
  387. if (e.target.column == 0) {
  388. this.locationList[1] = this.provinces[e.target.value].children || [];
  389. this.locationIndex = [e.target.value, 0];
  390. }
  391. },
  392. selectChangeLocation(e) {
  393. let multiIndex = e.target.value;
  394. this.locationIndex = multiIndex;
  395. // let location = this.formData.location;
  396. // let locationCode = this.formData.code.location;
  397. let location = this.provinces[multiIndex[0]].children
  398. ? this.provinces[multiIndex[0]].children[multiIndex[1]]
  399. : this.provinces[multiIndex[0]];
  400. location.code =
  401. location.code.length == 4 ? location.code.substr(1) : location.code;
  402. this.formData.location = location;
  403. this.formData.code.location = location.code;
  404. },
  405. },
  406. };
  407. </script>
  408. <style lang="less" scoped>
  409. .content {
  410. display: flex;
  411. flex-wrap: wrap;
  412. }
  413. .title {
  414. width: 100%;
  415. padding: 0 20px 20px 20px;
  416. background: #f8f8f8;
  417. font: 24px bold;
  418. }
  419. .form {
  420. margin: 20px 10% 0 10%;
  421. width: 100%;
  422. }
  423. .input {
  424. border: 1px solid #666;
  425. }
  426. .select {
  427. width: 100%;
  428. height: 72rpx;
  429. line-height: 70rpx;
  430. border: 1px solid #666;
  431. padding-left: 20rpx;
  432. }
  433. .group {
  434. width: 100%;
  435. display: flex;
  436. position: fixed;
  437. flex-wrap: wrap;
  438. justify-content: flex-start;
  439. bottom: 0;
  440. left: 0;
  441. .commit {
  442. width: 50%;
  443. border-radius: 0;
  444. margin: inherit;
  445. }
  446. }
  447. </style>