623 lines
20 KiB
Vue
623 lines
20 KiB
Vue
<template>
|
|
<div class="coupons">
|
|
<a-page-header title="优惠券管理" sub-title="管理系统优惠券" />
|
|
|
|
<a-card :bordered="false">
|
|
<template #extra>
|
|
<a-space>
|
|
<a-input-search
|
|
v-model:value="searchText"
|
|
placeholder="搜索优惠券名称或编码"
|
|
style="width: 250px"
|
|
@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"
|
|
@change="handleTableChange"
|
|
row-key="id"
|
|
>
|
|
<template #bodyCell="{ column, record }">
|
|
<template v-if="column.key === 'couponType'">
|
|
<a-tag :color="record.couponType === 1 ? 'blue' : 'green'">
|
|
{{ record.couponTypeName }}
|
|
</a-tag>
|
|
</template>
|
|
<template v-else-if="column.key === 'shop'">
|
|
<span>{{ record.shopName || '未知店铺' }}</span>
|
|
</template>
|
|
<template v-else-if="column.key === 'value'">
|
|
<template v-if="record.couponType === 1">
|
|
{{ record.discountValue }}折
|
|
</template>
|
|
<template v-else>
|
|
满{{ record.minAmount }}减{{ record.reduceAmount }}
|
|
</template>
|
|
</template>
|
|
<template v-else-if="column.key === 'status'">
|
|
<a-tag :color="getStatusColor(record.status)">
|
|
{{ record.statusName }}
|
|
</a-tag>
|
|
</template>
|
|
<template v-else-if="column.key === 'actions'">
|
|
<a-space>
|
|
<a-button type="link" size="small" @click="showEditModal(record)">
|
|
编辑
|
|
</a-button>
|
|
<a-button v-if="hasPermission('coupon:assign') && 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)">
|
|
删除
|
|
</a-button>
|
|
</a-space>
|
|
</template>
|
|
</template>
|
|
</a-table>
|
|
</a-card>
|
|
|
|
<a-modal
|
|
v-model:open="modalVisible"
|
|
:title="isEdit ? '编辑优惠券' : '新增优惠券'"
|
|
@ok="handleModalOk"
|
|
@cancel="handleModalCancel"
|
|
:confirmLoading="modalLoading"
|
|
width="800px"
|
|
>
|
|
<a-form
|
|
ref="formRef"
|
|
:model="formState"
|
|
:rules="formRules"
|
|
:label-col="{ span: 6 }"
|
|
:wrapper-col="{ span: 16 }"
|
|
>
|
|
<a-form-item label="优惠券编码" name="couponCode">
|
|
<a-input v-model:value="formState.couponCode" placeholder="请输入优惠券编码" />
|
|
</a-form-item>
|
|
<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="请选择所属店铺"
|
|
style="width: 100%"
|
|
:loading="loadingShops"
|
|
:disabled="getCurrentUserInfo()?.type === 2"
|
|
>
|
|
<a-select-option
|
|
v-if="shops.length === 0"
|
|
disabled
|
|
value=""
|
|
>
|
|
暂无店铺数据
|
|
</a-select-option>
|
|
<a-select-option
|
|
v-for="shop in shops"
|
|
:key="shop.id"
|
|
:value="shop.id"
|
|
>
|
|
{{ shop.shopName }}
|
|
</a-select-option>
|
|
</a-select>
|
|
</a-form-item>
|
|
<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 v-if="formState.couponType === 1" label="折扣值" name="discountValue">
|
|
<a-input-number v-model:value="formState.discountValue" placeholder="请输入折扣值(如9.5表示95折)" :min="0.1" :max="9.9" :step="0.1" />
|
|
</a-form-item>
|
|
<a-form-item v-if="formState.couponType === 2" label="最低消费" name="minAmount">
|
|
<a-input-number v-model:value="formState.minAmount" placeholder="请输入最低消费金额" :min="0" :step="0.01" />
|
|
</a-form-item>
|
|
<a-form-item v-if="formState.couponType === 2" label="减免金额" name="reduceAmount">
|
|
<a-input-number v-model:value="formState.reduceAmount" placeholder="请输入减免金额" :min="0" :step="0.01" />
|
|
</a-form-item>
|
|
<a-form-item label="总数量" name="totalCount">
|
|
<a-input-number v-model:value="formState.totalCount" placeholder="请输入优惠券总数量" :min="1" />
|
|
</a-form-item>
|
|
<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>
|
|
<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>
|
|
<a-form-item label="可用标签" name="availableTagIds">
|
|
<a-select
|
|
v-model:value="formState.availableTagIds"
|
|
mode="multiple"
|
|
placeholder="请选择可用标签"
|
|
style="width: 100%"
|
|
>
|
|
<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-item label="不可用标签" name="unavailableTagIds">
|
|
<a-select
|
|
v-model:value="formState.unavailableTagIds"
|
|
mode="multiple"
|
|
placeholder="请选择不可用标签"
|
|
style="width: 100%"
|
|
>
|
|
<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="distributeModalVisible"
|
|
title="发放优惠券"
|
|
@ok="handleDistributeOk"
|
|
@cancel="handleDistributeCancel"
|
|
:confirmLoading="distributeLoading"
|
|
width="800px"
|
|
>
|
|
<a-form
|
|
ref="distributeFormRef"
|
|
:model="distributeFormState"
|
|
:rules="distributeFormRules"
|
|
:label-col="{ span: 6 }"
|
|
:wrapper-col="{ span: 16 }"
|
|
>
|
|
<a-form-item label="优惠券" name="couponId">
|
|
<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="请选择要发放的用户"
|
|
style="width: 100%"
|
|
>
|
|
<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" placeholder="请输入发放数量" :min="1" />
|
|
</a-form-item>
|
|
</a-form>
|
|
</a-modal>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref, reactive, onMounted, computed } from 'vue'
|
|
import { message } from 'ant-design-vue'
|
|
import type { FormInstance } from 'ant-design-vue'
|
|
import { PlusOutlined } from '@ant-design/icons-vue'
|
|
import { adminCouponApi, adminTagApi, adminShopApi, type Shop } from '@/api/admin'
|
|
import type { Coupon } from '@/api/coupon'
|
|
|
|
interface CreateCouponRequest {
|
|
couponCode: string
|
|
couponName: string
|
|
couponType: number
|
|
discountValue?: number
|
|
minAmount?: number
|
|
reduceAmount?: number
|
|
totalCount: number
|
|
startTime: string
|
|
endTime: string
|
|
availableTagIds: number[]
|
|
unavailableTagIds: number[]
|
|
shopId: number
|
|
}
|
|
|
|
interface TagVO {
|
|
id: number
|
|
tagName: string
|
|
textColor: string
|
|
bgColor: string
|
|
borderColor: string
|
|
shopId: number
|
|
createTime: string
|
|
updateTime: string
|
|
}
|
|
import { usePermission } from '@/utils/permission'
|
|
|
|
const { hasPermission, loadUserPermissions } = usePermission()
|
|
|
|
const getCurrentUserInfo = () => {
|
|
try {
|
|
const userInfoStr = localStorage.getItem('adminUserInfo')
|
|
if (userInfoStr) {
|
|
const userInfo = JSON.parse(userInfoStr)
|
|
return userInfo
|
|
}
|
|
} catch (error) {
|
|
console.error('获取当前用户信息失败', error)
|
|
}
|
|
return null
|
|
}
|
|
|
|
const loading = ref(false)
|
|
const modalVisible = ref(false)
|
|
const modalLoading = ref(false)
|
|
const isEdit = ref(false)
|
|
const searchText = ref('')
|
|
const formRef = ref<FormInstance>()
|
|
const currentCoupon = ref<Coupon | null>(null)
|
|
|
|
// 发放优惠券相关状态
|
|
const distributeModalVisible = ref(false)
|
|
const distributeLoading = ref(false)
|
|
const distributeFormRef = ref<FormInstance>()
|
|
const currentDistributeCoupon = ref<Coupon | null>(null)
|
|
|
|
const couponList = ref<Coupon[]>([])
|
|
const tagList = ref<TagVO[]>([])
|
|
const userList = ref<any[]>([])
|
|
const shops = ref<Shop[]>([])
|
|
const loadingShops = ref(false)
|
|
|
|
const pagination = reactive({
|
|
current: 1,
|
|
pageSize: 10,
|
|
total: 0,
|
|
showSizeChanger: true,
|
|
showQuickJumper: true,
|
|
showTotal: (total: number) => `共 ${total} 条`
|
|
})
|
|
|
|
const formState = reactive({
|
|
couponCode: '',
|
|
couponName: '',
|
|
couponType: 1,
|
|
discountValue: 9.5,
|
|
minAmount: 100,
|
|
reduceAmount: 20,
|
|
totalCount: 100,
|
|
startTime: new Date(),
|
|
endTime: new Date(new Date().getTime() + 30 * 24 * 60 * 60 * 1000),
|
|
availableTagIds: [] as number[],
|
|
unavailableTagIds: [] as number[],
|
|
shopId: undefined as number | undefined
|
|
})
|
|
|
|
const formRules = {
|
|
couponCode: [{ required: true, message: '请输入优惠券编码', trigger: 'blur' }],
|
|
couponName: [{ required: true, message: '请输入优惠券名称', trigger: 'blur' }],
|
|
couponType: [{ required: true, message: '请选择优惠券类型', trigger: 'change' }],
|
|
discountValue: [{ required: formState.couponType === 1, message: '请输入折扣值', trigger: 'blur' }],
|
|
minAmount: [{ required: formState.couponType === 2, message: '请输入最低消费金额', trigger: 'blur' }],
|
|
reduceAmount: [{ required: formState.couponType === 2, message: '请输入减免金额', trigger: 'blur' }],
|
|
totalCount: [{ required: true, message: '请输入优惠券总数量', trigger: 'blur' }],
|
|
startTime: [{ required: true, message: '请选择开始时间', trigger: 'change' }],
|
|
endTime: [{ required: true, message: '请选择结束时间', trigger: 'change' }],
|
|
shopId: [{ required: true, message: '请选择所属店铺', trigger: 'change' }]
|
|
}
|
|
|
|
// 发放优惠券表单状态
|
|
const distributeFormState = reactive({
|
|
couponName: '',
|
|
userIds: [] as number[],
|
|
quantity: 1
|
|
})
|
|
|
|
// 发放优惠券表单规则
|
|
const distributeFormRules = {
|
|
userIds: [{ required: true, message: '请选择要发放的用户', trigger: 'change' }],
|
|
quantity: [{ required: true, message: '请输入发放数量', trigger: 'blur' }]
|
|
}
|
|
|
|
const columns = [
|
|
{ title: '优惠券编码', dataIndex: 'couponCode', key: 'couponCode' },
|
|
{ title: '优惠券名称', dataIndex: 'couponName', key: 'couponName' },
|
|
{ title: '所属店铺', key: 'shop', width: 150 },
|
|
{ title: '类型', key: 'couponType', width: 100 },
|
|
{ title: '优惠信息', key: 'value', width: 150 },
|
|
{ title: '总数量', dataIndex: 'totalCount', key: 'totalCount', width: 100 },
|
|
{ title: '已使用', dataIndex: 'usedCount', key: 'usedCount', width: 100 },
|
|
{ title: '剩余', dataIndex: 'remainingCount', key: 'remainingCount', width: 100 },
|
|
{ title: '开始时间', dataIndex: 'startTime', key: 'startTime' },
|
|
{ title: '结束时间', dataIndex: 'endTime', key: 'endTime' },
|
|
{ title: '状态', key: 'status', width: 100 },
|
|
{ title: '操作', key: 'actions', width: 150, fixed: 'right' as const }
|
|
]
|
|
|
|
const getStatusColor = (status: number) => {
|
|
switch (status) {
|
|
case 1:
|
|
return 'green'
|
|
case 2:
|
|
return 'red'
|
|
case 3:
|
|
return 'orange'
|
|
default:
|
|
return 'default'
|
|
}
|
|
}
|
|
|
|
const fetchCoupons = async () => {
|
|
loading.value = true
|
|
try {
|
|
const currentUser = getCurrentUserInfo()
|
|
const shopId = (currentUser && currentUser.shopId) ? currentUser.shopId : undefined
|
|
const res = await adminCouponApi.listAllCoupons(shopId)
|
|
// 处理后端返回的分页数据结构
|
|
couponList.value = res?.list || []
|
|
pagination.total = res?.total || 0
|
|
} catch (error) {
|
|
message.error('获取优惠券列表失败')
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
const fetchTags = async () => {
|
|
try {
|
|
const res = await adminTagApi.listAllTags()
|
|
tagList.value = res || []
|
|
} catch (error) {
|
|
console.error('获取标签列表失败:', error)
|
|
}
|
|
}
|
|
|
|
const fetchShops = async () => {
|
|
loadingShops.value = true
|
|
try {
|
|
const res = await adminShopApi.listAllShops()
|
|
shops.value = res || []
|
|
} catch (error) {
|
|
console.error('获取店铺列表失败:', error)
|
|
} finally {
|
|
loadingShops.value = false
|
|
}
|
|
}
|
|
|
|
const handleSearch = () => {
|
|
pagination.current = 1
|
|
fetchCoupons()
|
|
}
|
|
|
|
const handleTableChange = (pag: any) => {
|
|
pagination.current = pag.current
|
|
pagination.pageSize = pag.pageSize
|
|
fetchCoupons()
|
|
}
|
|
|
|
const showAddModal = () => {
|
|
isEdit.value = false
|
|
const currentUser = getCurrentUserInfo()
|
|
const shopId = (currentUser && currentUser.shopId) ? currentUser.shopId : undefined
|
|
Object.assign(formState, {
|
|
couponCode: '',
|
|
couponName: '',
|
|
couponType: 1,
|
|
discountValue: 9.5,
|
|
minAmount: 100,
|
|
reduceAmount: 20,
|
|
totalCount: 100,
|
|
startTime: undefined,
|
|
endTime: undefined,
|
|
availableTagIds: [],
|
|
unavailableTagIds: [],
|
|
shopId: shopId
|
|
})
|
|
modalVisible.value = true
|
|
}
|
|
|
|
const showEditModal = (record: Coupon) => {
|
|
console.log('showEditModal called with record:', record)
|
|
if (!record) {
|
|
message.error('优惠券数据不存在')
|
|
return
|
|
}
|
|
try {
|
|
isEdit.value = true
|
|
currentCoupon.value = record
|
|
console.log('Form state before:', formState)
|
|
Object.assign(formState, {
|
|
couponCode: record.couponCode || '',
|
|
couponName: record.couponName || '',
|
|
couponType: record.couponType || 1,
|
|
discountValue: record.discountValue || 0,
|
|
minAmount: record.minAmount || 0,
|
|
reduceAmount: record.reduceAmount || 0,
|
|
totalCount: record.totalCount || 0,
|
|
startTime: record.startTime ? new Date(record.startTime) : undefined,
|
|
endTime: record.endTime ? new Date(record.endTime) : undefined,
|
|
availableTagIds: record.availableTags ? record.availableTags.map(tag => tag.id) : [],
|
|
unavailableTagIds: record.unavailableTags ? record.unavailableTags.map(tag => tag.id) : [],
|
|
shopId: record.shopId
|
|
})
|
|
console.log('Form state after:', formState)
|
|
modalVisible.value = true
|
|
console.log('Modal visible set to:', modalVisible.value)
|
|
} catch (error) {
|
|
console.error('Error in showEditModal:', error)
|
|
message.error('打开编辑模态框失败')
|
|
}
|
|
}
|
|
|
|
const handleModalOk = async () => {
|
|
try {
|
|
await formRef.value?.validate()
|
|
modalLoading.value = true
|
|
|
|
const currentUser = getCurrentUserInfo()
|
|
const shopId = formState.shopId || (currentUser && currentUser.shopId)
|
|
|
|
if (!shopId) {
|
|
message.error('请选择所属店铺')
|
|
modalLoading.value = false
|
|
return
|
|
}
|
|
|
|
// 处理日期格式
|
|
let startTimeStr = ''
|
|
let endTimeStr = ''
|
|
|
|
if (formState.startTime) {
|
|
if (formState.startTime instanceof Date) {
|
|
startTimeStr = formState.startTime.toISOString()
|
|
} else {
|
|
startTimeStr = formState.startTime
|
|
}
|
|
}
|
|
|
|
if (formState.endTime) {
|
|
if (formState.endTime instanceof Date) {
|
|
endTimeStr = formState.endTime.toISOString()
|
|
} else {
|
|
endTimeStr = formState.endTime
|
|
}
|
|
}
|
|
|
|
const requestData: CreateCouponRequest = {
|
|
couponCode: formState.couponCode,
|
|
couponName: formState.couponName,
|
|
couponType: formState.couponType,
|
|
discountValue: formState.discountValue,
|
|
minAmount: formState.minAmount,
|
|
reduceAmount: formState.reduceAmount,
|
|
totalCount: formState.totalCount,
|
|
startTime: startTimeStr,
|
|
endTime: endTimeStr,
|
|
availableTagIds: formState.availableTagIds,
|
|
unavailableTagIds: formState.unavailableTagIds,
|
|
shopId: shopId
|
|
}
|
|
|
|
if (isEdit.value && currentCoupon.value) {
|
|
await adminCouponApi.updateCoupon(currentCoupon.value.id, requestData, shopId)
|
|
message.success('更新成功')
|
|
} else {
|
|
await adminCouponApi.createCoupon(requestData, shopId)
|
|
message.success('创建成功')
|
|
}
|
|
|
|
modalVisible.value = false
|
|
fetchCoupons()
|
|
} catch (error) {
|
|
console.error(error)
|
|
message.error('操作失败')
|
|
} finally {
|
|
modalLoading.value = false
|
|
}
|
|
}
|
|
|
|
const handleModalCancel = () => {
|
|
modalVisible.value = false
|
|
formRef.value?.resetFields()
|
|
}
|
|
|
|
const handleDelete = (record: Coupon) => {
|
|
message.confirm({
|
|
title: '确认删除',
|
|
content: `确定要删除优惠券 ${record.couponName} 吗?`,
|
|
onOk: async () => {
|
|
try {
|
|
const currentUser = getCurrentUserInfo()
|
|
const shopId = (currentUser && currentUser.shopId) ? currentUser.shopId : record.shopId
|
|
await adminCouponApi.deleteCoupon(record.id, shopId)
|
|
message.success('删除成功')
|
|
fetchCoupons()
|
|
} catch (error) {
|
|
message.error('删除失败')
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// 加载用户列表
|
|
const fetchUsers = async () => {
|
|
try {
|
|
// 这里应该调用获取用户列表的API
|
|
// 暂时使用模拟数据
|
|
userList.value = [
|
|
{ id: 1, nickname: '用户1', account: 'user1' },
|
|
{ id: 2, nickname: '用户2', account: 'user2' },
|
|
{ id: 3, nickname: '用户3', account: 'user3' }
|
|
]
|
|
} catch (error) {
|
|
console.error('获取用户列表失败:', error)
|
|
}
|
|
}
|
|
|
|
// 显示发放优惠券模态框
|
|
const showDistributeModal = (record: Coupon) => {
|
|
currentDistributeCoupon.value = record
|
|
distributeFormState.couponName = record.couponName
|
|
distributeFormState.userIds = []
|
|
distributeFormState.quantity = 1
|
|
fetchUsers()
|
|
distributeModalVisible.value = true
|
|
}
|
|
|
|
// 处理发放优惠券
|
|
const handleDistributeOk = async () => {
|
|
try {
|
|
await distributeFormRef.value?.validate()
|
|
distributeLoading.value = true
|
|
|
|
if (!currentDistributeCoupon.value) return
|
|
|
|
const currentUser = getCurrentUserInfo()
|
|
const shopId = (currentUser && currentUser.shopId) ? currentUser.shopId : currentDistributeCoupon.value.shopId
|
|
|
|
if (!shopId) {
|
|
message.error('请选择所属店铺')
|
|
distributeLoading.value = false
|
|
return
|
|
}
|
|
|
|
// 调用发放优惠券的API
|
|
await adminCouponApi.distributeCoupon({
|
|
couponId: currentDistributeCoupon.value.id,
|
|
userIds: distributeFormState.userIds,
|
|
quantity: distributeFormState.quantity
|
|
}, shopId)
|
|
|
|
message.success('发放成功')
|
|
distributeModalVisible.value = false
|
|
} catch (error) {
|
|
console.error(error)
|
|
message.error('发放失败')
|
|
} finally {
|
|
distributeLoading.value = false
|
|
}
|
|
}
|
|
|
|
// 取消发放优惠券
|
|
const handleDistributeCancel = () => {
|
|
distributeModalVisible.value = false
|
|
distributeFormRef.value?.resetFields()
|
|
}
|
|
|
|
onMounted(async () => {
|
|
await loadUserPermissions()
|
|
await fetchTags()
|
|
await fetchShops()
|
|
await fetchCoupons()
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.coupons {
|
|
padding: 0;
|
|
}
|
|
</style>
|