CouponManagement.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <template>
  2. <div class="coupon-management admin-page">
  3. <el-card>
  4. <PageHeader slot="header" title="优惠券管理">
  5. <div class="admin-page-actions">
  6. <el-input v-model="keyword" placeholder="搜索优惠券..." prefix-icon="el-icon-search" clearable @keyup.enter.native="handleSearch" @clear="handleSearch" />
  7. <el-tag
  8. v-if="sortState.length > 0"
  9. closable
  10. size="mini"
  11. class="sort-tag"
  12. @close="clearSort"
  13. @click="clearSort"
  14. >
  15. {{ sortLabel }}
  16. </el-tag>
  17. <el-button type="primary" size="small" @click="handleCreate">新建优惠券</el-button>
  18. <el-button type="success" size="small" @click="handleBatchIssue">批量发放</el-button>
  19. </div>
  20. </PageHeader>
  21. <div class="adm-table-wrap">
  22. <el-table height="100%" :data="list" v-loading="loading" border stripe>
  23. <el-table-column prop="id" label="ID" width="70" />
  24. <el-table-column prop="name" label="名称" min-width="140" show-overflow-tooltip>
  25. <template slot="header">
  26. <span class="sort-header" @click.stop="onSortClick('name', $event)" @dblclick.stop="onSortToggle('name')">名称<span v-if="sortIndex('name') !== null" class="sort-index" :class="sortClass('name') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('name') }}</span>{{ sortIcon('name') }}</span>
  27. </template>
  28. </el-table-column>
  29. <el-table-column label="类型" width="110">
  30. <template slot-scope="{ row }">
  31. <StatusTag size="mini" type="primary" :text="typeLabel(row.type)" />
  32. </template>
  33. </el-table-column>
  34. <el-table-column label="面值/折扣" width="100">
  35. <template slot-scope="{ row }">
  36. <span v-if="row.type === 'DISCOUNT'">{{ (row.discountRate / 100).toFixed(1) }}折</span>
  37. <span v-else>{{ (row.value / 100).toFixed(2) }}元</span>
  38. </template>
  39. </el-table-column>
  40. <el-table-column label="使用门槛(元)" width="110">
  41. <template slot-scope="{ row }">{{ row.minSpend ? (row.minSpend / 100).toFixed(2) : '无' }}</template>
  42. </el-table-column>
  43. <el-table-column label="适用对象" width="110">
  44. <template slot-scope="{ row }">{{ applicableLabel(row.applicableTo) }}</template>
  45. </el-table-column>
  46. <el-table-column label="积分价" width="80">
  47. <template slot-scope="{ row }">{{ row.pointsPrice || 0 }}</template>
  48. </el-table-column>
  49. <el-table-column label="发放规则" width="130">
  50. <template slot-scope="{ row }">
  51. <span v-if="row.grantType">{{ grantTypeLabel(row.grantType) }}</span>
  52. <span v-else>-</span>
  53. </template>
  54. </el-table-column>
  55. <el-table-column label="有效期" width="180">
  56. <template slot-scope="{ row }">{{ row.validFrom }} ~ {{ row.validUntil }}</template>
  57. </el-table-column>
  58. <el-table-column prop="totalCount" label="总量" width="70">
  59. <template slot="header">
  60. <span class="sort-header" @click.stop="onSortClick('totalCount', $event)">总量<span v-if="sortIndex('totalCount') !== null" class="sort-index" :class="sortClass('totalCount') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('totalCount') }}</span>{{ sortIcon('totalCount') }}</span>
  61. </template>
  62. </el-table-column>
  63. <el-table-column prop="usedCount" label="已用" width="70">
  64. <template slot="header">
  65. <span class="sort-header" @click.stop="onSortClick('usedCount', $event)">已用<span v-if="sortIndex('usedCount') !== null" class="sort-index" :class="sortClass('usedCount') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('usedCount') }}</span>{{ sortIcon('usedCount') }}</span>
  66. </template>
  67. </el-table-column>
  68. <el-table-column label="状态" width="80">
  69. <template slot-scope="{ row }">
  70. <StatusTag size="mini" :type="row.status !== 'DISABLED' ? 'success' : 'info'" :text="row.status !== 'DISABLED' ? '启用' : '停用'" />
  71. </template>
  72. </el-table-column>
  73. <el-table-column label="操作" width="180" fixed="right">
  74. <template slot-scope="{ row }">
  75. <el-button size="mini" type="primary" @click="handleEdit(row)">编辑</el-button>
  76. <el-dropdown trigger="hover" @command="(cmd) => handleActionCmd(row, cmd)">
  77. <el-button size="mini">
  78. 更多<i class="el-icon-arrow-down el-icon--right"></i>
  79. </el-button>
  80. <el-dropdown-menu slot="dropdown">
  81. <el-dropdown-item command="toggle-on" icon="el-icon-check" v-if="row.status === 'DISABLED'">启用</el-dropdown-item>
  82. <el-dropdown-item command="toggle-off" icon="el-icon-remove-outline" v-if="row.status !== 'DISABLED'">停用</el-dropdown-item>
  83. <el-dropdown-item command="delete" icon="el-icon-delete">删除</el-dropdown-item>
  84. </el-dropdown-menu>
  85. </el-dropdown>
  86. </template>
  87. </el-table-column>
  88. </el-table>
  89. </div>
  90. <el-pagination
  91. @current-change="onPageChange"
  92. @size-change="onSizeChange"
  93. :current-page="page"
  94. :page-size="size"
  95. :total="total"
  96. layout="total, prev, pager, next"
  97. class="pagination-wrap"
  98. />
  99. </el-card>
  100. <el-dialog :visible.sync="formDialogVisible" :title="isEdit ? '编辑优惠券' : '新建优惠券'" width="550px">
  101. <el-form :model="form" label-width="120px" ref="couponForm">
  102. <el-form-item label="优惠券名称" required>
  103. <el-input v-model="form.name" placeholder="请输入优惠券名称" />
  104. </el-form-item>
  105. <el-form-item label="类型" required>
  106. <el-select v-model="form.type" placeholder="请选择类型" class="full-width">
  107. <el-option label="FIXED(满减)" value="FIXED" />
  108. <el-option label="CASH(无门槛)" value="CASH" />
  109. <el-option label="DISCOUNT(折扣)" value="DISCOUNT" />
  110. </el-select>
  111. </el-form-item>
  112. <el-form-item v-if="form.type !== 'DISCOUNT'" label="面值(元)" required>
  113. <el-input-number v-model="form.valueYuan" :min="0.01" :step="0.5" :precision="2" class="w-200" />
  114. </el-form-item>
  115. <el-form-item v-if="form.type === 'DISCOUNT'" label="折扣率(千分比)" required>
  116. <el-input-number v-model="form.discountRate" :min="1" :max="10000" :step="100" class="w-200" />
  117. <div class="form-item-tip">9000 = 9折,10000 = 无折扣</div>
  118. </el-form-item>
  119. <el-form-item v-if="form.type === 'FIXED'" label="使用门槛(元)">
  120. <el-input-number v-model="form.minSpendYuan" :min="0" :step="10" :precision="2" class="w-200" />
  121. </el-form-item>
  122. <el-form-item label="适用对象">
  123. <el-select v-model="form.applicableTo" placeholder="请选择适用对象" class="full-width">
  124. <el-option label="全部商品" value="ALL" />
  125. <el-option label="会员服务" value="MEMBERSHIP" />
  126. <el-option label="特定商品" value="PRODUCT" />
  127. <el-option label="测评服务" value="ASSESSMENT" />
  128. </el-select>
  129. </el-form-item>
  130. <el-form-item label="绑定商品">
  131. <product-picker v-model="form.productId" placeholder="输入商品名称搜索,留空=全场通用" />
  132. <div class="form-item-tip">留空 = 全场通用;仅适用对象选"特定商品"时必填</div>
  133. </el-form-item>
  134. <el-form-item label="积分兑换价">
  135. <el-input-number v-model="form.pointsPrice" :min="0" :step="100" class="w-200" />
  136. <div class="form-item-tip">0 = 不可积分兑换</div>
  137. </el-form-item>
  138. <el-form-item label="自动发放规则">
  139. <el-select v-model="form.grantType" placeholder="不自动发放" class="full-width">
  140. <el-option label="不自动发放" value="" />
  141. <el-option label="JOIN(开通会员赠)" value="JOIN" />
  142. <el-option label="PERIODIC(周期补发)" value="PERIODIC" />
  143. <el-option label="POPULATION(家庭人口券)" value="POPULATION" />
  144. <el-option label="REGISTER(新用户注册赠)" value="REGISTER" />
  145. </el-select>
  146. </el-form-item>
  147. <el-form-item v-if="form.grantType === 'JOIN' || form.grantType === 'PERIODIC'" label="适用会员等级">
  148. <el-select v-model="form.grantLevelCode" placeholder="请选择会员等级" class="full-width">
  149. <el-option v-for="lv in memberLevelOptions" :key="lv.levelCode" :label="lv.levelName + '(' + lv.levelCode + ')'" :value="lv.levelCode" />
  150. </el-select>
  151. </el-form-item>
  152. <el-form-item v-if="form.grantType === 'PERIODIC'" label="补发周期">
  153. <el-select v-model="form.grantPeriod" placeholder="请选择补发周期" class="full-width">
  154. <el-option label="MONTHLY(每月)" value="MONTHLY" />
  155. <el-option label="QUARTERLY(每季度)" value="QUARTERLY" />
  156. </el-select>
  157. </el-form-item>
  158. <el-form-item v-if="form.grantType && form.grantType !== ''" label="每次发放数量">
  159. <el-input-number v-model="form.grantQuantity" :min="1" :step="1" class="w-200" />
  160. </el-form-item>
  161. <el-form-item label="有效期起" required>
  162. <el-date-picker v-model="form.validFrom" type="date" placeholder="选择开始日期" value-format="yyyy-MM-dd" class="full-width" />
  163. </el-form-item>
  164. <el-form-item label="有效期止" required>
  165. <el-date-picker v-model="form.validUntil" type="date" placeholder="选择结束日期" value-format="yyyy-MM-dd" class="full-width" />
  166. </el-form-item>
  167. <el-form-item label="发放总量" required>
  168. <el-input-number v-model="form.totalCount" :min="1" :step="100" class="w-200" />
  169. </el-form-item>
  170. </el-form>
  171. <div slot="footer">
  172. <el-button @click="formDialogVisible = false">取消</el-button>
  173. <el-button type="primary" @click="handleSubmit" :loading="submitting">保存</el-button>
  174. </div>
  175. </el-dialog>
  176. <el-dialog title="操作成功" :visible.sync="successDialogVisible" width="400px" :close-on-click-modal="false">
  177. <div class="success-body">
  178. <i class="el-icon-success success-icon"></i>
  179. <p class="success-text">保存成功</p>
  180. </div>
  181. <div slot="footer">
  182. <el-button type="primary" @click="handleContinue">继续添加</el-button>
  183. <el-button @click="handleClose">关闭</el-button>
  184. </div>
  185. </el-dialog>
  186. <el-dialog title="批量发放优惠券" :visible.sync="batchDialogVisible" width="500px">
  187. <el-form label-width="120px">
  188. <el-form-item label="选择优惠券" required>
  189. <el-select v-model="batchCouponId" placeholder="请选择优惠券" class="full-width">
  190. <el-option v-for="c in couponOptions" :key="c.id" :label="c.name" :value="c.id" />
  191. </el-select>
  192. </el-form-item>
  193. <el-form-item label="家庭ID列表" required>
  194. <el-input v-model="batchFamilyIds" type="textarea" :rows="6" placeholder="每行一个家庭ID&#10;例如:&#10;1001&#10;1002&#10;1003" />
  195. </el-form-item>
  196. </el-form>
  197. <div slot="footer">
  198. <el-button @click="batchDialogVisible = false">取消</el-button>
  199. <el-button type="primary" @click="confirmBatchIssue" :loading="batchSubmitting">确认发放</el-button>
  200. </div>
  201. </el-dialog>
  202. </div>
  203. </template>
  204. <script>
  205. import SortMixin from '@/mixins/SortMixin'
  206. import ListStateMixin from '@/mixins/ListStateMixin'
  207. import { getCouponList, createCoupon, updateCoupon, deleteCoupon, batchIssueCoupons, toggleCouponStatus } from '@/api/coupon'
  208. import { listMembershipLevels } from '@/api/admin'
  209. import ProductPicker from '@/components/ProductPicker'
  210. export default {
  211. mixins: [SortMixin, ListStateMixin],
  212. name: 'CouponManagement',
  213. components: { ProductPicker },
  214. computed: {
  215. tableHeight() {
  216. return window.innerHeight - 300
  217. }
  218. },
  219. data() {
  220. return {
  221. list: [],
  222. loading: false,
  223. page: 1,
  224. size: 20,
  225. total: 0,
  226. formDialogVisible: false,
  227. successDialogVisible: false,
  228. isEdit: false,
  229. submitting: false,
  230. form: this.getEmptyForm(),
  231. batchDialogVisible: false,
  232. batchCouponId: '',
  233. batchFamilyIds: '',
  234. batchSubmitting: false,
  235. couponOptions: [],
  236. memberLevelOptions: [],
  237. keyword: '',
  238. sortableColumns: { name: '名称', totalCount: '总量', usedCount: '已用' }
  239. }
  240. },
  241. created() {
  242. this.loadData()
  243. this.loadMemberLevels()
  244. },
  245. methods: {
  246. async loadMemberLevels() {
  247. try {
  248. const res = await listMembershipLevels()
  249. const list = res.data || []
  250. this.memberLevelOptions = Array.isArray(list) ? list.filter(function(l) { return l && typeof l === 'object' && l.levelCode }) : []
  251. } catch (e) {
  252. this.memberLevelOptions = []
  253. }
  254. },
  255. getEmptyForm() {
  256. return {
  257. id: null,
  258. name: '',
  259. type: 'FIXED',
  260. valueYuan: null,
  261. minSpendYuan: null,
  262. discountRate: null,
  263. productId: null,
  264. pointsPrice: 0,
  265. grantType: '',
  266. grantLevelCode: 'FAMILY',
  267. grantPeriod: 'MONTHLY',
  268. grantQuantity: 1,
  269. applicableTo: 'ALL',
  270. validFrom: '',
  271. validUntil: '',
  272. totalCount: null
  273. }
  274. },
  275. applicableLabel(val) {
  276. const map = { ALL: '全部商品', MEMBERSHIP: '会员服务', PRODUCT: '特定商品', ASSESSMENT: '测评服务' }
  277. return map[val] || val
  278. },
  279. typeLabel(val) {
  280. const map = { FIXED: '满减券', CASH: '无门槛券', DISCOUNT: '折扣券', PERCENT: '折扣券' }
  281. return map[val] || val
  282. },
  283. grantTypeLabel(val) {
  284. const map = { JOIN: '开通赠券', PERIODIC: '周期补发', POPULATION: '家庭人口券', REGISTER: '新用户注册赠券' }
  285. return map[val] || val
  286. },
  287. async loadData() {
  288. this.loading = true
  289. try {
  290. const res = await getCouponList({ page: this.page, size: this.size, keyword: this.keyword || undefined, sort: this.sortState.length > 0 ? this.sortState : undefined })
  291. if (res.data) {
  292. this.list = res.data.records || res.data.list || res.data
  293. this.total = res.data.total || this.list.length
  294. }
  295. } catch (e) {
  296. console.error(e)
  297. } finally {
  298. this.loading = false
  299. }
  300. },
  301. loadList() {
  302. this.loadData()
  303. },
  304. handleSearch() {
  305. this.page = 1
  306. this.loadData()
  307. },
  308. onSizeChange(val) {
  309. this.size = val
  310. this.page = 1
  311. this.loadData()
  312. },
  313. onPageChange(p) {
  314. if (p) this.page = p
  315. this.loadData()
  316. },
  317. handleCreate() {
  318. this.isEdit = false
  319. this.form = this.getEmptyForm()
  320. this.formDialogVisible = true
  321. },
  322. handleEdit(row) {
  323. this.isEdit = true
  324. this.form = {
  325. id: row.id,
  326. name: row.name,
  327. type: row.type,
  328. valueYuan: row.type === 'DISCOUNT' ? null : row.value / 100,
  329. minSpendYuan: row.minSpend ? row.minSpend / 100 : null,
  330. discountRate: row.discountRate || null,
  331. productId: row.productId || null,
  332. pointsPrice: row.pointsPrice || 0,
  333. grantType: row.grantType || '',
  334. grantLevelCode: row.grantLevelCode || 'FAMILY',
  335. grantPeriod: row.grantPeriod || 'MONTHLY',
  336. grantQuantity: row.grantQuantity || 1,
  337. applicableTo: row.applicableTo,
  338. validFrom: row.validFrom,
  339. validUntil: row.validUntil,
  340. totalCount: row.totalCount
  341. }
  342. this.formDialogVisible = true
  343. },
  344. async handleDelete(row) {
  345. try {
  346. await this.$confirm('确定删除该优惠券吗?', '提示', { type: 'warning' })
  347. await deleteCoupon(row.id)
  348. this.$message.success('删除成功')
  349. this.loadData()
  350. } catch (e) {
  351. if (e !== 'cancel') this.$message.error('删除失败')
  352. }
  353. },
  354. async handleSubmit() {
  355. const isDiscount = this.form.type === 'DISCOUNT'
  356. if (isDiscount) {
  357. if (!this.form.discountRate) {
  358. this.$message.warning('折扣券请填写折扣率')
  359. return
  360. }
  361. } else if (!this.form.valueYuan) {
  362. this.$message.warning('请填写面值')
  363. return
  364. }
  365. if (!this.form.name || !this.form.totalCount || !this.form.validFrom || !this.form.validUntil) {
  366. this.$message.warning('请填写完整信息')
  367. return
  368. }
  369. this.submitting = true
  370. try {
  371. const payload = {
  372. name: this.form.name,
  373. type: this.form.type,
  374. value: isDiscount ? 0 : Math.round(this.form.valueYuan * 100),
  375. minSpend: this.form.minSpendYuan ? Math.round(this.form.minSpendYuan * 100) : 0,
  376. discountRate: isDiscount ? this.form.discountRate : null,
  377. productId: this.form.productId || null,
  378. pointsPrice: this.form.pointsPrice || 0,
  379. grantType: this.form.grantType || null,
  380. grantLevelCode: this.form.grantType ? this.form.grantLevelCode : null,
  381. grantPeriod: this.form.grantType === 'PERIODIC' ? this.form.grantPeriod : null,
  382. grantQuantity: this.form.grantType ? this.form.grantQuantity : null,
  383. applicableTo: this.form.applicableTo,
  384. validFrom: this.form.validFrom,
  385. validUntil: this.form.validUntil,
  386. totalCount: this.form.totalCount
  387. }
  388. if (this.isEdit) payload.id = this.form.id
  389. if (this.isEdit) {
  390. await updateCoupon(payload)
  391. } else {
  392. await createCoupon(payload)
  393. }
  394. this.$message.success('保存成功')
  395. this.successDialogVisible = true
  396. } catch (e) {
  397. this.$message.error('保存失败')
  398. } finally {
  399. this.submitting = false
  400. }
  401. },
  402. async handleBatchIssue() {
  403. this.batchCouponId = ''
  404. this.batchFamilyIds = ''
  405. this.batchDialogVisible = true
  406. try {
  407. const res = await getCouponList({ page: 1, size: 999, sort: this.sortState.length > 0 ? this.sortState : undefined })
  408. this.couponOptions = res.data.records || res.data.list || res.data || []
  409. } catch (e) {
  410. console.error(e)
  411. }
  412. },
  413. async confirmBatchIssue() {
  414. if (!this.batchCouponId) {
  415. this.$message.warning('请选择优惠券')
  416. return
  417. }
  418. const ids = this.batchFamilyIds.split('\n').map(s => s.trim()).filter(Boolean)
  419. if (ids.length === 0) {
  420. this.$message.warning('请输入至少一个家庭ID')
  421. return
  422. }
  423. this.batchSubmitting = true
  424. try {
  425. await batchIssueCoupons({ couponId: this.batchCouponId, familyIds: ids })
  426. this.$message.success(`成功向 ${ids.length} 个家庭发放优惠券`)
  427. this.batchDialogVisible = false
  428. this.loadData()
  429. } catch (e) {
  430. this.$message.error('发放失败')
  431. } finally {
  432. this.batchSubmitting = false
  433. }
  434. },
  435. handleActionCmd(row, cmd) {
  436. switch (cmd) {
  437. case 'toggle-on':
  438. this.handleToggleStatus(row, 'ACTIVE')
  439. break
  440. case 'toggle-off':
  441. this.handleToggleStatus(row, 'DISABLED')
  442. break
  443. case 'delete':
  444. this.handleDelete(row)
  445. break
  446. }
  447. },
  448. async handleToggleStatus(row, status) {
  449. const action = status === 'ACTIVE' ? '启用' : '停用'
  450. try {
  451. await this.$confirm(`确定${action}该优惠券吗?${status === 'DISABLED' ? '停用后用户将无法领取和使用。' : ''}`, '提示', { type: 'warning' })
  452. await toggleCouponStatus(row.id, status)
  453. this.$message.success(`${action}成功`)
  454. this.loadData()
  455. } catch (e) {
  456. if (e !== 'cancel') this.$message.error(`${action}失败`)
  457. }
  458. },
  459. handleContinue() {
  460. this.successDialogVisible = false
  461. this.isEdit = false
  462. this.form = this.getEmptyForm()
  463. this.formDialogVisible = true
  464. },
  465. handleClose() {
  466. this.successDialogVisible = false
  467. this.formDialogVisible = false
  468. this.loadData()
  469. }
  470. }
  471. }
  472. </script>
  473. <style scoped>
  474. .coupon-management { padding: 24px; }
  475. /* == C 批内联清理工具类 == */
  476. .full-width { width: 100%; }
  477. .w-200 { width: 200px; }
  478. .success-body { text-align: center; padding: 10px 0; }
  479. .success-icon { color: var(--adm-success); font-size: 32px; }
  480. .success-text { margin: 12px 0 0; font-size: 16px; color: var(--adm-text-88); }
  481. </style>