1144 lines
36 KiB
Vue
1144 lines
36 KiB
Vue
<template>
|
||
<div class="users">
|
||
<a-page-header title="用户管理" sub-title="管理系统用户" />
|
||
|
||
<a-card :bordered="false">
|
||
<template #extra>
|
||
<a-space>
|
||
<a-input-search
|
||
v-model:value="searchText"
|
||
placeholder="搜索账号、昵称或手机号"
|
||
style="width: 260px"
|
||
@search="handleSearch"
|
||
/>
|
||
<a-button v-if="hasPermission('user:create')" type="primary" @click="showAddModal">
|
||
<template #icon>
|
||
<PlusOutlined />
|
||
</template>
|
||
新增用户
|
||
</a-button>
|
||
</a-space>
|
||
</template>
|
||
|
||
<a-table
|
||
:columns="columns"
|
||
:data-source="userList"
|
||
:loading="loading"
|
||
:pagination="pagination"
|
||
row-key="id"
|
||
@change="handleTableChange"
|
||
>
|
||
<template #bodyCell="{ column, record }">
|
||
<template v-if="column.key === 'avatar'">
|
||
<a-avatar :size="40" :src="record.avatar || DEFAULT_AVATAR" />
|
||
</template>
|
||
|
||
<template v-else-if="column.key === 'shopName'">
|
||
{{ getShopDisplay(record) || '-' }}
|
||
</template>
|
||
|
||
<template v-else-if="column.key === 'type'">
|
||
<a-tag :color="getUserTypeColor(record.type)">
|
||
{{ getUserTypeText(record.type) }}
|
||
</a-tag>
|
||
</template>
|
||
|
||
<template v-else-if="column.key === 'level'">
|
||
<a-tag :color="record.level && record.level !== '普通' ? 'gold' : 'default'">
|
||
{{ record.level || '普通' }}
|
||
</a-tag>
|
||
</template>
|
||
|
||
<template v-else-if="column.key === 'status'">
|
||
<a-switch
|
||
:checked="record.status === 1"
|
||
:disabled="!hasPermission('user:disable') || record.type === 4"
|
||
@change="(checked: any) => handleStatusChange(record, checked === true)"
|
||
/>
|
||
</template>
|
||
|
||
<template v-else-if="column.key === 'actions'">
|
||
<a-space wrap>
|
||
<a-button v-if="hasPermission('user:view')" type="link" size="small" @click="showViewModal(record)">
|
||
查看
|
||
</a-button>
|
||
<a-button v-if="hasPermission('user:edit')" type="link" size="small" @click="showEditModal(record)">
|
||
编辑
|
||
</a-button>
|
||
<a-button
|
||
v-if="canOpenAssetPanel"
|
||
type="link"
|
||
size="small"
|
||
@click="showAssetModal(record)"
|
||
>
|
||
余额/VIP
|
||
</a-button>
|
||
<a-button
|
||
v-if="hasPermission('user:reset_password')"
|
||
type="link"
|
||
size="small"
|
||
@click="handleResetPassword(record)"
|
||
>
|
||
重置密码
|
||
</a-button>
|
||
<a-popconfirm
|
||
v-if="hasPermission('user:delete') && record.type !== 4"
|
||
title="确定要将这个用户从当前店铺移除吗?"
|
||
@confirm="handleDelete(record)"
|
||
>
|
||
<a-button type="link" size="small" danger>
|
||
删除
|
||
</a-button>
|
||
</a-popconfirm>
|
||
</a-space>
|
||
</template>
|
||
</template>
|
||
</a-table>
|
||
</a-card>
|
||
|
||
<a-modal
|
||
v-model:open="modalVisible"
|
||
:title="isEdit ? '编辑用户' : '新增用户'"
|
||
:confirm-loading="modalLoading"
|
||
@ok="handleModalOk"
|
||
@cancel="handleModalCancel"
|
||
>
|
||
<DraftActions
|
||
:saved-at="draftSavedAt"
|
||
@save="handleSaveDraft"
|
||
@clear="handleClearDraft"
|
||
/>
|
||
<a-form
|
||
ref="formRef"
|
||
:model="formState"
|
||
:rules="formRules"
|
||
:label-col="{ span: 6 }"
|
||
:wrapper-col="{ span: 16 }"
|
||
>
|
||
<a-form-item label="账号" name="account">
|
||
<a-input
|
||
v-model:value="formState.account"
|
||
placeholder="请输入账号"
|
||
@input="handleAccountInput"
|
||
/>
|
||
</a-form-item>
|
||
<a-form-item label="昵称" name="nickname">
|
||
<a-input v-model:value="formState.nickname" placeholder="请输入昵称" />
|
||
</a-form-item>
|
||
<a-form-item label="手机号" name="phone">
|
||
<a-input
|
||
v-model:value="formState.phone"
|
||
placeholder="请输入手机号"
|
||
:maxlength="11"
|
||
inputmode="numeric"
|
||
autocomplete="off"
|
||
/>
|
||
</a-form-item>
|
||
<a-form-item v-if="!isEdit" label="密码" name="password">
|
||
<a-input-password v-model:value="formState.password" placeholder="请输入密码" />
|
||
</a-form-item>
|
||
<a-form-item label="所属店铺" name="shopIds">
|
||
<a-select
|
||
v-model:value="formState.shopIds"
|
||
mode="multiple"
|
||
:placeholder="currentUserInfo?.type === 4 ? '请选择一个或多个店铺' : '请选择店铺'"
|
||
:loading="shopLoading"
|
||
:disabled="currentUserInfo?.type !== 4 && !!currentUserInfo?.shopId"
|
||
>
|
||
<a-select-option
|
||
v-for="shop in shopList"
|
||
:key="shop.id"
|
||
:value="getShopOptionValue(shop)"
|
||
>
|
||
{{ shop.shopName }}
|
||
</a-select-option>
|
||
</a-select>
|
||
</a-form-item>
|
||
<a-form-item label="用户类型" name="type">
|
||
<a-select v-model:value="formState.type" placeholder="请选择用户类型">
|
||
<a-select-option v-if="currentUserInfo?.type === 4" :value="0">用户</a-select-option>
|
||
<a-select-option v-if="currentUserInfo?.type === 4" :value="2">店长</a-select-option>
|
||
<a-select-option v-if="currentUserInfo?.type === 4" :value="3">美甲师</a-select-option>
|
||
<a-select-option v-if="currentUserInfo?.type === 4" :value="4">管理员</a-select-option>
|
||
<a-select-option v-else-if="currentUserInfo?.type === 2" :value="0">用户</a-select-option>
|
||
<a-select-option v-else-if="currentUserInfo?.type === 2" :value="3">美甲师</a-select-option>
|
||
<a-select-option v-else-if="currentUserInfo?.type === 3" :value="0">用户</a-select-option>
|
||
</a-select>
|
||
</a-form-item>
|
||
</a-form>
|
||
</a-modal>
|
||
|
||
<a-modal
|
||
v-model:open="viewModalVisible"
|
||
title="查看用户详情"
|
||
:footer="null"
|
||
width="640px"
|
||
@cancel="viewModalVisible = false"
|
||
>
|
||
<a-descriptions bordered :column="1" size="small">
|
||
<a-descriptions-item label="账号">{{ viewRecord?.account || '-' }}</a-descriptions-item>
|
||
<a-descriptions-item label="昵称">{{ viewRecord?.nickname || '-' }}</a-descriptions-item>
|
||
<a-descriptions-item label="手机号">{{ viewRecord?.phone || '-' }}</a-descriptions-item>
|
||
<a-descriptions-item label="所属店铺">{{ getShopDisplay(viewRecord) || '-' }}</a-descriptions-item>
|
||
<a-descriptions-item label="用户类型">{{ getUserTypeText(viewRecord?.type || 0) }}</a-descriptions-item>
|
||
<a-descriptions-item label="VIP 等级">{{ viewRecord?.level || '普通' }}</a-descriptions-item>
|
||
<a-descriptions-item label="状态">{{ viewRecord?.status === 1 ? '正常' : '禁用' }}</a-descriptions-item>
|
||
<a-descriptions-item label="创建时间">{{ viewRecord?.createTime || '-' }}</a-descriptions-item>
|
||
</a-descriptions>
|
||
</a-modal>
|
||
|
||
<a-modal
|
||
v-model:open="assetModalVisible"
|
||
title="余额 / VIP 管理"
|
||
width="960px"
|
||
:footer="null"
|
||
@cancel="handleAssetModalCancel"
|
||
>
|
||
<a-spin :spinning="assetLoading">
|
||
<div v-if="assetTargetUser" class="asset-modal">
|
||
<a-alert
|
||
:message="`${assetTargetUser.nickname}(${assetTargetUser.account})`"
|
||
:description="getShopDisplay(assetTargetUser) ? `所属店铺:${getShopDisplay(assetTargetUser)}` : '未绑定店铺'"
|
||
type="info"
|
||
show-icon
|
||
style="margin-bottom: 16px"
|
||
/>
|
||
|
||
<a-row :gutter="16" class="asset-summary">
|
||
<a-col :span="8">
|
||
<a-card size="small" title="当前余额">
|
||
<div class="summary-value">¥{{ formatMoney(assetData?.balance) }}</div>
|
||
<div class="summary-sub">累计充值:¥{{ formatMoney(assetData?.totalRechargeAmount) }}</div>
|
||
</a-card>
|
||
</a-col>
|
||
<a-col :span="8">
|
||
<a-card size="small" title="VIP 状态">
|
||
<div class="summary-value">{{ assetData?.vipLevel || '普通' }}</div>
|
||
<div class="summary-sub">
|
||
{{ assetData?.manualOverride ? '当前为手工指定' : '当前按累计充值自动判级' }}
|
||
</div>
|
||
</a-card>
|
||
</a-col>
|
||
<a-col :span="8">
|
||
<a-card size="small" title="最近充值">
|
||
<div class="summary-value">{{ formatDate(assetData?.lastRechargeTime) }}</div>
|
||
<div class="summary-sub">权益说明:{{ assetData?.benefitInfo || '-' }}</div>
|
||
</a-card>
|
||
</a-col>
|
||
</a-row>
|
||
|
||
<a-row :gutter="16" class="asset-actions">
|
||
<a-col :span="12">
|
||
<a-card size="small" title="余额变动">
|
||
<DraftActions
|
||
:saved-at="balanceDraftSavedAt"
|
||
@save="handleSaveBalanceDraft"
|
||
@clear="handleClearBalanceDraft"
|
||
/>
|
||
<a-form layout="vertical">
|
||
<a-form-item label="变动场景">
|
||
<a-select v-model:value="balanceForm.changeScene">
|
||
<a-select-option value="RECHARGE">充值</a-select-option>
|
||
<a-select-option value="MANUAL_ADD">手工增加余额</a-select-option>
|
||
<a-select-option value="MANUAL_DEDUCT">手工扣减余额</a-select-option>
|
||
</a-select>
|
||
</a-form-item>
|
||
<a-form-item label="变动金额">
|
||
<a-input-number
|
||
v-model:value="balanceForm.amount"
|
||
:min="0.01"
|
||
:precision="2"
|
||
style="width: 100%"
|
||
placeholder="请输入金额"
|
||
/>
|
||
</a-form-item>
|
||
<a-form-item label="关联系统订单 ID(可选)">
|
||
<a-input-number
|
||
v-model:value="balanceForm.relatedOrderId"
|
||
:min="1"
|
||
:precision="0"
|
||
style="width: 100%"
|
||
placeholder="如有系统订单可填写"
|
||
/>
|
||
</a-form-item>
|
||
<a-form-item label="外部单号(扣减时二选一)">
|
||
<a-input
|
||
v-model:value="balanceForm.externalOrderNo"
|
||
placeholder="没有系统订单时请填写外部单号"
|
||
/>
|
||
</a-form-item>
|
||
<a-form-item label="备注">
|
||
<a-textarea
|
||
v-model:value="balanceForm.remark"
|
||
:rows="3"
|
||
placeholder="请输入备注"
|
||
/>
|
||
</a-form-item>
|
||
<a-button
|
||
v-if="hasPermission('user:balance:manage')"
|
||
type="primary"
|
||
:loading="assetSaving"
|
||
@click="handleBalanceChange"
|
||
>
|
||
提交余额变动
|
||
</a-button>
|
||
<a-typography-text v-else type="secondary">
|
||
当前账号只有查看权限,不能调整余额。
|
||
</a-typography-text>
|
||
</a-form>
|
||
</a-card>
|
||
</a-col>
|
||
|
||
<a-col :span="12">
|
||
<a-card size="small" title="VIP 管理">
|
||
<DraftActions
|
||
:saved-at="vipDraftSavedAt"
|
||
@save="handleSaveVipDraft"
|
||
@clear="handleClearVipDraft"
|
||
/>
|
||
<a-form layout="vertical">
|
||
<a-form-item label="目标等级">
|
||
<a-select v-model:value="vipForm.vipLevel">
|
||
<a-select-option value="普通">普通</a-select-option>
|
||
<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>
|
||
<a-form-item label="备注">
|
||
<a-textarea
|
||
v-model:value="vipForm.remark"
|
||
:rows="3"
|
||
placeholder="可填写手工调整原因"
|
||
/>
|
||
</a-form-item>
|
||
<a-space>
|
||
<a-button
|
||
v-if="hasPermission('user:vip:manage')"
|
||
type="primary"
|
||
:loading="assetSaving"
|
||
@click="handleVipOverride"
|
||
>
|
||
手工设置 VIP
|
||
</a-button>
|
||
<a-button
|
||
v-if="hasPermission('user:vip:manage')"
|
||
:loading="assetSaving"
|
||
@click="handleVipRecalculate"
|
||
>
|
||
恢复自动判级
|
||
</a-button>
|
||
</a-space>
|
||
<div class="vip-note">
|
||
{{ assetData?.manualOverride ? '当前处于手工指定状态。' : '当前按累计充值自动判级。' }}
|
||
</div>
|
||
</a-form>
|
||
</a-card>
|
||
</a-col>
|
||
</a-row>
|
||
|
||
<a-card size="small" title="余额流水" class="history-card">
|
||
<a-table
|
||
:columns="historyColumns"
|
||
:data-source="balanceHistory"
|
||
:pagination="false"
|
||
size="small"
|
||
row-key="id"
|
||
:scroll="{ y: 320 }"
|
||
>
|
||
<template #bodyCell="{ column, record }">
|
||
<template v-if="column.key === 'amount'">
|
||
<span :class="Number(record.amount) >= 0 ? 'amount-plus' : 'amount-minus'">
|
||
{{ Number(record.amount) >= 0 ? '+' : '' }}{{ formatMoney(record.amount) }}
|
||
</span>
|
||
</template>
|
||
<template v-else-if="column.key === 'changeScene'">
|
||
{{ getSceneText(record.changeScene) }}
|
||
</template>
|
||
<template v-else-if="column.key === 'balanceRange'">
|
||
{{ formatMoney(record.beforeBalance) }} -> {{ formatMoney(record.afterBalance) }}
|
||
</template>
|
||
<template v-else-if="column.key === 'relatedOrder'">
|
||
{{ record.relatedOrderNo || record.externalOrderNo || '-' }}
|
||
</template>
|
||
<template v-else-if="column.key === 'changeTime'">
|
||
{{ formatDate(record.changeTime) }}
|
||
</template>
|
||
</template>
|
||
</a-table>
|
||
</a-card>
|
||
</div>
|
||
</a-spin>
|
||
</a-modal>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, reactive, ref } from 'vue'
|
||
import { message, Modal } from 'ant-design-vue'
|
||
import type { FormInstance } from 'ant-design-vue'
|
||
import { PlusOutlined } from '@ant-design/icons-vue'
|
||
import DraftActions from '@/components/DraftActions.vue'
|
||
import { DEFAULT_AVATAR } from '@/constants'
|
||
import { ACCOUNT_REGEX, ACCOUNT_RULE_MESSAGE, sanitizeAccountInput } from '@/utils/account'
|
||
import { buildDraftStorageKey, clearDraftRecord, loadDraftRecord, saveDraftRecord } from '@/utils/form-draft'
|
||
import {
|
||
adminShopApi,
|
||
adminUserApi,
|
||
type AdminUserBalanceChangeRequest,
|
||
type AdminUserVipOverrideRequest,
|
||
type Shop,
|
||
type UserAssetVO,
|
||
type UserBalanceHistoryItem,
|
||
type UserVO
|
||
} from '@/api/admin'
|
||
import { usePermission } from '@/utils/permission'
|
||
|
||
const { hasPermission, hasAnyPermission, loadUserPermissions } = usePermission()
|
||
|
||
const currentUserInfo = (() => {
|
||
try {
|
||
const userInfoStr = localStorage.getItem('adminUserInfo')
|
||
return userInfoStr ? JSON.parse(userInfoStr) : null
|
||
} catch (error) {
|
||
console.error('获取当前用户信息失败', error)
|
||
return null
|
||
}
|
||
})()
|
||
|
||
const loading = ref(false)
|
||
const shopLoading = ref(false)
|
||
const modalVisible = ref(false)
|
||
const modalLoading = ref(false)
|
||
const viewModalVisible = ref(false)
|
||
const isEdit = ref(false)
|
||
const searchText = ref('')
|
||
const formRef = ref<FormInstance>()
|
||
const userList = ref<UserVO[]>([])
|
||
const shopList = ref<Shop[]>([])
|
||
const currentUser = ref<UserVO | null>(null)
|
||
const viewRecord = ref<UserVO | null>(null)
|
||
const draftSavedAt = ref<number | null>(null)
|
||
|
||
const assetModalVisible = ref(false)
|
||
const assetLoading = ref(false)
|
||
const assetSaving = ref(false)
|
||
const assetTargetUser = ref<UserVO | null>(null)
|
||
const assetData = ref<UserAssetVO | null>(null)
|
||
const balanceHistory = ref<UserBalanceHistoryItem[]>([])
|
||
const balanceDraftSavedAt = ref<number | null>(null)
|
||
const vipDraftSavedAt = ref<number | null>(null)
|
||
|
||
const canOpenAssetPanel = computed(() =>
|
||
hasAnyPermission(['user:balance:view', 'user:balance:manage', 'user:vip:manage'])
|
||
)
|
||
const PHONE_REGEX = /^1[3-9]\d{9}$/
|
||
|
||
const pagination = reactive({
|
||
current: 1,
|
||
pageSize: 10,
|
||
total: 0,
|
||
showSizeChanger: true,
|
||
showQuickJumper: true,
|
||
showTotal: (total: number) => `共 ${total} 条`
|
||
})
|
||
|
||
const formState = reactive({
|
||
account: '',
|
||
nickname: '',
|
||
phone: '',
|
||
password: '',
|
||
type: 0,
|
||
shopIds: [] as string[]
|
||
})
|
||
|
||
const handleAccountInput = () => {
|
||
const normalized = sanitizeAccountInput(formState.account)
|
||
if (normalized !== formState.account) {
|
||
formState.account = normalized
|
||
}
|
||
}
|
||
|
||
const balanceForm = reactive<AdminUserBalanceChangeRequest>({
|
||
changeScene: 'RECHARGE',
|
||
amount: undefined as unknown as number,
|
||
relatedOrderId: undefined,
|
||
externalOrderNo: '',
|
||
remark: ''
|
||
})
|
||
|
||
const vipForm = reactive<AdminUserVipOverrideRequest>({
|
||
vipLevel: '普通',
|
||
remark: ''
|
||
})
|
||
|
||
const getBalanceDraftKey = () => buildDraftStorageKey('admin-user-assets', 'balance', assetTargetUser.value?.id ?? 'unknown')
|
||
const getVipDraftKey = () => buildDraftStorageKey('admin-user-assets', 'vip', assetTargetUser.value?.id ?? 'unknown')
|
||
|
||
const restoreBalanceDraft = () => {
|
||
const draft = loadDraftRecord<{
|
||
changeScene: string
|
||
amount?: number
|
||
relatedOrderId?: number
|
||
externalOrderNo: string
|
||
remark: string
|
||
}>(getBalanceDraftKey())
|
||
if (!draft) {
|
||
balanceDraftSavedAt.value = null
|
||
return
|
||
}
|
||
Object.assign(balanceForm, draft.payload)
|
||
balanceDraftSavedAt.value = draft.savedAt
|
||
message.success('已恢复余额变动暂存')
|
||
}
|
||
|
||
const handleSaveBalanceDraft = () => {
|
||
const draft = saveDraftRecord(getBalanceDraftKey(), {
|
||
changeScene: balanceForm.changeScene,
|
||
amount: balanceForm.amount,
|
||
relatedOrderId: balanceForm.relatedOrderId,
|
||
externalOrderNo: balanceForm.externalOrderNo,
|
||
remark: balanceForm.remark
|
||
})
|
||
balanceDraftSavedAt.value = draft.savedAt
|
||
message.success('余额变动已暂存到本地')
|
||
}
|
||
|
||
const handleClearBalanceDraft = (showMessage = true) => {
|
||
clearDraftRecord(getBalanceDraftKey())
|
||
balanceDraftSavedAt.value = null
|
||
if (showMessage) {
|
||
message.success('已清除余额变动暂存')
|
||
}
|
||
}
|
||
|
||
const restoreVipDraft = () => {
|
||
const draft = loadDraftRecord<{
|
||
vipLevel: string
|
||
remark: string
|
||
}>(getVipDraftKey())
|
||
if (!draft) {
|
||
vipDraftSavedAt.value = null
|
||
return
|
||
}
|
||
Object.assign(vipForm, draft.payload)
|
||
vipDraftSavedAt.value = draft.savedAt
|
||
message.success('已恢复 VIP 暂存')
|
||
}
|
||
|
||
const handleSaveVipDraft = () => {
|
||
const draft = saveDraftRecord(getVipDraftKey(), {
|
||
vipLevel: vipForm.vipLevel,
|
||
remark: vipForm.remark || ''
|
||
})
|
||
vipDraftSavedAt.value = draft.savedAt
|
||
message.success('VIP 设置已暂存到本地')
|
||
}
|
||
|
||
const handleClearVipDraft = (showMessage = true) => {
|
||
clearDraftRecord(getVipDraftKey())
|
||
vipDraftSavedAt.value = null
|
||
if (showMessage) {
|
||
message.success('已清除 VIP 暂存')
|
||
}
|
||
}
|
||
|
||
const formRules = {
|
||
account: [
|
||
{ required: true, message: '请输入账号', trigger: 'blur' },
|
||
{ pattern: ACCOUNT_REGEX, message: ACCOUNT_RULE_MESSAGE, trigger: 'blur' }
|
||
],
|
||
nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
|
||
phone: [
|
||
{ required: true, message: '请输入手机号', trigger: 'blur' },
|
||
{
|
||
validator: async (_rule: unknown, value: string) => {
|
||
const normalized = value?.trim()
|
||
if (!normalized) {
|
||
return Promise.reject('请输入手机号')
|
||
}
|
||
if (!PHONE_REGEX.test(normalized)) {
|
||
return Promise.reject('请输入正确的 11 位手机号')
|
||
}
|
||
return Promise.resolve()
|
||
},
|
||
trigger: ['blur', 'change']
|
||
}
|
||
],
|
||
password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
|
||
shopIds: [{
|
||
validator: async (_rule: unknown, value: string[]) => {
|
||
if (Array.isArray(value) && value.length > 0) {
|
||
return Promise.resolve()
|
||
}
|
||
return Promise.reject('请选择至少一个店铺')
|
||
},
|
||
trigger: 'change'
|
||
}],
|
||
type: [{ required: true, message: '请选择用户类型', trigger: 'change' }]
|
||
} as any
|
||
|
||
const columns = [
|
||
{ title: '头像', key: 'avatar', width: 80 },
|
||
{ title: '账号', dataIndex: 'account', key: 'account' },
|
||
{ title: '昵称', dataIndex: 'nickname', key: 'nickname' },
|
||
{ title: '手机号', dataIndex: 'phone', key: 'phone' },
|
||
{ title: '所属店铺', key: 'shopName', width: 140 },
|
||
{ title: '用户类型', key: 'type', width: 110 },
|
||
{ title: 'VIP 等级', key: 'level', width: 110 },
|
||
{ title: '状态', key: 'status', width: 100 },
|
||
{ title: '创建时间', dataIndex: 'createTime', key: 'createTime', width: 180 },
|
||
{ title: '操作', key: 'actions', width: 280, fixed: 'right' as const }
|
||
]
|
||
|
||
const historyColumns = [
|
||
{ title: '场景', key: 'changeScene', width: 120 },
|
||
{ title: '金额', key: 'amount', width: 110 },
|
||
{ title: '余额变化', key: 'balanceRange', width: 170 },
|
||
{ title: '关联单号', key: 'relatedOrder', width: 180 },
|
||
{ title: '备注', dataIndex: 'remark', key: 'remark' },
|
||
{ title: '时间', key: 'changeTime', width: 180 }
|
||
]
|
||
|
||
const getUserTypeText = (type: number) => {
|
||
const map: Record<number, string> = {
|
||
0: '用户',
|
||
2: '店长',
|
||
3: '美甲师',
|
||
4: '管理员'
|
||
}
|
||
return map[type] || '未知'
|
||
}
|
||
|
||
const getUserTypeColor = (type: number) => {
|
||
const map: Record<number, string> = {
|
||
0: 'default',
|
||
2: 'blue',
|
||
3: 'green',
|
||
4: 'purple'
|
||
}
|
||
return map[type] || 'default'
|
||
}
|
||
|
||
const getSceneText = (scene?: string) => {
|
||
const map: Record<string, string> = {
|
||
RECHARGE: '充值',
|
||
MANUAL_ADD: '手工增加',
|
||
MANUAL_DEDUCT: '手工扣减',
|
||
ORDER_CONSUME: '订单消费',
|
||
ORDER_REFUND: '订单退款'
|
||
}
|
||
return scene ? map[scene] || scene : '-'
|
||
}
|
||
|
||
const formatMoney = (value?: number | string | null) => {
|
||
if (value === undefined || value === null || value === '') {
|
||
return '0.00'
|
||
}
|
||
return Number(value).toFixed(2)
|
||
}
|
||
|
||
const formatDate = (value?: string) => {
|
||
if (!value) {
|
||
return '-'
|
||
}
|
||
return value.replace('T', ' ').slice(0, 19)
|
||
}
|
||
|
||
const filterVisibleUsers = (list: UserVO[]) => {
|
||
if (!currentUserInfo || currentUserInfo.type === 4) {
|
||
return list
|
||
}
|
||
if (currentUserInfo.type === 2) {
|
||
return list.filter(user => user.type === 0 || user.type === 3)
|
||
}
|
||
if (currentUserInfo.type === 3) {
|
||
return list.filter(user => user.type === 0)
|
||
}
|
||
return list
|
||
}
|
||
|
||
const normalizeShopId = (value: unknown) => {
|
||
if (value === undefined || value === null || value === '') {
|
||
return undefined
|
||
}
|
||
return String(value)
|
||
}
|
||
|
||
const getShopOptionValue = (shop: Shop) => normalizeShopId((shop as any).id) || ''
|
||
|
||
const getShopNameById = (shopId?: string | number | null) => {
|
||
const targetShopId = normalizeShopId(shopId)
|
||
if (!targetShopId) {
|
||
return ''
|
||
}
|
||
const matchedShop = shopList.value.find(shop => getShopOptionValue(shop) === targetShopId)
|
||
return matchedShop?.shopName || ''
|
||
}
|
||
|
||
const getShopDisplay = (user?: Partial<UserVO> | Record<string, any> | null) => {
|
||
if (!user) {
|
||
return ''
|
||
}
|
||
const names = (user.shopNames || []).filter(Boolean)
|
||
if (names.length) {
|
||
return names.join('、')
|
||
}
|
||
if (user.shopName) {
|
||
return user.shopName
|
||
}
|
||
if (user.shopIds?.length) {
|
||
return (user.shopIds as Array<string | number>)
|
||
.map((id: string | number) => getShopNameById(id) || `店铺 ${id}`)
|
||
.join('、')
|
||
}
|
||
return getShopNameById(user.shopId) || ''
|
||
}
|
||
|
||
const resolveEditableShopIds = (user?: UserVO | null) => {
|
||
const sourceIds = user?.shopIds?.length
|
||
? user.shopIds
|
||
: (user?.shopId ? [user.shopId] : [])
|
||
const normalizedIds = sourceIds
|
||
.map(id => normalizeShopId(id))
|
||
.filter((id): id is string => Boolean(id))
|
||
.filter(id => shopList.value.some(shop => getShopOptionValue(shop) === id))
|
||
|
||
if (normalizedIds.length) {
|
||
return normalizedIds
|
||
}
|
||
|
||
return currentUserInfo?.type !== 4 && currentUserInfo?.shopId
|
||
? [normalizeShopId(currentUserInfo.shopId)!]
|
||
: []
|
||
}
|
||
|
||
const fetchShopList = async () => {
|
||
shopLoading.value = true
|
||
try {
|
||
const shops = await adminShopApi.listAllShops() as unknown as Shop[]
|
||
shopList.value = shops || []
|
||
if (currentUserInfo?.type !== 4 && currentUserInfo?.shopId) {
|
||
formState.shopIds = [normalizeShopId(currentUserInfo.shopId)!]
|
||
}
|
||
} catch (error) {
|
||
console.error('获取店铺列表失败:', error)
|
||
} finally {
|
||
shopLoading.value = false
|
||
}
|
||
}
|
||
|
||
const fetchUserList = async () => {
|
||
loading.value = true
|
||
try {
|
||
const params: Record<string, unknown> = {
|
||
page: pagination.current,
|
||
size: pagination.pageSize,
|
||
keyword: searchText.value || undefined
|
||
}
|
||
|
||
if (currentUserInfo?.shopId) {
|
||
params.shopId = currentUserInfo.shopId
|
||
}
|
||
|
||
const res = await adminUserApi.listUsers(params as any)
|
||
const list = filterVisibleUsers(res.list || [])
|
||
userList.value = list
|
||
pagination.total = res.total || list.length
|
||
} catch (error) {
|
||
console.error('获取用户列表失败:', error)
|
||
message.error('获取用户列表失败')
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
const resetFormState = () => {
|
||
Object.assign(formState, {
|
||
account: '',
|
||
nickname: '',
|
||
phone: '',
|
||
password: '',
|
||
type: 0,
|
||
shopIds: currentUserInfo?.type !== 4 && currentUserInfo?.shopId
|
||
? [normalizeShopId(currentUserInfo.shopId)!]
|
||
: []
|
||
})
|
||
}
|
||
|
||
const getDraftKey = () => buildDraftStorageKey('admin-users', isEdit.value ? 'edit' : 'create', currentUser.value?.id ?? 'new')
|
||
|
||
const buildDraftPayload = () => ({
|
||
account: formState.account,
|
||
nickname: formState.nickname,
|
||
phone: formState.phone,
|
||
password: formState.password,
|
||
type: formState.type,
|
||
shopIds: [...formState.shopIds]
|
||
})
|
||
|
||
const restoreDraft = () => {
|
||
const draft = loadDraftRecord<ReturnType<typeof buildDraftPayload>>(getDraftKey(), 'session')
|
||
if (!draft) {
|
||
draftSavedAt.value = null
|
||
return
|
||
}
|
||
|
||
Object.assign(formState, draft.payload)
|
||
draftSavedAt.value = draft.savedAt
|
||
message.success('已恢复暂存内容')
|
||
}
|
||
|
||
const handleSaveDraft = () => {
|
||
const draft = saveDraftRecord(getDraftKey(), buildDraftPayload(), 'session')
|
||
draftSavedAt.value = draft.savedAt
|
||
message.success('已暂存到当前会话')
|
||
}
|
||
|
||
const handleClearDraft = (showMessage = true) => {
|
||
clearDraftRecord(getDraftKey(), 'session')
|
||
draftSavedAt.value = null
|
||
if (showMessage) {
|
||
message.success('已清除暂存')
|
||
}
|
||
}
|
||
|
||
const handleSearch = () => {
|
||
pagination.current = 1
|
||
void fetchUserList()
|
||
}
|
||
|
||
const handleTableChange = (pag: any) => {
|
||
pagination.current = pag.current || 1
|
||
pagination.pageSize = pag.pageSize || pagination.pageSize
|
||
void fetchUserList()
|
||
}
|
||
|
||
const showAddModal = () => {
|
||
isEdit.value = false
|
||
currentUser.value = null
|
||
resetFormState()
|
||
restoreDraft()
|
||
modalVisible.value = true
|
||
}
|
||
|
||
const showEditModal = (record: any) => {
|
||
isEdit.value = true
|
||
currentUser.value = record
|
||
Object.assign(formState, {
|
||
account: record.account,
|
||
nickname: record.nickname,
|
||
phone: record.phone || '',
|
||
password: '',
|
||
type: record.type,
|
||
shopIds: resolveEditableShopIds(record)
|
||
})
|
||
restoreDraft()
|
||
modalVisible.value = true
|
||
}
|
||
|
||
const showViewModal = (record: any) => {
|
||
viewRecord.value = record
|
||
viewModalVisible.value = true
|
||
}
|
||
|
||
const loadUserAssetData = async (userId: number) => {
|
||
const [asset, history] = await Promise.all([
|
||
adminUserApi.getUserAsset(userId),
|
||
adminUserApi.getUserBalanceHistory(userId)
|
||
])
|
||
assetData.value = asset
|
||
balanceHistory.value = history || []
|
||
vipForm.vipLevel = asset.vipLevel || '普通'
|
||
}
|
||
|
||
const resetAssetForms = () => {
|
||
Object.assign(balanceForm, {
|
||
changeScene: 'RECHARGE',
|
||
amount: undefined,
|
||
relatedOrderId: undefined,
|
||
externalOrderNo: '',
|
||
remark: ''
|
||
})
|
||
Object.assign(vipForm, {
|
||
vipLevel: assetData.value?.vipLevel || '普通',
|
||
remark: ''
|
||
})
|
||
}
|
||
|
||
const showAssetModal = async (record: any) => {
|
||
assetTargetUser.value = record
|
||
assetModalVisible.value = true
|
||
assetLoading.value = true
|
||
assetData.value = null
|
||
balanceHistory.value = []
|
||
resetAssetForms()
|
||
try {
|
||
await loadUserAssetData(record.id)
|
||
resetAssetForms()
|
||
restoreBalanceDraft()
|
||
restoreVipDraft()
|
||
} catch (error) {
|
||
console.error('获取用户资产失败:', error)
|
||
message.error('获取用户资产失败')
|
||
} finally {
|
||
assetLoading.value = false
|
||
}
|
||
}
|
||
|
||
const handleAssetModalCancel = () => {
|
||
assetModalVisible.value = false
|
||
assetTargetUser.value = null
|
||
assetData.value = null
|
||
balanceHistory.value = []
|
||
resetAssetForms()
|
||
}
|
||
|
||
const handleModalOk = async () => {
|
||
try {
|
||
await formRef.value?.validate()
|
||
modalLoading.value = true
|
||
const requestData: Record<string, unknown> = {
|
||
account: formState.account.trim(),
|
||
nickname: formState.nickname.trim(),
|
||
phone: formState.phone.trim(),
|
||
type: formState.type,
|
||
shopIds: formState.shopIds,
|
||
shopId: formState.shopIds.length ? formState.shopIds[0] : undefined,
|
||
...(formState.password.trim() ? { password: formState.password.trim() } : {})
|
||
}
|
||
|
||
if (isEdit.value && currentUserInfo?.type !== 4) {
|
||
delete requestData.shopIds
|
||
delete requestData.shopId
|
||
}
|
||
|
||
if (isEdit.value && currentUser.value) {
|
||
await adminUserApi.updateUser(currentUser.value.id, requestData)
|
||
message.success('更新成功')
|
||
} else {
|
||
await adminUserApi.createUser(requestData)
|
||
message.success('创建成功')
|
||
}
|
||
|
||
handleClearDraft(false)
|
||
modalVisible.value = false
|
||
formRef.value?.resetFields()
|
||
await fetchUserList()
|
||
} catch (error: any) {
|
||
console.error('用户保存失败:', error)
|
||
message.error(error.message || '操作失败')
|
||
} finally {
|
||
modalLoading.value = false
|
||
}
|
||
}
|
||
|
||
const handleModalCancel = () => {
|
||
modalVisible.value = false
|
||
formRef.value?.resetFields()
|
||
resetFormState()
|
||
}
|
||
|
||
const handleStatusChange = async (record: any, checked: boolean) => {
|
||
try {
|
||
await adminUserApi.updateUserStatus(record.id, checked ? 1 : 0)
|
||
record.status = checked ? 1 : 0
|
||
message.success(`已${checked ? '启用' : '停用'}当前店铺下的用户访问`)
|
||
} catch (error) {
|
||
console.error('状态更新失败:', error)
|
||
message.error('状态更新失败')
|
||
}
|
||
}
|
||
|
||
const handleResetPassword = (record: any) => {
|
||
Modal.confirm({
|
||
title: '确认重置密码',
|
||
content: `确定要将用户 ${record.nickname} 的密码重置为 888888 吗?`,
|
||
async onOk() {
|
||
try {
|
||
await adminUserApi.resetPassword(record.id)
|
||
message.success('密码重置成功')
|
||
} catch (error) {
|
||
console.error('重置密码失败:', error)
|
||
message.error('密码重置失败')
|
||
}
|
||
}
|
||
})
|
||
}
|
||
|
||
const handleDelete = async (record: any) => {
|
||
try {
|
||
if (record.type === 4) {
|
||
message.warning('管理员账号无法被删除')
|
||
return
|
||
}
|
||
await adminUserApi.deleteUser(record.id)
|
||
message.success('已移除当前店铺下的用户绑定')
|
||
await fetchUserList()
|
||
} catch (error) {
|
||
console.error('删除用户失败:', error)
|
||
message.error('删除失败')
|
||
}
|
||
}
|
||
|
||
const refreshAssetPanel = async () => {
|
||
if (!assetTargetUser.value) {
|
||
return
|
||
}
|
||
assetLoading.value = true
|
||
try {
|
||
await loadUserAssetData(assetTargetUser.value.id)
|
||
const matchedUser = userList.value.find(user => user.id === assetTargetUser.value?.id)
|
||
if (matchedUser && assetData.value) {
|
||
matchedUser.level = assetData.value.vipLevel
|
||
matchedUser.vipId = assetData.value.vipInfoId
|
||
}
|
||
} finally {
|
||
assetLoading.value = false
|
||
}
|
||
}
|
||
|
||
const handleBalanceChange = async () => {
|
||
if (!assetTargetUser.value) {
|
||
return
|
||
}
|
||
if (!balanceForm.amount || Number(balanceForm.amount) <= 0) {
|
||
message.warning('请输入大于 0 的金额')
|
||
return
|
||
}
|
||
if (
|
||
balanceForm.changeScene === 'MANUAL_DEDUCT'
|
||
&& !balanceForm.relatedOrderId
|
||
&& !balanceForm.externalOrderNo?.trim()
|
||
) {
|
||
message.warning('手工扣减余额时必须填写系统订单 ID 或外部单号')
|
||
return
|
||
}
|
||
|
||
assetSaving.value = true
|
||
try {
|
||
await adminUserApi.changeUserBalance(assetTargetUser.value.id, {
|
||
amount: Number(balanceForm.amount),
|
||
changeScene: balanceForm.changeScene,
|
||
relatedOrderId: balanceForm.relatedOrderId,
|
||
externalOrderNo: balanceForm.externalOrderNo?.trim() || undefined,
|
||
remark: balanceForm.remark?.trim() || undefined
|
||
})
|
||
message.success('余额变动已保存')
|
||
handleClearBalanceDraft(false)
|
||
Object.assign(balanceForm, {
|
||
amount: undefined,
|
||
relatedOrderId: undefined,
|
||
externalOrderNo: '',
|
||
remark: ''
|
||
})
|
||
await refreshAssetPanel()
|
||
} catch (error: any) {
|
||
console.error('余额变动失败:', error)
|
||
message.error(error.message || '余额变动失败')
|
||
} finally {
|
||
assetSaving.value = false
|
||
}
|
||
}
|
||
|
||
const handleVipOverride = async () => {
|
||
if (!assetTargetUser.value) {
|
||
return
|
||
}
|
||
assetSaving.value = true
|
||
try {
|
||
await adminUserApi.overrideUserVip(assetTargetUser.value.id, {
|
||
vipLevel: vipForm.vipLevel,
|
||
remark: vipForm.remark?.trim() || undefined
|
||
})
|
||
message.success('VIP 已更新')
|
||
handleClearVipDraft(false)
|
||
vipForm.remark = ''
|
||
await refreshAssetPanel()
|
||
} catch (error: any) {
|
||
console.error('VIP 更新失败:', error)
|
||
message.error(error.message || 'VIP 更新失败')
|
||
} finally {
|
||
assetSaving.value = false
|
||
}
|
||
}
|
||
|
||
const handleVipRecalculate = () => {
|
||
if (!assetTargetUser.value) {
|
||
return
|
||
}
|
||
Modal.confirm({
|
||
title: '恢复自动判级',
|
||
content: '恢复后会清除手工指定状态,并按累计充值金额重新计算 VIP 等级,是否继续?',
|
||
async onOk() {
|
||
assetSaving.value = true
|
||
try {
|
||
await adminUserApi.recalculateUserVip(assetTargetUser.value!.id)
|
||
message.success('已恢复自动判级')
|
||
handleClearVipDraft(false)
|
||
vipForm.remark = ''
|
||
await refreshAssetPanel()
|
||
} catch (error: any) {
|
||
console.error('恢复自动判级失败:', error)
|
||
message.error(error.message || '恢复自动判级失败')
|
||
} finally {
|
||
assetSaving.value = false
|
||
}
|
||
}
|
||
})
|
||
}
|
||
|
||
onMounted(async () => {
|
||
await loadUserPermissions()
|
||
await fetchShopList()
|
||
await fetchUserList()
|
||
})
|
||
</script>
|
||
|
||
<style scoped>
|
||
.users {
|
||
padding: 0;
|
||
}
|
||
|
||
.asset-modal {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 16px;
|
||
}
|
||
|
||
.asset-summary,
|
||
.asset-actions,
|
||
.history-card {
|
||
margin-top: 0;
|
||
}
|
||
|
||
.summary-value {
|
||
font-size: 22px;
|
||
font-weight: 600;
|
||
line-height: 1.4;
|
||
}
|
||
|
||
.summary-sub {
|
||
margin-top: 8px;
|
||
color: #666;
|
||
font-size: 12px;
|
||
line-height: 1.6;
|
||
}
|
||
|
||
.vip-note {
|
||
margin-top: 12px;
|
||
color: #666;
|
||
font-size: 12px;
|
||
}
|
||
|
||
.amount-plus {
|
||
color: #52c41a;
|
||
font-weight: 600;
|
||
}
|
||
|
||
.amount-minus {
|
||
color: #ff4d4f;
|
||
font-weight: 600;
|
||
}
|
||
</style>
|