Files
JayuCloud/Frontend/src/views/admin/CouponsView.vue
T

738 lines
24 KiB
Vue
Raw Normal View History

2026-02-27 11:05:15 +08:00
<template>
<div class="coupons">
<a-page-header title="优惠券管理" sub-title="管理系统优惠券" />
2026-02-27 11:05:15 +08:00
<a-card :bordered="false">
<template #extra>
<a-space>
<a-input-search
v-model:value="searchText"
placeholder="搜索优惠券名称"
style="width: 260px"
2026-02-27 11:05:15 +08:00
@search="handleSearch"
/>
<a-button v-if="hasPermission('coupon:create')" type="primary" @click="showAddModal">
<template #icon>
<PlusOutlined />
</template>
新增优惠券
</a-button>
</a-space>
</template>
<a-table
:columns="columns"
:data-source="couponList"
:loading="loading"
:pagination="pagination"
row-key="id"
@change="handleTableChange"
2026-02-27 11:05:15 +08:00
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'shop'">
{{ record.shopName || '未指定店铺' }}
</template>
<template v-else-if="column.key === 'couponType'">
2026-02-27 11:05:15 +08:00
<a-tag :color="record.couponType === 1 ? 'blue' : 'green'">
{{ record.couponTypeName || (record.couponType === 1 ? '折扣券' : '满减券') }}
2026-02-27 11:05:15 +08:00
</a-tag>
</template>
<template v-else-if="column.key === 'couponScene'">
<a-tag :color="record.couponScene === 'VIP_AUTO' ? 'gold' : 'default'">
{{ getCouponSceneText(record.couponScene) }}
</a-tag>
</template>
<template v-else-if="column.key === 'requiredVipLevel'">
{{ record.requiredVipLevel || '-' }}
</template>
2026-02-27 11:05:15 +08:00
<template v-else-if="column.key === 'value'">
<template v-if="record.couponType === 1">
{{ record.discountValue }} 折
2026-02-27 11:05:15 +08:00
</template>
<template v-else>
满 {{ record.minAmount }} 减 {{ record.reduceAmount }}
2026-02-27 11:05:15 +08:00
</template>
</template>
2026-02-27 11:05:15 +08:00
<template v-else-if="column.key === 'status'">
<a-tag :color="getStatusColor(record.status || 1)">
{{ record.statusName || '未知' }}
2026-02-27 11:05:15 +08:00
</a-tag>
</template>
2026-02-27 11:05:15 +08:00
<template v-else-if="column.key === 'actions'">
<a-space wrap>
2026-02-28 14:32:28 +08:00
<a-button v-if="hasPermission('coupon:view')" type="link" size="small" @click="showViewModal(record)">
查看
</a-button>
<a-button v-if="hasPermission('coupon:edit')" type="link" size="small" @click="showEditModal(record)">
2026-02-27 11:05:15 +08:00
编辑
</a-button>
<a-button
v-if="hasPermission('coupon:assign') && !isVipAutoCoupon(record) && record.status === 1"
type="link"
size="small"
@click="showDistributeModal(record)"
>
发放
</a-button>
<a-button
v-if="hasPermission('coupon:delete') && record.status !== 2"
type="link"
size="small"
danger
@click="handleDelete(record)"
>
2026-02-27 11:05:15 +08:00
删除
</a-button>
</a-space>
</template>
</template>
</a-table>
</a-card>
<a-modal
v-model:open="modalVisible"
:title="isEdit ? '编辑优惠券' : '新增优惠券'"
width="820px"
:confirm-loading="modalLoading"
2026-02-27 11:05:15 +08:00
@ok="handleModalOk"
@cancel="handleModalCancel"
>
2026-03-26 12:04:02 +08:00
<DraftActions
:saved-at="draftSavedAt"
@save="handleSaveDraft"
@clear="handleClearDraft"
/>
2026-02-27 11:05:15 +08:00
<a-form
ref="formRef"
:model="formState"
:rules="formRules"
:label-col="{ span: 6 }"
:wrapper-col="{ span: 16 }"
>
<a-form-item label="优惠券名称" name="couponName">
<a-input v-model:value="formState.couponName" placeholder="请输入优惠券名称" />
</a-form-item>
<a-form-item label="所属店铺" name="shopId">
<a-select
v-model:value="formState.shopId"
placeholder="请选择所属店铺"
:loading="loadingShops"
:disabled="currentUserInfo?.type === 2"
>
<a-select-option v-for="shop in shops" :key="shop.id" :value="shop.id">
{{ shop.shopName }}
</a-select-option>
</a-select>
</a-form-item>
2026-02-27 11:05:15 +08:00
<a-form-item label="优惠券类型" name="couponType">
<a-select v-model:value="formState.couponType" placeholder="请选择优惠券类型">
<a-select-option :value="1">折扣券</a-select-option>
<a-select-option :value="2">满减券</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="优惠场景" name="couponScene">
<a-select v-model:value="formState.couponScene" placeholder="请选择优惠场景">
<a-select-option value="GENERAL">普通券</a-select-option>
<a-select-option value="VIP_AUTO">VIP 自动券</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="自动匹配">
<a-switch :checked="formState.autoMatch === 1" @change="(checked: any) => handleAutoMatchChange(checked === true)" />
</a-form-item>
<a-form-item v-if="isVipAutoForm" label="所需 VIP 等级" name="requiredVipLevel">
<a-select v-model:value="formState.requiredVipLevel" placeholder="请选择 VIP 等级">
<a-select-option value="青铜">青铜</a-select-option>
<a-select-option value="白银">白银</a-select-option>
<a-select-option value="黄金">黄金</a-select-option>
</a-select>
</a-form-item>
2026-02-27 11:05:15 +08:00
<a-form-item v-if="formState.couponType === 1" label="折扣值" name="discountValue">
<a-input-number v-model:value="formState.discountValue" :min="0.1" :max="9.9" :step="0.1" style="width: 100%" />
2026-02-27 11:05:15 +08:00
</a-form-item>
2026-02-27 11:05:15 +08:00
<a-form-item v-if="formState.couponType === 2" label="最低消费" name="minAmount">
<a-input-number v-model:value="formState.minAmount" :min="0" :step="0.01" style="width: 100%" />
2026-02-27 11:05:15 +08:00
</a-form-item>
2026-02-27 11:05:15 +08:00
<a-form-item v-if="formState.couponType === 2" label="减免金额" name="reduceAmount">
<a-input-number v-model:value="formState.reduceAmount" :min="0" :step="0.01" style="width: 100%" />
2026-02-27 11:05:15 +08:00
</a-form-item>
2026-02-27 11:05:15 +08:00
<a-form-item label="总数量" name="totalCount">
<a-input-number v-model:value="formState.totalCount" :min="1" style="width: 100%" />
2026-02-27 11:05:15 +08:00
</a-form-item>
2026-02-27 11:05:15 +08:00
<a-form-item label="开始时间" name="startTime">
<a-date-picker v-model:value="formState.startTime" show-time format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
</a-form-item>
2026-02-27 11:05:15 +08:00
<a-form-item label="结束时间" name="endTime">
<a-date-picker v-model:value="formState.endTime" show-time format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
</a-form-item>
2026-02-27 11:05:15 +08:00
<a-form-item label="可用标签" name="availableTagIds">
<a-select v-model:value="formState.availableTagIds" mode="multiple" placeholder="请选择可用标签">
2026-02-27 11:05:15 +08:00
<a-select-option v-for="tag in tagList" :key="tag.id" :value="tag.id">
{{ tag.tagName }}
</a-select-option>
</a-select>
</a-form-item>
2026-02-27 11:05:15 +08:00
<a-form-item label="不可用标签" name="unavailableTagIds">
<a-select v-model:value="formState.unavailableTagIds" mode="multiple" placeholder="请选择不可用标签">
2026-02-27 11:05:15 +08:00
<a-select-option v-for="tag in tagList" :key="tag.id" :value="tag.id">
{{ tag.tagName }}
</a-select-option>
</a-select>
</a-form-item>
</a-form>
</a-modal>
<a-modal
v-model:open="viewModalVisible"
title="查看优惠券详情"
:footer="null"
width="760px"
@cancel="viewModalVisible = false"
>
<a-descriptions bordered :column="1" size="small">
<a-descriptions-item label="优惠券名称">{{ currentCoupon?.couponName || '-' }}</a-descriptions-item>
<a-descriptions-item label="所属店铺">{{ currentCoupon?.shopName || '-' }}</a-descriptions-item>
<a-descriptions-item label="优惠券类型">{{ currentCoupon?.couponType === 1 ? '折扣券' : '满减券' }}</a-descriptions-item>
<a-descriptions-item label="优惠场景">{{ getCouponSceneText(currentCoupon?.couponScene) }}</a-descriptions-item>
<a-descriptions-item label="所需 VIP 等级">{{ currentCoupon?.requiredVipLevel || '-' }}</a-descriptions-item>
<a-descriptions-item label="自动匹配">{{ currentCoupon?.autoMatch === 1 ? '是' : '否' }}</a-descriptions-item>
<a-descriptions-item label="优惠信息">{{ getCouponValueText(currentCoupon) }}</a-descriptions-item>
<a-descriptions-item label="总数量">{{ currentCoupon?.totalCount ?? '-' }}</a-descriptions-item>
<a-descriptions-item label="已使用">{{ currentCoupon?.usedCount ?? '-' }}</a-descriptions-item>
<a-descriptions-item label="开始时间">{{ currentCoupon?.startTime || '-' }}</a-descriptions-item>
<a-descriptions-item label="结束时间">{{ currentCoupon?.endTime || '-' }}</a-descriptions-item>
</a-descriptions>
</a-modal>
<a-modal
v-model:open="distributeModalVisible"
title="发放优惠券"
width="760px"
:confirm-loading="distributeLoading"
@ok="handleDistributeOk"
@cancel="handleDistributeCancel"
>
<a-form
ref="distributeFormRef"
:model="distributeFormState"
:rules="distributeFormRules"
:label-col="{ span: 6 }"
:wrapper-col="{ span: 16 }"
>
<a-form-item label="优惠券">
<a-input v-model:value="distributeFormState.couponName" disabled />
</a-form-item>
<a-form-item label="选择用户" name="userIds">
<a-select
v-model:value="distributeFormState.userIds"
mode="multiple"
placeholder="请选择要发放的用户"
>
<a-select-option v-for="user in userList" :key="user.id" :value="user.id">
{{ user.nickname }}({{ user.account }})
</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="发放数量" name="quantity">
<a-input-number v-model:value="distributeFormState.quantity" :min="1" style="width: 100%" />
2026-02-28 14:32:28 +08:00
</a-form-item>
</a-form>
</a-modal>
2026-02-27 11:05:15 +08:00
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, ref, watch } from 'vue'
2026-03-10 23:23:15 +08:00
import { message, Modal } from 'ant-design-vue'
2026-02-27 11:05:15 +08:00
import type { FormInstance } from 'ant-design-vue'
import { PlusOutlined } from '@ant-design/icons-vue'
2026-03-26 12:04:02 +08:00
import DraftActions from '@/components/DraftActions.vue'
import dayjs, { type Dayjs } from 'dayjs'
import {
adminCouponApi,
adminShopApi,
adminTagApi,
adminUserApi,
type Coupon,
type Shop,
type Tag,
type UserVO
} from '@/api/admin'
2026-03-26 12:04:02 +08:00
import { buildDraftStorageKey, clearDraftRecord, loadDraftRecord, saveDraftRecord } from '@/utils/form-draft'
import { usePermission } from '@/utils/permission'
2026-02-27 11:05:15 +08:00
const { hasPermission, loadUserPermissions } = usePermission()
const currentUserInfo = (() => {
try {
const userInfoStr = localStorage.getItem('adminUserInfo')
return userInfoStr ? JSON.parse(userInfoStr) : null
} catch (error) {
console.error('获取当前用户信息失败', error)
return null
}
})()
interface CouponFormState {
2026-02-27 11:05:15 +08:00
couponCode: string
couponName: string
couponType: number
discountValue?: number
minAmount?: number
reduceAmount?: number
totalCount: number
startTime?: Dayjs
endTime?: Dayjs
2026-02-27 11:05:15 +08:00
availableTagIds: number[]
unavailableTagIds: number[]
shopId?: number
couponScene: string
requiredVipLevel?: string
autoMatch: number
}
2026-02-27 11:05:15 +08:00
const loading = ref(false)
const loadingShops = ref(false)
2026-02-27 11:05:15 +08:00
const modalVisible = ref(false)
2026-02-28 14:32:28 +08:00
const viewModalVisible = ref(false)
2026-02-27 11:05:15 +08:00
const modalLoading = ref(false)
const isEdit = ref(false)
const searchText = ref('')
const formRef = ref<FormInstance>()
const distributeFormRef = ref<FormInstance>()
2026-02-27 11:05:15 +08:00
const currentCoupon = ref<Coupon | null>(null)
2026-03-26 12:04:02 +08:00
const draftSavedAt = ref<number | null>(null)
const couponList = ref<Coupon[]>([])
const tagList = ref<Tag[]>([])
const userList = ref<UserVO[]>([])
const shops = ref<Shop[]>([])
2026-02-27 11:05:15 +08:00
const distributeModalVisible = ref(false)
const distributeLoading = ref(false)
const currentDistributeCoupon = ref<Coupon | null>(null)
2026-02-27 11:05:15 +08:00
const pagination = reactive({
current: 1,
pageSize: 10,
total: 0,
showSizeChanger: true,
showQuickJumper: true,
showTotal: (total: number) => `共 ${total} 条`
})
const formState = reactive<CouponFormState>({
2026-02-27 11:05:15 +08:00
couponCode: '',
couponName: '',
couponType: 1,
discountValue: 9.5,
minAmount: 100,
reduceAmount: 20,
totalCount: 100,
2026-02-27 22:34:18 +08:00
startTime: dayjs(),
endTime: dayjs().add(30, 'day'),
availableTagIds: [],
unavailableTagIds: [],
shopId: undefined,
couponScene: 'GENERAL',
requiredVipLevel: undefined,
autoMatch: 0
2026-02-27 11:05:15 +08:00
})
const distributeFormState = reactive({
couponName: '',
userIds: [] as number[],
quantity: 1
})
const formRules = {
couponName: [{ required: true, message: '请输入优惠券名称', trigger: 'blur' }],
couponType: [{ required: true, message: '请选择优惠券类型', trigger: 'change' }],
couponScene: [{ required: true, message: '请选择优惠场景', trigger: 'change' }],
requiredVipLevel: [{ required: true, message: '请选择所需 VIP 等级', trigger: 'change' }],
totalCount: [{ required: true, message: '请输入优惠券总数量', trigger: 'blur' }],
startTime: [{ required: true, message: '请选择开始时间', trigger: 'change' }],
endTime: [{ required: true, message: '请选择结束时间', trigger: 'change' }],
shopId: [{ required: true, message: '请选择所属店铺', trigger: 'change' }]
} as any
const distributeFormRules = {
userIds: [{ required: true, message: '请选择要发放的用户', trigger: 'change' }],
quantity: [{ required: true, message: '请输入发放数量', trigger: 'blur' }]
2026-03-10 23:23:15 +08:00
} as any
2026-02-27 11:05:15 +08:00
const columns = [
{ title: '优惠券名称', dataIndex: 'couponName', key: 'couponName' },
{ title: '所属店铺', key: 'shop', width: 140 },
2026-02-27 11:05:15 +08:00
{ title: '类型', key: 'couponType', width: 100 },
{ title: '场景', key: 'couponScene', width: 120 },
{ title: '所需 VIP', key: 'requiredVipLevel', width: 110 },
{ title: '优惠信息', key: 'value', width: 170 },
{ title: '总数量', dataIndex: 'totalCount', key: 'totalCount', width: 90 },
{ title: '已使用', dataIndex: 'usedCount', key: 'usedCount', width: 90 },
{ title: '剩余', dataIndex: 'remainingCount', key: 'remainingCount', width: 90 },
2026-02-27 11:05:15 +08:00
{ title: '状态', key: 'status', width: 100 },
{ title: '操作', key: 'actions', width: 180, fixed: 'right' as const }
2026-02-27 11:05:15 +08:00
]
const isVipAutoForm = computed(() => formState.couponScene === 'VIP_AUTO' || formState.autoMatch === 1)
2026-02-27 11:05:15 +08:00
const getStatusColor = (status: number) => {
switch (status) {
case 1:
return 'green'
case 2:
return 'red'
case 3:
return 'orange'
default:
return 'default'
}
}
const getCouponSceneText = (scene?: string) => {
if (scene === 'VIP_AUTO') {
return 'VIP 自动券'
2026-02-27 11:05:15 +08:00
}
return '普通券'
2026-02-27 11:05:15 +08:00
}
const isVipAutoCoupon = (coupon: any) => coupon?.couponScene === 'VIP_AUTO' || coupon?.autoMatch === 1
const getCouponValueText = (coupon: Coupon | null) => {
if (!coupon) {
return '-'
2026-02-27 11:05:15 +08:00
}
return coupon.couponType === 1
? `${coupon.discountValue} 折`
: `满 ${coupon.minAmount} 减 ${coupon.reduceAmount}`
2026-02-27 11:05:15 +08:00
}
const resetFormState = () => {
2026-02-27 11:05:15 +08:00
Object.assign(formState, {
couponCode: '',
couponName: '',
couponType: 1,
discountValue: 9.5,
minAmount: 100,
reduceAmount: 20,
totalCount: 100,
startTime: dayjs(),
endTime: dayjs().add(30, 'day'),
2026-02-27 11:05:15 +08:00
availableTagIds: [],
unavailableTagIds: [],
shopId: currentUserInfo?.shopId || undefined,
couponScene: 'GENERAL',
requiredVipLevel: undefined,
autoMatch: 0
})
}
2026-03-26 12:04:02 +08:00
const getDraftKey = () => buildDraftStorageKey('admin-coupons', isEdit.value ? 'edit' : 'create', currentCoupon.value?.id ?? 'new')
const buildDraftPayload = () => ({
couponCode: formState.couponCode,
couponName: formState.couponName,
couponType: formState.couponType,
discountValue: formState.discountValue,
minAmount: formState.minAmount,
reduceAmount: formState.reduceAmount,
totalCount: formState.totalCount,
startTime: formState.startTime?.format('YYYY-MM-DD HH:mm:ss'),
endTime: formState.endTime?.format('YYYY-MM-DD HH:mm:ss'),
availableTagIds: [...formState.availableTagIds],
unavailableTagIds: [...formState.unavailableTagIds],
shopId: formState.shopId,
couponScene: formState.couponScene,
requiredVipLevel: formState.requiredVipLevel,
autoMatch: formState.autoMatch
})
const restoreDraft = () => {
const draft = loadDraftRecord<ReturnType<typeof buildDraftPayload>>(getDraftKey())
if (!draft) {
draftSavedAt.value = null
return
}
Object.assign(formState, {
...draft.payload,
startTime: draft.payload.startTime ? dayjs(draft.payload.startTime) : undefined,
endTime: draft.payload.endTime ? dayjs(draft.payload.endTime) : undefined
})
draftSavedAt.value = draft.savedAt
message.success('已恢复暂存内容')
}
const handleSaveDraft = () => {
const draft = saveDraftRecord(getDraftKey(), buildDraftPayload())
draftSavedAt.value = draft.savedAt
message.success('已暂存到本地')
}
const handleClearDraft = (showMessage = true) => {
clearDraftRecord(getDraftKey())
draftSavedAt.value = null
if (showMessage) {
message.success('已清除暂存')
}
}
watch(
() => formState.couponScene,
value => {
if (value === 'VIP_AUTO') {
formState.autoMatch = 1
if (!formState.requiredVipLevel) {
formState.requiredVipLevel = '青铜'
}
} else {
formState.autoMatch = 0
formState.requiredVipLevel = undefined
}
}
)
const handleAutoMatchChange = (checked: boolean) => {
formState.autoMatch = checked ? 1 : 0
formState.couponScene = checked ? 'VIP_AUTO' : 'GENERAL'
}
const fetchCoupons = async () => {
loading.value = true
try {
const shopId = currentUserInfo?.shopId || undefined
const coupons = await adminCouponApi.listAllCoupons(shopId) as unknown as Coupon[]
const list = Array.isArray(coupons) ? coupons : []
const keyword = searchText.value.trim()
couponList.value = keyword
? list.filter(item => (item.couponName || '').includes(keyword))
: list
pagination.total = couponList.value.length
} catch (error) {
console.error('获取优惠券列表失败:', error)
message.error('获取优惠券列表失败')
} finally {
loading.value = false
}
}
const fetchTags = async () => {
try {
const tags = await adminTagApi.listAllTags() as unknown as Tag[]
tagList.value = tags || []
} catch (error) {
console.error('获取标签列表失败:', error)
}
}
const fetchShops = async () => {
loadingShops.value = true
try {
const shopList = await adminShopApi.listAllShops() as unknown as Shop[]
shops.value = shopList || []
} catch (error) {
console.error('获取店铺列表失败:', error)
} finally {
loadingShops.value = false
}
}
const fetchUsers = async () => {
try {
const params: Record<string, unknown> = { page: 1, size: 200 }
if (currentUserInfo?.shopId) {
params.shopId = currentUserInfo.shopId
}
const result = await adminUserApi.listUsers(params as any)
userList.value = result.list || []
} catch (error) {
console.error('获取用户列表失败:', error)
userList.value = []
}
}
const handleSearch = () => {
pagination.current = 1
void fetchCoupons()
}
const handleTableChange = (pag: any) => {
pagination.current = pag.current || 1
pagination.pageSize = pag.pageSize || pagination.pageSize
}
const showAddModal = () => {
isEdit.value = false
currentCoupon.value = null
resetFormState()
2026-03-26 12:04:02 +08:00
restoreDraft()
modalVisible.value = true
}
const showEditModal = (record: any) => {
isEdit.value = true
currentCoupon.value = record
Object.assign(formState, {
couponCode: record.couponCode || '',
couponName: record.couponName || '',
couponType: record.couponType || 1,
discountValue: record.discountValue ? Number(record.discountValue) : 9.5,
minAmount: record.minAmount ? Number(record.minAmount) : 100,
reduceAmount: record.reduceAmount ? Number(record.reduceAmount) : 20,
totalCount: record.totalCount || 100,
startTime: record.startTime ? dayjs(record.startTime) : undefined,
endTime: record.endTime ? dayjs(record.endTime) : undefined,
availableTagIds: record.availableTags?.map((tag: Tag) => tag.id) || [],
unavailableTagIds: record.unavailableTags?.map((tag: Tag) => tag.id) || [],
shopId: record.shopId,
couponScene: record.couponScene || 'GENERAL',
requiredVipLevel: record.requiredVipLevel || undefined,
autoMatch: record.autoMatch || 0
2026-02-27 11:05:15 +08:00
})
2026-03-26 12:04:02 +08:00
restoreDraft()
2026-02-27 11:05:15 +08:00
modalVisible.value = true
}
const showViewModal = (record: any) => {
currentCoupon.value = record
viewModalVisible.value = true
2026-02-27 11:05:15 +08:00
}
const buildCouponPayload = () => ({
couponCode: formState.couponCode,
couponName: formState.couponName,
couponType: formState.couponType,
discountValue: formState.couponType === 1 ? formState.discountValue : undefined,
minAmount: formState.couponType === 2 ? formState.minAmount : undefined,
reduceAmount: formState.couponType === 2 ? formState.reduceAmount : undefined,
totalCount: formState.totalCount,
startTime: formState.startTime?.format('YYYY-MM-DD HH:mm:ss'),
endTime: formState.endTime?.format('YYYY-MM-DD HH:mm:ss'),
availableTagIds: formState.availableTagIds,
unavailableTagIds: formState.unavailableTagIds,
shopId: formState.shopId,
couponScene: formState.couponScene,
requiredVipLevel: isVipAutoForm.value ? formState.requiredVipLevel : undefined,
autoMatch: isVipAutoForm.value ? 1 : 0
})
2026-02-28 14:32:28 +08:00
2026-02-27 11:05:15 +08:00
const handleModalOk = async () => {
try {
await formRef.value?.validate()
modalLoading.value = true
const payload = buildCouponPayload()
2026-02-27 11:05:15 +08:00
if (isEdit.value && currentCoupon.value) {
await adminCouponApi.updateCoupon(currentCoupon.value.id, payload)
2026-02-27 11:05:15 +08:00
message.success('更新成功')
} else {
await adminCouponApi.createCoupon(payload)
2026-02-27 11:05:15 +08:00
message.success('创建成功')
}
2026-03-26 12:04:02 +08:00
handleClearDraft(false)
2026-02-27 11:05:15 +08:00
modalVisible.value = false
formRef.value?.resetFields()
await fetchCoupons()
} catch (error: any) {
console.error('保存优惠券失败:', error)
message.error(error.message || '操作失败')
2026-02-27 11:05:15 +08:00
} finally {
modalLoading.value = false
}
}
const handleModalCancel = () => {
modalVisible.value = false
formRef.value?.resetFields()
resetFormState()
2026-02-27 11:05:15 +08:00
}
const handleDelete = (record: any) => {
2026-03-10 23:23:15 +08:00
Modal.confirm({
2026-02-27 11:05:15 +08:00
title: '确认删除',
content: `确定要删除优惠券 ${record.couponName} 吗?`,
async onOk() {
2026-02-27 11:05:15 +08:00
try {
await adminCouponApi.deleteCoupon(record.id)
2026-02-27 11:05:15 +08:00
message.success('删除成功')
await fetchCoupons()
2026-02-27 11:05:15 +08:00
} catch (error) {
console.error('删除优惠券失败:', error)
2026-02-27 11:05:15 +08:00
message.error('删除失败')
}
}
})
}
const showDistributeModal = async (record: any) => {
currentDistributeCoupon.value = record
distributeFormState.couponName = record.couponName
distributeFormState.userIds = []
distributeFormState.quantity = 1
await fetchUsers()
distributeModalVisible.value = true
}
const handleDistributeOk = async () => {
if (!currentDistributeCoupon.value) {
return
}
if (isVipAutoCoupon(currentDistributeCoupon.value)) {
message.warning('VIP 自动券为系统模板券,不能手动发放')
return
}
try {
await distributeFormRef.value?.validate()
distributeLoading.value = true
await adminCouponApi.distributeCoupon({
couponId: currentDistributeCoupon.value.id,
userIds: distributeFormState.userIds,
quantity: distributeFormState.quantity
2026-03-10 23:23:15 +08:00
})
message.success('发放成功')
distributeModalVisible.value = false
} catch (error: any) {
console.error('发放优惠券失败:', error)
message.error(error.message || '发放失败')
} finally {
distributeLoading.value = false
}
}
const handleDistributeCancel = () => {
distributeModalVisible.value = false
distributeFormRef.value?.resetFields()
}
2026-02-27 11:05:15 +08:00
onMounted(async () => {
await loadUserPermissions()
await Promise.all([fetchTags(), fetchShops(), fetchCoupons()])
2026-02-27 11:05:15 +08:00
})
</script>
<style scoped>
.coupons {
padding: 0;
}
</style>