index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <template>
  2. <div class="user">
  3. <div class="tree">
  4. <treeList title="物料分类" submitType="2" :data="treeListData" v-model="sourceList.pagination.productClassifyId" @change="treeChange"
  5. @changeTreeList="getTreeList">
  6. </treeList>
  7. </div>
  8. <div class="content">
  9. <byTable :source="sourceList.data" :pagination="sourceList.pagination" :config="config" :loading="loading" highlight-current-row
  10. :selectConfig="selectConfig" :table-events="{
  11. //element talbe事件都能传
  12. select: select,
  13. }" :action-list="[
  14. props.selectStatus
  15. ? {}
  16. : {
  17. text: 'Excel导入',
  18. action: () => openExcel(),
  19. disabled: false,
  20. },
  21. props.selectStatus
  22. ? {}
  23. : {
  24. text: '添加物料',
  25. action: () => openModal('add'),
  26. disabled: false,
  27. },
  28. ]" @get-list="getList">
  29. <template #pic="{ item }">
  30. <div v-if="item.fileList.length > 0">
  31. <img :src="item.fileList[0].fileUrl" class="pic" @click="handleClickFile(item.fileList[0])" />
  32. </div>
  33. <div v-else></div>
  34. </template>
  35. </byTable>
  36. </div>
  37. <el-dialog :title="modalType == 'add' ? '添加' : '编辑'" v-model="dialogVisible" width="500" v-loading="loading" destroy-on-close>
  38. <div class="public_height_dialog">
  39. <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="byform">
  40. <template #productPic>
  41. <div>
  42. <el-upload v-model:fileList="fileList" :action="uploadUrl" :data="uploadData" list-type="picture-card" :on-remove="handleRemove"
  43. :on-success="handleSuccess" :before-upload="handleBeforeUpload">
  44. <el-icon>
  45. <Plus />
  46. </el-icon>
  47. </el-upload>
  48. </div>
  49. </template>
  50. </byForm>
  51. </div>
  52. <template #footer>
  53. <el-button @click="dialogVisible = false" size="large">取 消</el-button>
  54. <el-button type="primary" @click="submitForm('byform')" size="large" :loading="submitLoading">
  55. 确 定
  56. </el-button>
  57. </template>
  58. </el-dialog>
  59. <el-dialog title="Excel导入" v-model="openExcelDialog" width="400" v-loading="loading">
  60. <template #footer>
  61. <el-button @click="openExcelDialog = false" size="large">取 消</el-button>
  62. <el-button type="primary" @click="submitExcel()" size="large" :loading="submitLoading">
  63. 确 定
  64. </el-button>
  65. </template>
  66. </el-dialog>
  67. </div>
  68. </template>
  69. <script setup>
  70. /* eslint-disable vue/no-unused-components */
  71. import { ElMessage, ElMessageBox } from "element-plus";
  72. import byTable from "@/components/byTable/index";
  73. import byForm from "@/components/byForm/index";
  74. import treeList from "@/components/product/treeList";
  75. import { computed, defineComponent, ref } from "vue";
  76. const loading = ref(false);
  77. const submitLoading = ref(false);
  78. const sourceList = ref({
  79. data: [],
  80. pagination: {
  81. total: 3,
  82. pageNum: 1,
  83. pageSize: 10,
  84. type: "",
  85. productClassifyId: "",
  86. keyword: "",
  87. definition: "2",
  88. },
  89. });
  90. let dialogVisible = ref(false);
  91. let openExcelDialog = ref(false);
  92. let modalType = ref("add");
  93. const materialUnit = ref([]);
  94. const materialType = ref([]);
  95. const treeData = ref([]);
  96. let rules = ref({
  97. productClassifyId: [
  98. { required: true, message: "请选择物料分类", trigger: "change" },
  99. ],
  100. type: [{ required: true, message: "请选择物料类型", trigger: "change" }],
  101. name: [{ required: true, message: "请输入物料名称", trigger: "blur" }],
  102. unit: [{ required: true, message: "请选择单位", trigger: "change" }],
  103. });
  104. const { proxy } = getCurrentInstance();
  105. const props = defineProps({
  106. selectStatus: Boolean,
  107. });
  108. const selectConfig = reactive([
  109. {
  110. label: "物料类型",
  111. prop: "type",
  112. data: [],
  113. },
  114. ]);
  115. const config = computed(() => {
  116. return [
  117. {
  118. attrs: {
  119. label: "物料类型",
  120. prop: "type",
  121. },
  122. render(type) {
  123. return proxy.dictValueLabel(type, materialType.value);
  124. },
  125. },
  126. {
  127. attrs: {
  128. label: "物料编码",
  129. prop: "code",
  130. },
  131. },
  132. {
  133. attrs: {
  134. label: "图片",
  135. prop: "unit",
  136. slot: "pic",
  137. },
  138. },
  139. {
  140. attrs: {
  141. label: "物料名称",
  142. prop: "name",
  143. },
  144. },
  145. {
  146. attrs: {
  147. label: "规格型号",
  148. prop: "spec",
  149. },
  150. },
  151. {
  152. attrs: {
  153. label: "单位",
  154. prop: "unit",
  155. },
  156. render(unit) {
  157. return proxy.dictValueLabel(unit, materialUnit.value);
  158. },
  159. },
  160. {
  161. attrs: {
  162. label: "物料备注",
  163. prop: "remark",
  164. },
  165. },
  166. {
  167. attrs: {
  168. label: "操作",
  169. width: "160",
  170. align: "center",
  171. fixed: "right",
  172. },
  173. renderHTML(row) {
  174. return [
  175. props.selectStatus
  176. ? {}
  177. : {
  178. attrs: {
  179. label: "复制",
  180. type: "primary",
  181. text: true,
  182. },
  183. el: "button",
  184. click() {
  185. getDtlOne(row);
  186. },
  187. },
  188. props.selectStatus
  189. ? {
  190. attrs: {
  191. label: "选择",
  192. type: "primary",
  193. text: true,
  194. },
  195. el: "button",
  196. click() {
  197. clickSelect(row);
  198. },
  199. }
  200. : {
  201. attrs: {
  202. label: "修改",
  203. type: "primary",
  204. text: true,
  205. },
  206. el: "button",
  207. click() {
  208. getDtl(row);
  209. },
  210. },
  211. props.selectStatus
  212. ? {}
  213. : {
  214. attrs: {
  215. label: "删除",
  216. type: "danger",
  217. text: true,
  218. },
  219. el: "button",
  220. click() {
  221. ElMessageBox.confirm(
  222. "此操作将永久删除该数据, 是否继续?",
  223. "提示",
  224. {
  225. confirmButtonText: "确定",
  226. cancelButtonText: "取消",
  227. type: "warning",
  228. }
  229. ).then(() => {
  230. proxy
  231. .post("/productInfo/delete", {
  232. id: row.id,
  233. })
  234. .then(() => {
  235. ElMessage({
  236. message: "删除成功",
  237. type: "success",
  238. });
  239. getList();
  240. });
  241. });
  242. },
  243. },
  244. ];
  245. },
  246. },
  247. ];
  248. });
  249. let formData = reactive({
  250. data: {},
  251. });
  252. const formOption = reactive({
  253. inline: true,
  254. labelWidth: 100,
  255. itemWidth: 100,
  256. rules: [],
  257. });
  258. const byform = ref(null);
  259. const treeListData = ref([]);
  260. const formConfig = computed(() => {
  261. return [
  262. {
  263. type: "treeSelect",
  264. prop: "productClassifyId",
  265. label: "物料分类",
  266. data: treeData.value,
  267. },
  268. {
  269. type: "select",
  270. prop: "type",
  271. label: "物料类型",
  272. required: true,
  273. data: materialType.value,
  274. },
  275. {
  276. type: "input",
  277. prop: "name",
  278. label: "物料名称",
  279. },
  280. {
  281. type: "input",
  282. prop: "spec",
  283. label: "规格型号",
  284. },
  285. {
  286. type: "input",
  287. prop: "barCode",
  288. label: "条码编号",
  289. },
  290. {
  291. type: "select",
  292. prop: "unit",
  293. label: "单位",
  294. required: true,
  295. data: materialUnit.value,
  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. sourceList.value.pagination.productClassifyId = e.id;
  358. getList({ productClassifyId: e.id });
  359. };
  360. const openModal = () => {
  361. dialogVisible.value = true;
  362. modalType.value = "add";
  363. formData.data = {
  364. definition: "2",
  365. fileList: [],
  366. // type: "1",
  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. treeData.value = 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. const getDtlOne = (row) => {
  430. modalType.value = "add";
  431. proxy.post("/productInfo/detail", { id: row.id }).then((res) => {
  432. fileList.value = [];
  433. fileListCopy.value = [];
  434. res.type = res.type + "";
  435. res.definition = "2";
  436. delete res.id;
  437. formData.data = res;
  438. dialogVisible.value = true;
  439. });
  440. };
  441. getTreeList();
  442. getList();
  443. const handleBeforeUpload = async (file) => {
  444. const res = await proxy.post("/fileInfo/getSing", { fileName: file.name });
  445. uploadData.value = res.uploadBody;
  446. fileListCopy.value.push({
  447. id: res.id,
  448. fileName: res.fileName,
  449. path: res.fileUrl,
  450. url: res.fileUrl,
  451. uid: file.uid,
  452. });
  453. };
  454. const handleSuccess = (res, file, files) => {
  455. // 查当前file的index值去赋值对应的copy变量的值
  456. // let uid = file.uid;
  457. // const index = fileList.value.findIndex((x) => x.uid === uid);
  458. // fileListCopy.value[index].uid = uid;
  459. };
  460. const handleRemove = (file) => {
  461. const index = fileListCopy.value.findIndex(
  462. (x) => x.uid === file.uid || x.id === file.id
  463. );
  464. fileListCopy.value.splice(index, 1);
  465. };
  466. const handleClickFile = (file) => {
  467. window.open(file.fileUrl, "_blank");
  468. };
  469. const getDict = () => {
  470. proxy.getDictOne(["material_unit", "material_type"]).then((res) => {
  471. materialUnit.value = res["material_unit"].map((x) => ({
  472. label: x.dictValue,
  473. value: x.dictKey,
  474. }));
  475. materialType.value = res["material_type"].map((x) => ({
  476. label: x.dictValue,
  477. value: x.dictKey,
  478. }));
  479. selectConfig[0].data = materialType.value;
  480. });
  481. };
  482. getDict();
  483. const clickSelect = (item) => {
  484. proxy.$emit("selectMaterial", item);
  485. };
  486. </script>
  487. <style lang="scss" scoped>
  488. .user {
  489. padding: 20px;
  490. display: flex;
  491. justify-content: space-between;
  492. .tree {
  493. width: 300px;
  494. }
  495. .content {
  496. width: calc(100% - 320px);
  497. }
  498. }
  499. .pic {
  500. object-fit: contain;
  501. width: 50px;
  502. height: 50px;
  503. cursor: pointer;
  504. vertical-align: middle;
  505. }
  506. </style>