| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- 'use strict';
- const ManagerController = require('./manager.js');
- /**
- * [exports 用户登录退出控制器]
- * @type {[type]}
- */
- module.exports = class LoginController extends ManagerController {
- /**
- * [dologinValidate 登录验证器]
- * @author szjcomo
- * @date 2020-11-02
- * @return {[type]} [description]
- */
- get dologinValidate() {
- const that = this;
- return {
- username: that.ctx.rules.name('管理员账号').required().notEmpty()
- .trim(),
- password: that.ctx.rules.name('管理员密码').required().notEmpty()
- .trim(),
- mobile: that.ctx.rules.default(0).number(),
- verify_code: that.ctx.rules.name('验证码').required().trim()
- .notEmpty()
- .number(),
- };
- }
- /**
- * [dologin 后台管理执行登录功能]
- * @author szjcomo
- * @date 2020-11-02
- * @return {[type]} [description]
- */
- async dologin() {
- const that = this;
- try {
- const data = await that.ctx.validate(that.dologinValidate, await that.ctx.postParse());
- data.login_ip = that.ctx.request.ip;
- const verify_code = that.ctx.session.verify_code;
- if (process.env.NODE_ENV != 'development') {
- if (!data.mobile) {
- if (verify_code != data.verify_code) throw new Error('验证码输入错误,请检查...');
- }
- }
- const loginBean = that.app.comoBean.instance(data, { where: { username: data.username } });
- loginBean.addCall(that.dologinBeanBefore);
- loginBean.addCall(that.dologinBeanAfter, 'after');
- const result = await that.service.manager.select(loginBean, that.app.model.AdminUser, '管理员账号查询失败,请检查账号或密码是否正确');
- result.mobile = data.mobile;
- const token = that.app.jwt.sign(result, that.app.config.jwt.secret, { expiresIn: '10h' });
- const menus = await that.service.manager.getRoleAuth(result.role_id, true, data.mobile);
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', {
- admin_user: result,
- menus: that.app.szjcomo.base64_encode(that.app.szjcomo.json_encode(menus)),
- token, admin_config: await that.service.manager.adminConfig(),
- }, false));
- } catch (err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- /**
- * [dologinBeanBefore 登录前数据处理]
- * @author szjcomo
- * @date 2020-11-02
- * @param {[type]} app [description]
- * @return {[type]} [description]
- */
- async dologinBeanBefore(app) {
- const that = this;
- const options = that.getOptions();
- const data = that.getData();
- const result = await app.model.AdminUser.findOne({
- where: { username: data.username }, raw: true,
- attributes: [ 'admin_code', 'admin_id', 'admin_status' ],
- });
- if (!result) throw new Error('管理员账号是否有误,请检查...');
- if (!result.admin_status) throw new Error('您的账号已经被禁用,请联系管理员处理');
- const password = app.szjcomo.MD5(`${data.password}${result.admin_code}`);
- if (!options.where) options.where = {};
- options.where.password = password;
- data.admin_id = result.admin_id;
- options.attributes = [ 'username', 'admin_id', 'admin_status', 'login_time', 'login_count', 'login_ip', 'role_id' ];
- options.raw = true;
- that.setOptions(options);
- that.setData(data);
- }
- /**
- * [dologinBeanAfter 登录成功后需更新登录信息]
- * @author szjcomo
- * @date 2020-11-02
- * @param {[type]} app [description]
- * @param {[type]} result [description]
- * @return {[type]} [description]
- */
- async dologinBeanAfter(app, result) {
- const that = this;
- const data = that.getData();
- const login_time = app.szjcomo.date('Y-m-d H:i:s');
- const update_data = { login_ip: data.login_ip, login_time, login_count: app.Sequelize.literal('login_count+1') };
- const res = await app.model.AdminUser.update(update_data, { where: { admin_id: data.admin_id } });
- if (!res[0]) app.logger.error('管理员登录信息更新失败,需要管理员处理...');
- }
- /**
- * [verify 登录验证码]
- * @author szjcomo
- * @date 2020-11-02
- * @return {[type]} [description]
- */
- async verify() {
- const that = this;
- try {
- const result = that.service.captcha.get();
- that.ctx.session.verify_code = result.text;
- that.ctx.response.type = 'image/svg+xml';
- return that.ctx.body = result.data;
- } catch (err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- /**
- * [manager 控制台数据]
- * @author szjcomo
- * @date 2020-11-02
- * @return {[type]} [description]
- */
- async manager() {
- const that = this;
- try {
- const result = await that.service.manager.console();
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
- } catch (err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- };
|