sysPerm.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict';
  2. const await = require('await-stream-ready/lib/await');
  3. const { mysql } = require('../../config/plugin');
  4. const Service = require('egg').Service;
  5. //const md5 = require('md5-node');
  6. class SysPermService extends Service {
  7. async addPerm( data )
  8. {
  9. console.log( data );
  10. let {status, permsType, route, icon, alwaysShow, hidden, keepAlive, internalOrExternal, menuType, name, url, component, redirect, sortNo, parentId} = data;
  11. if( !parentId ) parentId = 0;
  12. // status: '1',
  13. // permsType: '1',
  14. // route: true,
  15. // icon: 'afaf',
  16. // alwaysShow: false,
  17. // hidden: false,
  18. // keepAlive: false,
  19. // internalOrExternal: false,
  20. // menuType: 0,
  21. // name: 'tst',
  22. // url: 'aff',
  23. // component: 'adfdfa',
  24. // redirect: 'adfa',
  25. // sortNo: 1
  26. let mysql = this.app.mysql;
  27. 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});
  28. return ret;
  29. }
  30. async listPerm(query) {
  31. let ctx = this.ctx;
  32. let mysql = this.app.mysql;
  33. let ret = await mysql.query('select * from admin_permission');
  34. ctx.nameFmt2Camel( ret );
  35. return ctx.fmt2Tree(ret, node=>{ return {key: node.id, title:node.name, ...node} });
  36. }
  37. async deletePerm( query ) {
  38. if( !query.id ) throw "ID为空";
  39. let mysql = this.app.mysql;
  40. let ret = await mysql.query('delete from admin_permission where id=? or parent_id=?', [query.id, query.id]);
  41. return ret;
  42. }
  43. async editPerm( data ) {
  44. delete data["addTime"];
  45. if( !data.id ) throw "ID不能为空";
  46. delete data["children"];
  47. delete data["key"];
  48. delete data["title"];
  49. delete data["isLeaf"];
  50. delete data["route"];
  51. let ctx = this.ctx;
  52. ctx.nameFmtCamel2UnderLine( data );
  53. console.log("============");
  54. console.log( data );
  55. let mysql = this.app.mysql;
  56. let ret = await mysql.update("admin_permission", data, {where: {id: data.id}});
  57. return ret;
  58. // alwaysShow: false
  59. // children: null
  60. // component: "xxx"
  61. // componentName: null
  62. // description: null
  63. // hidden: false
  64. // icon: null
  65. // id: 1
  66. // internalOrExternal: false
  67. // isLeaf: true
  68. // isRoute: null
  69. // keepAlive: false
  70. // key: 1
  71. // menuType: null
  72. // name: "首页"
  73. // parentId: 0
  74. // perms: null
  75. // permsType: null
  76. // route: true
  77. // sortNo: 0
  78. // status: null
  79. // title: "首页"
  80. // url: "isystem/roles"
  81. }
  82. async deleteBatch(query) {
  83. ///sys/permission/deleteBatch?ids=1,2,3,6,7,8,9,10,11,12,13,16,18,19,
  84. let ids = query.ids;
  85. if( !ids ) throw "ids为空";
  86. ids = ids.split(",");
  87. let mysql = this.app.mysql;
  88. const conn = await mysql.beginTransaction();
  89. try {
  90. let n = ids.length;
  91. for( let i=0; i<n; i++) {
  92. if( ids[i] ) await conn.query(`delete from admin_permission where id=?`,[ids[i]]);
  93. }
  94. await conn.commit();
  95. } catch (error) {
  96. await conn.rollback();
  97. throw error;
  98. }
  99. return true;
  100. }
  101. async getUserPermissionByToken(query) {
  102. let authToken = query.token;
  103. if (!authToken) {
  104. throw "token为空";
  105. }
  106. let options = this.app.config.loginAdmin;
  107. let ctx = this.ctx;
  108. let ret = await ctx.jwtVerify(authToken, options.secret, options.options);
  109. if( !ret.verify )
  110. {
  111. throw "Token失效请重新登录";
  112. }
  113. let info = ret.message;
  114. let id = info.id;
  115. let permissions = [];
  116. let mysql = this.app.mysql;
  117. if( info.type == 2 ) {//参训人员自带'参训人员role_cxry'和'导调权限role_teacher'
  118. let role_code = info.role == 1? 'role_teacher' : 'role_cxry';
  119. let sql = `
  120. select p.* from admin_permission p
  121. where (
  122. exists(
  123. select a.id from admin_role_permission a
  124. join admin_role b on a.role_id = b.id
  125. where p.id=a.permission_id and b.role_code='${role_code}'
  126. )
  127. ) order by sort_no ASC`
  128. permissions = await mysql.query(sql);
  129. console.log( permissions );
  130. } else {
  131. //更加用户ID获取permission
  132. let sql = `
  133. select p.* from admin_permission p
  134. where (
  135. exists(
  136. select a.id from admin_role_permission a
  137. join admin_role b on a.role_id = b.id
  138. join admin_user_role c on c.role_id = b.id
  139. join admin_user d on d.id=c.user_id
  140. where p.id=a.permission_id and d.id=${id}
  141. )
  142. ) order by sort_no ASC`
  143. permissions = await mysql.query(sql);
  144. }
  145. function urlToRouteName( url ) {
  146. if( !url ) return "";
  147. if( url[0] == '/') {
  148. url = url.substr(1);
  149. }
  150. url = url.replace("/","-");
  151. url = url.replace(":","@");
  152. return url;
  153. }
  154. function createPermission( data ) {
  155. let ret = {};
  156. if( data.menu_type == 2) return null;
  157. console.log( data.menu_type );
  158. if( data.menu_type != 0 && data.menu_type !=1) return ret;
  159. ret.id = data.id;
  160. ret.route = data.is_route?1:0;
  161. ret.path = data.url;
  162. ret.name = data.component_name?data.component_name: urlToRouteName(data.url);
  163. ret.hidden = data.hidden?true:false;
  164. ret.alwaysShow = data.always_show?true:false;
  165. ret.component = data.component;
  166. ret.redirect = data.redirect;
  167. ret.meta = {
  168. keepAlive: data.keep_alive?true: false,
  169. internalOrExternal: data.internal_or_external?true:false,
  170. title: data.name,
  171. icon: data.icon?data.icon:null,
  172. }
  173. return ret;
  174. }
  175. let tree = ctx.fmt2Tree(permissions, node=>{ return createPermission(node) });
  176. return {menu: tree};
  177. }
  178. async queryallrole() {
  179. let mysql = this.app.mysql;
  180. let ret = await mysql.query("select * from admin_role");
  181. ret = ret.map(e=>{
  182. // e.roleCode = e.role_code;
  183. // e.roleName = e.role_name;
  184. // e.description = e.desc;
  185. return {id:e.id, roleCode:e.role_code, roleName: e.role_name, description:e.desc};
  186. });
  187. return ret;
  188. }
  189. }
  190. module.exports = SysPermService;