add.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <div class="form">
  3. <van-nav-bar
  4. :title="$t('productLibrary.name')"
  5. :left-text="$t('common.back')"
  6. left-arrow
  7. @click-left="onClickLeft"
  8. >
  9. </van-nav-bar>
  10. <testForm
  11. v-model="formData.data"
  12. :formOption="formOption"
  13. :formConfig="formConfig"
  14. :rules="rules"
  15. @onSubmit="onSubmit"
  16. ref="formDom"
  17. ></testForm>
  18. </div>
  19. </template>
  20. <script setup>
  21. import { ref, getCurrentInstance, onMounted,reactive } from "vue";
  22. import { showSuccessToast, showToast } from "vant";
  23. import { useRoute } from "vue-router";
  24. import { getUserInfo } from '@/utils/auth';
  25. import testForm from "@/components/testForm/index.vue";
  26. const proxy = getCurrentInstance().proxy;
  27. const route = useRoute();
  28. const show = ref(false);
  29. const typeModal = ref(false);
  30. const unitModal = ref(false);
  31. const classification = ref([]);
  32. const formData = reactive({
  33. data: {
  34. },
  35. });
  36. const formDom = ref(null);
  37. const formOption = reactive({
  38. readonly: false, //用于控制整个表单是否只读
  39. disabled: false,
  40. labelAlign: "top",
  41. scroll: true,
  42. labelWidth: "62pk",
  43. // hiddenSubmitBtn: true,
  44. });
  45. const formConfig = reactive([
  46. {
  47. type: "cascader",
  48. label: proxy.t('productLibrary.productClassification'),
  49. prop: "productClassifyId",
  50. itemType: "common",
  51. showPicker: false,
  52. // data: classification.value,
  53. data: [],
  54. fieldNames: {
  55. text: "label",
  56. value: "id",
  57. children: "children",
  58. },
  59. // onChangeFn: (option) => {
  60. // // console.log("aa");
  61. // },
  62. // finishFn: (current, option) => {
  63. // current.showPicker = false;
  64. // },
  65. },
  66. {
  67. type: "picker",
  68. label: proxy.t('productLibrary.productType'),
  69. prop: "type",
  70. itemType: "onePicker",
  71. showPicker: false,
  72. fieldNames: {
  73. text: "label",
  74. value: "id",
  75. },
  76. data: [
  77. {
  78. label: proxy.t('productLibrary.finishedProduct'),
  79. id: "10",
  80. },
  81. {
  82. label: proxy.t('productLibrary.semifinishedProduct'),
  83. id: "2",
  84. },
  85. ],
  86. },
  87. {
  88. type: "input",
  89. itemType: "text",
  90. label: proxy.t('productLibrary.productName'),
  91. prop: "name",
  92. clearable: true,
  93. },
  94. {
  95. type: "input",
  96. itemType: "text",
  97. label: proxy.t('productLibrary.specificationModel'),
  98. prop: "spec",
  99. clearable: true,
  100. },
  101. {
  102. type: "picker",
  103. label: proxy.t('productLibrary.unit'),
  104. prop: "unit",
  105. itemType: "onePicker",
  106. showPicker: false,
  107. fieldNames: {
  108. text: "dictValue",
  109. value: "dictKey",
  110. },
  111. data: []
  112. },
  113. {
  114. type: "upload",
  115. label: proxy.t('productLibrary.fileUpload'),
  116. prop: "fileList",
  117. },
  118. {
  119. type: "input",
  120. itemType: "textarea",
  121. label: proxy.t('productLibrary.remarks'),
  122. prop: "remark",
  123. },
  124. ]);
  125. const rules = {
  126. productClassifyId: [{ required: true, message: proxy.t('productLibrary.productClassificationCanNotBeEmpty') }],
  127. type: [{ required: true, message: proxy.t('productLibrary.productTypeCanNotBeEmpty') }],
  128. name: [{ required: true, message: proxy.t('productLibrary.productNameCanNotBeEmpty') }],
  129. spec: [{ required: true, message: proxy.t('productLibrary.specificationModelCanNotBeEmpty') }],
  130. unit: [{ required: true, message: proxy.t('productLibrary.unitCanNotBeEmpty') }],
  131. select: [{ required: true, message: proxy.t('productLibrary.pleaseSelect') }],
  132. date: [{ required: true, message: proxy.t('productLibrary.pleaseSelectTime') }],
  133. common: [{ required: true, message: proxy.t('productLibrary.pleaseSelectCascader') }],
  134. // city: [{ required: true, message: "请选择城市" }],
  135. };
  136. const unitList = ref([]);
  137. const getDict = () => {
  138. proxy
  139. .post("/dictTenantData/page", {
  140. pageNum: 1,
  141. pageSize: 999,
  142. tenantId: getUserInfo().tenantId,
  143. dictCode: "unit",
  144. })
  145. .then((res) => {
  146. formConfig[4].data = res.data.rows
  147. })
  148. }
  149. getDict()
  150. const fileList = ref([]);
  151. const onClickLeft = () => history.back();
  152. const onSubmit = () => {
  153. console.log(formData)
  154. proxy.post("/productInfo/" + route.query.type, formData.data).then(() => {
  155. showSuccessToast(proxy.t('common.addSuccess'));
  156. setTimeout(() => {
  157. history.back();
  158. }, 500);
  159. });
  160. };
  161. const treeToList = (arr) => {
  162. let res = []; // 用于存储递归结果(扁平数据)
  163. // 递归函数
  164. let fn = (source) => {
  165. source.forEach((el) => {
  166. res.push(el);
  167. el.children && el.children.length > 0 ? fn(el.children) : ""; // 子级递归
  168. });
  169. };
  170. fn(arr);
  171. return res;
  172. };
  173. onMounted(() => {
  174. proxy.post("/productClassify/tree", { parentId: "", name: "", definition: "1" }).then((res) => {
  175. formConfig[0].data = res.data;
  176. let classList = treeToList(res.data);
  177. if (route.query.id) {
  178. proxy.post("/productInfo/detail", { id: route.query.id }).then((resDetail) => {
  179. formData.data = resDetail.data
  180. });
  181. // proxy.post("/fileInfo/getList", { businessIdList: [route.query.id] }).then((res) => {
  182. // if (res.data[route.query.id] && res.data[route.query.id].length > 0) {
  183. // formData.value.fileList = res.data[route.query.id];
  184. // fileList.value = res.data[route.query.id].map((item) => {
  185. // return {
  186. // ...item,
  187. // url: item.fileUrl,
  188. // };
  189. // });
  190. // } else {
  191. // formData.value.fileList = [];
  192. // fileList.value = [];
  193. // }
  194. // });
  195. }else {
  196. }
  197. });
  198. });
  199. </script>