Commit 231ca77d by 王昆

gsb

parent bdbcc176
...@@ -31,5 +31,30 @@ class UserroleDao extends Dao { ...@@ -31,5 +31,30 @@ class UserroleDao extends Dao {
user_id: user_id user_id: user_id
}) || []; }) || [];
} }
async mapByUserIds(userIds) {
let result = {};
let sql = [];
sql.push("SELECT");
sql.push("user_id, role_id");
sql.push("FROM uc_user_role");
sql.push("WHERE user_id IN (:userIds)");
sql.push("GROUP BY user_id, role_id");
let list = await this.customQuery(sql.join(" "), {userIds: userIds});
if (!list || list.length == 0) {
return {};
}
for (let item of list) {
let roleIds = result[item.user_id];
if (!roleIds) {
roleIds = [];
}
roleIds.push(item.role_id);
result[item.user_id] = roleIds;
}
return result;
}
} }
module.exports = UserroleDao; module.exports = UserroleDao;
\ No newline at end of file
...@@ -101,25 +101,40 @@ class AuthService extends ServiceBase { ...@@ -101,25 +101,40 @@ class AuthService extends ServiceBase {
} }
async menuByRoleIds(params) { async menuByRoleIds(params) {
params.menuType = 1; params.menuType = 1;
var all = await this.byRoleIds(params); let all = await this.byRoleIds(params);
if(!all || all.length == 0) {
return [];
}
let pmenus = [];
let pmenuMap = {};
var pmap = {}; for (let item of all) {
for (var item of all) { if (item.pid === 0) {
var list = pmap[item.pid]; pmenus.push(item);
if (!list) { pmenuMap[item.id] = item;
list = [];
} }
list.push(item);
pmap[item.pid] = list;
} }
for(var item of all) { for (let item of all) {
item.childs = pmap[item.id] || []; let pid = item.pid;
if (pid === 0) {
continue;
}
let pmenu = pmenuMap[pid];
if (!pmenu) {
continue;
}
let childs = pmenu.childs;
if (!childs) {
childs = [];
}
childs.push(item);
pmenu.childs = childs;
} }
if(pmap.length == 0) {
return system.getResultSuccess({}); return pmenus;
}
return system.getResultSuccess(pmap[0][0]);
} }
async authByRoleIds(params) { async authByRoleIds(params) {
......
...@@ -259,6 +259,7 @@ class UserService extends ServiceBase { ...@@ -259,6 +259,7 @@ class UserService extends ServiceBase {
user.realName = info.realName; user.realName = info.realName;
user.roles = await this.userroleDao.listByUserId(id, "user_id, role_id"); user.roles = await this.userroleDao.listByUserId(id, "user_id, role_id");
await this.setRoleIds([user]);
this.handleDate(user, ["created_at"], null, -8); this.handleDate(user, ["created_at"], null, -8);
return system.getResultSuccess(user); return system.getResultSuccess(user);
} }
...@@ -293,9 +294,10 @@ class UserService extends ServiceBase { ...@@ -293,9 +294,10 @@ class UserService extends ServiceBase {
params.startRow = (currentPage - 1) * pageSize; params.startRow = (currentPage - 1) * pageSize;
result.rows = await this.dao.listByCondition(params) || []; result.rows = await this.dao.listByCondition(params) || [];
if (result.rows) { if (result.rows) {
for (var item of result.rows) { for (let item of result.rows) {
this.handleDate(item, ["created_at"], null, -8); this.handleDate(item, ["created_at"], null, -8);
} }
await this.setRoleIds(result.rows);
} }
return system.getResultSuccess(result); return system.getResultSuccess(result);
} }
...@@ -342,6 +344,22 @@ class UserService extends ServiceBase { ...@@ -342,6 +344,22 @@ class UserService extends ServiceBase {
let rs = await this.dao.findUsers(params); let rs = await this.dao.findUsers(params);
return system.getResultSuccess(rs); return system.getResultSuccess(rs);
} }
async setRoleIds(rows) {
if (!rows || rows.length == 0) {
return;
}
let userIds = [];
for (let row of rows) {
userIds.push(row.id);
}
let roleIdMap = await this.userroleDao.mapByUserIds(userIds);
for (let row of rows) {
row.roleIds = roleIdMap[row.id] || [];
}
}
} }
module.exports = UserService; module.exports = UserService;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment