const axios = require('axios'); module.exports = app => { return async (ctx, next) => { const sid = ctx.socket.id; const nsp = app.io.of('/'); const query = ctx.socket.handshake.query; const { token,type } = query; let islogin = false; if(!token){ if(type=="tgdp"){ ctx.socket.disconnect(); } if(type=="gzt"){ let datas = await ctx.service.gzt.getBindCode(sid); ctx.socket.emit('genericResponse', {method:"getBindCode",data:datas}); } }else{ //根据token获取用户信息 switch (type) { case 'tgdp': // code const apiUrl = 'https://ye4s.wms.gztbyc.com/api/user/index'; const headers = { 'token': token }; try { const response = await axios.get(apiUrl,{headers}); ctx.socket.userinfo = response.data.data[0]; ctx.socket.rules = response.data.data[1]; ctx.socket.istgdp = false; islogin = true; if(type=="tgdp"){ let istgdp = false; for(let i=0;i { if(ctx.socket.istgdp){ const listKey = `tgdp_user_${ctx.socket.userinfo.warehouseid}`; await app.redis.get('cache').lrem(listKey, 0, sid); } }); await next(); }; };