index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <template>
  2. <div class="table-box">
  3. <ProTable ref="proTable" :columns="columns" :request-api="getTableList" :init-param="initParam" :data-callback="dataCallback">
  4. <!-- 表格 header 按钮 -->
  5. <template #tableHeader="scope">
  6. <div class="table-header-btn">
  7. <el-button :icon="Plus" class="button" type="primary" @click="newGroupBuying"> 新建代金券 </el-button>
  8. <el-tabs v-if="showTabs" v-model="activeName" class="tabs" @tab-click="handleClick">
  9. <el-tab-pane v-for="tab in filteredTabOptions" :key="tab.name" :label="tab.label" :name="tab.name" />
  10. </el-tabs>
  11. </div>
  12. </template>
  13. <template #status="scope">
  14. <p>团购状态:{{ allTabOptions.find(item => item.name === scope.row.groupType)?.label ?? "--" }}</p>
  15. <p>审核状态:{{ statusEnum.find(item => item.value === scope.row.reviewType)?.label ?? "--" }}</p> </template
  16. >:
  17. <!-- 表格操作 -->
  18. <template #operation="scope">
  19. <el-button
  20. link
  21. type="primary"
  22. @click="changeTypes(scope.row, 'on')"
  23. v-if="canShowButton(scope.row.status, OPERATION_PERMISSIONS.上架)"
  24. >
  25. 上架
  26. </el-button>
  27. <el-button
  28. link
  29. type="primary"
  30. @click="changeTypes(scope.row, 'off')"
  31. v-if="canShowButton(scope.row.status, OPERATION_PERMISSIONS.下架)"
  32. >
  33. 下架
  34. </el-button>
  35. <el-button
  36. link
  37. type="primary"
  38. @click="changeInventory(scope.row)"
  39. v-if="canShowButton(scope.row.status, OPERATION_PERMISSIONS.修改库存)"
  40. >
  41. 修改库存
  42. </el-button>
  43. <el-button
  44. type="primary"
  45. link
  46. @click="toDetail(scope.row)"
  47. v-if="canShowButton(scope.row.status, OPERATION_PERMISSIONS.查看详情)"
  48. >
  49. 查看详情
  50. </el-button>
  51. <el-button
  52. link
  53. type="primary"
  54. @click="editRow(scope.row)"
  55. v-if="canShowButton(scope.row.status, OPERATION_PERMISSIONS.编辑)"
  56. >
  57. 编辑
  58. </el-button>
  59. <el-button
  60. link
  61. type="primary"
  62. @click="deleteRow(scope.row)"
  63. v-if="canShowButton(scope.row.status, OPERATION_PERMISSIONS.删除)"
  64. >
  65. 删除
  66. </el-button>
  67. </template>
  68. </ProTable>
  69. <el-dialog v-model="dialogFormVisible" title="修改库存" width="500">
  70. <el-form ref="ruleFormRef" :model="formInventory" :rules="rules" @submit.prevent>
  71. <el-form-item label="套餐名">
  72. {{ formInventory.packageName }}
  73. </el-form-item>
  74. <el-form-item label="剩余库存">
  75. {{ formInventory.inventoryNum }}
  76. </el-form-item>
  77. <el-form-item label="修改库存" prop="newInventory">
  78. <el-input v-model="formInventory.newInventory" placeholder="请输入" />
  79. </el-form-item>
  80. </el-form>
  81. <template #footer>
  82. <div class="dialog-footer">
  83. <el-button @click="closeDialog"> 取消 </el-button>
  84. <el-button type="primary" @click="handleSubmit"> 确定 </el-button>
  85. </div>
  86. </template>
  87. </el-dialog>
  88. </div>
  89. </template>
  90. <script setup lang="tsx" name="groupPackageManagement">
  91. import { computed, onActivated, onMounted, reactive, ref, watch } from "vue";
  92. import { useRouter } from "vue-router";
  93. import type { FormInstance, FormRules } from "element-plus";
  94. import { ElMessage } from "element-plus";
  95. import ProTable from "@/components/ProTable/index.vue";
  96. import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
  97. import { Plus } from "@element-plus/icons-vue";
  98. import { delThaliById, getThaliList, sjxj, updateNum } from "@/api/modules/voucherManagement";
  99. import { ElMessageBox } from "element-plus/es";
  100. import { localGet } from "@/utils";
  101. const router = useRouter();
  102. const dialogFormVisible = ref(false);
  103. const formInventory: any = ref({
  104. id: "",
  105. packageName: "",
  106. inventoryNum: "",
  107. newInventory: ""
  108. });
  109. const rowData = ref<any>();
  110. const activeName = ref("");
  111. // 定义表单类型
  112. interface RuleForm {
  113. newInventory: string;
  114. }
  115. const ruleFormRef = ref<FormInstance>();
  116. const rules = reactive<FormRules<RuleForm>>({
  117. newInventory: [
  118. { required: true, message: "请输入库存数量", trigger: "blur" },
  119. {
  120. pattern: /^(0|[1-9][0-9]*)$/,
  121. message: "请输入整数,不允许输入小数,负数",
  122. trigger: "blur"
  123. }
  124. ]
  125. });
  126. const statusEnum = [
  127. { value: "-1", label: "待审核" },
  128. { value: "-2", label: "审核通过" },
  129. { value: "0", label: "审核驳回" }
  130. ];
  131. // ProTable 实例(需要在使用它的地方之前定义)
  132. const proTable = ref<ProTableInstance>();
  133. // 表格配置项
  134. const columns = reactive<ColumnProps<any>[]>([
  135. {
  136. prop: "index",
  137. label: "序号",
  138. width: 100,
  139. render: (scope: any) => {
  140. return scope.$index + (proTable.value!.pageable.pageNum - 1) * proTable.value!.pageable.pageSize + 1;
  141. }
  142. },
  143. {
  144. prop: "groupName",
  145. label: "代金券名称",
  146. search: {
  147. el: "input"
  148. }
  149. },
  150. {
  151. prop: "preferentialPrice",
  152. label: "价格",
  153. render: (scope: any) => {
  154. return scope.row.preferentialPrice ? `¥${scope.row.preferentialPrice}` : "--";
  155. }
  156. },
  157. {
  158. prop: "goodsId",
  159. label: "已售"
  160. },
  161. {
  162. prop: "inventoryNum",
  163. label: "剩余库存"
  164. },
  165. {
  166. prop: "goodsId",
  167. label: "结束时间"
  168. },
  169. {
  170. prop: "reviewType",
  171. label: "审核状态",
  172. isShow: false,
  173. render: scope => {
  174. const statusObj = statusEnum.find(item => item.value === scope.row.reviewType);
  175. return statusObj ? statusObj.label : "--";
  176. },
  177. search: {
  178. el: "select"
  179. },
  180. enum: statusEnum,
  181. fieldNames: { label: "label", value: "value" }
  182. },
  183. {
  184. prop: "status",
  185. label: "状态"
  186. },
  187. { prop: "operation", label: "操作", fixed: "right", width: 330 }
  188. ]);
  189. const allTabOptions = [
  190. { label: "全部", name: "" },
  191. { label: "草稿", name: "0" },
  192. { label: "进行中", name: "5" },
  193. { label: "未开始", name: "2" },
  194. { label: "已下架", name: "6" },
  195. { label: "已售罄", name: "4" },
  196. { label: "已结束", name: "7" }
  197. ];
  198. // 状态枚举:0草稿 1待审核 2未开始 3审核拒绝 4已售罄 5进行中 6已下架 7已结束
  199. const STATUS = {
  200. 草稿: 0,
  201. 待审核: 1,
  202. 未开始: 2,
  203. 审核拒绝: 3,
  204. 已售罄: 4,
  205. 进行中: 5,
  206. 已下架: 6,
  207. 已结束: 7
  208. } as const;
  209. // 操作按钮权限配置:定义每个操作按钮在哪些状态下显示
  210. const OPERATION_PERMISSIONS = {
  211. // 查看详情:待审核、未开始、审核拒绝、进行中、已售罄、已下架
  212. 查看详情: [STATUS.待审核, STATUS.未开始, STATUS.审核拒绝, STATUS.进行中, STATUS.已售罄, STATUS.已下架],
  213. // 上架:未开始、已下架
  214. 上架: [STATUS.未开始, STATUS.已下架],
  215. // 下架:进行中
  216. 下架: [STATUS.进行中],
  217. // 修改库存:未开始、进行中、已售罄
  218. 修改库存: [STATUS.未开始, STATUS.进行中, STATUS.已售罄],
  219. // 编辑:草稿、审核拒绝、已售罄、已下架、已结束
  220. 编辑: [STATUS.草稿, STATUS.审核拒绝, STATUS.已售罄, STATUS.已下架, STATUS.已结束],
  221. // 删除:草稿、未开始、审核拒绝、已售罄、已结束
  222. 删除: [STATUS.草稿, STATUS.未开始, STATUS.审核拒绝, STATUS.已售罄, STATUS.已结束]
  223. } as const;
  224. // 判断按钮是否显示的工具函数
  225. const canShowButton = (status: number, allowedStatuses: readonly number[]) => {
  226. return allowedStatuses.includes(status);
  227. };
  228. // 获取当前审核状态
  229. const currentAuditStatus = computed(() => {
  230. if (!proTable.value?.searchParam) return "";
  231. return proTable.value.searchParam.reviewType || "";
  232. });
  233. // 控制 el-tabs 是否显示:当审核状态为空或审核通过时显示
  234. const showTabs = computed(() => {
  235. const status = currentAuditStatus.value;
  236. return !status || status === "-2";
  237. });
  238. // 根据审核状态过滤 tabOptions:如果审核状态为空,只显示草稿;审核通过时,显示除草稿外的所有标签页
  239. const filteredTabOptions = computed(() => {
  240. const status = currentAuditStatus.value;
  241. if (!status) {
  242. // 审核状态为空时,只显示草稿
  243. return allTabOptions;
  244. } else if (status === "-2") {
  245. // 审核通过时,显示除草稿外的所有标签页
  246. return allTabOptions.filter(tab => tab.name !== "1");
  247. }
  248. return [];
  249. });
  250. // 监听审核状态变化
  251. watch(
  252. currentAuditStatus,
  253. newStatus => {
  254. if (!newStatus || newStatus === "-2") {
  255. // 审核状态为空时,确保 activeName 为草稿
  256. activeName.value = "";
  257. }
  258. },
  259. { immediate: true }
  260. );
  261. // 如果表格需要初始化请求参数,直接定义传给 ProTable (之后每次请求都会自动带上该参数,此参数更改之后也会一直带上,改变此参数会自动刷新表格数据)
  262. const initParam = reactive({
  263. storeId: "104",
  264. groupType: "1",
  265. status: activeName
  266. });
  267. // 页面加载时触发查询
  268. onMounted(() => {
  269. proTable.value?.getTableList();
  270. });
  271. // 从其他页面返回时触发查询
  272. onActivated(() => {
  273. proTable.value?.getTableList();
  274. });
  275. // dataCallback 是对于返回的表格数据做处理,如果你后台返回的数据不是 list && total 这些字段,可以在这里进行处理成这些字段
  276. // 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
  277. const dataCallback = (data: any) => {
  278. return {
  279. list: data.records,
  280. total: data.total
  281. };
  282. };
  283. // 如果你想在请求之前对当前请求参数做一些操作,可以自定义如下函数:params 为当前所有的请求参数(包括分页),最后返回请求列表接口
  284. // 默认不做操作就直接在 ProTable 组件上绑定 :requestApi="getUserList"
  285. const getTableList = (params: any) => {
  286. let newParams = JSON.parse(JSON.stringify(params));
  287. return getThaliList(newParams);
  288. };
  289. const newGroupBuying = () => {
  290. router.push(`/voucherManagement/newVoucher?type=add`);
  291. };
  292. // 跳转详情页
  293. const toDetail = row => {
  294. router.push(`/voucherManagement/detail?id=${row.id}`);
  295. };
  296. const editRow = row => {
  297. router.push(`/voucherManagement/newVoucher?id=${row.id}&type=edit`);
  298. };
  299. const deleteRow = row => {
  300. ElMessageBox.confirm("确定要删除吗?", "提示", {
  301. confirmButtonText: "确定",
  302. cancelButtonText: "取消",
  303. type: "warning"
  304. }).then(() => {
  305. const params = {
  306. id: row.id,
  307. groupType: localGet("businessSection") || "1"
  308. };
  309. delThaliById(params).then(() => {
  310. ElMessage.success("删除成功");
  311. proTable.value?.getTableList();
  312. });
  313. });
  314. };
  315. const handleClick = () => {};
  316. const changeTypes = async (row: any, status: string) => {
  317. rowData.value = row;
  318. let res = await sjxj({ id: row.id, status: status });
  319. if (res && res.code == 200) {
  320. ElMessage.success("操作成功");
  321. proTable.value?.getTableList();
  322. }
  323. };
  324. const changeInventory = (row: any) => {
  325. formInventory.value.id = row.id;
  326. formInventory.value.packageName = row.packageName;
  327. formInventory.value.inventoryNum = row.inventoryNum;
  328. formInventory.value.newInventory = "";
  329. dialogFormVisible.value = true;
  330. };
  331. // 弹窗提交
  332. const handleSubmit = async () => {
  333. if (!ruleFormRef.value) return;
  334. await ruleFormRef.value.validate(async (valid, fields) => {
  335. if (valid) {
  336. let res = await updateNum({ id: formInventory.value.id, num: formInventory.value.newInventory });
  337. if (res && res.code == 200) {
  338. ElMessage.success("库存修改成功");
  339. dialogFormVisible.value = false;
  340. proTable.value?.getTableList();
  341. }
  342. }
  343. });
  344. };
  345. // 关闭弹窗;
  346. const closeDialog = () => {
  347. dialogFormVisible.value = false;
  348. formInventory.value = {
  349. id: "",
  350. packageName: "",
  351. inventoryNum: "",
  352. newInventory: ""
  353. };
  354. };
  355. </script>
  356. <style lang="scss" scoped>
  357. // 在组件样式中添加
  358. .date-range {
  359. display: block; // 确保换行生效
  360. padding: 0 8px; // 可选:增加内边距
  361. word-wrap: break-word; // 长单词内换行
  362. white-space: normal; // 允许自然换行
  363. }
  364. .table-header-btn {
  365. .tabs {
  366. margin-top: 10px;
  367. :deep(.el-tabs__nav-wrap::after) {
  368. height: 0;
  369. }
  370. }
  371. }
  372. </style>