Files
art-design/src/views/admin/user/index.vue
T

842 lines
25 KiB
Vue
Raw Normal View History

2025-08-05 18:25:32 +08:00
<template>
<div class="customer-page">
<!-- 顶部操作栏 -->
<div class="customer-toolbar boxshadow">
<div class="customer-search-group">
2025-08-13 19:27:49 +08:00
<el-form-item label="关键字:" label-width="70px">
<el-input style="min-width: 120px" v-model="keyword" placeholder="请输入关键字" />
</el-form-item>
<el-form-item label="状态:" label-width="50px">
<el-select v-model="selectValue" style="min-width: 120px">
<el-option label="全部" value="" />
<el-option label="正常" value="1" />
<el-option label="离职" value="2" />
</el-select>
</el-form-item>
2025-08-05 18:25:32 +08:00
</div>
</div>
<div class="boxshadow">
<!-- 表格 -->
<ArtTable
ref="tableRef"
row-key="id"
:loading="loading"
:data="tableData"
:currentPage="pagination.currentPage"
:pageSize="pagination.pageSize"
:total="pagination.total"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
lass="customer-table"
>
<template #default>
2025-08-11 19:24:24 +08:00
<!-- <el-table-column prop="id" label="用户ID" align="center" /> -->
2025-08-06 18:36:42 +08:00
<el-table-column prop="sn" label="SN" align="center" />
2025-08-05 18:25:32 +08:00
<el-table-column prop="follow" label="头像" align="center">
<template #default="scope">
2025-08-11 19:24:24 +08:00
<img
:src="scope.row.avatar"
alt="头像"
class="avatar"
2025-08-07 10:10:45 +08:00
@click="handleAvatarClick(scope.row.avatar)"
2025-08-11 19:24:24 +08:00
style="cursor: pointer"
2025-08-07 10:10:45 +08:00
/>
2025-08-05 18:25:32 +08:00
</template>
</el-table-column>
2025-08-11 19:24:24 +08:00
<el-table-column prop="name" label="名字" align="center" />
2025-08-05 18:25:32 +08:00
<el-table-column prop="phone" label="电话" align="center" />
<el-table-column prop="email" label="邮箱" align="center" />
2025-08-11 19:24:24 +08:00
<el-table-column prop="corp_name" label="所属公司" align="center" />
2025-08-05 18:25:32 +08:00
<el-table-column prop="follow" label="状态" align="center">
<template #default="scope">
<span :class="statusClass(scope.row.status)">
{{ scope.row.status_txt }}
</span>
</template>
</el-table-column>
2025-08-06 18:36:42 +08:00
<el-table-column prop="customer_count" label="客户数量" align="center" />
2025-08-05 18:25:32 +08:00
<el-table-column fixed="right" label="操作" width="200px" align="center">
<template #default="scope">
2025-08-13 19:27:49 +08:00
<el-link
type="success"
underline="never"
style="margin-right: 8px"
@click="showPermissionDialog(scope.row)"
v-if="hasAuth('user@direct_perm')"
>
<i class="iconfont-sys">&#xe723;</i>直接授权
</el-link>
<el-link
type="warning"
underline="never"
style="margin-right: 8px"
@click="showroleDialog(scope.row)"
v-if="hasAuth('user@role_system')"
>
<i class="iconfont-sys">&#xe830;</i>角色&系统
</el-link>
2025-08-05 18:25:32 +08:00
<el-link
type="primary"
underline="never"
style="margin-right: 8px"
@click="gotransfer(scope.row)"
v-if="hasAuth('user@customer_transfer')"
>
<i class="iconfont-sys"> &#xe794;</i>客户转移
</el-link>
2025-08-06 18:36:42 +08:00
<el-link
type="success"
underline="never"
style="margin-right: 8px"
@click="onEdit(scope.row)"
2025-08-07 10:51:17 +08:00
v-if="hasAuth('user@edit')"
2025-08-06 18:36:42 +08:00
>
<i class="iconfont-sys"> &#xe6bf;</i>编辑
</el-link>
2025-08-05 18:25:32 +08:00
</template>
</el-table-column>
</template>
</ArtTable>
</div>
</div>
<!-- 详情对话框 -->
<el-dialog v-model="detailDialogVisible" title="详情" width="75%">
<div>
<el-button type="primary" @click="plzyDialogVisiblefn">
<i class="iconfont-sys">&#xe794;</i> 批量转移
</el-button>
<!-- 表格 -->
<ArtTable
ref="tableRef"
row-key="id"
:loading="loadingkh"
:data="tableDatakh"
:currentPage="paginationkh.currentPage"
:pageSize="paginationkh.pageSize"
:total="paginationkh.total"
@selection-change="handleSelectionChange"
@size-change="handleSizeChangekh"
@current-change="handleCurrentChangekh"
lass="customer-table"
>
<template #default>
<el-table-column type="selection" width="55px" align="center"></el-table-column>
<el-table-column prop="contacts" label="客户名称" align="center" />
<el-table-column prop="magnitude_txt" label="客户商机" align="center" />
<el-table-column prop="channel_name" label="渠道" align="center" />
<el-table-column prop="country_name" label="国家" align="center" />
<el-table-column prop="phone" label="电话" align="center" />
<el-table-column prop="WhatsApp" label="WhatsApp" align="center" />
<el-table-column prop="email" label="邮箱" align="center" />
<el-table-column prop="grade_txt" label="等级" align="center" />
<el-table-column prop="follow" label="状态" align="center">
<template #default="scope">
<span :class="statusClass(scope.row.follow)">
{{ scope.row.follow_txt }}
</span>
</template>
</el-table-column>
</template>
</ArtTable>
</div>
</el-dialog>
<!-- 批量转移对话框 -->
<el-dialog v-model="plzyDialogVisible" title="批量转移" width="360px">
<el-form :model="plzyForm" label-width="0">
<el-form-item>
<el-select
v-model="plzyForm.user_id"
filterable
placeholder="请选择转移人"
style="width: 100%"
>
<el-option
v-for="item in userOptions"
:key="item.id"
:label="item.name"
:value="item.id"
/>
</el-select>
</el-form-item>
<div style="display: flex; justify-content: space-around; margin-top: 24px">
<el-button @click="plzyDialogVisible = false">取消</el-button>
<el-button type="primary" @click="handleplzySubmit">确定</el-button>
</div>
</el-form>
</el-dialog>
2025-08-06 18:36:42 +08:00
<el-dialog
title="编辑用户"
v-model="AddDialogVisible"
width="800px"
:close-on-click-modal="false"
>
<el-form :model="addForm" ref="addFormRef" label-width="80px" class="add-form">
<el-row :gutter="16">
<el-col :span="24">
<el-form-item label="用户名" prop="country">
<el-input v-model="addForm.name" placeholder="请输入用户名" />
</el-form-item>
</el-col>
<el-col :span="24">
<el-form-item label="Email" prop="email">
<el-input v-model="addForm.email" placeholder="请输入Email" />
</el-form-item>
</el-col>
<el-col :span="24">
<el-form-item label="电话" prop="tel">
<el-input v-model="addForm.phone" placeholder="请输入电话" />
</el-form-item>
</el-col>
<el-col :span="24">
<el-form-item label="状态" prop="status">
<el-select v-model="addForm.status">
<el-option label="正常" :value="1" />
<el-option label="离职" :value="2" />
</el-select>
</el-form-item>
</el-col>
</el-row>
<div class="form-submit-row">
<el-button type="primary" size="large" style="width: 50%" @click="handleAddSubmit"
>提交</el-button
>
</div>
</el-form>
</el-dialog>
2025-08-07 10:10:45 +08:00
<!-- 图片预览组件 -->
<ElImageViewer
v-if="showImageViewer"
:url-list="[currentImageUrl]"
:hide-on-click-modal="true"
@close="showImageViewer = false"
/>
2025-08-13 19:27:49 +08:00
<ElDialog
v-model="permissionDialog"
title="菜单权限"
width="520px"
align-center
class="el-dialog-border"
>
<ElScrollbar height="70vh">
<ElTree
ref="treeRef"
:data="processedMenuList"
show-checkbox
node-key="id"
:default-expand-all="isExpandAll"
:props="defaultProps"
@check="handleTreeCheck"
>
<template #default="{ data }">
<div style="display: flex; align-items: center">
<span v-if="data.isAuth">
{{ data.label }}
</span>
<span v-else>{{ defaultProps.label(data) }}</span>
</div>
</template>
</ElTree>
</ElScrollbar>
<template #footer>
<div class="dialog-footer">
<ElButton @click="toggleExpandAll">{{ isExpandAll ? '全部收起' : '全部展开' }}</ElButton>
<ElButton @click="toggleSelectAll" style="margin-left: 8px">{{
isSelectAll ? '取消全选' : '全部选择'
}}</ElButton>
<ElButton type="primary" @click="savePermission">保存</ElButton>
</div>
</template>
</ElDialog>
<el-dialog
title="角色&系统设置"
v-model="roleDialogVisible"
width="700px"
:close-on-click-modal="false"
>
<el-form :model="roleForm" ref="addFormRef" label-width="80px" class="add-form">
<el-row :gutter="16">
<el-col :span="24">
<el-form-item label="角色" prop="status">
<ElSelect
v-model="roleForm.role_ids"
multiple
class="account-select"
placeholder="请选择角色"
>
<ElOption
v-for="account in roleOptions"
:key="account.id"
:label="account.name"
:value="account.id"
>
<span>{{ account.name }}</span>
</ElOption>
</ElSelect>
</el-form-item>
</el-col>
<el-col :span="24">
<el-form-item label="系统" prop="status">
<ElSelect
v-model="roleForm.system_ids"
multiple
class="account-select"
placeholder="请选择系统"
>
<ElOption
v-for="account in systemsList"
:key="account.id"
:label="account.name"
:value="account.id"
>
<span>{{ account.name }}</span>
</ElOption>
</ElSelect>
</el-form-item>
</el-col>
</el-row>
<div class="form-submit-row">
<el-button type="primary" size="large" style="width: 70%" @click="handleRoleSubmit"
>提交</el-button
>
</div>
</el-form>
</el-dialog>
2025-08-05 18:25:32 +08:00
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, watch } from 'vue'
2025-08-07 10:10:45 +08:00
import { ElImageViewer } from 'element-plus'
2025-08-05 18:25:32 +08:00
import { manageService } from '@/api/adminApi'
import { useAuth } from '@/composables/useAuth'
2025-08-13 19:27:49 +08:00
import { systemService } from '@/api/systemApi'
import { formatMenuTitle } from '@/router/utils/utils'
import { UserService } from '@/api/usersApi'
2025-08-05 18:25:32 +08:00
const { hasAuth } = useAuth()
const selectValue = ref<string[]>([])
2025-08-06 18:36:42 +08:00
const keyword = ref<string>('')
2025-08-05 18:25:32 +08:00
const loading = ref(false)
const loadingkh = ref(false)
const detailDialogVisible = ref(false)
const plzyDialogVisible = ref(false)
2025-08-06 18:36:42 +08:00
const AddDialogVisible = ref(false)
2025-08-13 19:27:49 +08:00
const permissionDialog = ref(false)
const roleDialogVisible = ref(false)
// 权限树数据(来自接口 getPermissionMenuList)
const permissionMenuList = ref<any[]>([])
const treeRef = ref()
const isExpandAll = ref(true)
const isSelectAll = ref(false)
2025-08-05 18:25:32 +08:00
const pagination = reactive({
currentPage: 1,
pageSize: 10,
total: 0
})
const paginationkh = reactive({
currentPage: 1,
pageSize: 10,
total: 0
})
const plzyForm = reactive({
customer_id: '',
user_id: ''
})
const zyForm = reactive({
customer_id: ''
})
2025-08-06 18:36:42 +08:00
const addForm = reactive({
id: '',
name: '',
email: '',
phone: '',
status: ''
})
2025-08-13 19:27:49 +08:00
const roleForm = reactive({
user_id: '',
role_ids: '',
system_ids: ''
})
2025-08-05 18:25:32 +08:00
const userOptions = ref<any>()
2025-08-13 19:27:49 +08:00
//系统
const systemsList = ref<any>()
const getsystemsList = async () => {
const response = await UserService.getsystemsList()
systemsList.value = response || []
}
//角色下拉框
const roleOptions = ref<any>()
const getroleOptions = async () => {
const response = await systemService.getRoleSelectList()
roleOptions.value = response || []
}
2025-08-05 18:25:32 +08:00
const getuserList = async () => {
// 这里调用获取用户列表的接口
const res = await manageService.presetusers()
userOptions.value = res || []
// return res || []
}
// 表格数据
const tableData = ref<any[]>([])
const tableDatakh = ref<any[]>([])
onMounted(() => {
getuserList()
gettableList()
})
// tabs 切换联动
2025-08-06 18:36:42 +08:00
watch([selectValue, keyword], () => {
2025-08-05 18:25:32 +08:00
pagination.currentPage = 1
gettableList()
})
const gettableList = async () => {
loading.value = true
try {
const { currentPage, pageSize } = pagination
const params: any = {
current: currentPage,
size: pageSize
}
if (selectValue.value) {
2025-08-13 19:27:49 +08:00
params.status = selectValue.value
2025-08-06 18:36:42 +08:00
}
if (keyword.value) {
params.keyword = keyword.value
2025-08-05 18:25:32 +08:00
}
const { records, current, size, total } = await manageService.getUserList(params)
tableData.value = records.map((item: any) => ({
...item
}))
Object.assign(pagination, { currentPage: current, pageSize: size, total })
} catch (error) {
console.error('获取用户列表失败:', error)
} finally {
loading.value = false
}
}
const gettableListkh = async (user_id: any) => {
loadingkh.value = true
try {
const { currentPage, pageSize } = paginationkh
const params: any = {
current: currentPage,
size: pageSize,
user_id: user_id
}
const { data, current, size, total } = await manageService.getcrmCustomerList(params)
tableDatakh.value = data.map((item: any) => ({
...item
}))
Object.assign(paginationkh, { currentPage: current, pageSize: size, total })
} catch (error) {
console.error('获取用户列表失败:', error)
} finally {
loadingkh.value = false
}
}
// 处理表格分页变化
const handleSizeChange = (newPageSize: number) => {
pagination.pageSize = newPageSize
gettableList()
}
const handleCurrentChange = (newCurrentPage: number) => {
pagination.currentPage = newCurrentPage
gettableList()
}
// 处理表格分页变化
const handleSizeChangekh = (newPageSize: number) => {
paginationkh.pageSize = newPageSize
gettableListkh(zyForm.customer_id)
}
const handleCurrentChangekh = (newCurrentPage: number) => {
paginationkh.currentPage = newCurrentPage
gettableListkh(zyForm.customer_id)
}
function statusClass(status: any) {
if (status === 1) return 'status-following'
if (status === 0) return 'status-lost'
return ''
}
const gotransfer = (row: any) => {
paginationkh.currentPage = 1
paginationkh.pageSize = 10
paginationkh.total = 0
gettableListkh(row.id)
zyForm.customer_id = row.id
detailDialogVisible.value = true
}
2025-08-13 19:27:49 +08:00
// 显示角色&系统对话框
const showroleDialog = (row: any) => {
getsystemsList()
getroleOptions()
roleDialogVisible.value = true
roleForm.user_id = row.id
roleForm.role_ids = row.role_ids || []
roleForm.system_ids = row.system_ids || []
}
// 提交角色&系统
const handleRoleSubmit = async () => {
const res = await manageService.userSetRole(roleForm)
ElMessage.success(res?.message || '角色&系统设置成功!')
roleDialogVisible.value = false
gettableList()
}
// 显示权限对话框
const userId = ref('')
const showPermissionDialog = async (row: any) => {
permissionDialog.value = true
userId.value = row.id
try {
const res = await systemService.getPermissionMenuList({ user_id: row.id })
// 兼容不同返回结构(data/records/直接数组)
const list = (res as any)?.data ?? (res as any)?.records ?? (res as any)
permissionMenuList.value = Array.isArray(list) ? list : []
// 根据接口返回的 selected 字段设置默认选中
nextTick(() => {
const keys = buildDefaultCheckedKeys(permissionMenuList.value)
treeRef.value?.setCheckedKeys(keys)
handleTreeCheck()
})
} catch (e) {
permissionMenuList.value = []
}
}
// 构建默认选中的 key 列表(与 node-key="id" 一致)
const buildDefaultCheckedKeys = (source: any[]): Array<string | number> => {
const keys: Array<string | number> = []
const walk = (node: any) => {
if (node?.meta?.selected) {
keys.push(node.id)
}
if (Array.isArray(node?.meta?.authList)) {
node.meta.authList.forEach((auth: any) => {
if (auth?.selected) {
keys.push(`${node.id}__auth__${auth.authMark}`)
}
})
}
if (Array.isArray(node?.children)) node.children.forEach(walk)
}
source.forEach(walk)
return keys
}
// 处理菜单数据,将 authList 转换为子节点
const processedMenuList = computed(() => {
const processNode = (node: any) => {
// 规范化 id,确保树节点唯一键与 node-key 对应
const processed: any = { ...node, id: node.id }
// 如果有 authList,将其转换为子节点
if (node.meta && node.meta.authList && node.meta.authList.length) {
const authNodes = node.meta.authList.map((auth: any) => ({
// 为权限子节点提供稳定唯一 id(数字+字符串),与 node-key="id" 匹配
id: `${node.id}__auth__${auth.authMark}`,
name: `${node.name}_${auth.authMark}`,
label: auth.title,
auth_mark: auth.authMark,
auth_id: auth.id,
isAuth: true,
checked: auth.selected || false
}))
processed.children = processed.children ? [...processed.children, ...authNodes] : authNodes
}
// 递归处理子节点
if (processed.children) {
processed.children = processed.children.map(processNode)
}
return processed
}
const source = permissionMenuList.value || []
return source.map(processNode)
})
const defaultProps = {
children: 'children',
label: (data: any) => formatMenuTitle(data.meta?.title) || ''
}
const toggleExpandAll = () => {
const tree = treeRef.value
if (!tree) return
// 使用store.nodesMap直接控制所有节点的展开状态
const nodes = tree.store.nodesMap
for (const node in nodes) {
nodes[node].expanded = !isExpandAll.value
}
isExpandAll.value = !isExpandAll.value
}
const toggleSelectAll = () => {
const tree = treeRef.value
if (!tree) return
if (!isSelectAll.value) {
// 全选:获取所有节点的key并设置为选中
const allKeys = getAllNodeKeys(processedMenuList.value)
tree.setCheckedKeys(allKeys)
} else {
// 取消全选:清空所有选中
tree.setCheckedKeys([])
}
isSelectAll.value = !isSelectAll.value
}
const getAllNodeKeys = (nodes: any[]): Array<string | number> => {
const keys: Array<string | number> = []
const traverse = (nodeList: any[]) => {
nodeList.forEach((node) => {
if (node.id !== undefined && node.id !== null) {
keys.push(node.id)
}
if (node.children && node.children.length > 0) {
traverse(node.children)
}
})
}
traverse(nodes)
return keys
}
const handleTreeCheck = () => {
const tree = treeRef.value
if (!tree) return
// 使用树组件的getCheckedKeys方法获取选中的节点
const checkedKeys = tree.getCheckedKeys()
const allKeys = getAllNodeKeys(processedMenuList.value)
// 判断是否全选:选中的节点数量等于总节点数量
isSelectAll.value = checkedKeys.length === allKeys.length && allKeys.length > 0
}
const getSelectedIds = () => {
const tree = treeRef.value
if (!tree) return { menuIds: [], authIds: [] }
const checkedNodes = tree.getCheckedNodes?.(false) || []
const halfCheckedNodes = tree.getHalfCheckedNodes?.() || []
const allNodes = [...checkedNodes, ...halfCheckedNodes]
const menuIds = Array.from(
new Set(
allNodes
.filter((n: any) => !n.isAuth && n.id !== undefined && n.id !== null)
.map((n: any) => n.id)
)
)
const authIds = Array.from(
new Set(
checkedNodes.filter((n: any) => n.isAuth && n.auth_id != null).map((n: any) => n.auth_id)
)
)
const ids = Array.from(new Set([...(menuIds as any[]), ...(authIds as any[])]))
return { menuIds, authIds, ids }
}
const savePermission = async () => {
const { ids } = getSelectedIds()
const res = await manageService.userMenuAuth({
user_id: userId.value,
resources: ids
})
ElMessage.success(res?.message || '权限保存成功')
permissionDialog.value = false
}
2025-08-05 18:25:32 +08:00
// 选中的行数据
const selectedRows = ref<any[]>([])
// 处理表格行选择变化
const handleSelectionChange = (selection: any[]) => {
selectedRows.value = selection
}
const plzyDialogVisiblefn = () => {
if (selectedRows.value.length === 0) {
ElMessage.warning('请至少选择一条记录')
return
}
plzyDialogVisible.value = true
}
// 批量转移
const handleplzySubmit = async () => {
if (selectedRows.value.length === 0) {
ElMessage.warning('请至少选择一条记录')
return
}
plzyForm.customer_id = selectedRows.value.map((item) => item.id).join(',')
const res = await manageService.crmCustomerTransfer(plzyForm)
ElMessage.success(res?.message || '批量转移成功!')
plzyDialogVisible.value = false
detailDialogVisible.value = false
gettableList()
}
2025-08-06 18:36:42 +08:00
// 编辑用户
const onEdit = (row: any) => {
AddDialogVisible.value = true
addForm.id = row.id
addForm.name = row.name
addForm.email = row.email
addForm.phone = row.phone
addForm.status = row.status
}
const handleAddSubmit = async () => {
const res = await manageService.userEdit(addForm)
ElMessage.success(res?.message || '编辑成功!')
AddDialogVisible.value = false
gettableList()
}
2025-08-07 10:10:45 +08:00
// 图片预览相关
const showImageViewer = ref(false)
const currentImageUrl = ref('')
const handleAvatarClick = (avatarUrl: string) => {
if (avatarUrl) {
currentImageUrl.value = avatarUrl
showImageViewer.value = true
}
}
2025-08-05 18:25:32 +08:00
</script>
<style scoped lang="scss">
:deep(.el-badge) {
--el-badge-size: 16px;
}
.avatar {
width: 40px;
height: 40px;
2025-08-07 10:10:45 +08:00
border-radius: 50%;
transition: opacity 0.2s;
&:hover {
opacity: 0.8;
}
2025-08-05 18:25:32 +08:00
}
.itemtag {
margin-top: -16px;
margin-left: -13px;
}
:deep(.el-table__header th) {
background: #f7f9fa !important;
color: #333;
font-weight: bold;
}
:deep(.el-table__empty-block) {
height: 450px !important;
}
:deep(.el-table__body tr.custom-table-row td) {
border-bottom: 1px solid #f7f9fa !important;
border-right: 1px solid #f7f9fa !important;
}
:deep(.el-table__body tr td:last-child) {
border-right: none !important;
}
:deep(.el-table__body tr:last-child td) {
border-bottom: none !important;
}
:deep(.el-table__header-wrapper th) {
border-bottom: 1px solid #f7f9fa !important;
border-right: 1px solid #f7f9fa !important;
}
:deep(.el-table__header-wrapper th:last-child) {
border-right: none !important;
}
:deep(.el-table) {
border: 1px solid #f7f9fa !important;
border-radius: 6px;
}
.customer-page {
background: var(--art-main-bg-color);
min-height: 100vh;
}
.customer-toolbar {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
padding: 20px 20px;
background: var(--art-main-bg-color);
}
.customer-search-group {
display: flex;
gap: 12px;
}
.customer-tags {
background: var(--art-main-bg-color);
border-radius: 8px;
padding: 16px 20px;
margin-bottom: 16px;
}
.filter-row {
display: flex;
align-items: center;
margin-bottom: 14px;
}
.filter-label {
width: 120px;
color: #666;
margin-right: 8px;
flex-shrink: 0;
font-size: 14px;
}
.filter-tags {
flex: 1;
color: #333;
font-weight: 500;
font-size: 14px;
}
.customer-tabs {
margin-bottom: 8px;
}
.status-following {
color: #00a43f;
font-weight: bold;
}
.status-dealed {
color: #3471ff;
font-weight: bold;
}
.status-lost {
color: #ff0000;
font-weight: bold;
}
.customer-table {
background: var(--art-main-bg-color);
border-radius: 8px;
margin-top: 0;
}
.filter-more-btn {
text-align: center;
margin-top: 8px;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.2s;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
2025-08-06 18:36:42 +08:00
.form-submit-row {
text-align: center;
2025-08-05 18:25:32 +08:00
}
</style>