index.vue 11 KB

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