'use strict'; const assert = require('assert'); const _ = require('lodash'); 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.User; this.rmodel = this.ctx.model.Role; } // 用户登录 async login(data) { const { phone, passwd, role } = data; // 根据用户输入的手机号查询其他用户表中是否存在相应数据 let user = await this.model.findOne({ phone, role }); // 增设使用code模式登陆的判断变量 let is_code = false; // 如果用户不存在抛出异常 if (!user) { // 添加code作为登录的方式 user = await this.model.findOne({ code: phone, role }); if (!user) { throw new BusinessError(ErrorCode.USER_NOT_EXIST); } else is_code = true; } const pdata = {}; if (is_code) pdata.code = phone; else pdata.phone = phone; const _user = await this.model.findOne(pdata, '+passwd'); // 将用户输入的密码进行加密并与查询到的用户数据密码相比对 const pas = await this.createJwtPwd(passwd); // 如果两个密码不一致抛出异常 if (pas !== _user.passwd.secret) { throw new BusinessError(ErrorCode.BAD_PASSWORD); } if (_user.role === '4' || _user.role === '5') { const url = 'http://127.0.0.1:9004/api/market/user/' + _user.uid; const marketuser = await this.ctx.curl(url, { method: 'get', headers: { 'content-type': 'application/json', }, dataType: 'json', }); if (marketuser.data.data.status !== '1') { throw new BusinessError(ErrorCode.ACCESS_DENIED); } } else if (_user.role === '6') { const url = 'http://127.0.0.1:9004/api/market/expertsuser/' + _user.uid; const expertsuser = await this.ctx.curl(url, { method: 'get', headers: { 'content-type': 'application/json', }, dataType: 'json', }); if (expertsuser.data.data.status !== '1') { throw new BusinessError(ErrorCode.ACCESS_DENIED); } } else if (_user.role === '8') { const url = 'http://127.0.0.1:9008/api/live/dock/getdock/' + _user.id; const vipuser = await this.ctx.curl(url, { method: 'post', headers: { 'content-type': 'application/json', }, dataType: 'json', }); const r = _.get(vipuser, 'data.res'); if (r) { if (_.isArray(r)) { const rh = _.head(r); if (rh) user.remark = rh.id; } else if (_.isObject(r)) { user.remark = _.get(r, 'id'); } } } // 取出用户的类型,根据用户类型返回相应信息 const state = uuid(); const key = `free:auth:state:${state}`; const _menus = []; for (const elm of user.menus) { const _menu = await this.rmodel.findById({ _id: ObjectId(elm) }); if (_menu) { _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url }); } } user.menus = JSON.stringify(_menus); const token = await this.createJwt(user); await this.app.redis.set(key, token, 'EX', 60 * 60 * 24); let logFlag = false; let dockList = []; // "4568"如果是456的需要去dock里面查有没有这个人(是否在apply),2,8必存 if (_user.role === '4' ||_user.role === '5' ||_user.role === '6' ||_user.role === '8') { const applydata = { user_id: user.id }; const url = 'http://127.0.0.1:9008/api/live/getapply'; const applyflag = await this.ctx.curl(url, { method: 'post', headers: { 'content-type': 'application/json', }, dataType: 'json', data: JSON.stringify(applydata), }); // 如果有值true,说明登录者是申请用户,反之,false if (applyflag.data.res.length > 0) { logFlag = true; dockList = applyflag.data.res; }else { logFlag = true; } } else if (_user.role === '2') { logFlag = true; } if (logFlag) { if (dockList.length > 0) { for (const dock of dockList) { const operationlogdata = { dockid: dock.id, login_name: user.name, login_role: user.role, type: '0', operation_edit: '登录', }; const url = 'http://127.0.0.1:9004/api/market/operationlog/'; const operationlog = await this.ctx.curl(url, { method: 'post', headers: { 'content-type': 'application/json', }, dataType: 'json', data: JSON.stringify(operationlogdata), }); } } else { const operationlogdata = { login_id: user.id, login_name: user.name, login_role: user.role, type: '0', operation_edit: '登录', }; const url = 'http://127.0.0.1:9004/api/market/operationlog/'; const operationlog = await this.ctx.curl(url, { method: 'post', headers: { 'content-type': 'application/json', }, dataType: 'json', data: JSON.stringify(operationlogdata), }); } } return { key }; } // 创建登录Token async createJwtPwd(password) { const { secret, expiresIn, issuer } = this.config.jwt; const token = await jwt.sign(password, secret); return token; } // 创建登录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; } // 取得redis内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;