|
@@ -0,0 +1,132 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+const assert = require('assert');
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const jwt = require('jsonwebtoken');
|
|
|
+const uuid = require('uuid');
|
|
|
+
|
|
|
+class LoginService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'login');
|
|
|
+ this.model = this.ctx.model.Dock;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async login(data) {
|
|
|
+ const { room_phone, passwd, role } = data;
|
|
|
+ const phone = room_phone;
|
|
|
+ const room_id = room_phone;
|
|
|
+
|
|
|
+ let user = await this.model.findOne({ phone, role });
|
|
|
+ const flag = true;
|
|
|
+ if (!user) {
|
|
|
+ user = await this.model.findOne({ room_id, role });
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!user) {
|
|
|
+ throw new BusinessError(ErrorCode.USER_NOT_EXIST);
|
|
|
+ }
|
|
|
+ let _user = '';
|
|
|
+ if (flag) {
|
|
|
+
|
|
|
+ _user = await this.model.findOne({ phone }, '+passwd');
|
|
|
+ } else {
|
|
|
+
|
|
|
+ _user = await this.model.findOne({ room_id }, '+passwd');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ const pas = await this.createJwtPwd(passwd);
|
|
|
+
|
|
|
+ if (pas !== _user.passwd.secret) {
|
|
|
+ throw new BusinessError(ErrorCode.BAD_PASSWORD);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ const state = uuid();
|
|
|
+ const key = `free:auth:state:${state}`;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ const token = await this.createJwt(user);
|
|
|
+ await this.app.redis.set(key, token, 'EX', 60 * 60 * 24);
|
|
|
+ return { key };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async createJwtPwd(password) {
|
|
|
+ const { secret } = this.config.jwt;
|
|
|
+ const token = await jwt.sign(password, secret);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async createJwt({ id, name, uid, phone, role, menus, remark, openid, deptid, deptname, pid, code }) {
|
|
|
+ const { secret, expiresIn = '1d', issuer = role } = this.config.jwt;
|
|
|
+ const subject = phone;
|
|
|
+ const res = { uid: id, userid: uid, name, phone, role, menus, openid, remark, deptid, deptname, pid, code };
|
|
|
+ const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async token({ key }) {
|
|
|
+ assert(key, 'key不能为空');
|
|
|
+ const token = await this.app.redis.get(key);
|
|
|
+ if (!token) {
|
|
|
+ throw new BusinessError(ErrorCode.SERVICE_FAULT, 'token已经过期');
|
|
|
+ }
|
|
|
+ return { token };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ async destroy({ key }) {
|
|
|
+ const res = await this.app.redis.del(key);
|
|
|
+ console.log(res);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+module.exports = LoginService;
|