index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <doc-alert title="功能开启" url="https://doc.iocoder.cn/mall/build/" />
  3. <!-- 搜索工作栏 -->
  4. <ContentWrap>
  5. <el-form
  6. ref="queryFormRef"
  7. :inline="true"
  8. :model="queryParams"
  9. class="-mb-15px"
  10. label-width="82px"
  11. >
  12. <el-form-item label="优惠券名称" prop="name">
  13. <el-input
  14. v-model="queryParams.name"
  15. class="!w-240px"
  16. placeholder="请输入优惠劵名"
  17. clearable
  18. @keyup="handleQuery"
  19. />
  20. </el-form-item>
  21. <el-form-item label="优惠券类型" prop="discountType">
  22. <el-select
  23. v-model="queryParams.discountType"
  24. class="!w-240px"
  25. placeholder="请选择优惠券类型"
  26. clearable
  27. >
  28. <el-option
  29. v-for="dict in getIntDictOptions(DICT_TYPE.PROMOTION_DISCOUNT_TYPE)"
  30. :key="dict.value"
  31. :label="dict.label"
  32. :value="dict.value"
  33. />
  34. </el-select>
  35. </el-form-item>
  36. <el-form-item label="优惠券状态" prop="status">
  37. <el-select
  38. v-model="queryParams.status"
  39. class="!w-240px"
  40. placeholder="请选择优惠券状态"
  41. clearable
  42. >
  43. <el-option
  44. v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
  45. :key="dict.value"
  46. :label="dict.label"
  47. :value="dict.value"
  48. />
  49. </el-select>
  50. </el-form-item>
  51. <el-form-item label="创建时间" prop="createTime">
  52. <el-date-picker
  53. v-model="queryParams.createTime"
  54. value-format="YYYY-MM-DD HH:mm:ss"
  55. type="daterange"
  56. start-placeholder="开始日期"
  57. end-placeholder="结束日期"
  58. :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
  59. class="!w-240px"
  60. />
  61. </el-form-item>
  62. <el-form-item>
  63. <el-button @click="handleQuery"> <Icon icon="ep:search" class="mr-5px" />搜索 </el-button>
  64. <el-button @click="resetQuery"> <Icon icon="ep:refresh" class="mr-5px" />重置 </el-button>
  65. <el-button
  66. v-hasPermi="['promotion:coupon-template:create']"
  67. plain
  68. type="primary"
  69. @click="openForm('create')"
  70. >
  71. <Icon class="mr-5px" icon="ep:plus" /> 新增
  72. </el-button>
  73. <el-button
  74. plain
  75. type="success"
  76. @click="$router.push('/promotion/coupon')"
  77. v-hasPermi="['promotion:coupon:query']"
  78. >
  79. <Icon icon="ep:operation" class="mr-5px" />会员优惠劵
  80. </el-button>
  81. </el-form-item>
  82. </el-form>
  83. </ContentWrap>
  84. <!-- 列表 -->
  85. <ContentWrap>
  86. <el-table v-loading="loading" :data="list">
  87. <el-table-column label="优惠券名称" align="center" prop="name" />
  88. <el-table-column label="优惠券类型" align="center" prop="discountType">
  89. <template #default="scope">
  90. <dict-tag :type="DICT_TYPE.PROMOTION_DISCOUNT_TYPE" :value="scope.row.discountType" />
  91. </template>
  92. </el-table-column>
  93. <el-table-column
  94. label="优惠金额 / 折扣"
  95. align="center"
  96. prop="discount"
  97. :formatter="discountFormat"
  98. />
  99. <el-table-column label="发放数量" align="center" prop="totalCount" />
  100. <el-table-column
  101. label="剩余数量"
  102. align="center"
  103. prop="totalCount"
  104. :formatter="(row) => row.totalCount - row.takeCount"
  105. />
  106. <el-table-column
  107. label="领取上限"
  108. align="center"
  109. prop="takeLimitCount"
  110. :formatter="takeLimitCountFormat"
  111. />
  112. <el-table-column
  113. label="有效期限"
  114. align="center"
  115. prop="validityType"
  116. width="190"
  117. :formatter="validityTypeFormat"
  118. />
  119. <el-table-column label="状态" align="center" prop="status">
  120. <template #default="scope">
  121. <el-switch
  122. v-model="scope.row.status"
  123. :active-value="0"
  124. :inactive-value="1"
  125. @change="handleStatusChange(scope.row)"
  126. />
  127. </template>
  128. </el-table-column>
  129. <el-table-column
  130. label="创建时间"
  131. align="center"
  132. prop="createTime"
  133. :formatter="dateFormatter"
  134. width="180"
  135. />
  136. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  137. <template #default="scope">
  138. <el-button
  139. v-hasPermi="['promotion:coupon-template:update']"
  140. link
  141. type="primary"
  142. @click="openForm('update', scope.row.id)"
  143. >
  144. 修改
  145. </el-button>
  146. <el-button
  147. v-hasPermi="['promotion:coupon-template:delete']"
  148. link
  149. type="danger"
  150. @click="handleDelete(scope.row.id)"
  151. >
  152. 删除
  153. </el-button>
  154. </template>
  155. </el-table-column>
  156. </el-table>
  157. <!-- 分页 -->
  158. <Pagination
  159. v-model:limit="queryParams.pageSize"
  160. v-model:page="queryParams.pageNo"
  161. :total="total"
  162. @pagination="getList"
  163. />
  164. </ContentWrap>
  165. <!-- 表单弹窗:添加/修改 -->
  166. <CouponTemplateForm ref="formRef" @success="getList" />
  167. </template>
  168. <script lang="ts" setup>
  169. import * as CouponTemplateApi from '@/api/mall/promotion/coupon/couponTemplate'
  170. import {
  171. CommonStatusEnum,
  172. CouponTemplateValidityTypeEnum,
  173. PromotionDiscountTypeEnum
  174. } from '@/utils/constants'
  175. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  176. import { dateFormatter, formatDate } from '@/utils/formatTime'
  177. import CouponTemplateForm from './CouponTemplateForm.vue'
  178. defineOptions({ name: 'PromotionCouponTemplate' })
  179. const message = useMessage() // 消息弹窗
  180. const { t } = useI18n() // 国际化
  181. const loading = ref(true) // 列表的加载中
  182. const total = ref(0) // 列表的总页数
  183. const list = ref([]) // 字典表格数据
  184. const queryParams = reactive({
  185. pageNo: 1,
  186. pageSize: 10,
  187. name: null,
  188. status: null,
  189. type: null,
  190. createTime: []
  191. })
  192. const queryFormRef = ref() // 搜索的表单
  193. /** 查询列表 */
  194. const getList = async () => {
  195. loading.value = true
  196. try {
  197. // 执行查询
  198. const data = await CouponTemplateApi.getCouponTemplatePage(queryParams)
  199. list.value = data.list
  200. total.value = data.total
  201. } finally {
  202. loading.value = false
  203. }
  204. }
  205. /** 搜索按钮操作 */
  206. const handleQuery = () => {
  207. queryParams.pageNo = 1
  208. getList()
  209. }
  210. /** 重置按钮操作 */
  211. const resetQuery = () => {
  212. queryFormRef?.value?.resetFields()
  213. handleQuery()
  214. }
  215. /** 添加/修改操作 */
  216. const formRef = ref()
  217. const openForm = (type: string, id?: number) => {
  218. formRef.value.open(type, id)
  219. }
  220. /** 优惠劵模板状态修改 */
  221. const handleStatusChange = async (row: any) => {
  222. // 此时,row 已经变成目标状态了,所以可以直接提交请求和提示
  223. let text = row.status === CommonStatusEnum.ENABLE ? '启用' : '停用'
  224. try {
  225. await message.confirm('确认要"' + text + '""' + row.name + '"优惠劵吗?')
  226. await CouponTemplateApi.updateCouponTemplateStatus(row.id, row.status)
  227. message.success(text + '成功')
  228. } catch {
  229. // 异常时,需要将 row.status 状态重置回之前的
  230. row.status =
  231. row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.DISABLE : CommonStatusEnum.ENABLE
  232. }
  233. }
  234. /** 删除按钮操作 */
  235. const handleDelete = async (id: number) => {
  236. try {
  237. // 删除的二次确认
  238. await message.confirm('是否确认删除优惠劵编号为"' + id + '"的数据项?')
  239. // 发起删除
  240. await CouponTemplateApi.deleteCouponTemplate(id)
  241. message.success(t('common.delSuccess'))
  242. // 刷新列表
  243. await getList()
  244. } catch {}
  245. }
  246. // 格式化【优惠金额/折扣】
  247. const discountFormat = (row: any) => {
  248. if (row.discountType === PromotionDiscountTypeEnum.PRICE.type) {
  249. return `¥${(row.discountPrice / 100.0).toFixed(2)}`
  250. }
  251. if (row.discountType === PromotionDiscountTypeEnum.PERCENT.type) {
  252. return `¥${(row.discountPrice / 100.0).toFixed(2)}`
  253. }
  254. return '未知【' + row.discountType + '】'
  255. }
  256. // 格式化【领取上限】
  257. const takeLimitCountFormat = (row: any) => {
  258. if (row.takeLimitCount === -1) {
  259. return '无领取限制'
  260. }
  261. return `${row.takeLimitCount} 张/人`
  262. }
  263. // 格式化【有效期限】
  264. const validityTypeFormat = (row: any) => {
  265. if (row.validityType === CouponTemplateValidityTypeEnum.DATE.type) {
  266. return `${formatDate(row.validStartTime)} 至 ${formatDate(row.validEndTime)}`
  267. }
  268. if (row.validityType === CouponTemplateValidityTypeEnum.TERM.type) {
  269. return `领取后第 ${row.fixedStartTerm} - ${row.fixedEndTerm} 天内可用`
  270. }
  271. return '未知【' + row.validityType + '】'
  272. }
  273. /** 初始化 **/
  274. onMounted(() => {
  275. getList()
  276. })
  277. </script>