修复一些问题以及优惠券管理功能
This commit is contained in:
@@ -34,6 +34,9 @@
|
||||
{{ 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 }}折
|
||||
@@ -49,7 +52,7 @@
|
||||
</template>
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button v-if="hasPermission('coupon:edit')" type="link" size="small" @click="showEditModal(record)">
|
||||
<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)">
|
||||
@@ -85,6 +88,30 @@
|
||||
<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>
|
||||
@@ -180,7 +207,7 @@ 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 } from '@/api/admin'
|
||||
import { adminCouponApi, adminTagApi, adminShopApi, type Shop } from '@/api/admin'
|
||||
import type { Coupon } from '@/api/coupon'
|
||||
|
||||
interface CreateCouponRequest {
|
||||
@@ -195,6 +222,7 @@ interface CreateCouponRequest {
|
||||
endTime: string
|
||||
availableTagIds: number[]
|
||||
unavailableTagIds: number[]
|
||||
shopId: number
|
||||
}
|
||||
|
||||
interface TagVO {
|
||||
@@ -211,6 +239,19 @@ 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)
|
||||
@@ -228,6 +269,8 @@ 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,
|
||||
@@ -249,7 +292,8 @@ const formState = reactive({
|
||||
startTime: new Date(),
|
||||
endTime: new Date(new Date().getTime() + 30 * 24 * 60 * 60 * 1000),
|
||||
availableTagIds: [] as number[],
|
||||
unavailableTagIds: [] as number[]
|
||||
unavailableTagIds: [] as number[],
|
||||
shopId: undefined as number | undefined
|
||||
})
|
||||
|
||||
const formRules = {
|
||||
@@ -261,7 +305,8 @@ const formRules = {
|
||||
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' }]
|
||||
endTime: [{ required: true, message: '请选择结束时间', trigger: 'change' }],
|
||||
shopId: [{ required: true, message: '请选择所属店铺', trigger: 'change' }]
|
||||
}
|
||||
|
||||
// 发放优惠券表单状态
|
||||
@@ -280,6 +325,7 @@ const distributeFormRules = {
|
||||
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 },
|
||||
@@ -307,7 +353,9 @@ const getStatusColor = (status: number) => {
|
||||
const fetchCoupons = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await adminCouponApi.listAllCoupons()
|
||||
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
|
||||
@@ -327,6 +375,18 @@ const fetchTags = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
@@ -340,6 +400,8 @@ const handleTableChange = (pag: any) => {
|
||||
|
||||
const showAddModal = () => {
|
||||
isEdit.value = false
|
||||
const currentUser = getCurrentUserInfo()
|
||||
const shopId = (currentUser && currentUser.shopId) ? currentUser.shopId : undefined
|
||||
Object.assign(formState, {
|
||||
couponCode: '',
|
||||
couponName: '',
|
||||
@@ -351,28 +413,43 @@ const showAddModal = () => {
|
||||
startTime: undefined,
|
||||
endTime: undefined,
|
||||
availableTagIds: [],
|
||||
unavailableTagIds: []
|
||||
unavailableTagIds: [],
|
||||
shopId: shopId
|
||||
})
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
const showEditModal = (record: Coupon) => {
|
||||
isEdit.value = true
|
||||
currentCoupon.value = record
|
||||
Object.assign(formState, {
|
||||
couponCode: record.couponCode,
|
||||
couponName: record.couponName,
|
||||
couponType: record.couponType,
|
||||
discountValue: record.discountValue,
|
||||
minAmount: record.minAmount,
|
||||
reduceAmount: record.reduceAmount,
|
||||
totalCount: record.totalCount,
|
||||
startTime: undefined,
|
||||
endTime: undefined,
|
||||
availableTagIds: record.availableTags.map(tag => tag.id),
|
||||
unavailableTagIds: record.unavailableTags.map(tag => tag.id)
|
||||
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
|
||||
})
|
||||
modalVisible.value = true
|
||||
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 () => {
|
||||
@@ -380,6 +457,35 @@ const handleModalOk = async () => {
|
||||
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,
|
||||
@@ -388,17 +494,18 @@ const handleModalOk = async () => {
|
||||
minAmount: formState.minAmount,
|
||||
reduceAmount: formState.reduceAmount,
|
||||
totalCount: formState.totalCount,
|
||||
startTime: formState.startTime.toISOString(),
|
||||
endTime: formState.endTime.toISOString(),
|
||||
startTime: startTimeStr,
|
||||
endTime: endTimeStr,
|
||||
availableTagIds: formState.availableTagIds,
|
||||
unavailableTagIds: formState.unavailableTagIds
|
||||
unavailableTagIds: formState.unavailableTagIds,
|
||||
shopId: shopId
|
||||
}
|
||||
|
||||
if (isEdit.value && currentCoupon.value) {
|
||||
await adminCouponApi.updateCoupon(currentCoupon.value.id, requestData)
|
||||
await adminCouponApi.updateCoupon(currentCoupon.value.id, requestData, shopId)
|
||||
message.success('更新成功')
|
||||
} else {
|
||||
await adminCouponApi.createCoupon(requestData)
|
||||
await adminCouponApi.createCoupon(requestData, shopId)
|
||||
message.success('创建成功')
|
||||
}
|
||||
|
||||
@@ -406,6 +513,7 @@ const handleModalOk = async () => {
|
||||
fetchCoupons()
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
message.error('操作失败')
|
||||
} finally {
|
||||
modalLoading.value = false
|
||||
}
|
||||
@@ -422,7 +530,9 @@ const handleDelete = (record: Coupon) => {
|
||||
content: `确定要删除优惠券 ${record.couponName} 吗?`,
|
||||
onOk: async () => {
|
||||
try {
|
||||
await adminCouponApi.deleteCoupon(record.id)
|
||||
const currentUser = getCurrentUserInfo()
|
||||
const shopId = (currentUser && currentUser.shopId) ? currentUser.shopId : record.shopId
|
||||
await adminCouponApi.deleteCoupon(record.id, shopId)
|
||||
message.success('删除成功')
|
||||
fetchCoupons()
|
||||
} catch (error) {
|
||||
@@ -465,12 +575,21 @@ const handleDistributeOk = async () => {
|
||||
|
||||
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
|
||||
@@ -491,6 +610,7 @@ const handleDistributeCancel = () => {
|
||||
onMounted(async () => {
|
||||
await loadUserPermissions()
|
||||
await fetchTags()
|
||||
await fetchShops()
|
||||
await fetchCoupons()
|
||||
})
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user