添加新的需求

This commit is contained in:
xuqiuyun
2025-10-20 17:32:09 +08:00
parent 9979e00b47
commit 361d5ab1ae
247 changed files with 34249 additions and 1 deletions

View File

@@ -0,0 +1,165 @@
import { getMenuList, assignRoleMenus, getUserList } from '@/api/permission.js';
import { ElMessage } from 'element-plus';
/**
* 为超级管理员账户分配所有菜单权限
* @param {string} mobile 手机号,默认为 '15900000000'
* @returns {Promise<boolean>} 是否成功
*/
export async function assignAllPermissionsToSuperAdmin(mobile = '15900000000') {
try {
console.log(`=== 开始为超级管理员 ${mobile} 分配所有菜单权限 ===`);
// 1. 获取用户列表,找到目标用户
const userListRes = await getUserList();
if (userListRes.code !== 200) {
throw new Error('获取用户列表失败');
}
const targetUser = userListRes.data.find(user => user.mobile === mobile);
if (!targetUser) {
throw new Error(`未找到手机号为 ${mobile} 的用户`);
}
console.log('=== 找到目标用户 ===', targetUser);
// 2. 获取所有菜单列表
const menuListRes = await getMenuList();
if (menuListRes.code !== 200) {
throw new Error('获取菜单列表失败');
}
const allMenus = menuListRes.data || [];
const allMenuIds = allMenus.map(menu => menu.id);
console.log('=== 获取到所有菜单 ===', {
totalMenus: allMenus.length,
menuIds: allMenuIds
});
// 3. 为超级管理员角色分配所有菜单权限
const assignRes = await assignRoleMenus({
roleId: targetUser.roleId,
menuIds: allMenuIds
});
if (assignRes.code === 200) {
console.log('=== 权限分配成功 ===');
ElMessage.success(`成功为超级管理员 ${mobile} 分配了 ${allMenuIds.length} 个菜单权限`);
return true;
} else {
throw new Error(assignRes.msg || '权限分配失败');
}
} catch (error) {
console.error('=== 权限分配失败 ===', error);
ElMessage.error(`权限分配失败: ${error.message}`);
return false;
}
}
/**
* 检查超级管理员权限状态
* @param {string} mobile 手机号,默认为 '15900000000'
* @returns {Promise<Object>} 权限状态信息
*/
export async function checkSuperAdminPermissions(mobile = '15900000000') {
try {
console.log(`=== 检查超级管理员 ${mobile} 的权限状态 ===`);
// 1. 获取用户列表,找到目标用户
const userListRes = await getUserList();
if (userListRes.code !== 200) {
throw new Error('获取用户列表失败');
}
const targetUser = userListRes.data.find(user => user.mobile === mobile);
if (!targetUser) {
throw new Error(`未找到手机号为 ${mobile} 的用户`);
}
// 2. 获取该用户当前已分配的菜单权限
const { getRoleMenuIds } = await import('@/api/permission.js');
const roleMenuRes = await getRoleMenuIds(targetUser.roleId);
if (roleMenuRes.code !== 200) {
throw new Error('获取角色菜单权限失败');
}
const assignedMenuIds = roleMenuRes.data || [];
// 3. 获取所有菜单数量
const menuListRes = await getMenuList();
if (menuListRes.code !== 200) {
throw new Error('获取菜单列表失败');
}
const totalMenus = menuListRes.data.length;
const status = {
user: targetUser,
totalMenus: totalMenus,
assignedMenus: assignedMenuIds.length,
isComplete: assignedMenuIds.length === totalMenus,
missingMenus: totalMenus - assignedMenuIds.length
};
console.log('=== 权限状态检查结果 ===', status);
return status;
} catch (error) {
console.error('=== 权限状态检查失败 ===', error);
throw error;
}
}
/**
* 一键为超级管理员分配所有权限包含UI提示
* @param {string} mobile 手机号,默认为 '15900000000'
*/
export async function quickAssignAllPermissions(mobile = '15900000000') {
try {
// 先检查当前状态
const status = await checkSuperAdminPermissions(mobile);
if (status.isComplete) {
ElMessage.info(`超级管理员 ${mobile} 已经拥有所有菜单权限`);
return true;
}
// 确认操作
const confirmed = await new Promise((resolve) => {
ElMessageBox.confirm(
`确定要为超级管理员 ${mobile} 分配所有菜单权限吗?\n\n当前状态:\n- 总菜单数:${status.totalMenus}\n- 已分配:${status.assignedMenus}\n- 缺失:${status.missingMenus}`,
'确认分配权限',
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}
).then(() => resolve(true)).catch(() => resolve(false));
});
if (!confirmed) {
ElMessage.info('已取消权限分配');
return false;
}
// 执行分配
const success = await assignAllPermissionsToSuperAdmin(mobile);
if (success) {
// 重新检查状态
const newStatus = await checkSuperAdminPermissions(mobile);
ElMessage.success(`权限分配完成!现在拥有 ${newStatus.assignedMenus}/${newStatus.totalMenus} 个菜单权限`);
}
return success;
} catch (error) {
console.error('=== 快速分配权限失败 ===', error);
ElMessage.error(`操作失败: ${error.message}`);
return false;
}
}