index.vue 13 KB

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