'use strict'; const Service = require('egg').Service; const assert = require('assert'); const crypto = require('crypto'); class UserService extends Service { async login({ acct, password }) { assert(acct, '帐号不存在'); assert(password, '密码不存在'); const { AdminUser: model } = this.ctx.model; const hash = crypto.createHmac('sha256', this.app.config.userSecret); const pwd = hash.update(password).digest('hex'); try { const res = await model.find({ acct }); if (res.state !== 0) { return { errmsg: '用户状态异常', errcode: -2003 }; } if (res.password !== pwd) { return { errmsg: '密码错误', errcode: -2003 }; } const token = this.app.jwt.sign(res, this.app.config.jwt.secret); return { errmsg: '', errcode: 0, token }; } catch (error) { throw new Error({ errcode: -2001, errmsg: '登录失败' }); } } } module.exports = UserService;