add.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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="flow">
  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>
  45. <uni-forms
  46. :modelValue="formData"
  47. label-position="left"
  48. class="form"
  49. ref="fullForm"
  50. :rules="rules"
  51. v-show="!formData.type || (formData.type && formData.type.id != 7)"
  52. >
  53. <uni-forms-item label="行业名称" name="industry" required>
  54. <picker
  55. @change="selectChangeIndustry"
  56. :range="industryList"
  57. :range-key="'name'"
  58. :value="
  59. formData.industry &&
  60. typeList.findIndex((item) => item.id == formData.industry.id)
  61. "
  62. >
  63. <view class="select">
  64. {{
  65. formData.industry &&
  66. `${formData.industry.name}(${formData.industry.code})`
  67. }}
  68. </view>
  69. </picker>
  70. </uni-forms-item>
  71. <uni-forms-item label="项目地区" name="location" required>
  72. <picker
  73. mode="multiSelector"
  74. @columnchange="bindChangeLocationColumn"
  75. @change="selectChangeLocation"
  76. :range="locationList"
  77. :range-key="'name'"
  78. :value="locationIndex"
  79. >
  80. <view class="select">
  81. {{
  82. formData.location &&
  83. `${formData.location.name}(${formData.code.location})`
  84. }}
  85. </view>
  86. </picker>
  87. </uni-forms-item>
  88. <uni-forms-item label="项目简称" name="name" required>
  89. <uni-easyinput
  90. type="text"
  91. v-model="formData.name"
  92. class="input"
  93. :clearable="false"
  94. @blur="onChangeName"
  95. />
  96. </uni-forms-item>
  97. <uni-forms-item label="项目期数" name="version" required>
  98. <picker
  99. @change="selectChangeVersion"
  100. :range="versionList"
  101. :value="formData.version && formData.version - 1"
  102. >
  103. <view class="select">
  104. {{ versionList[this.formData.version - 1] }}
  105. </view>
  106. </picker>
  107. </uni-forms-item>
  108. <uni-forms-item label="项目编号" name="code">
  109. <view>
  110. {{
  111. `${formData.code.type}-${formData.code.industry}-${formData.code.location}-${formData.code.name}-${formData.code.version}`
  112. }}
  113. </view>
  114. </uni-forms-item>
  115. </uni-forms>
  116. <view class="group">
  117. <button @click="onHandleSubmit(0)" class="commit">取消</button>
  118. <button @click="onHandleSubmit(1)" type="primary" class="commit">
  119. 确定
  120. </button>
  121. </view>
  122. </view>
  123. </template>
  124. <script>
  125. import {
  126. queryProjectType,
  127. queryIndustry,
  128. queryFlow,
  129. createApproval,
  130. updateApproval,
  131. } from "@/services/project";
  132. import provinces from "./provinces";
  133. import { mapState } from "vuex";
  134. const versionList = ["一期", "二期", "三期", "四期", "五期"];
  135. export default {
  136. data() {
  137. return {
  138. formData: {
  139. project_name: null,
  140. type: null,
  141. flow: null,
  142. industry: null,
  143. location: null,
  144. name: null,
  145. version: null,
  146. code: {
  147. type: "***",
  148. industry: "***",
  149. location: "***",
  150. name: "***",
  151. version: "*",
  152. },
  153. },
  154. rules: {
  155. project_name: {
  156. rules: [
  157. {
  158. required: true,
  159. errorMessage: "请输入项目名称",
  160. },
  161. ],
  162. },
  163. type: {
  164. rules: [
  165. {
  166. required: true,
  167. errorMessage: "请选择项目类别",
  168. },
  169. ],
  170. },
  171. },
  172. fullRules: {
  173. industry: {
  174. rules: [
  175. {
  176. required: true,
  177. errorMessage: "请选择行业名称",
  178. },
  179. ],
  180. },
  181. location: {
  182. rules: [
  183. {
  184. required: true,
  185. errorMessage: "请选择地区",
  186. },
  187. ],
  188. },
  189. name: {
  190. rules: [
  191. {
  192. required: true,
  193. errorMessage: "请输入项目简称",
  194. },
  195. {
  196. maxLength: 3,
  197. errorMessage: "项目简称不能超过3位",
  198. },
  199. {
  200. validateFunction: function (rule, value, data, callback) {
  201. if (value.match(/[^A-Za-z]/g)) {
  202. callback("项目简称只能是英文字符");
  203. } else {
  204. callback();
  205. }
  206. },
  207. },
  208. ],
  209. },
  210. version: {
  211. rules: [
  212. {
  213. required: true,
  214. errorMessage: "请选择期数",
  215. },
  216. ],
  217. },
  218. },
  219. project_id: 0,
  220. typeList: [],
  221. industryList: [],
  222. flowList: [],
  223. versionList,
  224. provinces,
  225. locationList: [provinces, []],
  226. locationIndex: [0, 0],
  227. };
  228. },
  229. computed: {
  230. ...mapState(["currentProject"]),
  231. },
  232. onLoad(options) {
  233. if (options.project_id) {
  234. this.project_id = options.project_id;
  235. }
  236. this.init(options);
  237. },
  238. onReady() {
  239. this.$refs.fullForm.setRules(this.fullRules);
  240. },
  241. methods: {
  242. async submitHelper(full) {
  243. let payload = {
  244. project_name: this.formData.project_name,
  245. type_id: Number(this.formData.type.id),
  246. flow_id: Number(this.formData.flow.id),
  247. node_id: this.flowList.find((item) => item.id == this.formData.flow.id)
  248. .Nodes[0].id,
  249. };
  250. if (full) {
  251. payload = {
  252. ...payload,
  253. industry_id: Number(this.formData.industry.id),
  254. location: this.formData.location.name,
  255. location_code: this.formData.location.code,
  256. name: this.formData.name,
  257. version: this.formData.version + "",
  258. project_full_code:
  259. this.formData.code.type +
  260. this.formData.code.industry +
  261. this.formData.code.location +
  262. this.formData.code.name +
  263. this.formData.code.version,
  264. };
  265. }
  266. if (this.project_id == 0) {
  267. await createApproval(payload);
  268. } else {
  269. payload.id = Number(this.project_id);
  270. await updateApproval(payload);
  271. }
  272. uni.redirectTo({
  273. url: "./list",
  274. });
  275. },
  276. onHandleSubmit(value) {
  277. if (value) {
  278. this.$refs.form.validate((err) => {
  279. if (!this.formData.type || this.formData.type.id != 7) {
  280. this.$refs.fullForm.validate((errFull) => {
  281. if (!err && !errFull) {
  282. this.submitHelper(1);
  283. }
  284. });
  285. } else {
  286. if (!err) {
  287. this.submitHelper(0);
  288. }
  289. }
  290. });
  291. } else {
  292. uni.redirectTo({
  293. url: "./list",
  294. });
  295. }
  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.fullForm.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.$refs.form.setValue("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. this.binddata("location", e.target.value);
  405. },
  406. },
  407. };
  408. </script>
  409. <style lang="less" scoped>
  410. .content {
  411. display: flex;
  412. flex-wrap: wrap;
  413. }
  414. .title {
  415. width: 100%;
  416. padding: 0 20px 20px 20px;
  417. background: #f8f8f8;
  418. font: 24px bold;
  419. }
  420. .form {
  421. margin: 20px 10% 0 10%;
  422. width: 100%;
  423. }
  424. .input {
  425. border: 1px solid #666;
  426. }
  427. .select {
  428. width: 100%;
  429. height: 72rpx;
  430. line-height: 70rpx;
  431. border: 1px solid #666;
  432. padding-left: 20rpx;
  433. }
  434. .group {
  435. width: 100%;
  436. display: flex;
  437. position: fixed;
  438. flex-wrap: wrap;
  439. justify-content: flex-start;
  440. bottom: 0;
  441. left: 0;
  442. .commit {
  443. width: 50%;
  444. border-radius: 0;
  445. margin: inherit;
  446. }
  447. }
  448. </style>