SelectMaterial.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <template>
  2. <div class="user">
  3. <div class="tree">
  4. <treeList
  5. title="物料分类"
  6. submitType="2"
  7. :data="treeListData"
  8. v-model="sourceList.pagination.productClassifyId"
  9. @change="treeChange"
  10. @changeTreeList="getTreeList"
  11. >
  12. </treeList>
  13. </div>
  14. <div class="content">
  15. <byTable
  16. :source="sourceList.data"
  17. :pagination="sourceList.pagination"
  18. :config="config"
  19. :loading="loading"
  20. highlight-current-row
  21. :selectConfig="selectConfig"
  22. :table-events="{
  23. //element talbe事件都能传
  24. select: select,
  25. }"
  26. :action-list="[]"
  27. @get-list="getList"
  28. >
  29. <template #pic="{ item }">
  30. <div v-if="item.fileList.length > 0">
  31. <img
  32. :src="item.fileList[0].fileUrl"
  33. class="pic"
  34. @click="handleClickFile(item.fileList[0])"
  35. />
  36. </div>
  37. <div v-else></div>
  38. </template>
  39. </byTable>
  40. </div>
  41. <el-dialog
  42. :title="modalType == 'add' ? '添加' : '编辑'"
  43. v-model="dialogVisible"
  44. width="500"
  45. v-loading="loading"
  46. >
  47. <byForm
  48. :formConfig="formConfig"
  49. :formOption="formOption"
  50. v-model="formData.data"
  51. :rules="rules"
  52. ref="byform"
  53. >
  54. <template #productPic>
  55. <div>
  56. <el-upload
  57. v-model:fileList="fileList"
  58. action="https://winfaster.obs.cn-south-1.myhuaweicloud.com"
  59. :data="uploadData"
  60. list-type="picture-card"
  61. :on-remove="handleRemove"
  62. :on-success="handleSuccess"
  63. :before-upload="handleBeforeUpload"
  64. >
  65. <el-icon><Plus /></el-icon>
  66. </el-upload>
  67. </div>
  68. </template>
  69. </byForm>
  70. <template #footer>
  71. <el-button @click="dialogVisible = false" size="large">取 消</el-button>
  72. <el-button
  73. type="primary"
  74. @click="submitForm('byform')"
  75. size="large"
  76. :loading="submitLoading"
  77. >
  78. 确 定
  79. </el-button>
  80. </template>
  81. </el-dialog>
  82. <el-dialog
  83. title="Excel导入"
  84. v-model="openExcelDialog"
  85. width="400"
  86. v-loading="loading"
  87. >
  88. <template #footer>
  89. <el-button @click="openExcelDialog = false" size="large"
  90. >取 消</el-button
  91. >
  92. <el-button
  93. type="primary"
  94. @click="submitExcel()"
  95. size="large"
  96. :loading="submitLoading"
  97. >
  98. 确 定
  99. </el-button>
  100. </template>
  101. </el-dialog>
  102. </div>
  103. </template>
  104. <script setup>
  105. /* eslint-disable vue/no-unused-components */
  106. import { ElMessage, ElMessageBox } from "element-plus";
  107. import byTable from "@/components/byTable/index";
  108. import byForm from "@/components/byForm/index";
  109. import treeList from "@/components/product/treeList";
  110. import { computed, defineComponent, ref } from "vue";
  111. const loading = ref(false);
  112. const submitLoading = ref(false);
  113. const materialUnit = ref([]);
  114. const materialType = ref([]);
  115. const sourceList = ref({
  116. data: [],
  117. pagination: {
  118. total: 3,
  119. pageNum: 1,
  120. pageSize: 10,
  121. type: "",
  122. productClassifyId: "",
  123. keyword: "",
  124. definition: "2",
  125. },
  126. });
  127. let dialogVisible = ref(false);
  128. let openExcelDialog = ref(false);
  129. let modalType = ref("add");
  130. let rules = ref({
  131. productClassifyId: [
  132. { required: true, message: "请选择物料分类", trigger: "change" },
  133. ],
  134. type: [{ required: true, message: "请选择物料类型", trigger: "change" }],
  135. name: [{ required: true, message: "请输入物料名称", trigger: "blur" }],
  136. unit: [{ required: true, message: "请选择单位", trigger: "change" }],
  137. });
  138. const { proxy } = getCurrentInstance();
  139. const selectConfig = computed(() => {
  140. return [
  141. {
  142. label: "物料类型",
  143. prop: "type",
  144. data: materialType.value,
  145. },
  146. ];
  147. });
  148. const config = computed(() => {
  149. return [
  150. {
  151. attrs: {
  152. label: "物料类型",
  153. prop: "type",
  154. },
  155. render(type) {
  156. return proxy.dictValueLabel(type, materialType.value);
  157. },
  158. },
  159. {
  160. attrs: {
  161. label: "物料编码",
  162. prop: "code",
  163. },
  164. },
  165. {
  166. attrs: {
  167. label: "物料名称",
  168. prop: "name",
  169. },
  170. },
  171. {
  172. attrs: {
  173. label: "图片",
  174. slot: "pic",
  175. },
  176. },
  177. {
  178. attrs: {
  179. label: "单位",
  180. prop: "unit",
  181. },
  182. render(unit) {
  183. return proxy.dictValueLabel(unit, materialUnit.value);
  184. },
  185. },
  186. {
  187. attrs: {
  188. label: "规格",
  189. prop: "spec",
  190. },
  191. },
  192. {
  193. attrs: {
  194. label: "物料备注",
  195. prop: "remark",
  196. },
  197. },
  198. {
  199. attrs: {
  200. label: "操作",
  201. width: "60",
  202. align: "center",
  203. fixed: "right",
  204. },
  205. // 渲染 el-button,一般用在最后一列。
  206. renderHTML(row) {
  207. return [
  208. {
  209. attrs: {
  210. label: "选择",
  211. type: "primary",
  212. text: true,
  213. },
  214. el: "button",
  215. click() {
  216. handleSelect(row);
  217. },
  218. },
  219. ];
  220. },
  221. },
  222. ];
  223. });
  224. let formData = reactive({
  225. data: {},
  226. });
  227. const formOption = reactive({
  228. inline: true,
  229. labelWidth: 100,
  230. itemWidth: 100,
  231. rules: [],
  232. });
  233. const byform = ref(null);
  234. const treeListData = ref([]);
  235. const formConfig = computed(() => {
  236. return [
  237. {
  238. type: "treeSelect",
  239. prop: "productClassifyId",
  240. label: "物料分类",
  241. data: [],
  242. },
  243. {
  244. type: "select",
  245. prop: "type",
  246. label: "物料类型",
  247. required: true,
  248. data: [
  249. {
  250. label: "原料",
  251. id: "1",
  252. },
  253. {
  254. label: "辅料",
  255. id: "2",
  256. },
  257. {
  258. label: "配件",
  259. id: "3",
  260. },
  261. {
  262. label: "包材",
  263. id: "4",
  264. },
  265. {
  266. label: "其他",
  267. id: "5",
  268. },
  269. ],
  270. },
  271. {
  272. type: "input",
  273. prop: "name",
  274. label: "物料名称",
  275. },
  276. {
  277. type: "input",
  278. prop: "spec",
  279. label: "规格",
  280. },
  281. {
  282. type: "select",
  283. prop: "unit",
  284. label: "单位",
  285. required: true,
  286. data: [
  287. {
  288. label: "个",
  289. id: "个",
  290. },
  291. {
  292. label: "双",
  293. id: "双",
  294. },
  295. ],
  296. },
  297. {
  298. type: "slot",
  299. slotName: "productPic",
  300. prop: "fileList",
  301. label: "产品图片",
  302. },
  303. {
  304. type: "input",
  305. prop: "remark",
  306. label: "备注",
  307. itemType: "textarea",
  308. },
  309. ];
  310. });
  311. const newPassword = () => {
  312. formData.data.password = generatePassword();
  313. };
  314. const generatePassword = () => {
  315. var length = 12,
  316. charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
  317. password = "";
  318. for (var i = 0, n = charset.length; i < length; ++i) {
  319. password += charset.charAt(Math.floor(Math.random() * n));
  320. }
  321. return password;
  322. };
  323. const getList = async (req) => {
  324. sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  325. loading.value = true;
  326. proxy
  327. .post("/productInfo/page", sourceList.value.pagination)
  328. .then((message) => {
  329. console.log(message);
  330. sourceList.value.data = message.rows.map((x) => ({ ...x, fileList: [] }));
  331. sourceList.value.pagination.total = message.total;
  332. setTimeout(() => {
  333. loading.value = false;
  334. }, 200);
  335. const productIdList = message.rows.map((x) => x.id);
  336. // 请求文件数据并回显
  337. if (productIdList.length > 0) {
  338. proxy
  339. .post("/fileInfo/getList", { businessIdList: productIdList })
  340. .then((fileObj) => {
  341. for (let i = 0; i < sourceList.value.data.length; i++) {
  342. const e = sourceList.value.data[i];
  343. for (const key in fileObj) {
  344. if (e.id === key) {
  345. e.fileList = fileObj[key];
  346. }
  347. }
  348. }
  349. });
  350. }
  351. });
  352. };
  353. const uploadData = ref({});
  354. const fileList = ref([]);
  355. const fileListCopy = ref([]);
  356. const treeChange = (e) => {
  357. console.log(e);
  358. sourceList.value.pagination.productClassifyId = e.id;
  359. getList({ productClassifyId: e.id });
  360. };
  361. const openModal = () => {
  362. dialogVisible.value = true;
  363. modalType.value = "add";
  364. formData.data = {
  365. definition: "2",
  366. fileList: [],
  367. };
  368. fileList.value = [];
  369. fileListCopy.value = [];
  370. };
  371. const openExcel = () => {
  372. openExcelDialog.value = true;
  373. };
  374. const submitExcel = () => {
  375. openExcelDialog.value = false;
  376. };
  377. const TreetenantId = ref("");
  378. const selection = ref({
  379. data: [],
  380. });
  381. const select = (_selection, row) => {
  382. selection.value.data = _selection;
  383. console.log(_selection.length);
  384. };
  385. const tree = ref(null);
  386. const submitForm = () => {
  387. console.log(byform.value);
  388. byform.value.handleSubmit((valid) => {
  389. formData.data.fileList = fileListCopy.value.map((x) => ({
  390. id: x.id,
  391. fileName: x.fileName,
  392. }));
  393. submitLoading.value = true;
  394. proxy.post("/productInfo/" + modalType.value, formData.data).then(
  395. (res) => {
  396. ElMessage({
  397. message: modalType.value == "add" ? "添加成功" : "编辑成功",
  398. type: "success",
  399. });
  400. dialogVisible.value = false;
  401. submitLoading.value = false;
  402. getList();
  403. },
  404. (err) => {
  405. submitLoading.value = false;
  406. }
  407. );
  408. });
  409. };
  410. const getTreeList = () => {
  411. proxy
  412. .post("/productClassify/tree", { parentId: "", name: "", definition: "2" })
  413. .then((message) => {
  414. treeListData.value = message;
  415. formConfig.value[0].data = message;
  416. });
  417. };
  418. const getDtl = (row) => {
  419. modalType.value = "edit";
  420. proxy.post("/productInfo/detail", { id: row.id }).then((res) => {
  421. fileList.value = row.fileList.map((x) => ({ ...x, url: x.fileUrl }));
  422. fileListCopy.value = [...fileList.value];
  423. res.type = res.type + "";
  424. res.definition = "2";
  425. formData.data = res;
  426. dialogVisible.value = true;
  427. });
  428. };
  429. getTreeList();
  430. getList();
  431. const handleBeforeUpload = async (file) => {
  432. const res = await proxy.post("/fileInfo/getSing", { fileName: file.name });
  433. uploadData.value = res.uploadBody;
  434. fileListCopy.value.push({
  435. id: res.id,
  436. fileName: res.fileName,
  437. path: res.fileUrl,
  438. url: res.fileUrl,
  439. uid: file.uid,
  440. });
  441. };
  442. const handleSuccess = (res, file, files) => {
  443. // 查当前file的index值去赋值对应的copy变量的值
  444. // let uid = file.uid;
  445. // const index = fileList.value.findIndex((x) => x.uid === uid);
  446. // fileListCopy.value[index].uid = uid;
  447. };
  448. const handleRemove = (file) => {
  449. const index = fileListCopy.value.findIndex(
  450. (x) => x.uid === file.uid || x.id === file.id
  451. );
  452. fileListCopy.value.splice(index, 1);
  453. };
  454. const handleClickFile = (file) => {
  455. window.open(file.fileUrl, "_blank");
  456. };
  457. const handleSelect = (row) => {
  458. proxy.$emit("handleSelect", toRaw(row));
  459. };
  460. const getDict = () => {
  461. proxy.getDictOne(["material_unit", "material_type"]).then((res) => {
  462. materialUnit.value = res["material_unit"].map((x) => ({
  463. label: x.dictValue,
  464. value: x.dictKey,
  465. }));
  466. materialType.value = res["material_type"].map((x) => ({
  467. label: x.dictValue,
  468. value: x.dictKey,
  469. }));
  470. });
  471. };
  472. getDict();
  473. </script>
  474. <style lang="scss" scoped>
  475. .user {
  476. padding: 20px;
  477. display: flex;
  478. justify-content: space-between;
  479. .tree {
  480. width: 300px;
  481. }
  482. .content {
  483. width: calc(100% - 320px);
  484. }
  485. }
  486. .pic {
  487. object-fit: contain;
  488. width: 50px;
  489. height: 50px;
  490. cursor: pointer;
  491. vertical-align: middle;
  492. }
  493. </style>