12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- 'use strict';
- const Service = require('egg').Service;
- const assert = require('assert');
- const _ = require('lodash');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { ObjectId } = require('mongoose').Types;
- class YunjiuyeService extends Service {
- // 检查用户是否和云就业有关联
- // 没关联就直接重定向到关联页
- // 有关联就直接去登录
- async checkUserIsConnect({ code, org }) {
- assert(code, '缺少用户标识');
- assert(org, '缺少机构');
- const yjycModel = this.ctx.model.Yjyconnect;
- const result = await yjycModel.findOne({ ycode: code, yorg: org }).lean();
- if (!result) return false;
- return result.suid;
- }
- // 用户与云就业关联
- async userBind({ type, mobile, passwd, code, org }) {
- assert(mobile, '登录账户不能为空');
- assert(passwd, '密码不能为空');
- assert(type, '用户角色不能为空');
- const userModel = this.ctx.model.User;
- const res = await userModel.findOne({ mobile, type }, '+passwd');
- if (!res) {
- throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- }
- if (res.passwd.secret !== passwd) {
- throw new BusinessError(ErrorCode.BAD_PASSWORD);
- }
- const yjycModel = this.ctx.model.Yjyconnect;
- const obj = { ycode: code, yorg: org, suid: ObjectId(res._id).toString() };
- const isHas = await yjycModel.count({ suid: obj.suid });
- if (isHas > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '账户已绑定,无需再次绑定');
- await yjycModel.create(obj);
- return obj.suid;
- }
- // 用户id获取用户信息,登录获取token返回
- async userToken(id) {
- assert(id, '缺少双困生平台用户标识');
- const userModel = this.ctx.model.User;
- const userService = this.ctx.service.login;
- const user = await userModel.findById(id);
- if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- const token = await userService.createJwt(user);
- return token;
- }
- }
- module.exports = YunjiuyeService;
|