fetch(add): 账户管理

This commit is contained in:
sexygoat
2026-01-23 17:18:24 +08:00
parent 339abca4c0
commit b53fea43c6
93 changed files with 7094 additions and 3153 deletions

View File

@@ -1,276 +1,570 @@
<template>
<div class="page-content">
<ElRow>
<ElCol :xs="24" :sm="12" :lg="6">
<ElInput v-model="searchQuery" placeholder="账号/姓名/手机号" clearable></ElInput>
</ElCol>
<div style="width: 12px"></div>
<ElCol :xs="24" :sm="12" :lg="6">
<ElSelect v-model="typeFilter" placeholder="客户类型" clearable style="width: 100%">
<ElOption label="代理商" value="agent" />
<ElOption label="企业客户" value="enterprise" />
</ElSelect>
</ElCol>
<div style="width: 12px"></div>
<ElCol :xs="24" :sm="12" :lg="6">
<ElSelect v-model="statusFilter" placeholder="账号状态" clearable style="width: 100%">
<ElOption label="正常" value="active" />
<ElOption label="禁用" value="disabled" />
<ElOption label="已锁定" value="locked" />
</ElSelect>
</ElCol>
<div style="width: 12px"></div>
<ElCol :xs="24" :sm="12" :lg="6" class="el-col2">
<ElButton v-ripple @click="handleSearch">搜索</ElButton>
</ElCol>
</ElRow>
<ArtTableFullScreen>
<div class="customer-account-page" id="table-full-screen">
<!-- 搜索栏 -->
<ArtSearchBar
v-model:filter="searchForm"
:items="searchFormItems"
:show-expand="false"
@reset="handleReset"
@search="handleSearch"
></ArtSearchBar>
<ArtTable :data="filteredData" index>
<template #default>
<ElTableColumn label="账号" prop="username" min-width="120" />
<ElTableColumn label="真实姓名" prop="realName" />
<ElTableColumn label="客户类型" prop="customerType">
<template #default="scope">
<ElTag :type="scope.row.customerType === 'agent' ? '' : 'success'">
{{ scope.row.customerType === 'agent' ? '代理商' : '企业客户' }}
</ElTag>
<ElCard shadow="never" class="art-table-card">
<!-- 表格头部 -->
<ArtTableHeader
:columnList="columnOptions"
v-model:columns="columnChecks"
@refresh="handleRefresh"
>
<template #left>
<ElButton @click="showDialog('add')">新增代理商账号</ElButton>
</template>
</ElTableColumn>
<ElTableColumn label="所属组织" prop="organizationName" show-overflow-tooltip />
<ElTableColumn label="手机号" prop="phone" />
<ElTableColumn label="邮箱" prop="email" show-overflow-tooltip />
<ElTableColumn label="登录次数" prop="loginCount" />
<ElTableColumn label="最后登录" prop="lastLoginTime" width="180" />
<ElTableColumn label="状态" prop="status">
<template #default="scope">
<ElTag :type="getStatusTagType(scope.row.status)">
{{ getStatusText(scope.row.status) }}
</ElTag>
</template>
</ElTableColumn>
<ElTableColumn label="创建时间" prop="createTime" width="180" />
<ElTableColumn fixed="right" label="操作" width="280">
<template #default="scope">
<el-button link @click="viewDetail(scope.row)">详情</el-button>
<el-button link @click="unbindPhone(scope.row)">解绑手机</el-button>
<el-button link @click="resetPassword(scope.row)">重置密码</el-button>
<el-button
link
:type="scope.row.status === 'active' ? 'danger' : 'primary'"
@click="toggleStatus(scope.row)"
>
{{ scope.row.status === 'active' ? '禁用' : '启用' }}
</el-button>
</template>
</ElTableColumn>
</template>
</ArtTable>
</ArtTableHeader>
<!-- 操作记录对话框 -->
<ElDialog v-model="detailDialogVisible" title="账号详情" width="800px" align-center>
<ElDescriptions :column="2" border>
<ElDescriptionsItem label="账号">{{ currentAccount?.username }}</ElDescriptionsItem>
<ElDescriptionsItem label="真实姓名">{{ currentAccount?.realName }}</ElDescriptionsItem>
<ElDescriptionsItem label="客户类型">{{
currentAccount?.customerType === 'agent' ? '代理商' : '企业客户'
}}</ElDescriptionsItem>
<ElDescriptionsItem label="所属组织">{{
currentAccount?.organizationName
}}</ElDescriptionsItem>
<ElDescriptionsItem label="手机号">{{ currentAccount?.phone }}</ElDescriptionsItem>
<ElDescriptionsItem label="邮箱">{{ currentAccount?.email }}</ElDescriptionsItem>
<ElDescriptionsItem label="注册时间">{{ currentAccount?.createTime }}</ElDescriptionsItem>
<ElDescriptionsItem label="最后登录">{{
currentAccount?.lastLoginTime
}}</ElDescriptionsItem>
<ElDescriptionsItem label="登录次数">{{ currentAccount?.loginCount }}</ElDescriptionsItem>
<ElDescriptionsItem label="状态">
<ElTag :type="getStatusTagType(currentAccount?.status || 'active')">
{{ getStatusText(currentAccount?.status || 'active') }}
</ElTag>
</ElDescriptionsItem>
</ElDescriptions>
<!-- 表格 -->
<ArtTable
ref="tableRef"
row-key="id"
:loading="loading"
:data="accountList"
:currentPage="pagination.page"
:pageSize="pagination.pageSize"
:total="pagination.total"
:marginTop="10"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
>
<template #default>
<ElTableColumn v-for="col in columns" :key="col.prop || col.type" v-bind="col" />
</template>
</ArtTable>
<ElDivider>操作记录</ElDivider>
<ArtTable :data="operationRecords" index max-height="300">
<template #default>
<ElTableColumn label="操作类型" prop="operationType" />
<ElTableColumn label="操作描述" prop="description" />
<ElTableColumn label="操作人" prop="operator" />
<ElTableColumn label="操作时间" prop="operateTime" width="180" />
</template>
</ArtTable>
</ElDialog>
</div>
<!-- 新增/编辑对话框 -->
<ElDialog
v-model="dialogVisible"
:title="dialogType === 'add' ? '新增代理商账号' : '编辑账号'"
width="500px"
>
<ElForm ref="formRef" :model="form" :rules="rules" label-width="80px">
<ElFormItem label="用户名" prop="username">
<ElInput
v-model="form.username"
placeholder="请输入用户名"
:disabled="dialogType === 'edit'"
/>
</ElFormItem>
<ElFormItem label="手机号" prop="phone">
<ElInput v-model="form.phone" placeholder="请输入手机号" />
</ElFormItem>
<ElFormItem v-if="dialogType === 'add'" label="密码" prop="password">
<ElInput
v-model="form.password"
type="password"
placeholder="请输入密码(6-20位)"
show-password
/>
</ElFormItem>
<ElFormItem label="店铺" prop="shop_id">
<ElSelect
v-model="form.shop_id"
placeholder="请选择店铺"
filterable
remote
:remote-method="searchShops"
:loading="shopLoading"
clearable
style="width: 100%"
>
<ElOption
v-for="shop in shopList"
:key="shop.id"
:label="shop.shop_name"
:value="shop.id"
/>
</ElSelect>
</ElFormItem>
</ElForm>
<template #footer>
<div class="dialog-footer">
<ElButton @click="dialogVisible = false">取消</ElButton>
<ElButton type="primary" @click="handleSubmit(formRef)" :loading="submitLoading">
提交
</ElButton>
</div>
</template>
</ElDialog>
<!-- 修改密码对话框 -->
<ElDialog v-model="passwordDialogVisible" title="修改密码" width="400px">
<ElForm ref="passwordFormRef" :model="passwordForm" :rules="passwordRules">
<ElFormItem label="新密码" prop="password">
<ElInput
v-model="passwordForm.password"
type="password"
placeholder="请输入新密码(6-20位)"
show-password
/>
</ElFormItem>
</ElForm>
<template #footer>
<div class="dialog-footer">
<ElButton @click="passwordDialogVisible = false">取消</ElButton>
<ElButton
type="primary"
@click="handlePasswordSubmit(passwordFormRef)"
:loading="passwordSubmitLoading"
>
提交
</ElButton>
</div>
</template>
</ElDialog>
</ElCard>
</div>
</ArtTableFullScreen>
</template>
<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import { h } from 'vue'
import { CustomerAccountService, ShopService } from '@/api/modules'
import { ElMessage, ElTag, ElSwitch } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import type { CustomerAccountItem, ShopResponse } from '@/types/api'
import type { SearchFormItem } from '@/types'
import { useCheckedColumns } from '@/composables/useCheckedColumns'
import ArtButtonTable from '@/components/core/forms/ArtButtonTable.vue'
import { formatDateTime } from '@/utils/business/format'
defineOptions({ name: 'CustomerAccount' })
interface CustomerAccount {
id: string
username: string
realName: string
customerType: 'agent' | 'enterprise'
organizationName: string
phone: string
email: string
loginCount: number
lastLoginTime: string
status: 'active' | 'disabled' | 'locked'
createTime: string
const dialogVisible = ref(false)
const passwordDialogVisible = ref(false)
const loading = ref(false)
const submitLoading = ref(false)
const passwordSubmitLoading = ref(false)
const shopLoading = ref(false)
const tableRef = ref()
const currentAccountId = ref<number>(0)
const shopList = ref<ShopResponse[]>([])
// 搜索表单初始值
const initialSearchState = {
username: '',
phone: '',
user_type: undefined as number | undefined,
status: undefined as number | undefined
}
// Mock 数据
const mockData = ref<CustomerAccount[]>([
// 搜索表单
const searchForm = reactive({ ...initialSearchState })
// 用户类型选项
const userTypeOptions = [
{ label: '个人账号', value: 2 },
{ label: '代理账号', value: 3 },
{ label: '企业账号', value: 4 }
]
// 状态选项
const statusOptions = [
{ label: '启用', value: 1 },
{ label: '禁用', value: 0 }
]
// 搜索表单配置
const searchFormItems = computed<SearchFormItem[]>(() => [
{
id: '1',
username: 'agent001',
realName: '张三',
customerType: 'agent',
organizationName: '华东区总代理',
phone: '13800138001',
email: 'zhangsan@example.com',
loginCount: 328,
lastLoginTime: '2026-01-09 08:30:00',
status: 'active',
createTime: '2026-01-01 10:00:00'
label: '用户名',
prop: 'username',
type: 'input',
config: {
clearable: true,
placeholder: '请输入用户名'
}
},
{
id: '2',
username: 'enterprise001',
realName: '李四',
customerType: 'enterprise',
organizationName: '某某科技有限公司',
phone: '13800138002',
email: 'lisi@example.com',
loginCount: 156,
lastLoginTime: '2026-01-08 18:45:00',
status: 'active',
createTime: '2026-01-03 11:00:00'
label: '手机号',
prop: 'phone',
type: 'input',
config: {
clearable: true,
placeholder: '请输入手机号'
}
},
{
id: '3',
username: 'agent002',
realName: '王五',
customerType: 'agent',
organizationName: '江苏省代理',
phone: '13800138003',
email: 'wangwu@example.com',
loginCount: 89,
lastLoginTime: '2026-01-07 14:20:00',
status: 'disabled',
createTime: '2026-01-05 12:00:00'
label: '用户类型',
prop: 'user_type',
type: 'select',
options: userTypeOptions,
config: {
clearable: true,
placeholder: '请选择用户类型'
}
},
{
label: '状态',
prop: 'status',
type: 'select',
options: statusOptions,
config: {
clearable: true,
placeholder: '请选择状态'
}
}
])
const operationRecords = ref([
{
operationType: '重置密码',
description: '管理员重置登录密码',
operator: 'admin',
operateTime: '2026-01-08 10:00:00'
},
{
operationType: '解绑手机',
description: '解绑原手机号并重新绑定',
operator: 'admin',
operateTime: '2026-01-06 15:30:00'
}
])
const searchQuery = ref('')
const typeFilter = ref('')
const statusFilter = ref('')
const detailDialogVisible = ref(false)
const currentAccount = ref<CustomerAccount | null>(null)
const filteredData = computed(() => {
let data = mockData.value
if (searchQuery.value) {
const query = searchQuery.value.toLowerCase()
data = data.filter(
(item) =>
item.username.toLowerCase().includes(query) ||
item.realName.includes(query) ||
item.phone.includes(query)
)
}
if (typeFilter.value) {
data = data.filter((item) => item.customerType === typeFilter.value)
}
if (statusFilter.value) {
data = data.filter((item) => item.status === statusFilter.value)
}
return data
// 分页
const pagination = reactive({
page: 1,
pageSize: 20,
total: 0
})
const getStatusText = (status: string) => {
const statusMap: Record<string, string> = {
active: '正常',
disabled: '禁用',
locked: '已锁定'
// 列配置
const columnOptions = [
{ label: 'ID', prop: 'id' },
{ label: '用户名', prop: 'username' },
{ label: '手机号', prop: 'phone' },
{ label: '用户类型', prop: 'user_type_name' },
{ label: '店铺名称', prop: 'shop_name' },
{ label: '企业名称', prop: 'enterprise_name' },
{ label: '状态', prop: 'status' },
{ label: '创建时间', prop: 'created_at' },
{ label: '操作', prop: 'operation' }
]
const formRef = ref<FormInstance>()
const passwordFormRef = ref<FormInstance>()
const rules = reactive<FormRules>({
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' },
{ min: 2, max: 50, message: '用户名长度为2-50个字符', trigger: 'blur' }
],
phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ min: 6, max: 20, message: '密码长度为6-20位', trigger: 'blur' }
],
shop_id: [{ required: true, message: '请输入店铺ID', trigger: 'blur' }]
})
const passwordRules = reactive<FormRules>({
password: [
{ required: true, message: '请输入新密码', trigger: 'blur' },
{ min: 6, max: 20, message: '密码长度为6-20位', trigger: 'blur' }
]
})
const dialogType = ref('add')
const form = reactive<any>({
id: 0,
username: '',
phone: '',
password: '',
shop_id: null
})
const passwordForm = reactive({
password: ''
})
const accountList = ref<CustomerAccountItem[]>([])
// 动态列配置
const { columnChecks, columns } = useCheckedColumns(() => [
{
prop: 'id',
label: 'ID',
width: 80
},
{
prop: 'username',
label: '用户名',
minWidth: 120
},
{
prop: 'phone',
label: '手机号',
width: 130
},
{
prop: 'user_type_name',
label: '用户类型',
width: 100,
formatter: (row: CustomerAccountItem) => {
return h(
ElTag,
{ type: row.user_type === 3 ? 'primary' : 'success' },
() => row.user_type_name
)
}
},
{
prop: 'shop_name',
label: '店铺名称',
minWidth: 150,
showOverflowTooltip: true,
formatter: (row: CustomerAccountItem) => row.shop_name || '-'
},
{
prop: 'enterprise_name',
label: '企业名称',
minWidth: 150,
showOverflowTooltip: true,
formatter: (row: CustomerAccountItem) => row.enterprise_name || '-'
},
{
prop: 'status',
label: '状态',
width: 100,
formatter: (row: CustomerAccountItem) => {
return h(ElSwitch, {
modelValue: row.status,
activeValue: 1,
inactiveValue: 0,
activeText: '启用',
inactiveText: '禁用',
inlinePrompt: true,
'onUpdate:modelValue': (val: string | number | boolean) => handleStatusChange(row, val as number)
})
}
},
{
prop: 'created_at',
label: '创建时间',
width: 180,
formatter: (row: CustomerAccountItem) => formatDateTime(row.created_at)
},
{
prop: 'operation',
label: '操作',
width: 160,
fixed: 'right',
formatter: (row: CustomerAccountItem) => {
return h('div', { style: 'display: flex; gap: 8px;' }, [
h(ArtButtonTable, {
icon: '&#xe72b;',
onClick: () => showPasswordDialog(row)
}),
h(ArtButtonTable, {
type: 'edit',
onClick: () => showDialog('edit', row)
})
])
}
}
])
onMounted(() => {
getTableData()
loadShopList()
})
// 加载店铺列表(默认加载20条)
const loadShopList = async (shopName?: string) => {
shopLoading.value = true
try {
const params: any = {
page: 1,
pageSize: 20
}
if (shopName) {
params.shop_name = shopName
}
const res = await ShopService.getShops(params)
if (res.code === 0) {
shopList.value = res.data.items || []
}
} catch (error) {
console.error('获取店铺列表失败:', error)
} finally {
shopLoading.value = false
}
return statusMap[status] || '未知'
}
const getStatusTagType = (status: string) => {
const typeMap: Record<string, string> = {
active: 'success',
disabled: 'info',
locked: 'danger'
// 搜索店铺
const searchShops = (query: string) => {
if (query) {
loadShopList(query)
} else {
loadShopList()
}
return typeMap[status] || 'info'
}
// 获取客户账号列表
const getTableData = async () => {
loading.value = true
try {
const params = {
page: pagination.page,
page_size: pagination.pageSize,
username: searchForm.username || undefined,
phone: searchForm.phone || undefined,
user_type: searchForm.user_type,
status: searchForm.status
}
const res = await CustomerAccountService.getCustomerAccounts(params)
if (res.code === 0) {
accountList.value = res.data.items || []
pagination.total = res.data.total || 0
}
} catch (error) {
console.error(error)
} finally {
loading.value = false
}
}
// 重置搜索
const handleReset = () => {
Object.assign(searchForm, { ...initialSearchState })
pagination.page = 1
getTableData()
}
// 搜索
const handleSearch = () => {
// 搜索逻辑已通过 computed 实现
pagination.page = 1
getTableData()
}
const viewDetail = (row: CustomerAccount) => {
currentAccount.value = row
detailDialogVisible.value = true
// 刷新表格
const handleRefresh = () => {
getTableData()
}
const unbindPhone = (row: CustomerAccount) => {
ElMessageBox.confirm(`确定要解绑账号 ${row.username} 的手机号吗?`, '解绑确认', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
ElMessage.success('手机号解绑成功')
// 处理表格分页变化
const handleSizeChange = (newPageSize: number) => {
pagination.pageSize = newPageSize
getTableData()
}
const handleCurrentChange = (newCurrentPage: number) => {
pagination.page = newCurrentPage
getTableData()
}
// 显示新增/编辑对话框
const showDialog = (type: string, row?: CustomerAccountItem) => {
dialogType.value = type
if (type === 'edit' && row) {
form.id = row.id
form.username = row.username
form.phone = row.phone
form.shop_id = row.shop_id
} else {
form.id = 0
form.username = ''
form.phone = ''
form.password = ''
form.shop_id = null
}
// 重置表单验证状态
nextTick(() => {
formRef.value?.clearValidate()
})
dialogVisible.value = true
}
// 显示修改密码对话框
const showPasswordDialog = (row: CustomerAccountItem) => {
currentAccountId.value = row.id
passwordForm.password = ''
// 重置表单验证状态
nextTick(() => {
passwordFormRef.value?.clearValidate()
})
passwordDialogVisible.value = true
}
// 提交表单
const handleSubmit = async (formEl: FormInstance | undefined) => {
if (!formEl) return
await formEl.validate(async (valid) => {
if (valid) {
submitLoading.value = true
try {
if (dialogType.value === 'add') {
const data = {
username: form.username,
phone: form.phone,
password: form.password,
shop_id: form.shop_id
}
await CustomerAccountService.createCustomerAccount(data)
ElMessage.success('新增成功')
} else {
const data: any = {}
if (form.username) data.username = form.username
if (form.phone) data.phone = form.phone
await CustomerAccountService.updateCustomerAccount(form.id, data)
ElMessage.success('修改成功')
}
dialogVisible.value = false
formEl.resetFields()
getTableData()
} catch (error) {
console.error(error)
} finally {
submitLoading.value = false
}
}
})
}
const resetPassword = (row: CustomerAccount) => {
ElMessageBox.confirm(`确定要重置账号 ${row.username} 的密码吗?新密码将发送至其手机。`, '重置密码', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
ElMessage.success('密码重置成功,新密码已发送至手机')
// 提交修改密码
const handlePasswordSubmit = async (formEl: FormInstance | undefined) => {
if (!formEl) return
await formEl.validate(async (valid) => {
if (valid) {
passwordSubmitLoading.value = true
try {
await CustomerAccountService.updateCustomerAccountPassword(currentAccountId.value, {
password: passwordForm.password
})
ElMessage.success('密码修改成功')
passwordDialogVisible.value = false
formEl.resetFields()
} catch (error) {
console.error(error)
} finally {
passwordSubmitLoading.value = false
}
}
})
}
const toggleStatus = (row: CustomerAccount) => {
const action = row.status === 'active' ? '禁用' : '启用'
ElMessageBox.confirm(`确定要${action}账号 ${row.username} 吗?`, `${action}确认`, {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
row.status = row.status === 'active' ? 'disabled' : 'active'
ElMessage.success(`${action}成功`)
})
// 状态切换
const handleStatusChange = async (row: CustomerAccountItem, newStatus: number) => {
const oldStatus = row.status
// 先更新UI
row.status = newStatus
try {
await CustomerAccountService.updateCustomerAccountStatus(row.id, {
status: newStatus as 0 | 1
})
ElMessage.success('状态切换成功')
} catch (error) {
// 切换失败,恢复原状态
row.status = oldStatus
console.error(error)
}
}
</script>
<style lang="scss" scoped>
.page-content {
:deep(.el-descriptions__label) {
font-weight: 500;
}
.customer-account-page {
// 可以在这里添加客户账号页面特定样式
}
</style>