yunjiuye.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const assert = require('assert');
  4. const _ = require('lodash');
  5. const { BusinessError, ErrorCode } = require('naf-core').Error;
  6. const { ObjectId } = require('mongoose').Types;
  7. class YunjiuyeService extends Service {
  8. // 检查用户是否和云就业有关联
  9. // 没关联就直接重定向到关联页
  10. // 有关联就直接去登录
  11. async checkUserIsConnect({ code, org }) {
  12. assert(code, '缺少用户标识');
  13. assert(org, '缺少机构');
  14. const yjycModel = this.ctx.model.Yjyconnect;
  15. const result = await yjycModel.findOne({ ycode: code, yorg: org }).lean();
  16. if (!result) return false;
  17. return result.suid;
  18. }
  19. // 用户与云就业关联
  20. async userBind({ type, mobile, passwd, code, org }) {
  21. assert(mobile, '登录账户不能为空');
  22. assert(passwd, '密码不能为空');
  23. assert(type, '用户角色不能为空');
  24. const userModel = this.ctx.model.User;
  25. const res = await userModel.findOne({ mobile, type }, '+passwd');
  26. if (!res) {
  27. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  28. }
  29. if (res.passwd.secret !== passwd) {
  30. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  31. }
  32. const yjycModel = this.ctx.model.Yjyconnect;
  33. const obj = { ycode: code, yorg: org, suid: ObjectId(res._id).toString() };
  34. const isHas = await yjycModel.count({ suid: obj.suid });
  35. if (isHas > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '账户已绑定,无需再次绑定');
  36. await yjycModel.create(obj);
  37. return obj.suid;
  38. }
  39. // 用户id获取用户信息,登录获取token返回
  40. async userToken(id) {
  41. assert(id, '缺少双困生平台用户标识');
  42. const userModel = this.ctx.model.User;
  43. const userService = this.ctx.service.login;
  44. const user = await userModel.findById(id);
  45. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  46. const token = await userService.createJwt(user);
  47. return token;
  48. }
  49. }
  50. module.exports = YunjiuyeService;