'use strict'; module.exports = () => { return async function interceptor(ctx, next) { let sessionId = ''; if (ctx.query && ctx.query.sessionId) { sessionId = ctx.query.sessionId; } if (ctx.request.body && ctx.request.body.sessionId) { sessionId = ctx.request.body.sessionId; } if (ctx.get('sessionId')) { sessionId = ctx.get('sessionId'); } let openId = ''; if (ctx.query && ctx.query.openId) { openId = ctx.query.openId; } if (ctx.request.body && ctx.request.body.openId) { openId = ctx.request.body.openId; } if (ctx.get('openId')) { openId = ctx.get('openId'); } let appletsId = ''; if (ctx.query && ctx.query.appletsId) { appletsId = ctx.query.appletsId; } if (ctx.request.body && ctx.request.body.appletsId) { appletsId = ctx.request.body.appletsId; } if (ctx.get('appletsId')) { appletsId = ctx.get('appletsId'); } ctx.sessionId = sessionId; ctx.openId = openId; ctx.appletsId = appletsId; console.log('拦截器appletsId======================' + appletsId) if (sessionId) { const user = await ctx.app.redis.get(sessionId); if (user) { const newUser = await ctx.service.sysUserService.one(JSON.parse(user)._id, ctx.getUserPop()); if (newUser) { ctx.user = newUser; await ctx.app.redis.expire(sessionId, ctx.app.config.sessionTimeOut); // Redis Expire 命令用于设置 key 的过期时间,key 过期后将不再可用 await next(); } else { ctx.error('登录失效', 2); } } else { ctx.error('登录失效', 2); } } else { if (openId) { const user = await ctx.service.sysUserService.oneData({ openId }, ctx.getUserPop()); if (user) { ctx.user = user; await next(); } else { ctx.error('当前openId没有绑定用户', 2); } } else { // ctx.error('登录失效', 2); if (appletsId) { const user = await ctx.service.sysUserService.oneData({ appletsId }, ctx.getUserPop()); if (user) { ctx.user = user; await next(); } else { ctx.error('当前appletsId没有绑定用户', 2); } } else { ctx.error('登录失效', 2); } } } }; };