'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{ 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;