yunjiuye.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. class YunjiuyeController extends Controller {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.service = this.ctx.service.yunjiuye;
  8. }
  9. // http://127.0.0.1:7001/api/train/y/login?code=中心&org=0
  10. async login() {
  11. this.service.getDomain();
  12. const pstr = this.ctx.request.querystring;
  13. let params = pstr.replace('params=', '');
  14. if (!params) throw new BusinessError(ErrorCode.BADPARAM, '缺少登录参数');
  15. params = this.service.decryptData(params);
  16. if (!params) throw new BusinessError(ErrorCode.BADPARAM, '参数解析失败');
  17. const { code, org, type } = params;
  18. const res = await this.service.checkUserIsConnect(params);
  19. const { toBindPage, toDealTokenPage } = this.app.config.yunjiuye;
  20. // 没关联,转到登录页面
  21. if (!res) {
  22. this.ctx.redirect(`${toBindPage}?code=${code}&org=${org}&ytype=${type}`);
  23. } else {
  24. // 已关联,转到加载token页面
  25. const token = await this.service.userToken(res);
  26. this.ctx.redirect(`${toDealTokenPage}?token=${token}`);
  27. }
  28. }
  29. async userBind() {
  30. const body = this.ctx.request.body;
  31. // 成功返回suid
  32. const res = await this.service.userBind(body);
  33. // 请求token,返回token
  34. const token = await this.service.userToken(res);
  35. this.ctx.ok({ data: token });
  36. }
  37. async logout() {
  38. const query = this.ctx.query;
  39. const { id } = query;
  40. const data = await this.service.getBindInfo(id);
  41. const encDataStr = this.service.encryptData(data);
  42. await this.service.yLogout(encDataStr);
  43. this.ctx.ok();
  44. }
  45. }
  46. module.exports = YunjiuyeController;