From ce8bdd8b46c7ac6b35d48abe6b23fe7c39c3d48f Mon Sep 17 00:00:00 2001 From: zhanglp Date: Sun, 24 Jul 2022 12:31:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=9D=83=E9=99=90=E5=92=8C?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/menuController.js | 14 +----- controllers/roleController.js | 10 +++++ controllers/userController.js | 5 +++ model/menuModel.js | 6 +++ router/menu.js | 19 +------- router/role.js | 15 ++++++- router/user.js | 7 +++ services/menuServices.js | 84 ++++++----------------------------- services/roleMenuServices.js | 19 +++----- services/roleServices.js | 33 ++++++++++++++ services/userServices.js | 71 +++++++++++++++++++++++++++-- 11 files changed, 166 insertions(+), 117 deletions(-) diff --git a/controllers/menuController.js b/controllers/menuController.js index aa5335e..fe7c981 100644 --- a/controllers/menuController.js +++ b/controllers/menuController.js @@ -9,15 +9,7 @@ const { } = require("../services/menuServices"); class MenuController { - constructor() {} - getRoleMenuLsit(body){ - const {id:roleId}=body - return getPermissionMenuListByRoleId(roleId) - } - getPermission(body){ - const {id:userId}=body - return getPermissionMenuListByUserId(userId) - } + constructor() {} getList() { return getMenuList(); } @@ -33,9 +25,5 @@ class MenuController { const { id } = body; return deleteMenu(id); } - assign(body) { - const { roleId,menuIds } = body; - return assignPermission(roleId,menuIds); - } } module.exports = new MenuController(); diff --git a/controllers/roleController.js b/controllers/roleController.js index 14d790f..89d060e 100644 --- a/controllers/roleController.js +++ b/controllers/roleController.js @@ -1,11 +1,17 @@ const { + getPermissionMenuListByRoleId, getRoleList, addRole, updateRole, deleteRole, + assignPermission, } = require("../services/roleServices"); class RoleController { constructor() {} + getMenuPermission(body){ + const {id:roleId}=body + return getPermissionMenuListByRoleId(roleId) + } getList() { return getRoleList(); } @@ -21,5 +27,9 @@ class RoleController { const { id } = body; return deleteRole(id); } + assignMenu(body) { + const { roleId,menuIds } = body; + return assignPermission(roleId,menuIds); + } } module.exports = new RoleController(); diff --git a/controllers/userController.js b/controllers/userController.js index 871c31d..8be991f 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -1,4 +1,5 @@ const { + getUserPermission, getUserPageList, getUserDetail, getUserInfo, @@ -14,6 +15,10 @@ const { class UserController { constructor() {} + getPermission(body){ + const {id:userId}=body + return getUserPermission(userId) + } getPageList(query) { const { userName, current, size } = query; return getUserPageList(current, size, userName); diff --git a/model/menuModel.js b/model/menuModel.js index 099b0f5..dbc579e 100644 --- a/model/menuModel.js +++ b/model/menuModel.js @@ -12,6 +12,12 @@ class MenuModel { const sqlArr = []; return db.queryAsync(sql, sqlArr); } + selectOne(id) { + const sql = `select * from sys_menus where id=?`; + const sqlArr = [id]; + console.log('11111111111111'); + return db.queryAsync(sql, sqlArr); + } insert(menu) { const sql = "insert into sys_menus(menu_id, menu_name, path, menu_type, icon, sort, pid, link, enabled, status) values(?,?,?,?,?,?,?,?,?,?)"; diff --git a/router/menu.js b/router/menu.js index 1f004a6..12fd8bb 100644 --- a/router/menu.js +++ b/router/menu.js @@ -10,18 +10,6 @@ const { assign, } = require("../controllers/menuController"); -router.get("/api/menu/getRoleMenuLsit", (req, res) => { - const { query } = req; - getRoleMenuLsit(query).then((data) => { - res.send(data); - }); -}); -router.get("/api/menu/getPermission", (req, res) => { - const { query } = req; - getPermission(query).then((data) => { - res.send(data); - }); -}); router.get("/api/menu/getList", (req, res) => { const { query } = req; getList(query).then((data) => { @@ -47,12 +35,7 @@ router.post("/api/menu/delete", (req, res) => { res.send(data); }); }); -router.post("/api/menu/assignPermission", (req, res) => { - const { body } = req; - assign(body).then((data) => { - res.send(data); - }); -}); + module.exports = router; diff --git a/router/role.js b/router/role.js index 12b6430..ab43ca6 100644 --- a/router/role.js +++ b/router/role.js @@ -1,12 +1,20 @@ const express = require("express"); const router = express.Router(); const { + getMenuPermission, getList, add, update, remove, + assignMenu, } = require("../controllers/roleController"); +router.get("/api/role/getMenuPermission", (req, res) => { + const { query } = req; + getMenuPermission(query).then((data) => { + res.send(data); + }); +}); router.get("/api/role/getList", (req, res) => { const { query } = req; getList(query).then((data) => { @@ -32,5 +40,10 @@ router.post("/api/role/delete", (req, res) => { res.send(data); }); }); - +router.post("/api/role/assignPermission", (req, res) => { + const { body } = req; + assignMenu(body).then((data) => { + res.send(data); + }); +}); module.exports = router; diff --git a/router/user.js b/router/user.js index 0a0e0a0..b66b5e6 100644 --- a/router/user.js +++ b/router/user.js @@ -1,6 +1,7 @@ const express = require("express"); const router = express.Router(); const { + getPermission, getPageList, getDetail, getInfo, @@ -14,6 +15,12 @@ const { disable, } = require("../controllers/userController"); +router.get("/api/user/getPermission", (req, res) => { + const { query } = req; + getPermission(query).then((data) => { + res.send(data); + }); +}); router.get("/api/user/getPageList", (req, res) => { const { query } = req; getPageList(query).then((data) => { diff --git a/services/menuServices.js b/services/menuServices.js index 2a33b30..0ad5287 100644 --- a/services/menuServices.js +++ b/services/menuServices.js @@ -1,37 +1,17 @@ const { select, selectWhere, + selectOne, insert, update, remove } = require("../model/menuModel"); - const {getUserRoleListByUserId} =require('./userRoleServices') -const { - getRoleMenuListByIds, - bindMenuForRole -} =require('./roleMenuServices') + class MenuServices { constructor() {} - async getPermissionMenuListByRoleId(roleId){ - const menuIdArr=[] - const roleMenuList=await getRoleMenuListByIds(roleId) - roleMenuList.forEach(element => { - const menuId=element.menu_id - const index=menuIdArr.indexOf(menuId) - if(index==-1){ - menuIdArr.push(menuId) - } - }); - if(menuIdArr.length<=0){ - return { - status: 0, - msg: "授权菜单列表", - data:[] - }; - } - const menuIds= menuIdArr.join(',') - const menuListResult = await selectWhere(menuIds); + async getMenuListByIds(menuIds) { + const menuListResult = await selectWhere(menuIds); const menuList = menuListResult.results && menuListResult.results; const jsonObj = { status: 0, @@ -40,57 +20,26 @@ class MenuServices { }; return jsonObj; } - async getPermissionMenuListByUserId(userId){ - - const userRoleList=await getUserRoleListByUserId(userId) - const roleIdArr=[] - const menuIdArr=[] - userRoleList.forEach(async (element) => { - const roleId=element.role_id - roleIdArr.push(roleId) - }); - if(roleIdArr.length<=0){ - return { - status: 0, - msg: "授权菜单列表", - data:[] - }; - } - const roleIds= roleIdArr.join(',') - const roleMenuList=await getRoleMenuListByIds(roleIds) - roleMenuList.forEach(element => { - const menuId=element.menu_id - const index=menuIdArr.indexOf(menuId) - if(index==-1){ - menuIdArr.push(menuId) - } - }); - if(menuIdArr.length<=0){ - return { - status: 0, - msg: "授权菜单列表", - data:[] - }; - } - const menuIds= menuIdArr.join(',') - const menuListResult = await selectWhere(menuIds); + async getMenuList() { + const menuListResult = await select(); const menuList = menuListResult.results && menuListResult.results; const jsonObj = { status: 0, - msg: "授权菜单列表", + msg: "菜单列表", data:menuList }; return jsonObj; } - async getMenuList() { - const menuListResult = await select(); - const menuList = menuListResult.results && menuListResult.results; + async getMenuDetail(id){ + const menuResult=await selectOne(id) + console.log('22222222222'); + const menuDetail = menuResult.results && menuResult.results[0]; const jsonObj = { status: 0, - msg: "菜单列表", - data:menuList + msg: "菜单详情", + data:menuDetail }; - return jsonObj; + return jsonObj } async addMenu(menu) { await insert(menu); @@ -119,10 +68,5 @@ class MenuServices { }; return jsonObj; } - - async assignPermission(roleId,menuIds) { - const jsonObj =await bindMenuForRole(roleId,menuIds); - return jsonObj; - } } module.exports = new MenuServices(); diff --git a/services/roleMenuServices.js b/services/roleMenuServices.js index 3205a47..9cdb4a6 100644 --- a/services/roleMenuServices.js +++ b/services/roleMenuServices.js @@ -6,10 +6,15 @@ const { class RoleMenuServices { constructor() {} - async getRoleMenuListByIds(roleIds) { + async getRoleMenuListByRoleIds(roleIds) { const roleMenuListResult = await selectWhere(roleIds) const roleMenuList = roleMenuListResult.results && roleMenuListResult.results; - return roleMenuList + const jsonObj = { + status: 0, + msg: "分配的菜单列表", + data:roleMenuList + }; + return jsonObj; } async getAssignedUserList(userId) { const userRoleListResult = await selectWhere('user_id',userId) @@ -35,15 +40,5 @@ class RoleMenuServices { }; return jsonObj; } - - // async unassignUser(id) { - // await remove(id); - // const jsonObj = { - // status: 0, - // msg: "取消分配用户成功", - // data: null, - // }; - // return jsonObj; - // } } module.exports = new RoleMenuServices(); diff --git a/services/roleServices.js b/services/roleServices.js index 5f977c9..1383651 100644 --- a/services/roleServices.js +++ b/services/roleServices.js @@ -4,9 +4,37 @@ const { update, remove } = require("../model/roleModel"); + const { + getRoleMenuListByRoleIds, + bindMenuForRole +} =require('./roleMenuServices') +const { + getMenuListByIds, +} =require('./menuServices') class RoleServices { constructor() {} + async getPermissionMenuListByRoleId(roleId){ + const menuIdArr=[] + const roleMenuReuslt=await getRoleMenuListByRoleIds(roleId) + const roleMenuList=roleMenuReuslt.data + roleMenuList.forEach(element => { + const menuId=element.menu_id + const index=menuIdArr.indexOf(menuId) + if(index==-1){ + menuIdArr.push(menuId) + } + }); + if(menuIdArr.length<=0){ + return { + status: 0, + msg: "授权菜单列表", + data:[] + }; + } + const menuIds= menuIdArr.join(',') + return await getMenuListByIds(menuIds); + } async getRoleList() { const roleListResult = await select(); const roleList = roleListResult.results && roleListResult.results; @@ -44,5 +72,10 @@ class RoleServices { }; return jsonObj; } + + async assignPermission(roleId,menuIds) { + const jsonObj =await bindMenuForRole(roleId,menuIds); + return jsonObj; + } } module.exports = new RoleServices(); diff --git a/services/userServices.js b/services/userServices.js index 66d2acf..a4594e3 100644 --- a/services/userServices.js +++ b/services/userServices.js @@ -8,14 +8,79 @@ const { updateField, remove, } = require("../model/userModel"); +const { getUserRoleListByUserId } = require("./userRoleServices"); +const { getRoleMenuListByRoleIds } = require("./roleMenuServices"); +const { + getMenuListByIds, + getMenuList, +} = require("./menuServices"); class UserServices { constructor() {} + async getUserPermission(userId) { + const userRoleList = await getUserRoleListByUserId(userId); + const roleIdArr = []; + const menuIdArr = []; + userRoleList.forEach(async (element) => { + const roleId = element.role_id; + roleIdArr.push(roleId); + }); + if (roleIdArr.length <= 0) { + return { + status: 0, + msg: "授权菜单列表", + data: [], + }; + } + const roleIds = roleIdArr.join(","); + const roleMenuResult = await getRoleMenuListByRoleIds(roleIds); + const roleMenuList = roleMenuResult.data; + roleMenuList.forEach((element) => { + const menuId = element.menu_id; + const index = menuIdArr.indexOf(menuId); + if (index == -1) { + menuIdArr.push(menuId); + } + }); + if (menuIdArr.length <= 0) { + return { + status: 0, + msg: "授权菜单列表", + data: [], + }; + } + const menuListResult = await getMenuList(); + const menuList = menuListResult.data; + + const menuIdArrNew = []; + menuIdArr.forEach((menuId) => { + menuIdArrNew.push(menuId); + const menu = menuList.find((x) => { + return x.id === menuId; + }); + const pid = menu.pid; + const index = menuIdArrNew.indexOf(pid); + if (pid !== 0 && index === -1) { + menuIdArrNew.push(pid); + + const parentMenu = menuList.find((x) => { + return x.id === pid; + }); + const pid1 = parentMenu.pid; + const index1 = menuIdArrNew.indexOf(pid1); + if (pid1 !== 0 && index1 === -1) { + menuIdArrNew.push(pid1); + } + } + }); + const menuIds = menuIdArrNew.join(","); + return await getMenuListByIds(menuIds); + } async userLogin(loginInfo) { const { userName, userPassword } = loginInfo; - const fields=['user_name'] - const values=[userName] - const result = await selectOneWhere(fields,values); + const fields = ["user_name"]; + const values = [userName]; + const result = await selectOneWhere(fields, values); const user = result.results && result.results[0]; const password = user && user.password; const passwordMd5 = quickMd5(userPassword); -- Gitee