'use strict'; const ManagerController = require('./manager.js'); /** * [exports 权限节点控制骂] * @type {[type]} */ module.exports = class AccesssController extends ManagerController { /** * [actionModel 当前操作模型 必填] * @author szjcomo * @date 2020-10-23 * @return {[type]} [description] */ get useModel() { return this.app.model.Accesss; } /** * [createValidate 添加数据验证提示 必填] * @author szjcomo * @date 2020-10-23 * @return {[type]} [description] */ get createValidate() { let that = this; return { access_name:that.ctx.rules.name('节点名称').required().min_length(2).max_length(20), router_name:that.ctx.rules.name('路由名称').required().min_length(2).max_length(100), pid:that.ctx.rules.default(0).number(), router_path:that.ctx.rules.default('').required(), admin_id:that.ctx.rules.default(that.service.manager.ActionAdminUserId()).number(), access_icon:that.ctx.rules.default('el-icon-menu').required(), is_nav:that.ctx.rules.default(0).number(), access_sort:that.ctx.rules.default(1).number(), access_status:that.ctx.rules.default(1).number(), create_time:that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).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 selectBean = that.app.comoBean.instance(data,{ where:{},offset:(data.page - 1) * data.limit,limit:data.limit }); selectBean.addCall(that.selectBeanBefore); 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)); } } /** * [selectBeanBefore 查询前置操作] * @author szjcomo * @date 2020-11-02 * @param {[type]} app [description] * @return {[type]} [description] */ async selectBeanBefore(app) { let that = this; let seq = app.Sequelize; let options = that.getOptions(); let data = that.getData(); options.raw = true; if(data.access_id == 0) options.order = [['access_sort','asc'],['access_id','asc']]; options.include = [ {model:app.model.AdminUser,as:'admin_user',attributes:[]} ]; options.attributes = { include:[ [seq.col('admin_user.username'),'username'] ], exclude:['admin_id'] }; that.setOptions(options); } /** * [create 重写添加] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async create() { let that = this; try { let data = await that.ctx.validate(that.createValidate,await that.ctx.postParse()); let level = await that.service.manager.getLevel(data.pid,that.useModel,{ where:{access_id:data.pid},attributes:['level'] }); data.level = level; if(that.app.szjcomo.empty(data.access_icon)) data.access_icon = 'el-icon-menu'; let createBean = that.app.comoBean.instance(data); let result = await that.service.manager.create(createBean,that.useModel,'权限节点添加失败,请稍候重试'); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { console.log(err) return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [update 重写更新] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async update() { let that = this; try { let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse()); data.admin_id = that.service.manager.ActionAdminUserId(); if(data.hasOwnProperty('pid')) { let level = await that.service.manager.getLevel(data.pid,that.useModel,{ where:{access_id:data.pid},attributes:['level'] }); data.level = level; } if(data.hasOwnProperty('access_icon')) { data.access_icon = await that.ctx.anyParse('access_icon','el-icon-menu',(value)=>{return value;}); } let updateBean = that.app.comoBean.instance(data,{where:{access_id:data.access_id}}); let result = await that.service.manager.update(updateBean,that.useModel,'权限节点更新失败,请稍候重试'); 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; try { let data = await that.ctx.validate(that.pkValidate,await that.ctx.anyParse()); let deleteBean = that.app.comoBean.instance(data,{where:{access_id:data.access_id}}); deleteBean.addCall(that.deleteBeanBefore); let result = await that.service.manager.delete(deleteBean,that.useModel,'权限节点删除失败,请稍候重试'); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { 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 info = await app.model.Accesss.findOne({where:{pid:data.access_id},attributes:['access_id']}); if(info) throw new Error('当前节点下还有子节点,无法删除,如果需要删除,请先删除子节点后再删除本节点数据'); } }