优惠券相关管理功能实现
This commit is contained in:
@@ -0,0 +1,385 @@
|
||||
<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 === '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 v-if="hasPermission('coupon:edit')" type="link" size="small" @click="showEditModal(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="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>
|
||||
</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 } 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[]
|
||||
}
|
||||
|
||||
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 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 couponList = ref<Coupon[]>([])
|
||||
const tagList = ref<TagVO[]>([])
|
||||
|
||||
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[]
|
||||
})
|
||||
|
||||
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' }]
|
||||
}
|
||||
|
||||
const columns = [
|
||||
{ title: '优惠券编码', dataIndex: 'couponCode', key: 'couponCode' },
|
||||
{ title: '优惠券名称', dataIndex: 'couponName', key: 'couponName' },
|
||||
{ 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 res = await adminCouponApi.listAllCoupons()
|
||||
couponList.value = res || []
|
||||
pagination.total = res.length || 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 handleSearch = () => {
|
||||
pagination.current = 1
|
||||
fetchCoupons()
|
||||
}
|
||||
|
||||
const handleTableChange = (pag: any) => {
|
||||
pagination.current = pag.current
|
||||
pagination.pageSize = pag.pageSize
|
||||
fetchCoupons()
|
||||
}
|
||||
|
||||
const showAddModal = () => {
|
||||
isEdit.value = false
|
||||
Object.assign(formState, {
|
||||
couponCode: '',
|
||||
couponName: '',
|
||||
couponType: 1,
|
||||
discountValue: 9.5,
|
||||
minAmount: 100,
|
||||
reduceAmount: 20,
|
||||
totalCount: 100,
|
||||
startTime: undefined,
|
||||
endTime: undefined,
|
||||
availableTagIds: [],
|
||||
unavailableTagIds: []
|
||||
})
|
||||
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)
|
||||
})
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
const handleModalOk = async () => {
|
||||
try {
|
||||
await formRef.value?.validate()
|
||||
modalLoading.value = true
|
||||
|
||||
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: formState.startTime.toISOString(),
|
||||
endTime: formState.endTime.toISOString(),
|
||||
availableTagIds: formState.availableTagIds,
|
||||
unavailableTagIds: formState.unavailableTagIds
|
||||
}
|
||||
|
||||
if (isEdit.value && currentCoupon.value) {
|
||||
await adminCouponApi.updateCoupon(currentCoupon.value.id, requestData)
|
||||
message.success('更新成功')
|
||||
} else {
|
||||
await adminCouponApi.createCoupon(requestData)
|
||||
message.success('创建成功')
|
||||
}
|
||||
|
||||
modalVisible.value = false
|
||||
fetchCoupons()
|
||||
} catch (error) {
|
||||
console.error(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 {
|
||||
await adminCouponApi.deleteCoupon(record.id)
|
||||
message.success('删除成功')
|
||||
fetchCoupons()
|
||||
} catch (error) {
|
||||
message.error('删除失败')
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
await loadUserPermissions()
|
||||
await fetchTags()
|
||||
await fetchCoupons()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.coupons {
|
||||
padding: 0;
|
||||
}
|
||||
</style>
|
||||
@@ -227,10 +227,10 @@
|
||||
</a-select-option>
|
||||
<a-select-option
|
||||
v-for="coupon in coupons"
|
||||
:key="coupon.id"
|
||||
:value="coupon.id.toString()"
|
||||
:key="coupon?.id"
|
||||
:value="coupon?.id?.toString()"
|
||||
>
|
||||
{{ coupon.couponName }}
|
||||
{{ coupon?.couponName || '' }}
|
||||
</a-select-option>
|
||||
</a-select>
|
||||
</a-form-item>
|
||||
|
||||
@@ -274,9 +274,26 @@ const fetchUserList = async () => {
|
||||
params.shopId = currentUserInfo.shopId
|
||||
}
|
||||
|
||||
// 后端只支持单个type参数,所以这里不设置类型过滤
|
||||
// 依赖前端的二次过滤来确保只显示级别比当前用户低的用户
|
||||
|
||||
const res = await userApi.listUsers(params)
|
||||
userList.value = res.list || []
|
||||
pagination.total = res.total || 0
|
||||
// 前端再次过滤,确保只显示级别比当前用户低的用户
|
||||
let filteredList = res.list || []
|
||||
if (currentUserInfo && currentUserInfo.type !== 4) {
|
||||
filteredList = filteredList.filter(user => {
|
||||
if (currentUserInfo.type === 2) {
|
||||
// 店长可以看到美甲师(3)和用户(0)
|
||||
return user.type === 3 || user.type === 0
|
||||
} else if (currentUserInfo.type === 3) {
|
||||
// 美甲师只能看到用户(0)
|
||||
return user.type === 0
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
userList.value = filteredList
|
||||
pagination.total = filteredList.length || 0
|
||||
} catch (error) {
|
||||
message.error('获取用户列表失败')
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user