123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- 'use strict';
- const await = require('await-stream-ready/lib/await');
- const { mysql } = require('../../config/plugin');
- const Service = require('egg').Service;
- //const md5 = require('md5-node');
- class SysPermService extends Service {
- async addPerm( data )
- {
- console.log( data );
- let {status, permsType, route, icon, alwaysShow, hidden, keepAlive, internalOrExternal, menuType, name, url, component, redirect, sortNo, parentId} = data;
- if( !parentId ) parentId = 0;
-
- // status: '1',
- // permsType: '1',
- // route: true,
- // icon: 'afaf',
- // alwaysShow: false,
- // hidden: false,
- // keepAlive: false,
- // internalOrExternal: false,
- // menuType: 0,
- // name: 'tst',
- // url: 'aff',
- // component: 'adfdfa',
- // redirect: 'adfa',
- // sortNo: 1
- let mysql = this.app.mysql;
-
- let ret = await mysql.insert("admin_permission", {add_time:mysql.literals.now, parent_id:parentId, status, perms_type:permsType, is_route:route, icon, always_show: alwaysShow, hidden, keep_alive:keepAlive, internal_or_external:internalOrExternal, menu_type:menuType, name, url, component, redirect, sort_no:sortNo});
- return ret;
- }
- async listPerm(query) {
- let ctx = this.ctx;
- let mysql = this.app.mysql;
- let ret = await mysql.query('select * from admin_permission');
- ctx.nameFmt2Camel( ret );
-
- return ctx.fmt2Tree(ret, node=>{ return {key: node.id, title:node.name, ...node} });
- }
- async deletePerm( query ) {
- if( !query.id ) throw "ID为空";
- let mysql = this.app.mysql;
- let ret = await mysql.query('delete from admin_permission where id=? or parent_id=?', [query.id, query.id]);
- return ret;
- }
- async editPerm( data ) {
-
- delete data["addTime"];
- if( !data.id ) throw "ID不能为空";
- delete data["children"];
- delete data["key"];
- delete data["title"];
- delete data["isLeaf"];
- delete data["route"];
- let ctx = this.ctx;
- ctx.nameFmtCamel2UnderLine( data );
- console.log("============");
- console.log( data );
- let mysql = this.app.mysql;
- let ret = await mysql.update("admin_permission", data, {where: {id: data.id}});
-
- return ret;
- // alwaysShow: false
- // children: null
- // component: "xxx"
- // componentName: null
- // description: null
- // hidden: false
- // icon: null
- // id: 1
- // internalOrExternal: false
- // isLeaf: true
- // isRoute: null
- // keepAlive: false
- // key: 1
- // menuType: null
- // name: "首页"
- // parentId: 0
- // perms: null
- // permsType: null
- // route: true
- // sortNo: 0
- // status: null
- // title: "首页"
- // url: "isystem/roles"
- }
- async deleteBatch(query) {
- ///sys/permission/deleteBatch?ids=1,2,3,6,7,8,9,10,11,12,13,16,18,19,
- let ids = query.ids;
- if( !ids ) throw "ids为空";
- ids = ids.split(",");
- let mysql = this.app.mysql;
- const conn = await mysql.beginTransaction();
- try {
-
- let n = ids.length;
-
- for( let i=0; i<n; i++) {
- if( ids[i] ) await conn.query(`delete from admin_permission where id=?`,[ids[i]]);
- }
- await conn.commit();
- } catch (error) {
- await conn.rollback();
- throw error;
- }
- return true;
- }
- async getUserPermissionByToken(query) {
-
- let authToken = query.token;
- if (!authToken) {
- throw "token为空";
- }
- let options = this.app.config.loginAdmin;
- let ctx = this.ctx;
- let ret = await ctx.jwtVerify(authToken, options.secret, options.options);
- if( !ret.verify )
- {
- throw "Token失效请重新登录";
- }
-
- let info = ret.message;
- let id = info.id;
- let permissions = [];
- let mysql = this.app.mysql;
- if( info.type == 2 ) {//参训人员自带'参训人员role_cxry'和'导调权限role_teacher'
- let role_code = info.role == 1? 'role_teacher' : 'role_cxry';
- let sql = `
- select p.* from admin_permission p
- where (
- exists(
- select a.id from admin_role_permission a
- join admin_role b on a.role_id = b.id
- where p.id=a.permission_id and b.role_code='${role_code}'
- )
- ) order by sort_no ASC`
-
- permissions = await mysql.query(sql);
-
- console.log( permissions );
-
- } else {
- //更加用户ID获取permission
- let sql = `
- select p.* from admin_permission p
- where (
- exists(
- select a.id from admin_role_permission a
- join admin_role b on a.role_id = b.id
- join admin_user_role c on c.role_id = b.id
- join admin_user d on d.id=c.user_id
- where p.id=a.permission_id and d.id=${id}
- )
- ) order by sort_no ASC`
-
- permissions = await mysql.query(sql);
- }
-
- function urlToRouteName( url ) {
- if( !url ) return "";
-
- if( url[0] == '/') {
- url = url.substr(1);
- }
- url = url.replace("/","-");
- url = url.replace(":","@");
- return url;
- }
- function createPermission( data ) {
- let ret = {};
- if( data.menu_type == 2) return null;
- console.log( data.menu_type );
- if( data.menu_type != 0 && data.menu_type !=1) return ret;
- ret.id = data.id;
- ret.route = data.is_route?1:0;
- ret.path = data.url;
- ret.name = data.component_name?data.component_name: urlToRouteName(data.url);
- ret.hidden = data.hidden?true:false;
- ret.alwaysShow = data.always_show?true:false;
- ret.component = data.component;
- ret.redirect = data.redirect;
- ret.meta = {
- keepAlive: data.keep_alive?true: false,
- internalOrExternal: data.internal_or_external?true:false,
- title: data.name,
- icon: data.icon?data.icon:null,
- }
- return ret;
- }
- let tree = ctx.fmt2Tree(permissions, node=>{ return createPermission(node) });
- return {menu: tree};
- }
- async queryallrole() {
- let mysql = this.app.mysql;
- let ret = await mysql.query("select * from admin_role");
-
- ret = ret.map(e=>{
- // e.roleCode = e.role_code;
- // e.roleName = e.role_name;
- // e.description = e.desc;
- return {id:e.id, roleCode:e.role_code, roleName: e.role_name, description:e.desc};
- });
- return ret;
- }
- }
- module.exports = SysPermService;
|