'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; console.log('--------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'); console.log(passwd); console.log(role); console.log(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 = ''; // 将用户输入的密码进行加密并与查询到的用户数据密码相比对 const pasw = await this.createJwtPwd(passwd); if (flag) { // 手机 _user = await this.model.findOne({ phone, pasw }); } else { // 房间号 _user = await this.model.findOne({ room_id, pasw }); } if (_user === '') { throw new BusinessError(ErrorCode.USER_NOT_EXIST); // throw new BusinessError(ErrorCode.BAD_PASSWORD); } // 将用户输入的密码进行加密并与查询到的用户数据密码相比对 // const pas = await this.createJwtPwd(passwd); // // 如果两个密码不一致抛出异常 // if (pas !== _user.passwd.secret) { // throw new BusinessError(ErrorCode.BAD_PASSWORD); // } // if (role === '3') { // console.log('vip用户'); // } else if (role === '8') { // console.log('展会vip用户'); // } else { // console.log('无此展会'); // } // if (_user.role === '3' || _user.role === '8') { // 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); // } // } // 取出用户的类型,根据用户类型返回相应信息 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); return { key }; } // 创建登录Token async createJwtPwd(password) { const { secret } = 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;