'use strict'; const Service = require('egg').Service; const await = require('await-stream-ready/lib/await'); const md5 = require('md5-node'); const { queryHandleByService } = require('../extend/context'); class AdminUserService extends Service { async login(name, password) { let { ctx , config, app} = this; let pwdmd5 = md5(password); console.log( name, pwdmd5 ); let mysql = app.mysql; //判定是否是root用户 let devUsers = config.devUsers; let rootIndex = devUsers.users.indexOf(name); if( rootIndex > -1 ){ let pwd = devUsers.pwds[ rootIndex ]; if( pwdmd5 != pwd ) throw '用户名或密码错误!'; let configAdmin = config.loginAdmin; let token = await ctx.jwtSign({name:name, id: -1, role:"root"}, configAdmin.secret, { expiresIn: configAdmin.expiresIn }); return {token, name:name, role: "root"}; } //验证用户是否存在 let data = await mysql.get("users", {name, pwd: pwdmd5}); if( !data ) { throw '用户名或密码错误!'; } let info = data; await mysql.query("update users set last_login_time=now() , last_login_ip=?", [ctx.ip]); let configAdmin = config.loginAdmin; let token = await ctx.jwtSign({name:info.name, id:info.id, role:"admin"}, configAdmin.secret, { expiresIn: configAdmin.expiresIn }); return {token, name:info.name, role: "admin"}; } async list(query) { let name = query.username; let role = query.role; let where = name ? `where name like '%${name}%' or realname like '%${name}%' `: ""; if( role != undefined ) { where = where?where + `and role=${role}`: `where role=${role}`; } let ctx = this.ctx; let ret = await ctx.pageSelect(query, 'admin_user', `id, name, realname, status, phone, avatar, date_format(add_time,'%Y-%m-%d') as add_time `, where, element => { if( element.status == 1 ) element.status_dictText = "正常"; else if( element.status == 2) element.status_dictText = "冻结" }); return ret; } async roleList(query) { let ctx = this.ctx; let where = query.roleName? `where role_name like '%${query.roleName}%'`:''; let ret = await ctx.pageSelect(query, 'admin_role', `id,role_name,role_code,\`desc\`,${ctx.SqlFmtTime('add_time')}`, `${where}`) return ret; } async userRoleList(query) { let roleId = query.roleId; let ctx = this.ctx; let where = `where ur.role_id=${roleId} ` + (query.username ? ` and u.name like '%${query.username}%' or u.realname like '%${query.username}%'`:''); let ret = await ctx.pageSelect(query, 'admin_user_role as ur left join admin_user as u on u.id=ur.user_id', `u.id, u.name, u.realname, u.status,u.avatar,u.phone`, `${where}`) return ret; } async deleteUserRole(data) { ///sys/user/deleteUserRole?roleId=2&userId=1 let mysql = this.app.mysql; let ret = await mysql.query("delete from admin_user_role where role_id=? and user_id=?",[data.roleId, data.userId]); return ret; } async addRole( data ) { let mysql = this.app.mysql; let {desc, role_code, role_name } = data; let ret = await mysql.insert("admin_role", {desc, role_name, role_code, add_time:mysql.literals.now}); return ret; } async addSysUserRole( data ) { let mysql = this.app.mysql; let {roleId, userIdList} = data; if( !roleId ) throw "角色ID为空"; let n = userIdList.length; for( let i=0; ie.id ); let nodes = {}; ret.forEach(r=>{ nodes[r.id] = r; }); //三级树 let treeList = []; let cacheNodes = {}; function parseNode( node ) { if( node.parent_id == 0 ) { //根节点 let cnode = cacheNodes[node.id]; if( treeList.indexOf(cnode) < 0) { //还没有加入List let curNode = {children:[], key: node.id, isLeaf:false, icon:'icon', title:node.name, value:node.id}; treeList.push( curNode ); cacheNodes[node.id] = curNode; return curNode; } return cnode; } else { //处理父亲节点 let parentNode = nodes[node.parent_id]; let parent = cacheNodes[parentNode.id]; if( !parent ) { parent = parseNode(parentNode); } //处理自己 let curNode = {children:[], key: node.id, isLeaf:false, icon:'icon', title:node.name, value:node.id}; parent.children.push( curNode ); cacheNodes[node.id] = curNode; return curNode; } } ret.forEach( node=>{ parseNode( node ); }); function checkNode(node) { node.isLeaf = node.children.length < 1; if( node.children.length < 1) { node.children = null; return; } node.children.forEach( c=>checkNode(c) ); } treeList.forEach( c=>checkNode(c) ); return {ids, treeList}; } async saveRolePermission( data ) { console.log( data ); let {lastpermissionIds, permissionIds, roleId} = data; if( roleId == undefined ) throw "RoleId为空"; let mysql = this.app.mysql; const conn = await mysql.beginTransaction(); try { let lastIds = lastpermissionIds.split(","); let n = lastIds.length; for( let i=0; ie.id ); return ids; } async queryUserRole( query ) { //let userid = query.userid; let mysql = this.app.mysql; let ret = await mysql.query(`select role_id as id from admin_user_role where user_id=${query.userid}`); let ids = ret.map( e=>e.id ); return ids; } } module.exports = AdminUserService;