| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- <template>
- <div class="coupon-management admin-page">
- <el-card>
- <PageHeader slot="header" title="优惠券管理">
-
- <div class="admin-page-actions">
- <el-input v-model="keyword" placeholder="搜索优惠券..." prefix-icon="el-icon-search" clearable @keyup.enter.native="handleSearch" @clear="handleSearch" />
- <el-tag
- v-if="sortState.length > 0"
- closable
- size="mini"
- class="sort-tag"
- @close="clearSort"
- @click="clearSort"
- >
- {{ sortLabel }}
- </el-tag>
- <el-button type="primary" size="small" @click="handleCreate">新建优惠券</el-button>
- <el-button type="success" size="small" @click="handleBatchIssue">批量发放</el-button>
- </div>
-
- </PageHeader>
- <div class="adm-table-wrap">
- <el-table height="100%" :data="list" v-loading="loading" border stripe>
- <el-table-column prop="id" label="ID" width="70" />
- <el-table-column prop="name" label="名称" min-width="140" show-overflow-tooltip>
- <template slot="header">
- <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>
- </template>
- </el-table-column>
- <el-table-column label="类型" width="110">
- <template slot-scope="{ row }">
- <StatusTag size="mini" type="primary" :text="typeLabel(row.type)" />
- </template>
- </el-table-column>
- <el-table-column label="面值/折扣" width="100">
- <template slot-scope="{ row }">
- <span v-if="row.type === 'DISCOUNT'">{{ (row.discountRate / 100).toFixed(1) }}折</span>
- <span v-else>{{ (row.value / 100).toFixed(2) }}元</span>
- </template>
- </el-table-column>
- <el-table-column label="使用门槛(元)" width="110">
- <template slot-scope="{ row }">{{ row.minSpend ? (row.minSpend / 100).toFixed(2) : '无' }}</template>
- </el-table-column>
- <el-table-column label="适用对象" width="110">
- <template slot-scope="{ row }">{{ applicableLabel(row.applicableTo) }}</template>
- </el-table-column>
- <el-table-column label="积分价" width="80">
- <template slot-scope="{ row }">{{ row.pointsPrice || 0 }}</template>
- </el-table-column>
- <el-table-column label="发放规则" width="130">
- <template slot-scope="{ row }">
- <span v-if="row.grantType">{{ grantTypeLabel(row.grantType) }}</span>
- <span v-else>-</span>
- </template>
- </el-table-column>
- <el-table-column label="有效期" width="180">
- <template slot-scope="{ row }">{{ row.validFrom }} ~ {{ row.validUntil }}</template>
- </el-table-column>
- <el-table-column prop="totalCount" label="总量" width="70">
- <template slot="header">
- <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>
- </template>
- </el-table-column>
- <el-table-column prop="usedCount" label="已用" width="70">
- <template slot="header">
- <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>
- </template>
- </el-table-column>
- <el-table-column label="状态" width="80">
- <template slot-scope="{ row }">
- <StatusTag size="mini" :type="row.status !== 'DISABLED' ? 'success' : 'info'" :text="row.status !== 'DISABLED' ? '启用' : '停用'" />
- </template>
- </el-table-column>
- <el-table-column label="操作" width="180" fixed="right">
- <template slot-scope="{ row }">
- <el-button size="mini" type="primary" @click="handleEdit(row)">编辑</el-button>
- <el-dropdown trigger="hover" @command="(cmd) => handleActionCmd(row, cmd)">
- <el-button size="mini">
- 更多<i class="el-icon-arrow-down el-icon--right"></i>
- </el-button>
- <el-dropdown-menu slot="dropdown">
- <el-dropdown-item command="toggle-on" icon="el-icon-check" v-if="row.status === 'DISABLED'">启用</el-dropdown-item>
- <el-dropdown-item command="toggle-off" icon="el-icon-remove-outline" v-if="row.status !== 'DISABLED'">停用</el-dropdown-item>
- <el-dropdown-item command="delete" icon="el-icon-delete">删除</el-dropdown-item>
- </el-dropdown-menu>
- </el-dropdown>
- </template>
- </el-table-column>
- </el-table>
- </div>
- <el-pagination
- @current-change="onPageChange"
- @size-change="onSizeChange"
- :current-page="page"
- :page-size="size"
- :total="total"
- layout="total, prev, pager, next"
- class="pagination-wrap"
- />
- </el-card>
- <el-dialog :visible.sync="formDialogVisible" :title="isEdit ? '编辑优惠券' : '新建优惠券'" width="550px">
- <el-form :model="form" label-width="120px" ref="couponForm">
- <el-form-item label="优惠券名称" required>
- <el-input v-model="form.name" placeholder="请输入优惠券名称" />
- </el-form-item>
- <el-form-item label="类型" required>
- <el-select v-model="form.type" placeholder="请选择类型" class="full-width">
- <el-option label="FIXED(满减)" value="FIXED" />
- <el-option label="CASH(无门槛)" value="CASH" />
- <el-option label="DISCOUNT(折扣)" value="DISCOUNT" />
- </el-select>
- </el-form-item>
- <el-form-item v-if="form.type !== 'DISCOUNT'" label="面值(元)" required>
- <el-input-number v-model="form.valueYuan" :min="0.01" :step="0.5" :precision="2" class="w-200" />
- </el-form-item>
- <el-form-item v-if="form.type === 'DISCOUNT'" label="折扣率(千分比)" required>
- <el-input-number v-model="form.discountRate" :min="1" :max="10000" :step="100" class="w-200" />
- <div class="form-item-tip">9000 = 9折,10000 = 无折扣</div>
- </el-form-item>
- <el-form-item v-if="form.type === 'FIXED'" label="使用门槛(元)">
- <el-input-number v-model="form.minSpendYuan" :min="0" :step="10" :precision="2" class="w-200" />
- </el-form-item>
- <el-form-item label="适用对象">
- <el-select v-model="form.applicableTo" placeholder="请选择适用对象" class="full-width">
- <el-option label="全部商品" value="ALL" />
- <el-option label="会员服务" value="MEMBERSHIP" />
- <el-option label="特定商品" value="PRODUCT" />
- <el-option label="测评服务" value="ASSESSMENT" />
- </el-select>
- </el-form-item>
- <el-form-item label="绑定商品">
- <product-picker v-model="form.productId" placeholder="输入商品名称搜索,留空=全场通用" />
- <div class="form-item-tip">留空 = 全场通用;仅适用对象选"特定商品"时必填</div>
- </el-form-item>
- <el-form-item label="积分兑换价">
- <el-input-number v-model="form.pointsPrice" :min="0" :step="100" class="w-200" />
- <div class="form-item-tip">0 = 不可积分兑换</div>
- </el-form-item>
- <el-form-item label="自动发放规则">
- <el-select v-model="form.grantType" placeholder="不自动发放" class="full-width">
- <el-option label="不自动发放" value="" />
- <el-option label="JOIN(开通会员赠)" value="JOIN" />
- <el-option label="PERIODIC(周期补发)" value="PERIODIC" />
- <el-option label="POPULATION(家庭人口券)" value="POPULATION" />
- <el-option label="REGISTER(新用户注册赠)" value="REGISTER" />
- </el-select>
- </el-form-item>
- <el-form-item v-if="form.grantType === 'JOIN' || form.grantType === 'PERIODIC'" label="适用会员等级">
- <el-select v-model="form.grantLevelCode" placeholder="请选择会员等级" class="full-width">
- <el-option v-for="lv in memberLevelOptions" :key="lv.levelCode" :label="lv.levelName + '(' + lv.levelCode + ')'" :value="lv.levelCode" />
- </el-select>
- </el-form-item>
- <el-form-item v-if="form.grantType === 'PERIODIC'" label="补发周期">
- <el-select v-model="form.grantPeriod" placeholder="请选择补发周期" class="full-width">
- <el-option label="MONTHLY(每月)" value="MONTHLY" />
- <el-option label="QUARTERLY(每季度)" value="QUARTERLY" />
- </el-select>
- </el-form-item>
- <el-form-item v-if="form.grantType && form.grantType !== ''" label="每次发放数量">
- <el-input-number v-model="form.grantQuantity" :min="1" :step="1" class="w-200" />
- </el-form-item>
- <el-form-item label="有效期起" required>
- <el-date-picker v-model="form.validFrom" type="date" placeholder="选择开始日期" value-format="yyyy-MM-dd" class="full-width" />
- </el-form-item>
- <el-form-item label="有效期止" required>
- <el-date-picker v-model="form.validUntil" type="date" placeholder="选择结束日期" value-format="yyyy-MM-dd" class="full-width" />
- </el-form-item>
- <el-form-item label="发放总量" required>
- <el-input-number v-model="form.totalCount" :min="1" :step="100" class="w-200" />
- </el-form-item>
- </el-form>
- <div slot="footer">
- <el-button @click="formDialogVisible = false">取消</el-button>
- <el-button type="primary" @click="handleSubmit" :loading="submitting">保存</el-button>
- </div>
- </el-dialog>
- <el-dialog title="操作成功" :visible.sync="successDialogVisible" width="400px" :close-on-click-modal="false">
- <div class="success-body">
- <i class="el-icon-success success-icon"></i>
- <p class="success-text">保存成功</p>
- </div>
- <div slot="footer">
- <el-button type="primary" @click="handleContinue">继续添加</el-button>
- <el-button @click="handleClose">关闭</el-button>
- </div>
- </el-dialog>
- <el-dialog title="批量发放优惠券" :visible.sync="batchDialogVisible" width="500px">
- <el-form label-width="120px">
- <el-form-item label="选择优惠券" required>
- <el-select v-model="batchCouponId" placeholder="请选择优惠券" class="full-width">
- <el-option v-for="c in couponOptions" :key="c.id" :label="c.name" :value="c.id" />
- </el-select>
- </el-form-item>
- <el-form-item label="家庭ID列表" required>
- <el-input v-model="batchFamilyIds" type="textarea" :rows="6" placeholder="每行一个家庭ID 例如: 1001 1002 1003" />
- </el-form-item>
- </el-form>
- <div slot="footer">
- <el-button @click="batchDialogVisible = false">取消</el-button>
- <el-button type="primary" @click="confirmBatchIssue" :loading="batchSubmitting">确认发放</el-button>
- </div>
- </el-dialog>
- </div>
- </template>
- <script>
- import SortMixin from '@/mixins/SortMixin'
- import ListStateMixin from '@/mixins/ListStateMixin'
- import { getCouponList, createCoupon, updateCoupon, deleteCoupon, batchIssueCoupons, toggleCouponStatus } from '@/api/coupon'
- import { listMembershipLevels } from '@/api/admin'
- import ProductPicker from '@/components/ProductPicker'
- export default {
- mixins: [SortMixin, ListStateMixin],
- name: 'CouponManagement',
- components: { ProductPicker },
- computed: {
- tableHeight() {
- return window.innerHeight - 300
- }
- },
- data() {
- return {
- list: [],
- loading: false,
- page: 1,
- size: 20,
- total: 0,
- formDialogVisible: false,
- successDialogVisible: false,
- isEdit: false,
- submitting: false,
- form: this.getEmptyForm(),
- batchDialogVisible: false,
- batchCouponId: '',
- batchFamilyIds: '',
- batchSubmitting: false,
- couponOptions: [],
- memberLevelOptions: [],
- keyword: '',
- sortableColumns: { name: '名称', totalCount: '总量', usedCount: '已用' }
- }
- },
- created() {
- this.loadData()
- this.loadMemberLevels()
- },
- methods: {
- async loadMemberLevels() {
- try {
- const res = await listMembershipLevels()
- const list = res.data || []
- this.memberLevelOptions = Array.isArray(list) ? list.filter(function(l) { return l && typeof l === 'object' && l.levelCode }) : []
- } catch (e) {
- this.memberLevelOptions = []
- }
- },
- getEmptyForm() {
- return {
- id: null,
- name: '',
- type: 'FIXED',
- valueYuan: null,
- minSpendYuan: null,
- discountRate: null,
- productId: null,
- pointsPrice: 0,
- grantType: '',
- grantLevelCode: 'FAMILY',
- grantPeriod: 'MONTHLY',
- grantQuantity: 1,
- applicableTo: 'ALL',
- validFrom: '',
- validUntil: '',
- totalCount: null
- }
- },
- applicableLabel(val) {
- const map = { ALL: '全部商品', MEMBERSHIP: '会员服务', PRODUCT: '特定商品', ASSESSMENT: '测评服务' }
- return map[val] || val
- },
- typeLabel(val) {
- const map = { FIXED: '满减券', CASH: '无门槛券', DISCOUNT: '折扣券', PERCENT: '折扣券' }
- return map[val] || val
- },
- grantTypeLabel(val) {
- const map = { JOIN: '开通赠券', PERIODIC: '周期补发', POPULATION: '家庭人口券', REGISTER: '新用户注册赠券' }
- return map[val] || val
- },
- async loadData() {
- this.loading = true
- try {
- const res = await getCouponList({ page: this.page, size: this.size, keyword: this.keyword || undefined, sort: this.sortState.length > 0 ? this.sortState : undefined })
- if (res.data) {
- this.list = res.data.records || res.data.list || res.data
- this.total = res.data.total || this.list.length
- }
- } catch (e) {
- console.error(e)
- } finally {
- this.loading = false
- }
- },
- loadList() {
- this.loadData()
- },
- handleSearch() {
- this.page = 1
- this.loadData()
- },
- onSizeChange(val) {
- this.size = val
- this.page = 1
- this.loadData()
- },
- onPageChange(p) {
- if (p) this.page = p
- this.loadData()
- },
- handleCreate() {
- this.isEdit = false
- this.form = this.getEmptyForm()
- this.formDialogVisible = true
- },
- handleEdit(row) {
- this.isEdit = true
- this.form = {
- id: row.id,
- name: row.name,
- type: row.type,
- valueYuan: row.type === 'DISCOUNT' ? null : row.value / 100,
- minSpendYuan: row.minSpend ? row.minSpend / 100 : null,
- discountRate: row.discountRate || null,
- productId: row.productId || null,
- pointsPrice: row.pointsPrice || 0,
- grantType: row.grantType || '',
- grantLevelCode: row.grantLevelCode || 'FAMILY',
- grantPeriod: row.grantPeriod || 'MONTHLY',
- grantQuantity: row.grantQuantity || 1,
- applicableTo: row.applicableTo,
- validFrom: row.validFrom,
- validUntil: row.validUntil,
- totalCount: row.totalCount
- }
- this.formDialogVisible = true
- },
- async handleDelete(row) {
- try {
- await this.$confirm('确定删除该优惠券吗?', '提示', { type: 'warning' })
- await deleteCoupon(row.id)
- this.$message.success('删除成功')
- this.loadData()
- } catch (e) {
- if (e !== 'cancel') this.$message.error('删除失败')
- }
- },
- async handleSubmit() {
- const isDiscount = this.form.type === 'DISCOUNT'
- if (isDiscount) {
- if (!this.form.discountRate) {
- this.$message.warning('折扣券请填写折扣率')
- return
- }
- } else if (!this.form.valueYuan) {
- this.$message.warning('请填写面值')
- return
- }
- if (!this.form.name || !this.form.totalCount || !this.form.validFrom || !this.form.validUntil) {
- this.$message.warning('请填写完整信息')
- return
- }
- this.submitting = true
- try {
- const payload = {
- name: this.form.name,
- type: this.form.type,
- value: isDiscount ? 0 : Math.round(this.form.valueYuan * 100),
- minSpend: this.form.minSpendYuan ? Math.round(this.form.minSpendYuan * 100) : 0,
- discountRate: isDiscount ? this.form.discountRate : null,
- productId: this.form.productId || null,
- pointsPrice: this.form.pointsPrice || 0,
- grantType: this.form.grantType || null,
- grantLevelCode: this.form.grantType ? this.form.grantLevelCode : null,
- grantPeriod: this.form.grantType === 'PERIODIC' ? this.form.grantPeriod : null,
- grantQuantity: this.form.grantType ? this.form.grantQuantity : null,
- applicableTo: this.form.applicableTo,
- validFrom: this.form.validFrom,
- validUntil: this.form.validUntil,
- totalCount: this.form.totalCount
- }
- if (this.isEdit) payload.id = this.form.id
- if (this.isEdit) {
- await updateCoupon(payload)
- } else {
- await createCoupon(payload)
- }
- this.$message.success('保存成功')
- this.successDialogVisible = true
- } catch (e) {
- this.$message.error('保存失败')
- } finally {
- this.submitting = false
- }
- },
- async handleBatchIssue() {
- this.batchCouponId = ''
- this.batchFamilyIds = ''
- this.batchDialogVisible = true
- try {
- const res = await getCouponList({ page: 1, size: 999, sort: this.sortState.length > 0 ? this.sortState : undefined })
- this.couponOptions = res.data.records || res.data.list || res.data || []
- } catch (e) {
- console.error(e)
- }
- },
- async confirmBatchIssue() {
- if (!this.batchCouponId) {
- this.$message.warning('请选择优惠券')
- return
- }
- const ids = this.batchFamilyIds.split('\n').map(s => s.trim()).filter(Boolean)
- if (ids.length === 0) {
- this.$message.warning('请输入至少一个家庭ID')
- return
- }
- this.batchSubmitting = true
- try {
- await batchIssueCoupons({ couponId: this.batchCouponId, familyIds: ids })
- this.$message.success(`成功向 ${ids.length} 个家庭发放优惠券`)
- this.batchDialogVisible = false
- this.loadData()
- } catch (e) {
- this.$message.error('发放失败')
- } finally {
- this.batchSubmitting = false
- }
- },
- handleActionCmd(row, cmd) {
- switch (cmd) {
- case 'toggle-on':
- this.handleToggleStatus(row, 'ACTIVE')
- break
- case 'toggle-off':
- this.handleToggleStatus(row, 'DISABLED')
- break
- case 'delete':
- this.handleDelete(row)
- break
- }
- },
- async handleToggleStatus(row, status) {
- const action = status === 'ACTIVE' ? '启用' : '停用'
- try {
- await this.$confirm(`确定${action}该优惠券吗?${status === 'DISABLED' ? '停用后用户将无法领取和使用。' : ''}`, '提示', { type: 'warning' })
- await toggleCouponStatus(row.id, status)
- this.$message.success(`${action}成功`)
- this.loadData()
- } catch (e) {
- if (e !== 'cancel') this.$message.error(`${action}失败`)
- }
- },
- handleContinue() {
- this.successDialogVisible = false
- this.isEdit = false
- this.form = this.getEmptyForm()
- this.formDialogVisible = true
- },
- handleClose() {
- this.successDialogVisible = false
- this.formDialogVisible = false
- this.loadData()
- }
- }
- }
- </script>
- <style scoped>
- .coupon-management { padding: 24px; }
- /* == C 批内联清理工具类 == */
- .full-width { width: 100%; }
- .w-200 { width: 200px; }
- .success-body { text-align: center; padding: 10px 0; }
- .success-icon { color: var(--adm-success); font-size: 32px; }
- .success-text { margin: 12px 0 0; font-size: 16px; color: var(--adm-text-88); }
- </style>
|