Browse Source

修改 bug

master
王泽彦 8 months ago
parent
commit
881204196a
  1. 108
      niucloud/app/service/admin/uniapp/UniappAuthService.php
  2. 187
      niucloud/app/service/api/login/UnifiedLoginService.php
  3. 4165
      uniapp/api/apiRoute.js
  4. 8
      uniapp/pages/common/home/index.vue

108
niucloud/app/service/admin/uniapp/UniappAuthService.php

@ -54,13 +54,57 @@ class UniappAuthService extends BaseAdminService
*/ */
public function getRoleMenus(int $roleId) public function getRoleMenus(int $roleId)
{ {
$sql = "SELECT m.id, m.menu_key, m.menu_name, m.icon as menu_icon, // 优先从school_sys_role.mobile_rules字段获取权限配置
m.router_path as menu_path, '' as menu_params, m.sort as sort_order $roleData = Db::table('school_sys_role')
FROM role_menu_permissions rmp ->where('role_id', $roleId)
LEFT JOIN school_sys_menu m ON rmp.menu_id = m.id ->where('status', 1)
WHERE rmp.role_id = ? AND rmp.is_enabled = 1 ->field('mobile_rules')
ORDER BY m.sort ASC"; ->find();
$menuList = Db::query($sql, [$roleId]);
if ($roleData && !empty($roleData['mobile_rules'])) {
// 解析mobile_rules JSON字符串
$mobileRules = json_decode($roleData['mobile_rules'], true);
if (is_array($mobileRules)) {
// 使用查询构造器获取所有菜单信息
$allMenus = Db::table('school_sys_menu')
->where('app_type', 'uniapp')
->where('status', 1)
->field('id, menu_key, menu_name, icon as menu_icon, router_path as menu_path, sort as sort_order')
->select()
->toArray();
// 创建menu_key到菜单信息的映射
$menuKeyToMenu = [];
foreach ($allMenus as $menu) {
$menuKeyToMenu[$menu['menu_key']] = $menu;
}
// 按mobile_rules中的顺序构建菜单列表
$menuList = [];
foreach ($mobileRules as $menuKey) {
if (isset($menuKeyToMenu[$menuKey])) {
$menu = $menuKeyToMenu[$menuKey];
$menu['menu_params'] = [];
$menu['menu_icon'] = $menu['menu_icon'] ?? '';
$menu['menu_path'] = $menu['menu_path'] ?? '';
$menuList[] = $menu;
}
}
return $menuList;
}
}
// 如果mobile_rules为空,则从role_menu_permissions表获取(向后兼容)
$menuList = Db::table('role_menu_permissions')
->alias('rmp')
->leftJoin('school_sys_menu m', 'rmp.menu_id = m.id')
->where('rmp.role_id', $roleId)
->where('rmp.is_enabled', 1)
->field('m.id, m.menu_key, m.menu_name, m.icon as menu_icon, m.router_path as menu_path, m.sort as sort_order')
->order('m.sort', 'ASC')
->select()
->toArray();
// 处理参数和数据转换 // 处理参数和数据转换
foreach ($menuList as &$item) { foreach ($menuList as &$item) {
@ -84,38 +128,52 @@ class UniappAuthService extends BaseAdminService
{ {
Db::startTrans(); Db::startTrans();
try { try {
// 1. 更新school_sys_role表的mobile_rules字段(主要存储方式)
$mobileRulesJson = json_encode($menuKeys, JSON_UNESCAPED_UNICODE);
Db::table('school_sys_role')
->where('role_id', $roleId)
->update(['mobile_rules' => $mobileRulesJson]);
// 2. 同步到role_menu_permissions表(向后兼容)
// 删除原有权限 // 删除原有权限
Db::execute("DELETE FROM role_menu_permissions WHERE role_id = ?", [$roleId]); Db::table('role_menu_permissions')
->where('role_id', $roleId)
->delete();
// 添加新权限 // 添加新权限
if (!empty($menuKeys)) { if (!empty($menuKeys)) {
// 将menu_key转换为对应的数据库ID // 使用查询构造器获取菜单ID映射
$menuIds = []; $menuList = Db::table('school_sys_menu')
$sql = "SELECT id, menu_key FROM school_sys_menu WHERE app_type = 'uniapp' AND status = 1"; ->where('app_type', 'uniapp')
$menuList = Db::query($sql); ->where('status', 1)
->field('id, menu_key')
->select()
->toArray();
// 创建menu_key到ID的映射
$menuKeyToId = []; $menuKeyToId = [];
foreach ($menuList as $menu) { foreach ($menuList as $menu) {
$menuKeyToId[$menu['menu_key']] = $menu['id']; $menuKeyToId[$menu['menu_key']] = $menu['id'];
} }
// 过滤出有效的菜单ID // 过滤出有效的菜单ID并构造插入数据
$insertData = [];
$currentTime = date('Y-m-d H:i:s');
foreach ($menuKeys as $menuKey) { foreach ($menuKeys as $menuKey) {
if (isset($menuKeyToId[$menuKey])) { if (isset($menuKeyToId[$menuKey])) {
$menuIds[] = $menuKeyToId[$menuKey]; $insertData[] = [
'role_id' => $roleId,
'menu_id' => $menuKeyToId[$menuKey],
'is_enabled' => 1,
'created_at' => $currentTime,
'updated_at' => $currentTime
];
} }
} }
// 插入新的权限数据 // 批量插入权限数据
if (!empty($menuIds)) { if (!empty($insertData)) {
$values = []; Db::table('role_menu_permissions')->insertAll($insertData);
$params = [];
foreach ($menuIds as $menuId) {
$values[] = "(?, ?, 1, NOW(), NOW())";
$params[] = $roleId;
$params[] = $menuId;
}
$sql = "INSERT INTO role_menu_permissions (role_id, menu_id, is_enabled, created_at, updated_at) VALUES " . implode(', ', $values);
Db::execute($sql, $params);
} }
} }

187
niucloud/app/service/api/login/UnifiedLoginService.php

@ -439,7 +439,25 @@ class UnifiedLoginService extends BaseService
]; ];
} }
// 解析用户的角色ID列表 // 首先从 school_campus_person_role 表查询角色信息(优先级最高)
$campusRoleData = Db::table('school_campus_person_role')
->alias('cpr')
->leftJoin('school_sys_role sr', 'cpr.role_id = sr.role_id')
->where('cpr.person_id', $staffInfo['id'])
->where('sr.status', 1)
->field('sr.role_id, sr.role_name, sr.role_key')
->find();
if ($campusRoleData) {
return [
'role_id' => $campusRoleData['role_id'],
'role_name' => $campusRoleData['role_name'],
'role_code' => $campusRoleData['role_key'] ?: 'staff',
'role_key' => $campusRoleData['role_key'],
];
}
// 其次解析 sys_user 表的角色ID列表
$roleIds = []; $roleIds = [];
if (!empty($staffInfo['role_ids'])) { if (!empty($staffInfo['role_ids'])) {
$roleIdsStr = trim($staffInfo['role_ids'], '[]"'); $roleIdsStr = trim($staffInfo['role_ids'], '[]"');
@ -448,28 +466,25 @@ class UnifiedLoginService extends BaseService
} }
} }
// 如果没有角色分配,根据account_type推断 // 如果有系统角色ID,查询第一个角色的详细信息
if (empty($roleIds)) { if (!empty($roleIds)) {
return $this->getRoleInfoByAccountType($staffInfo['account_type']); $roleId = $roleIds[0]; // 取第一个角色
} $roleData = Db::table('school_sys_role')
->where('role_id', $roleId)
// 查询第一个角色的详细信息 ->where('status', 1)
$roleId = $roleIds[0]; // 取第一个角色 ->find();
$roleData = Db::table('school_sys_role')
->where('role_id', $roleId) if ($roleData) {
->where('status', 1) return [
->find(); 'role_id' => $roleData['role_id'],
'role_name' => $roleData['role_name'],
if ($roleData) { 'role_code' => $roleData['role_key'] ?: 'staff',
return [ 'role_key' => $roleData['role_key'],
'role_id' => $roleData['role_id'], ];
'role_name' => $roleData['role_name'], }
'role_code' => $roleData['role_key'] ?: 'staff',
'role_key' => $roleData['role_key'],
];
} }
// 如果查询失败,使用默认角色 // 最后根据account_type推断默认角色
return $this->getRoleInfoByAccountType($staffInfo['account_type']); return $this->getRoleInfoByAccountType($staffInfo['account_type']);
} catch (\Exception $e) { } catch (\Exception $e) {
@ -543,45 +558,28 @@ class UnifiedLoginService extends BaseService
private function getStaffMenuList(int $roleType, int $isAdmin = 0) private function getStaffMenuList(int $roleType, int $isAdmin = 0)
{ {
try { try {
// 如果是超级管理员或校长,返回所有菜单 // 如果是超级管理员,返回所有菜单
if ($isAdmin == 1 || $roleType == 999) { if ($isAdmin == 1) {
return $this->getAllMenuList(); return $this->getAllMenuList();
} }
// 查询角色对应的菜单权限 // 从 school_sys_role 表查询移动端权限配置
$menuList = Db::table('sys_menus') $roleData = Db::table('school_sys_role')
->alias('m') ->where('role_id', $roleType)
->leftJoin('role_menu_permissions rmp', 'm.id = rmp.menu_id') ->where('status', 1)
->where('rmp.role_id', $roleType) ->field('mobile_rules')
->where('rmp.is_enabled', 1) ->find();
->where('m.status', 1)
->field('m.menu_key, m.menu_name, m.menu_icon, m.menu_path, m.menu_params, m.sort_order')
->order('m.sort_order ASC')
->select()
->toArray();
// 转换为前端需要的格式
$result = [];
foreach ($menuList as $menu) {
$menuItem = [
'key' => $menu['menu_key'],
'title' => $menu['menu_name'],
'icon' => $menu['menu_icon'],
'path' => $menu['menu_path'],
];
// 如果有参数,解析JSON参数 if ($roleData && !empty($roleData['mobile_rules'])) {
if (!empty($menu['menu_params'])) { // 解析mobile_rules JSON字符串
$params = json_decode($menu['menu_params'], true); $mobileRules = json_decode($roleData['mobile_rules'], true);
if ($params) { if (is_array($mobileRules)) {
$menuItem['params'] = $params; return $this->convertMobileRulesToMenuList($mobileRules);
}
} }
$result[] = $menuItem;
} }
return $result; // 如果查询不到,返回默认菜单
return $this->getDefaultStaffMenuList($roleType);
} catch (\Exception $e) { } catch (\Exception $e) {
// 如果数据库查询失败,返回默认菜单(兼容处理) // 如果数据库查询失败,返回默认菜单(兼容处理)
@ -589,6 +587,89 @@ class UnifiedLoginService extends BaseService
} }
} }
/**
* 将 mobile_rules 转换为菜单列表
* @param array $mobileRules
* @return array
*/
private function convertMobileRulesToMenuList(array $mobileRules)
{
// 定义移动端页面路径与菜单项的映射关系(包含首页所有功能)
$menuMapping = [
// 考勤管理
'pages-common/my_attendance' => ['key' => 'attendance', 'title' => '考勤管理', 'icon' => 'clock-filled', 'path' => '/pages-common/my_attendance'],
// 合同管理
'pages-common/contract/my_contract' => ['key' => 'contract_management', 'title' => '合同管理', 'icon' => 'document-filled', 'path' => '/pages-common/contract/my_contract'],
'pages-common/contract/contract_sign' => ['key' => 'contract_sign', 'title' => '合同签署', 'icon' => 'edit-filled', 'path' => '/pages-common/contract/contract_sign'],
// 客户资源管理
'pages-market/clue/index' => ['key' => 'customer_resource', 'title' => '客户资源', 'icon' => 'person-filled', 'path' => '/pages-market/clue/index'],
'pages-market/clue/add_clues' => ['key' => 'add_customer', 'title' => '添加资源', 'icon' => 'plus-filled', 'path' => '/pages-market/clue/add_clues'],
// 教学管理
'pages-coach/coach/student/student_list' => ['key' => 'student_management', 'title' => '学员管理', 'icon' => 'contact-filled', 'path' => '/pages-coach/coach/student/student_list'],
'pages-coach/coach/schedule/schedule_table' => ['key' => 'course_query', 'title' => '课程查询', 'icon' => 'search', 'path' => '/pages-coach/coach/schedule/schedule_table'],
'pages-market/clue/class_arrangement' => ['key' => 'course_arrangement', 'title' => '课程安排', 'icon' => 'calendar-filled', 'path' => '/pages-market/clue/class_arrangement'],
'pages-coach/coach/my/teaching_management' => ['key' => 'resource_library', 'title' => '资料库', 'icon' => 'folder-add-filled', 'path' => '/pages-coach/coach/my/teaching_management'],
// 财务管理
'pages-market/reimbursement/list' => ['key' => 'reimbursement', 'title' => '报销管理', 'icon' => 'wallet-filled', 'path' => '/pages-market/reimbursement/list'],
// 系统页面
'pages/common/home/index' => ['key' => 'home', 'title' => '首页', 'icon' => 'home-filled', 'path' => '/pages/common/home/index'],
'pages/common/profile/index' => ['key' => 'personal_center', 'title' => '个人中心', 'icon' => 'user-filled', 'path' => '/pages/common/profile/index'],
// 数据统计
'pages/common/dashboard/webview?type=my_data' => ['key' => 'my_data', 'title' => '我的数据', 'icon' => 'bars', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'my_data']],
'pages/common/dashboard/webview?type=dept_data' => ['key' => 'dept_data', 'title' => '部门数据', 'icon' => 'home', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'dept_data']],
'pages/common/dashboard/webview?type=campus_data' => ['key' => 'campus_data', 'title' => '校区数据', 'icon' => 'location', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'campus_data']],
// 消息管理
'pages-common/my_message' => ['key' => 'my_message', 'title' => '我的消息', 'icon' => 'chat-filled', 'path' => '/pages-common/my_message'],
];
$menuList = [];
foreach ($mobileRules as $rule) {
if (isset($menuMapping[$rule])) {
$menuList[] = $menuMapping[$rule];
}
}
return $menuList;
}
/**
* 获取所有移动端菜单(校长专用)
* @return array
*/
private function getAllMobileMenuList()
{
return [
// 考勤管理
['key' => 'attendance', 'title' => '考勤管理', 'icon' => 'clock-filled', 'path' => '/pages-common/my_attendance'],
// 合同管理
['key' => 'contract_management', 'title' => '合同管理', 'icon' => 'document-filled', 'path' => '/pages-common/contract/my_contract'],
// 客户资源管理
['key' => 'customer_resource', 'title' => '客户资源', 'icon' => 'person-filled', 'path' => '/pages-market/clue/index'],
['key' => 'add_customer', 'title' => '添加资源', 'icon' => 'plus-filled', 'path' => '/pages-market/clue/add_clues'],
// 教学管理
['key' => 'student_management', 'title' => '学员管理', 'icon' => 'contact-filled', 'path' => '/pages-coach/coach/student/student_list'],
['key' => 'course_query', 'title' => '课程查询', 'icon' => 'search', 'path' => '/pages-coach/coach/schedule/schedule_table'],
// 数据统计(校长权限)
['key' => 'my_data', 'title' => '我的数据', 'icon' => 'bars', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'my_data']],
['key' => 'dept_data', 'title' => '部门数据', 'icon' => 'chart-filled', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'dept_data']],
['key' => 'campus_data', 'title' => '校区数据', 'icon' => 'graph-filled', 'path' => '/pages/common/dashboard/webview', 'params' => ['type' => 'campus_data']],
// 消息管理
['key' => 'my_message', 'title' => '我的消息', 'icon' => 'chat-filled', 'path' => '/pages/common/my/my_message'],
];
}
/** /**
* 获取所有功能菜单(校长/管理员使用) * 获取所有功能菜单(校长/管理员使用)
* @return array * @return array

4165
uniapp/api/apiRoute.js

File diff suppressed because it is too large

8
uniapp/pages/common/home/index.vue

@ -105,7 +105,13 @@
icon: 'location', icon: 'location',
path: '/pages/common/dashboard/webview', path: '/pages/common/dashboard/webview',
params: { type: 'campus_data' } params: { type: 'campus_data' }
} },
{
title: '报销管理',
icon: 'wallet-filled',
path: '/pages-market/reimbursement/list',
params: { type: 'reimbursement' }
}
] ]
} }
}, },

Loading…
Cancel
Save