selectGood.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <van-nav-bar :title="'选择物品'" left-text="" left-arrow @click-left="onClickLeft" @click-right="onClickRight">
  3. <!-- <template #right> {{$t('common.add')}} </template> -->
  4. </van-nav-bar>
  5. <van-search v-model="req.keyword" :placeholder="$t('common.pleaseEnterKeywords')" @search="onRefresh" />
  6. <van-pull-refresh v-model="loading" @refresh="onRefresh">
  7. <div class="list">
  8. <van-list v-model:loading="loading" :finished="finished" :finished-text="$t('common.noMore')" @load="onLoad" style="margin-bottom: 60px">
  9. <!-- @onClick="toDtl" -->
  10. <commonList :data="listData" :config="listConfig" :showMore="false">
  11. <template #btn="{row}">
  12. <div style="width:100%;text-align:right">
  13. <van-button type="primary" size="small" @click="selectRow(row)">选择</van-button>
  14. </div>
  15. </template>
  16. </commonList>
  17. </van-list>
  18. </div>
  19. </van-pull-refresh>
  20. </template>
  21. <script setup>
  22. import { ref, getCurrentInstance } from "vue";
  23. import commonList from "@/components/common-list.vue";
  24. import { useRoute } from "vue-router";
  25. import useSelectGoodStore from "@/store/manyGood";
  26. import { showSuccessToast, showFailToast } from "vant";
  27. const selectGoodStore = useSelectGoodStore();
  28. const loading = ref(false);
  29. const router = useRoute();
  30. const req = ref({
  31. pageNum: 1,
  32. keyword: null,
  33. definition: "",
  34. });
  35. const finished = ref(false);
  36. const proxy = getCurrentInstance().proxy;
  37. const listData = ref([]);
  38. const classification = ref([]);
  39. const listConfig = ref([
  40. {
  41. label: proxy.t("productLibrary.productCode"),
  42. prop: "customCode",
  43. },
  44. {
  45. label: proxy.t("productLibrary.productName"),
  46. prop: "name",
  47. },
  48. {
  49. label: "规格",
  50. prop: "spec",
  51. },
  52. {
  53. type: "slot",
  54. slotName: "btn",
  55. label: "",
  56. },
  57. ]);
  58. const onRefresh = () => {
  59. req.value.pageNum = 1;
  60. finished.value = false;
  61. getList("refresh");
  62. };
  63. const onLoad = () => {
  64. // getClassification();
  65. getList();
  66. };
  67. const onClickLeft = () => history.back();
  68. const onClickRight = () => {
  69. proxy.$router.push({
  70. path: "productLibraryAdd",
  71. query: {
  72. type: "add",
  73. },
  74. });
  75. };
  76. proxy.uploadDdRightBtn(onClickRight, proxy.t("common.add"));
  77. const toDtl = (row) => {
  78. proxy.$router.push({
  79. path: "productLibraryAdd",
  80. query: {
  81. id: row.id,
  82. type: "edit",
  83. },
  84. });
  85. };
  86. const treeToList = (arr) => {
  87. let res = []; // 用于存储递归结果(扁平数据)
  88. // 递归函数
  89. let fn = (source) => {
  90. source.forEach((el) => {
  91. res.push(el);
  92. el.children && el.children.length > 0 ? fn(el.children) : ""; // 子级递归
  93. });
  94. };
  95. fn(arr);
  96. return res;
  97. };
  98. const getClassification = () => {
  99. if (classification.value && classification.value.length > 0) {
  100. getList();
  101. } else {
  102. proxy
  103. .post("/productClassify/tree", {
  104. parentId: "",
  105. name: "",
  106. definition: "1",
  107. })
  108. .then((res) => {
  109. classification.value = treeToList(res.data);
  110. getList();
  111. });
  112. }
  113. };
  114. const getList = (type) => {
  115. loading.value = true;
  116. proxy
  117. .post("/productInfo/pageByWdly", req.value)
  118. .then((res) => {
  119. listData.value =
  120. type === "refresh"
  121. ? res.data.rows
  122. : listData.value.concat(res.data.rows);
  123. if (req.value.pageNum * 10 >= res.data.total) {
  124. finished.value = true;
  125. }
  126. req.value.pageNum++;
  127. loading.value = false;
  128. })
  129. .catch((err) => {
  130. loading.value = false;
  131. });
  132. };
  133. const selectRow = (row) => {
  134. selectGoodStore.pushGood(row);
  135. return showSuccessToast("选择成功");
  136. };
  137. </script>
  138. <style lang="scss" scoped>
  139. .list {
  140. min-height: 70vh;
  141. }
  142. </style>