add.vue 14 KB

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