yunjiuye.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const moment = require('moment');
  5. class YunjiuyeController extends Controller {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.service = this.ctx.service.yunjiuye;
  9. this.yjyLoginKey = this.app.config.yjyLoginKey;
  10. }
  11. // http://127.0.0.1:7001/api/train/y/login?params=
  12. async login() {
  13. const pstr = this.ctx.request.querystring;
  14. let params = pstr.replace('params=', '');
  15. if (!params) throw new BusinessError(ErrorCode.BADPARAM, '缺少登录参数');
  16. params = this.service.decryptData(params);
  17. if (!params) throw new BusinessError(ErrorCode.BADPARAM, '参数解析失败');
  18. const { code, org, type, logoutUrl } = params;
  19. this.service.getDomain(logoutUrl);
  20. const res = await this.service.checkUserIsConnect(params);
  21. const { toBindPage, toDealTokenPage } = this.app.config.yunjiuye;
  22. // 没关联,转到登录页面
  23. if (!res) {
  24. this.ctx.redirect(`${toBindPage}?code=${code}&org=${org}&ytype=${type}`);
  25. } else {
  26. // 已关联,转到加载token页面
  27. const token = await this.service.userToken(res);
  28. this.ctx.redirect(`${toDealTokenPage}?token=${token}`);
  29. }
  30. }
  31. async userBind() {
  32. const body = this.ctx.request.body;
  33. // 成功返回suid
  34. const res = await this.service.userBind(body);
  35. // 请求token,返回token
  36. const token = await this.service.userToken(res);
  37. this.ctx.ok({ data: token });
  38. }
  39. async logout() {
  40. const query = this.ctx.query;
  41. const { id } = query;
  42. const data = await this.service.getBindInfo(id);
  43. const encDataStr = this.service.encryptData(data);
  44. const url = await this.service.yLogout(encDataStr);
  45. this.ctx.ok({ data: url });
  46. }
  47. // loading页设置完用户后走该接口,进行用户登录的登记
  48. async loginSuccess() {
  49. const body = this.ctx.request.body;
  50. if (!body.id) throw new BusinessError(ErrorCode.BADPARAM, '缺少参数');
  51. const redis = this.app.redis;
  52. await redis.set(`${this.yjyLoginKey}${body.id}`, moment().format('YYYY-MM-DD HH:mm:ss'));
  53. this.ctx.ok();
  54. }
  55. // 云就业主动注销时的函数
  56. async toLogout() {
  57. const pstr = this.ctx.request.querystring;
  58. let params = pstr.replace('params=', '');
  59. if (!params) throw new BusinessError(ErrorCode.BADPARAM, '缺少注销参数');
  60. params = this.service.decryptData(params);
  61. if (!params) throw new BusinessError(ErrorCode.BADPARAM, '参数解析失败');
  62. const res = await this.service.checkUserIsConnect(params);
  63. if (!res) throw new BusinessError(ErrorCode.USER_NOT_BIND, '用户未绑定,无法检测登录状态');
  64. else {
  65. const redis = this.app.redis;
  66. const loginTime = await redis.get(`${this.yjyLoginKey}${res}`);
  67. if (loginTime) {
  68. await redis.del(`${this.yjyLoginKey}${res}`);
  69. this.ctx.ok();
  70. } else throw new BusinessError(ErrorCode.NOT_LOGIN, '用户未登录');
  71. }
  72. }
  73. }
  74. module.exports = YunjiuyeController;