123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- 'use strict';
- const ManagerController = require('./manager.js');
- /**
- * [exports {:this}控制器]
- * @type {[type]}
- */
- module.exports = class RolesController extends ManagerController {
- /**
- * [actionModel 当前操作模型 必填]
- * @author szjcomo
- * @date 2020-10-23
- * @return {[type]} [description]
- */
- get useModel() {
- return this.ctx.model.Roles;
- }
- /**
- * [createValidate 添加数据验证提示 必填]
- * @author szjcomo
- * @date 2020-10-23
- * @return {[type]} [description]
- */
- get createValidate() {
- let that = this;
- return {
- role_name:that.ctx.rules.name('角色名称').required().max_length(19).min_length(2),
- role_desc:that.ctx.rules.default('').max_length(255),
- create_time:that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required(),
- admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).number()
- };
- }
- /**
- * [roleAuthValidate 角色授权]
- * @author szjcomo
- * @date 2020-10-23
- * @return {[type]} [description]
- */
- get roleAuthValidate() {
- let that = this;
- return {
- role_id:that.ctx.rules.name('角色ID').required().number(),
- access_id:that.ctx.rules.name('节点ID').required()
- };
- }
- /**
- * [select 重写查询接口]
- * @author szjcomo
- * @date 2020-11-02
- * @return {[type]} [description]
- */
- async select() {
- let that = this;
- try {
- let data = await that.ctx.validate(that.selectValidate,await that.ctx.getParse());
- let seq = that.app.Sequelize;
- let options = {
- offset:(data.page - 1) * data.limit,
- limit:data.limit,
- include:[{model:that.app.model.AdminUser,as:'admin_user',attributes:[]}],
- attributes:{
- include:[[seq.col('admin_user.username'),'username']],
- exclude:['admin_id']
- },
- order:[['role_id','desc']]
- };
- let selectBean = that.app.comoBean.instance(data,options);
- let result = await that.service.manager.select(selectBean,that.useModel,'角色查询失败,请稍候重试',true,true);
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
- } catch(err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- /**
- * [delete 重写删除接口]
- * @author szjcomo
- * @date 2020-11-02
- * @return {[type]} [description]
- */
- async delete() {
- let that = this;
- let transaction;
- try {
- let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse());
- transaction = await that.app.model.transaction();
- let deleteBean = that.app.comoBean.instance(data,{where:{role_id:data.role_id},transaction:transaction});
- deleteBean.addCall(that.deleteBeanBefore);
- let result = await that.service.manager.delete(deleteBean,that.useModel,'角色删除失败,请稍候重试');
- await transaction.commit();
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
- } catch(err) {
- if(transaction) await transaction.rollback();
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- /**
- * [deleteBeanBefore 删除管理员前置检查]
- * @author szjcomo
- * @date 2020-11-02
- * @param {[type]} app [description]
- * @return {[type]} [description]
- */
- async deleteBeanBefore(app) {
- let that = this;
- let data = that.getData();
- let options = that.getOptions();
- let result = await app.model.AdminUser.findOne({where:{role_id:data.role_id},attributes:['admin_id']});
- if(result) throw new Error('当前角色还有管理员在使用,请先处理后再进行删除');
- await app.model.AccessRole.destroy({where:{role_id:data.role_id},transaction:options.transaction});
- }
- /**
- * [roleAuth 给角色授权]
- * @author szjcomo
- * @date 2020-10-23
- * @return {[type]} [description]
- */
- async roleAuth() {
- let that = this;
- let transaction;
- try {
- let data = await that.ctx.validate(that.roleAuthValidate,await that.ctx.postParse());
- transaction = await that.app.model.transaction();
- let modelBean = that.app.comoBean.instance(data,{transaction:transaction});
- modelBean.addCall(that.roleAuthBeforeHandle);
- let result = await that.app.comoBean.create(modelBean,that.app.model.AccessRole,'角色授权失败,请稍候重试');
- await transaction.commit();
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
- } catch(err) {
- if(transaction) await transaction.rollback();
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- /**
- * [roleAuthBeforeHandle 角色授权前置检测]
- * @author szjcomo
- * @date 2020-10-23
- * @param {[type]} app [description]
- * @return {[type]} [description]
- */
- async roleAuthBeforeHandle(app) {
- let that = this;
- let data = that.getData();
- let options = that.getOptions();
- let info = await app.model.AccessRole.findOne({where:{role_id:data.role_id},attributes:['role_id']});
- let deleteRole = true;
- if(info) deleteRole = await app.model.AccessRole.destroy({where:{role_id:data.role_id},transaction:options.transaction});
- if(!deleteRole) throw new Error('角色授权前删除失败,请联系管理员处理!');
- }
- /**
- * [getRoleAuth 获取角色权限]
- * @author szjcomo
- * @date 2020-10-23
- * @return {[type]} [description]
- */
- async getRoleAuth() {
- let that = this;
- try {
- let role_id = await that.ctx.getParse('role_id',0,Number);
- if(role_id <= 0) throw new Error('角色参数错误,请检查是否正确');
- let result = await that.service.manager.getRoleAuth(role_id);
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
- } catch(err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- }
|