package com.yc.phoneQRLogin.action; import com.yc.action.grid.GridUtils;; import com.yc.action.login.TokenInfo; import com.yc.factory.FactoryBean; import com.yc.im.service.RedisDAO; import com.yc.multiData.SpObserver; import com.yc.phoneQRLogin.entity.QrCodeToken; import com.yc.phoneQRLogin.utils.BackMsg; import com.yc.sdk.shopping.action.api.GenerationQrCodeForApp; import com.yc.sdk.shopping.action.api.InvitationCode; import com.yc.sdk.shopping.entity.QrCodeForAppEntity; import com.yc.sdk.shopping.service.imagedata.ShoppingImageDataIfc; import com.yc.utils.AESUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.TimeUnit; /** * @USER: xinyb_ * @DATE: 2022-01-15 10:49 */ @RestController public class LoginQRCode { @Autowired ShoppingImageDataIfc imgData; @Autowired RedisDAO redisDAO; public static Map sessionMapMap = new HashMap<>(); public static Map responseMap=new HashMap<>(); /** * 生成系统登录用的二维码 xin 2022-1-15 10:58:37 * * @param appId * @param request * @return */ @RequestMapping("/ws/qrCode.do") public BackMsg getLoginQRCode(String appId, HttpServletRequest request,HttpServletResponse response) { BackMsg msg = new BackMsg(); try { SpObserver.setDBtoInstance("_" + appId); QrCodeForAppEntity qrCodeForAppEntity = new QrCodeForAppEntity(); qrCodeForAppEntity.setAction(QrCodeForAppEntity.Login); // 设置行为 Action qrCodeForAppEntity.setAuthorCode("SYSTEM"); qrCodeForAppEntity.setAuthorName("系统管理员"); qrCodeForAppEntity = GenerationQrCodeForApp.saveQrCodeForApp(qrCodeForAppEntity, false); String url = imgData.getImageUrl(qrCodeForAppEntity.getQrCodeUnid(), 250, 250, true, false, request); Map map = new HashMap<>(); map.put("qrcode", qrCodeForAppEntity.getQrCode()); map.put("url", url); msg.setOk(map); sessionMapMap.put(qrCodeForAppEntity.getQrCode(), request.getSession());//当前登录的session对象 } catch (Exception e) { msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); } finally { SpObserver.setDBtoInstance(); } return msg; } @RequestMapping("/ws/qrLoginToken.do") public void getLoginToken(@RequestBody QrCodeToken token, HttpServletResponse response) throws InterruptedException { responseMap.put(token.getQrCode(),response); // token.setToken(genToken("666666","15918137189",82)); redisDAO.publishMessage("Onbus_Qrcode", GridUtils.toJsonNotNull(token)); Thread.sleep(1000);//沉睡1秒 } //测试模拟值 public String genToken(String pwd,String usercode,int dbid) { TokenInfo tokenInfo=new TokenInfo(); tokenInfo.setUsercode(usercode); tokenInfo.setDbid(dbid); double rand=new Random().nextDouble(); tokenInfo.setRand(rand);//增加随机数,以便用于生成不同的token try { //格式dbid:usercode String key = InvitationCode.TOKEN_STR+tokenInfo.getDbid() + ":" + tokenInfo.getUsercode(); RedisTemplate redisTemplate = (RedisTemplate) FactoryBean.getBean("redisTemplate"); final int OVERTIME = 12;//12小时过期,时间限制 redisTemplate.opsForValue().set(key, pwd+"#"+rand,OVERTIME, TimeUnit.HOURS); String token= AESUtils.encrypt(GridUtils.toJson(tokenInfo));//dbid+usercode加密返回给APP客户端保存 return token; }catch(Exception ex){ ex.printStackTrace(); return ""; } } }