12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- 'use strict';
- const Controller = require('egg').Controller;
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class YunjiuyeController extends Controller {
- constructor(ctx) {
- super(ctx);
- this.service = this.ctx.service.yunjiuye;
- }
- // http://127.0.0.1:7001/api/train/y/login?code=中心&org=0
- async login() {
- this.service.getDomain();
- const pstr = this.ctx.request.querystring;
- let params = pstr.replace('params=', '');
- if (!params) throw new BusinessError(ErrorCode.BADPARAM, '缺少登录参数');
- params = this.service.decryptData(params);
- if (!params) throw new BusinessError(ErrorCode.BADPARAM, '参数解析失败');
- const { code, org, type } = params;
- const res = await this.service.checkUserIsConnect(params);
- const { toBindPage, toDealTokenPage } = this.app.config.yunjiuye;
- // 没关联,转到登录页面
- if (!res) {
- this.ctx.redirect(`${toBindPage}?code=${code}&org=${org}&ytype=${type}`);
- } else {
- // 已关联,转到加载token页面
- const token = await this.service.userToken(res);
- this.ctx.redirect(`${toDealTokenPage}?token=${token}`);
- }
- }
- async userBind() {
- const body = this.ctx.request.body;
- // 成功返回suid
- const res = await this.service.userBind(body);
- // 请求token,返回token
- const token = await this.service.userToken(res);
- this.ctx.ok({ data: token });
- }
- async logout() {
- const query = this.ctx.query;
- const { id } = query;
- const data = await this.service.getBindInfo(id);
- const encDataStr = this.service.encryptData(data);
- await this.service.yLogout(encDataStr);
- this.ctx.ok();
- }
- }
- module.exports = YunjiuyeController;
|