add.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 { queryProjectType, queryIndustry, queryFlow } from "@/services/project";
  141. import provinces from "./provinces";
  142. import { mapState } from "vuex";
  143. const versionList = ["一期", "二期", "三期", "四期", "五期"];
  144. export default {
  145. data() {
  146. return {
  147. formData: {
  148. project_name: null,
  149. type: null,
  150. flow: null,
  151. industry: null,
  152. location: null,
  153. name: null,
  154. version: null,
  155. code: {
  156. type: "***",
  157. industry: "***",
  158. location: "***",
  159. name: "***",
  160. version: "*",
  161. },
  162. },
  163. rules: {
  164. project_name: {
  165. rules: [
  166. {
  167. required: true,
  168. errorMessage: "请输入项目名称",
  169. },
  170. ],
  171. },
  172. type: {
  173. rules: [
  174. {
  175. required: true,
  176. errorMessage: "请选择项目类别",
  177. },
  178. ],
  179. },
  180. industry: {
  181. rules: [
  182. {
  183. required: true,
  184. errorMessage: "请选择行业名称",
  185. },
  186. ],
  187. },
  188. location: {
  189. rules: [
  190. {
  191. required: true,
  192. errorMessage: "请选择地区",
  193. },
  194. ],
  195. },
  196. name: {
  197. rules: [
  198. {
  199. required: true,
  200. errorMessage: "请输入项目简称",
  201. },
  202. {
  203. maxLength: 3,
  204. errorMessage: "项目简称不能超过3位",
  205. },
  206. {
  207. validateFunction: function (rule, value, data, callback) {
  208. if (value.match(/[^A-Za-z]/g)) {
  209. callback("项目简称只能是英文字符");
  210. } else {
  211. callback();
  212. }
  213. },
  214. },
  215. ],
  216. },
  217. version: {
  218. rules: [
  219. {
  220. required: true,
  221. errorMessage: "请选择期数",
  222. },
  223. ],
  224. },
  225. },
  226. project_id: 0,
  227. typeList: [],
  228. industryList: [],
  229. flowList: [],
  230. versionList,
  231. provinces,
  232. locationList: [provinces, []],
  233. locationIndex: [0, 0],
  234. };
  235. },
  236. computed: {
  237. ...mapState(["currentProject"]),
  238. },
  239. onLoad(options) {
  240. this.project_id = options.project_id;
  241. this.init(options);
  242. },
  243. onReady() {
  244. this.$refs.form.setRules(this.rules);
  245. },
  246. methods: {
  247. onHandleSubmit(value) {
  248. if (value) {
  249. this.$refs.form.validate((err, formData) => {
  250. if (!err) {
  251. console.log("success", formData);
  252. uni.navigateTo({
  253. url: "./list",
  254. });
  255. }
  256. });
  257. } else uni.navigateTo({ url: "./list" });
  258. },
  259. async init(options) {
  260. let res;
  261. res = await queryProjectType();
  262. this.typeList = res.data;
  263. res = await queryIndustry();
  264. this.industryList = res.data;
  265. res = await queryFlow();
  266. this.flowList = res.data;
  267. let form = this.formData;
  268. let code = this.formData.code;
  269. let current = this.currentProject;
  270. if (options.project_id) {
  271. form.project_name = current.project_name;
  272. form.type = this.typeList.find(
  273. (item) => item.id == this.currentProject.type_id
  274. );
  275. form.flow = this.flowList.find(
  276. (item) => item.id == this.currentProject.flow_id
  277. );
  278. if (current.type_id != 7) {
  279. form.industry = this.industryList.find(
  280. (item) => item.id == this.currentProject.industry_id
  281. );
  282. form.location = {
  283. name: current.location,
  284. code: current.location_code,
  285. };
  286. form.name = current.name;
  287. form.version = current.version;
  288. code.type = form.type.code;
  289. code.industry = form.industry.code;
  290. code.location = form.location.code;
  291. code.name = form.name;
  292. code.version = form.version;
  293. const searchLocation = (list, name) => {
  294. for (let i = 0; i < list.length; ++i) {
  295. if (list[i].children) {
  296. let childrenIndex = searchLocation(list[i].children, name);
  297. if (childrenIndex[0] != -1) return [i, childrenIndex[0]];
  298. } else {
  299. if (list[i].name == name) return [i, 0];
  300. }
  301. }
  302. return [-1, -1];
  303. };
  304. this.locationIndex = searchLocation(this.provinces, current.location);
  305. if (this.provinces[this.locationIndex[0]].children)
  306. this.locationList[1] = this.provinces[this.locationIndex[0]].children;
  307. }
  308. }
  309. },
  310. binddata(name, value) {
  311. this.$refs.form.setValue(name, value);
  312. },
  313. selectChangeType(e) {
  314. let form = this.formData;
  315. let code = this.formData.code;
  316. form.type = this.typeList[e.target.value];
  317. code.type = form.type.code;
  318. this.binddata("type", e.target.value);
  319. if (form.type.id == 7) {
  320. form.flow = this.flowList.find((item) => item.id == 1);
  321. form.industry = null;
  322. form.location = null;
  323. form.name = null;
  324. form.version = null;
  325. code.industry = "***";
  326. code.location = "***";
  327. code.name = "***";
  328. code.version = "*";
  329. } else form.flow = this.flowList.find((item) => item.id == 4);
  330. },
  331. selectChangeIndustry(e) {
  332. this.formData.industry = this.industryList[e.target.value];
  333. this.formData.code.industry = this.formData.industry.code;
  334. this.binddata("industry", e.target.value);
  335. },
  336. onChangeName(e) {
  337. let name = e.detail.value.toUpperCase();
  338. while (name.length < 3) name += "V";
  339. this.formData.name = name;
  340. this.formData.code.name = name;
  341. },
  342. selectChangeVersion(e) {
  343. this.formData.version = e.target.value + 1;
  344. this.formData.code.version = this.formData.version + "";
  345. this.binddata("version", e.target.value);
  346. },
  347. bindChangeLocationColumn(e) {
  348. if (e.target.column == 0) {
  349. this.locationList[1] = this.provinces[e.target.value].children || [];
  350. this.locationIndex = [e.target.value, 0];
  351. }
  352. },
  353. selectChangeLocation(e) {
  354. let multiIndex = e.target.value;
  355. this.locationIndex = multiIndex;
  356. // let location = this.formData.location;
  357. // let locationCode = this.formData.code.location;
  358. let location = this.provinces[multiIndex[0]].children
  359. ? this.provinces[multiIndex[0]].children[multiIndex[1]]
  360. : this.provinces[multiIndex[0]];
  361. location.code =
  362. location.code.length == 4 ? location.code.substr(1) : location.code;
  363. this.formData.location = location;
  364. this.formData.code.location = location.code;
  365. },
  366. },
  367. };
  368. </script>
  369. <style lang="less" scoped>
  370. .content {
  371. display: flex;
  372. flex-wrap: wrap;
  373. }
  374. .title {
  375. width: 100%;
  376. padding: 0 20px 20px 20px;
  377. background: #f8f8f8;
  378. font: 24px bold;
  379. }
  380. .form {
  381. margin: 20px 10% 0 10%;
  382. width: 100%;
  383. }
  384. .input {
  385. border: 1px solid #666;
  386. }
  387. .select {
  388. width: 100%;
  389. height: 72rpx;
  390. line-height: 70rpx;
  391. border: 1px solid #666;
  392. padding-left: 20rpx;
  393. }
  394. .group {
  395. width: 100%;
  396. display: flex;
  397. position: fixed;
  398. flex-wrap: wrap;
  399. justify-content: flex-start;
  400. bottom: 0;
  401. left: 0;
  402. .commit {
  403. width: 50%;
  404. border-radius: 0;
  405. margin: inherit;
  406. }
  407. }
  408. </style>