Files
finance-backend/finance-web/src/views/vip/VipPlanView.vue
T

399 lines
13 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, nextTick, onMounted, reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import PageHeader from '@/components/PageHeader.vue'
import { appApi } from '@/api/core'
import { vipApi } from '@/api/settings'
import type { AppInfo, VipPlan } from '@/types'
import { centToYuanInput, formatCent, yuanInputToCent } from '@/utils/money'
import { formatDateTime } from '@/utils/format'
import { askTotp, confirmAction } from '@/utils/confirm'
import { userMessage } from '@/utils/http'
import type { ApiError } from '@/utils/http'
interface PlanForm {
appCode: string
planCode: string
planName: string
durationDays: number
priceYuan: string
freeDeduction: number
}
const apps = ref<AppInfo[]>([])
const plans = ref<VipPlan[]>([])
const appCode = ref('')
const loading = ref(false)
const page = ref(1)
const pageSize = ref(10)
const pagedPlans = computed(() =>
plans.value.slice((page.value - 1) * pageSize.value, page.value * pageSize.value),
)
const dialogVisible = ref(false)
const editingPlan = ref<VipPlan | null>(null)
const submitting = ref(false)
const formRef = ref<FormInstance>()
const toggleId = ref<number | null>(null)
const isEdit = computed(() => editingPlan.value !== null)
const form = reactive<PlanForm>({
appCode: '',
planCode: '',
planName: '',
durationDays: 365,
priceYuan: '',
freeDeduction: 0,
})
function validateDuration(_rule: unknown, value: unknown, callback: (error?: string | Error) => void): void {
const days = Number(value)
if (!Number.isInteger(days) || days <= 0) {
callback(new Error('有效期天数必须为正整数'))
return
}
callback()
}
function validatePrice(_rule: unknown, value: unknown, callback: (error?: string | Error) => void): void {
if (yuanInputToCent(String(value ?? '')) === null) {
callback(new Error('请输入正确的价格(元,最多两位小数且大于 0)'))
return
}
callback()
}
const planRules: FormRules = {
appCode: [{ required: true, message: '请选择应用', trigger: 'change' }],
planCode: [
{ required: true, message: '请输入档位编码', trigger: 'blur' },
{
pattern: /^[A-Z][A-Z0-9_]{2,31}$/,
message: '需为 3~32 位大写字母、数字或下划线,且以字母开头',
trigger: 'blur',
},
],
planName: [
{ required: true, message: '请输入档位名称', trigger: 'blur' },
{ min: 2, max: 32, message: '档位名称需为 2~32 个字', trigger: 'blur' },
],
durationDays: [{ validator: validateDuration, trigger: 'change' }],
priceYuan: [{ validator: validatePrice, trigger: 'blur' }],
}
async function loadApps(): Promise<void> {
try {
apps.value = await appApi.list()
} catch {
apps.value = []
}
}
async function loadPlans(): Promise<void> {
loading.value = true
try {
plans.value = await vipApi.plans(appCode.value || undefined)
page.value = 1
} catch {
plans.value = []
} finally {
loading.value = false
}
}
function handleAppChange(): void {
page.value = 1
void loadPlans()
}
function handleSizeChange(): void {
page.value = 1
}
function openCreate(): void {
editingPlan.value = null
Object.assign(form, {
appCode: appCode.value || apps.value[0]?.appCode || '',
planCode: '',
planName: '',
durationDays: 365,
priceYuan: '',
freeDeduction: 0,
})
dialogVisible.value = true
void nextTick(() => formRef.value?.clearValidate())
}
function openEdit(row: VipPlan): void {
editingPlan.value = row
Object.assign(form, {
appCode: row.appCode,
planCode: row.planCode,
planName: row.planName,
durationDays: row.durationDays,
priceYuan: centToYuanInput(row.priceCent),
freeDeduction: row.freeDeduction,
})
dialogVisible.value = true
void nextTick(() => formRef.value?.clearValidate())
}
async function submitPlan(): Promise<void> {
const formEl = formRef.value
if (!formEl) return
let valid = false
try {
valid = await formEl.validate()
} catch {
valid = false
}
if (!valid) return
const priceCent = yuanInputToCent(form.priceYuan)
if (priceCent === null) {
ElMessage.error('请输入正确的价格(元)')
return
}
const durationDays = Number(form.durationDays)
if (!Number.isInteger(durationDays) || durationDays <= 0) {
ElMessage.error('有效期天数必须为正整数')
return
}
if (isEdit.value && editingPlan.value) {
const before = editingPlan.value
if (priceCent !== before.priceCent) {
const confirmed = await confirmAction({
title: '确认改价',
message: `档位「${before.planName}」价格将由 ${formatCent(before.priceCent)} 调整为 ${formatCent(
priceCent,
)},改价将写入审计日志,确认继续?`,
requireType: '确认改价',
confirmText: '确认改价',
})
if (!confirmed) return
}
const totp = await askTotp('修改 VIP 档位')
if (!totp) return
submitting.value = true
try {
await vipApi.updatePlan(before.id, {
appCode: before.appCode,
planName: form.planName.trim(),
durationDays,
priceCent,
freeDeduction: form.freeDeduction,
totp,
})
ElMessage.success('档位已更新')
dialogVisible.value = false
await loadPlans()
} catch (error) {
ElMessage.error(userMessage(error as ApiError))
} finally {
submitting.value = false
}
return
}
submitting.value = true
try {
await vipApi.createPlan({
appCode: form.appCode,
planCode: form.planCode.trim(),
planName: form.planName.trim(),
durationDays,
priceCent,
freeDeduction: form.freeDeduction,
})
ElMessage.success('档位已创建')
dialogVisible.value = false
await loadPlans()
} catch (error) {
ElMessage.error(userMessage(error as ApiError))
} finally {
submitting.value = false
}
}
function beforeToggle(row: VipPlan): Promise<boolean> {
const action = row.enabled === 1 ? '下架' : '上架'
return confirmAction({
title: `${action} VIP 档位`,
message:
row.enabled === 1
? `确认下架档位「${row.planName}」?下架后收银台不再展示该档位,存量用户权益不受影响。`
: `确认上架档位「${row.planName}」?上架后用户可在收银台购买。`,
confirmText: `确认${action}`,
})
}
async function handleToggle(row: VipPlan, value: string | number | boolean): Promise<void> {
const enabled = Number(value) === 1 ? 1 : 0
toggleId.value = row.id
try {
await vipApi.togglePlan(row.id, enabled)
ElMessage.success(enabled === 1 ? '档位已上架' : '档位已下架')
await loadPlans()
} catch {
// 非静默接口,错误提示由 http 层统一处理
} finally {
toggleId.value = null
}
}
onMounted(async () => {
await loadApps()
await loadPlans()
})
</script>
<template>
<div class="page">
<PageHeader
title="VIP 档位"
desc="按应用维护 VIP 档位编码、价格与免扣权益,档位新增、改价与上下架均写入审计日志。"
>
<el-select
v-model="appCode"
placeholder="全部应用"
style="width: 180px"
@change="handleAppChange"
>
<el-option label="全部应用" value="" />
<el-option v-for="app in apps" :key="app.appCode" :label="app.appName" :value="app.appCode" />
</el-select>
<el-button v-perm="'fin:vip:manage'" type="primary" @click="openCreate">
<el-icon><Plus /></el-icon>
新建档位
</el-button>
</PageHeader>
<div class="notice info" style="margin-bottom: 16px">
<el-icon><InfoFilled /></el-icon>
<div>
免扣联动说明:档位标记为「免扣费」仅表示该档位享受免扣权益,是否真正免扣还取决于「兑率与策略」中对应应用的
VIP 免扣(vipExempt)开关,两处需同时开启才生效。调整档位或策略后,请到「VIP 持有」核对存量用户权益。
</div>
</div>
<div class="card">
<el-table v-loading="loading" :data="pagedPlans" border empty-text="暂无 VIP 档位">
<el-table-column prop="appName" label="应用" min-width="130" />
<el-table-column label="档位编码" width="150">
<template #default="{ row }">
<span class="mono">{{ row.planCode }}</span>
</template>
</el-table-column>
<el-table-column prop="planName" label="档位名称" min-width="150" />
<el-table-column label="有效期天数" width="110" align="right">
<template #default="{ row }">{{ row.durationDays }} 天</template>
</el-table-column>
<el-table-column label="价格" width="120" align="right">
<template #default="{ row }">
<span class="money">{{ formatCent(row.priceCent) }}</span>
</template>
</el-table-column>
<el-table-column label="免扣" width="100" align="center">
<template #default="{ row }">
<el-tag v-if="row.freeDeduction === 1" type="success" effect="plain">免扣费</el-tag>
<span v-else class="muted">—</span>
</template>
</el-table-column>
<el-table-column label="状态" width="90" align="center">
<template #default="{ row }">
<el-switch
v-perm="'fin:vip:manage'"
:model-value="row.enabled"
:active-value="1"
:inactive-value="0"
:loading="toggleId === row.id"
:before-change="() => beforeToggle(row)"
@change="(value: string | number | boolean) => handleToggle(row, value)"
/>
</template>
</el-table-column>
<el-table-column label="持有数" width="90" align="right">
<template #default="{ row }">{{ row.holderCount }}</template>
</el-table-column>
<el-table-column label="创建时间" width="170">
<template #default="{ row }">{{ formatDateTime(row.createTime) }}</template>
</el-table-column>
<el-table-column label="操作" width="90" fixed="right">
<template #default="{ row }">
<el-button v-perm="'fin:vip:manage'" link type="primary" @click="openEdit(row)">
编辑
</el-button>
</template>
</el-table-column>
</el-table>
<div class="table-footer">
<el-pagination
v-model:current-page="page"
v-model:page-size="pageSize"
:total="plans.length"
:page-sizes="[10, 20, 50]"
layout="total, sizes, prev, pager, next"
@size-change="handleSizeChange"
/>
</div>
</div>
<el-dialog
v-model="dialogVisible"
:title="isEdit ? '编辑 VIP 档位' : '新建 VIP 档位'"
width="540px"
:close-on-click-modal="false"
>
<el-form ref="formRef" :model="form" :rules="planRules" label-width="110px">
<el-form-item label="应用" prop="appCode">
<el-select
v-model="form.appCode"
:disabled="isEdit"
placeholder="请选择应用"
style="width: 100%"
>
<el-option
v-for="app in apps"
:key="app.appCode"
:label="`${app.appName}(${app.appCode})`"
:value="app.appCode"
/>
</el-select>
</el-form-item>
<el-form-item label="档位编码" prop="planCode">
<el-input
v-model="form.planCode"
:disabled="isEdit"
maxlength="32"
placeholder="如 VIP_YEAR"
/>
</el-form-item>
<el-form-item label="档位名称" prop="planName">
<el-input v-model="form.planName" maxlength="32" placeholder="如 年度 VIP" />
</el-form-item>
<el-form-item label="有效期天数" prop="durationDays">
<el-input-number v-model="form.durationDays" :min="1" :step="1" :precision="0" />
</el-form-item>
<el-form-item label="价格(元)" prop="priceYuan">
<el-input v-model="form.priceYuan" placeholder="如 199.00">
<template #append>元</template>
</el-input>
</el-form-item>
<el-form-item label="免扣费">
<el-switch v-model="form.freeDeduction" :active-value="1" :inactive-value="0" />
<span class="small muted" style="margin-left: 10px">开启后该档位用户扣费时免扣点数</span>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" :loading="submitting" @click="submitPlan">保存</el-button>
</template>
</el-dialog>
</div>
</template>