1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 'use strict';
- const Controller = require('egg').Controller;
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const moment = require('moment');
- class YunjiuyeController extends Controller {
- constructor(ctx) {
- super(ctx);
- this.service = this.ctx.service.yunjiuye;
- this.yjyLoginKey = this.app.config.yjyLoginKey;
- }
- // http://127.0.0.1:7001/api/train/y/login?params=
- async login() {
- 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, logoutUrl } = params;
- this.service.getDomain(logoutUrl);
- 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);
- const url = await this.service.yLogout(encDataStr);
- this.ctx.ok({ data: url });
- }
- // loading页设置完用户后走该接口,进行用户登录的登记
- async loginSuccess() {
- const body = this.ctx.request.body;
- if (!body.id) throw new BusinessError(ErrorCode.BADPARAM, '缺少参数');
- const redis = this.app.redis;
- await redis.set(`${this.yjyLoginKey}${body.id}`, moment().format('YYYY-MM-DD HH:mm:ss'));
- this.ctx.ok();
- }
- // 云就业主动注销时的函数
- async toLogout() {
- 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 res = await this.service.checkUserIsConnect(params);
- if (!res) throw new BusinessError(ErrorCode.USER_NOT_BIND, '用户未绑定,无法检测登录状态');
- else {
- const redis = this.app.redis;
- const loginTime = await redis.get(`${this.yjyLoginKey}${res}`);
- if (loginTime) {
- await redis.del(`${this.yjyLoginKey}${res}`);
- this.ctx.ok();
- } else throw new BusinessError(ErrorCode.NOT_LOGIN, '用户未登录');
- }
- }
- }
- module.exports = YunjiuyeController;
|