瀏覽代碼

配合云就业修改,redis记录登录;云就业退出登录;检查是否登录中间件

lrf 1 年之前
父節點
當前提交
05f275bb88
共有 5 個文件被更改,包括 70 次插入5 次删除
  1. 29 1
      app/controller/yunjiuye.js
  2. 22 0
      app/middleware/checkIsLogin.js
  3. 7 3
      app/router.js
  4. 10 0
      app/service/yunjiuye.js
  5. 2 1
      config/config.default.js

+ 29 - 1
app/controller/yunjiuye.js

@@ -1,11 +1,12 @@
 '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() {
@@ -45,5 +46,32 @@ class YunjiuyeController extends Controller {
     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;

+ 22 - 0
app/middleware/checkIsLogin.js

@@ -0,0 +1,22 @@
+'use strict';
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+module.exports = (options, app) => {
+  return async (ctx, next) => {
+    const url = ctx.request.url;
+    if (url.includes('/y/')) await next();
+    else {
+      const { authorization, dtype } = ctx.request.header;
+      if (!dtype || dtype !== 'pc') await next();
+      else if (authorization) {
+        let user = decodeURI(authorization);
+        if (user) {
+          user = JSON.parse(user);
+          if (user.type === '4') await next();
+          const res = await ctx.service.yunjiuye.checkYjyLogin(user.id);
+          if (!res) throw new BusinessError(ErrorCode.NOT_LOGIN);
+          else await next();
+        }
+      } else throw new BusinessError(ErrorCode.NOT_LOGIN);
+    }
+  };
+};

+ 7 - 3
app/router.js

@@ -6,11 +6,15 @@
 module.exports = app => {
   const { router, controller } = app;
   router.get('/', controller.home.index);
-  // 云就业退出
+  // 云就业主动注销----提供给云就业
+  router.get('/api/train/y/toLogout', controller.yunjiuye.toLogout);
+  // 云就业loading页,前端登录成功----双困生用
+  router.post('/api/train/y/loginSuccess', controller.yunjiuye.loginSuccess);
+  // 云就业退出(主动注销)----双困生用
   router.get('/api/train/y/logout', controller.yunjiuye.logout);
-  // 云就业登录
+  // 云就业登录----提供给云就业
   router.get('/api/train/y/login', controller.yunjiuye.login);
-  // 云就业登录
+  // 云就业绑定----双困生用
   router.post('/api/train/y/userBind', controller.yunjiuye.userBind);
   // 共通查询单条记录方法
   router.get('/api/train/common/findone/:modelname', controller.common.findone);

+ 10 - 0
app/service/yunjiuye.js

@@ -118,6 +118,16 @@ class YunjiuyeService extends Service {
     const url = `${domain}?params=${params}`;
     return url;
   }
+
+  // 检查用户是否登录.未登录就跳走
+  async checkYjyLogin(id) {
+    if (!id) throw new BusinessError(ErrorCode.BADPARAM, '缺少参数');
+    const redis = this.app.redis;
+    const key = this.app.config.yjyLoginKey;
+    const loginTime = await redis.get(`${key}${id}`);
+    if (loginTime) return true;
+  }
+
   // 加密,使用云就业提供的公钥对数据进行加密,供退出登录加密参数使用
   encryptData(data) {
     const enc = crypto.publicEncrypt(

+ 2 - 1
config/config.default.js

@@ -18,7 +18,7 @@ module.exports = appInfo => {
   config.keys = appInfo.name + '_1578642242928_5726';
 
   // add your middleware config here
-  config.middleware = [ 'logs' ];
+  config.middleware = [ 'checkIsLogin', 'logs' ];
 
   // add your user config here
   const userConfig = {
@@ -156,6 +156,7 @@ module.exports = appInfo => {
       timeout: 3000,
     },
   };
+  config.yjyLoginKey = 'yjyLoginKey:';
 
   return {
     ...config,