修改登录,添加超级管理员权限
This commit is contained in:
@ -17,42 +17,26 @@
|
||||
style="width: 100%"
|
||||
>
|
||||
<el-table-column prop="username" label="用户名" width="150" />
|
||||
|
||||
<el-table-column prop="department" label="所属部门" width="150">
|
||||
<template #default="scope">
|
||||
<el-tag>{{ scope.row.department }}</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column prop="role" label="系统角色" width="180">
|
||||
<template #default="scope">
|
||||
{{ formatRole(scope.row.role) }}
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column prop="email" label="邮箱" min-width="200" />
|
||||
|
||||
<el-table-column prop="created_at" label="创建时间" width="180">
|
||||
<template #default="scope">
|
||||
{{ formatDate(scope.row.created_at) }}
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="操作" width="180" fixed="right">
|
||||
<template #default="scope">
|
||||
<el-button
|
||||
link
|
||||
type="primary"
|
||||
size="small"
|
||||
@click="handleEdit(scope.row)"
|
||||
>
|
||||
编辑
|
||||
</el-button>
|
||||
|
||||
<el-popconfirm
|
||||
title="确定要删除该用户吗?此操作无法撤销。"
|
||||
@confirm="handleDelete(scope.row)"
|
||||
>
|
||||
<el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
|
||||
<el-popconfirm title="确定要删除该用户吗?" @confirm="handleDelete(scope.row)">
|
||||
<template #reference>
|
||||
<el-button link type="danger" size="small">删除</el-button>
|
||||
</template>
|
||||
@ -82,10 +66,7 @@
|
||||
/>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item
|
||||
label="密码"
|
||||
prop="password"
|
||||
>
|
||||
<el-form-item label="密码" prop="password">
|
||||
<el-input
|
||||
v-model="form.password"
|
||||
type="password"
|
||||
@ -103,24 +84,18 @@
|
||||
allow-create
|
||||
default-first-option
|
||||
>
|
||||
<el-option
|
||||
v-for="item in departmentOptions"
|
||||
:key="item"
|
||||
:label="item"
|
||||
:value="item"
|
||||
/>
|
||||
<el-option v-for="item in departmentOptions" :key="item" :label="item" :value="item" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="系统角色" prop="role">
|
||||
<el-select v-model="form.role" placeholder="授予权限" style="width: 100%">
|
||||
<el-option label="主管" value="supervisor" />
|
||||
<el-option label="财务" value="finance" />
|
||||
<el-option label="库管" value="warehouse_manager" />
|
||||
<el-option label="入库员" value="inbound" />
|
||||
<el-option label="出库员" value="outbound" />
|
||||
<el-option label="采购员" value="purchaser" />
|
||||
<el-option label="销售" value="sales" />
|
||||
<el-option
|
||||
v-for="option in roleOptions"
|
||||
:key="option.value"
|
||||
:label="option.label"
|
||||
:value="option.value"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
|
||||
@ -144,9 +119,11 @@
|
||||
<script setup lang="ts">
|
||||
import { reactive, ref, onMounted, computed } from 'vue'
|
||||
import { createUser, updateUser, getUserList, deleteUser } from '@/api/auth'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
// --- 状态定义 ---
|
||||
const userStore = useUserStore()
|
||||
const tableLoading = ref(false)
|
||||
const submitLoading = ref(false)
|
||||
const dialogVisible = ref(false)
|
||||
@ -154,7 +131,6 @@ const tableData = ref<any[]>([])
|
||||
const departmentOptions = ref<string[]>([])
|
||||
const formRef = ref()
|
||||
|
||||
// [新增] 区分编辑/创建模式
|
||||
const isEdit = ref(false)
|
||||
const currentId = ref<number | null>(null)
|
||||
|
||||
@ -166,20 +142,52 @@ const form = reactive({
|
||||
email: ''
|
||||
})
|
||||
|
||||
// [关键] 动态规则:编辑模式下密码不是必填项
|
||||
// [核心逻辑] 角色选项列表 (保留了之前的强力判断逻辑,但去除了 debug 变量)
|
||||
const roleOptions = computed(() => {
|
||||
const options = [
|
||||
{ label: '主管', value: 'SUPERVISOR' },
|
||||
{ label: '财务', value: 'FINANCE' },
|
||||
{ label: '库管', value: 'WAREHOUSE_MGR' },
|
||||
{ label: '入库员', value: 'INBOUND' },
|
||||
{ label: '出库员', value: 'OUTBOUND' },
|
||||
{ label: '采购员', value: 'PURCHASER' },
|
||||
{ label: '销售', value: 'SALES' }
|
||||
]
|
||||
|
||||
// 1. 尝试从 Pinia Store 获取信息
|
||||
let role = userStore.user?.role || userStore.role
|
||||
let username = userStore.user?.username || userStore.name
|
||||
|
||||
// 2. 如果 Store 没数据,尝试从 LocalStorage 补救
|
||||
if (!role) role = localStorage.getItem('role')
|
||||
if (!username) username = localStorage.getItem('username')
|
||||
|
||||
// 3. 统一转大写
|
||||
const safeRole = role ? String(role).toUpperCase() : ''
|
||||
const safeUsername = username ? String(username).toUpperCase() : ''
|
||||
|
||||
// 4. [关键] 只要是 IRIS 或者 角色包含 ADMIN,就开启上帝模式
|
||||
const isSuperAdmin = (safeRole === 'SUPER_ADMIN') || (safeUsername === 'IRIS')
|
||||
|
||||
if (isSuperAdmin) {
|
||||
options.unshift({ label: '超级管理员', value: 'SUPER_ADMIN' })
|
||||
}
|
||||
|
||||
return options
|
||||
})
|
||||
|
||||
// 验证规则
|
||||
const rules = computed(() => {
|
||||
const commonRules = {
|
||||
const commonRules: any = {
|
||||
username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
|
||||
role: [{ required: true, message: '请选择角色', trigger: 'change' }],
|
||||
department: [{ required: true, message: '请输入或选择部门', trigger: ['blur', 'change'] }],
|
||||
// [新增] 邮箱必填校验规则
|
||||
email: [
|
||||
{ required: true, message: '请输入邮箱', trigger: 'blur' },
|
||||
{ type: 'email', message: '请输入正确的邮箱格式', trigger: ['blur', 'change'] }
|
||||
]
|
||||
}
|
||||
|
||||
// 如果是创建模式,密码必填
|
||||
if (!isEdit.value) {
|
||||
return {
|
||||
...commonRules,
|
||||
@ -189,7 +197,6 @@ const rules = computed(() => {
|
||||
]
|
||||
}
|
||||
} else {
|
||||
// 如果是编辑模式,密码选填(只有输入了才校验长度)
|
||||
return {
|
||||
...commonRules,
|
||||
password: [
|
||||
@ -226,51 +233,47 @@ const extractDepartments = (data: any[]) => {
|
||||
departmentOptions.value = Array.from(deptSet)
|
||||
}
|
||||
|
||||
// 2. 打开新增弹窗
|
||||
const handleCreate = () => {
|
||||
isEdit.value = false
|
||||
currentId.value = null
|
||||
resetFormState() // 清空数据
|
||||
form.username = ''
|
||||
form.password = ''
|
||||
form.department = ''
|
||||
form.role = ''
|
||||
form.email = ''
|
||||
if (formRef.value) formRef.value.clearValidate()
|
||||
dialogVisible.value = true
|
||||
}
|
||||
|
||||
// [新增] 打开编辑弹窗
|
||||
const handleEdit = (row: any) => {
|
||||
isEdit.value = true
|
||||
currentId.value = row.id
|
||||
|
||||
// 回填数据
|
||||
form.username = row.username
|
||||
form.department = row.department
|
||||
form.role = row.role
|
||||
form.email = row.email || ''
|
||||
form.password = '' // 编辑时不回显密码,留空表示不修改
|
||||
|
||||
form.password = ''
|
||||
if (formRef.value) formRef.value.clearValidate()
|
||||
dialogVisible.value = true
|
||||
}
|
||||
|
||||
// 3. 提交 (兼容创建和修改)
|
||||
const onSubmit = async () => {
|
||||
if (!formRef.value) return
|
||||
|
||||
await formRef.value.validate(async (valid: boolean) => {
|
||||
if (valid) {
|
||||
submitLoading.value = true
|
||||
try {
|
||||
if (isEdit.value && currentId.value) {
|
||||
// 编辑逻辑
|
||||
await updateUser(currentId.value, form)
|
||||
ElMessage.success(`用户 ${form.username} 修改成功!`)
|
||||
} else {
|
||||
// 创建逻辑
|
||||
await createUser(form)
|
||||
ElMessage.success(`用户 ${form.username} 创建成功!`)
|
||||
}
|
||||
|
||||
dialogVisible.value = false
|
||||
getList()
|
||||
} catch (error) {
|
||||
// 错误已处理
|
||||
// request 拦截器会处理错误
|
||||
} finally {
|
||||
submitLoading.value = false
|
||||
}
|
||||
@ -278,14 +281,9 @@ const onSubmit = async () => {
|
||||
})
|
||||
}
|
||||
|
||||
// 4. 重置表单
|
||||
const resetForm = () => {
|
||||
if (!formRef.value) return
|
||||
formRef.value.resetFields()
|
||||
resetFormState()
|
||||
}
|
||||
|
||||
const resetFormState = () => {
|
||||
form.username = ''
|
||||
form.password = ''
|
||||
form.department = ''
|
||||
@ -293,19 +291,15 @@ const resetFormState = () => {
|
||||
form.email = ''
|
||||
}
|
||||
|
||||
// 5. 删除用户
|
||||
const handleDelete = async (row: any) => {
|
||||
try {
|
||||
await deleteUser(row.id)
|
||||
ElMessage.success('删除成功')
|
||||
getList()
|
||||
} catch (error) {
|
||||
// 错误处理
|
||||
}
|
||||
}
|
||||
|
||||
// --- 辅助显示方法 ---
|
||||
|
||||
const formatDate = (dateStr: string) => {
|
||||
if (!dateStr) return '-'
|
||||
return dateStr.replace('T', ' ').substring(0, 19)
|
||||
@ -313,19 +307,19 @@ const formatDate = (dateStr: string) => {
|
||||
|
||||
const formatRole = (val: string) => {
|
||||
const map: Record<string, string> = {
|
||||
'supervisor': '主管',
|
||||
'finance': '财务',
|
||||
'warehouse_manager': '库管',
|
||||
'inbound': '入库员',
|
||||
'outbound': '出库员',
|
||||
'purchaser': '采购员',
|
||||
'sales': '销售',
|
||||
'super_admin': '超级管理员'
|
||||
'SUPER_ADMIN': '超级管理员',
|
||||
'SUPERVISOR': '主管',
|
||||
'FINANCE': '财务',
|
||||
'WAREHOUSE_MGR': '库管',
|
||||
'INBOUND': '入库员',
|
||||
'OUTBOUND': '出库员',
|
||||
'PURCHASER': '采购员',
|
||||
'SALES': '销售'
|
||||
}
|
||||
return map[val] || val
|
||||
const key = val ? val.toUpperCase() : ''
|
||||
return map[key] || val
|
||||
}
|
||||
|
||||
// --- 初始化 ---
|
||||
onMounted(() => {
|
||||
getList()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user