add.vue 12 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="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. uni.navigateBack();
  269. } else {
  270. payload.id = Number(this.project_id);
  271. await updateApproval(payload);
  272. uni.navigateBack({
  273. delta: 2,
  274. });
  275. }
  276. },
  277. onHandleSubmit(value) {
  278. if (value) {
  279. this.$refs.form.validate((err) => {
  280. if (!this.formData.type || this.formData.type.id != 7) {
  281. this.$refs.fullForm.validate((errFull) => {
  282. if (!err && !errFull) {
  283. this.submitHelper(1);
  284. }
  285. });
  286. } else {
  287. if (!err) {
  288. this.submitHelper(0);
  289. }
  290. }
  291. });
  292. } else {
  293. uni.navigateBack();
  294. }
  295. },
  296. async init(options) {
  297. let res;
  298. res = await queryProjectType();
  299. this.typeList = res.data;
  300. res = await queryIndustry();
  301. this.industryList = res.data;
  302. res = await queryFlow();
  303. this.flowList = res.data;
  304. let form = this.formData;
  305. let code = this.formData.code;
  306. let current = this.currentProject;
  307. if (options.project_id) {
  308. form.project_name = current.project_name;
  309. form.type = this.typeList.find(
  310. (item) => item.id == this.currentProject.type_id
  311. );
  312. form.flow = this.flowList.find(
  313. (item) => item.id == this.currentProject.flow_id
  314. );
  315. if (current.type_id != 7) {
  316. form.industry = this.industryList.find(
  317. (item) => item.id == this.currentProject.industry_id
  318. );
  319. form.location = {
  320. name: current.location,
  321. code: current.location_code,
  322. };
  323. form.name = current.name;
  324. form.version = current.version;
  325. code.type = form.type.code;
  326. code.industry = form.industry.code;
  327. code.location = form.location.code;
  328. code.name = form.name;
  329. code.version = form.version;
  330. const searchLocation = (list, name) => {
  331. for (let i = 0; i < list.length; ++i) {
  332. if (list[i].children) {
  333. let childrenIndex = searchLocation(list[i].children, name);
  334. if (childrenIndex[0] != -1) return [i, childrenIndex[0]];
  335. } else {
  336. if (list[i].name == name) return [i, 0];
  337. }
  338. }
  339. return [-1, -1];
  340. };
  341. this.locationIndex = searchLocation(this.provinces, current.location);
  342. if (this.provinces[this.locationIndex[0]].children)
  343. this.locationList[1] =
  344. this.provinces[this.locationIndex[0]].children;
  345. }
  346. }
  347. },
  348. binddata(name, value) {
  349. this.$refs.fullForm.setValue(name, value);
  350. },
  351. selectChangeType(e) {
  352. let form = this.formData;
  353. let code = this.formData.code;
  354. form.type = this.typeList[e.target.value];
  355. code.type = form.type.code;
  356. this.$refs.form.setValue("type", e.target.value);
  357. if (form.type.id == 7) {
  358. form.flow = this.flowList.find((item) => item.id == 4);
  359. form.industry = null;
  360. form.location = null;
  361. form.name = null;
  362. form.version = null;
  363. code.industry = "***";
  364. code.location = "***";
  365. code.name = "***";
  366. code.version = "*";
  367. } else form.flow = this.flowList.find((item) => item.id == 1);
  368. },
  369. selectChangeIndustry(e) {
  370. this.formData.industry = this.industryList[e.target.value];
  371. this.formData.code.industry = this.formData.industry.code;
  372. this.binddata("industry", e.target.value);
  373. },
  374. onChangeName(e) {
  375. let name = e.detail.value.toUpperCase();
  376. while (name.length < 3) name += "V";
  377. this.formData.name = name;
  378. this.formData.code.name = name;
  379. },
  380. selectChangeVersion(e) {
  381. this.formData.version = e.target.value + 1;
  382. this.formData.code.version = this.formData.version + "";
  383. this.binddata("version", e.target.value);
  384. },
  385. bindChangeLocationColumn(e) {
  386. if (e.target.column == 0) {
  387. this.locationList[1] = this.provinces[e.target.value].children || [];
  388. this.locationIndex = [e.target.value, 0];
  389. }
  390. },
  391. selectChangeLocation(e) {
  392. let multiIndex = e.target.value;
  393. this.locationIndex = multiIndex;
  394. // let location = this.formData.location;
  395. // let locationCode = this.formData.code.location;
  396. let location = this.provinces[multiIndex[0]].children
  397. ? this.provinces[multiIndex[0]].children[multiIndex[1]]
  398. : this.provinces[multiIndex[0]];
  399. location.code =
  400. location.code.length == 4 ? location.code.substr(1) : location.code;
  401. this.formData.location = location;
  402. this.formData.code.location = location.code;
  403. this.binddata("location", e.target.value);
  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>