'use strict'; const ManagerController = require('./manager.js'); /** * [exports 后台管理员控制器] * @type {[type]} */ module.exports = class AdminUser extends ManagerController { /** * [useModel 使用模型] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ get useModel() { const that = this; return that.app.model.AdminUser; } /** * [createValidate 添加管理员验证器] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ get createValidate() { const that = this; return { username: that.ctx.rules.name('管理员名称').required().min_length(2) .max_length(20) .extend(async (field, value) => { const result = await that.ctx.model.AdminUser.findOne({ where: { username: value }, attributes: [ 'admin_id' ] }); if (result) throw new Error('管理员名称已经存在,无法重复添加'); }), password: that.ctx.rules.name('管理员密码').required().min_length(6) .max_length(20), role_id: that.ctx.rules.name('管理员角色').required().number(), admin_status: that.ctx.rules.default(1).required().number(), login_time: that.ctx.rules.default('').required(), admin_code: that.ctx.rules.default(that.app.szjcomo.mt_rand(1000, 9999)).required(), create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required(), action_user: that.ctx.rules.default(that.service.manager.ActionAdminUserId()).required().number(), }; } /** * [selectValidate 查询验证器] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ get selectValidate() { const that = this; return { username: that.ctx.rules.default('').required(), role_id: that.ctx.rules.default('').required(), limit: that.ctx.rules.default(20).number(), page: that.ctx.rules.default(1).number(), admin_id: that.ctx.rules.default(0).number(), }; } /** * [updateValidate 数据更新功能] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ get updateValidate() { const that = this; return { admin_id: that.ctx.rules.name('管理员ID').required().number(), action_user: that.ctx.rules.default(that.service.manager.ActionAdminUserId()).required(), update_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s')).required(), }; } /** * [create 添加数据实现] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async create() { const that = this; try { const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse()); const password = that.app.szjcomo.MD5(`${data.password}${data.admin_code}`); data.password = password; const createBean = that.app.comoBean.instance(data); const result = await that.service.manager.create(createBean, 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)); } } /** * [select 管理员查询自定义实现] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async select() { const that = this; try { const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse()); const seq = that.app.Sequelize; let options = {}; if (data.admin_id > 0) { options.where = {}; options.where.admin_id = data.admin_id; } else { options = { offset: (data.page - 1) * data.limit, limit: data.limit, include: [{ model: that.app.model.Roles, as: 'role', attributes: [] }], attributes: { include: [[ seq.col('role.role_name'), 'role_name' ]], exclude: [ 'password', 'action_user', 'admin_code', 'login_count' ], }, order: [[ 'admin_id', 'desc' ]], }; options.where = { role_id: { [seq.Op.gt]: 0 } }; if (data.username) options.where.username = { [seq.Op.regexp]: `${data.username}` }; if (data.role_id) options.where.role_id = Number(data.role_id); } const selectBean = that.app.comoBean.instance(data, options); const count = !(data.admin_id > 0); const result = await that.service.manager.select(selectBean, that.useModel, '管理员查询失败,请稍候重试', count, count); 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() { const that = this; try { const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse()); const updateBean = that.app.comoBean.instance(data, { where: { admin_id: data.admin_id } }); updateBean.addCall(that.updateBeanBefore); const 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)); } } /** * [updateBeanBefore 更新前置操作] * @author szjcomo * @date 2020-11-02 * @param {[type]} app [description] * @return {[type]} [description] */ async updateBeanBefore(app) { const that = this; const data = that.getData(); if (data.hasOwnProperty('role_id') && data.role_id == 0) throw new Error('管理员角色ID不能为空,请检查'); if (data.username && (data.username.length < 2 || data.username.length > 20)) throw new Error('管理员账号名称长度不符号要求,长度最小为2个字符,最大20个字符'); if (data.username) { const user = await app.model.AdminUser.findOne({ where: { username: data.username }, attributes: [ 'admin_id' ] }); if (user && (data.admin_id != user.admin_id)) throw new Error('管理员账号已经被占用,请重新改一个管理员账号名称吧!'); } if (data.password) { if (data.password.length < 6) throw new Error('密码太弱,请重新设置,密码最小长度需要6位,最大20位长度'); const userinfo = await app.model.AdminUser.findOne({ where: { admin_id: data.admin_id }, raw: true, attributes: [ 'admin_code', 'password' ], }); if (!userinfo) throw new Error('管理员信息不存在,请检查'); if (userinfo.password != data.password) { const password = app.szjcomo.MD5(`${data.password}${userinfo.admin_code}`); data.password = password; } else { delete data.password; } } that.setData(data); } };