1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| @Controller @RequestMapping("/wx") @Slf4j @PropertySource(value = "classpath:conf/wx.properties", ignoreResourceNotFound = true) public class WxController {
@Autowired private WxMpService wxMpService; @Autowired private JedisClient jedisClient; @Autowired private BuyerService buyerService;
private static final String REDIS_LOGIN_UUID_PRE = "LOGIN_UUID";
private static final int LOGIN_EXPIRE = 15;
@Value("XXXXXXX.nat300.top") private String URL;
@GetMapping("/portal") @ResponseBody public String portal(String echostr, String timestamp, String nonce, String signature) { log.info("WeChat校验"); if (!wxMpService.checkSignature(timestamp, nonce, signature)) { log.warn("非法请求"); return "false"; } if (StringUtils.isNotBlank(echostr)) { log.info(echostr); return echostr; } log.warn("未知请求"); return null; }
@GetMapping(value = "/authorization", produces = "application/json;charset=UTF-8") @ResponseBody public String authorization() { String url = URL + "/wx/getUserInfo"; String UUIDParam = UUID.randomUUID().toString().replace("-", ""); url = url + "/" + UUIDParam; String authorizationUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null); log.info("authorizationUrl:" + authorizationUrl); String json = "{\"uuid\":\"" + UUIDParam + "\",\"url\":\"" + authorizationUrl + "\"}"; jedisClient.set(REDIS_LOGIN_UUID_PRE + ":" + UUIDParam + ":BASE", "NULL"); jedisClient.expire(REDIS_LOGIN_UUID_PRE + ":" + UUIDParam + ":BASE", LOGIN_EXPIRE); return json; }
@GetMapping(value = "/getUserInfo/{uuid}") public String getUserInfo(String code, @PathVariable String uuid, Model model) { if (StringUtils.isBlank(uuid) || StringUtils.isBlank(code)) { model.addAttribute("result", GMResult.build(400, "登录失败")); return "wx/loginStats"; } System.out.println(uuid); String redisPre = REDIS_LOGIN_UUID_PRE + ":" + uuid + ":BASE"; log.info("code:" + code); try { WxMpOAuth2AccessToken token = wxMpService.oauth2getAccessToken(code); WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(token, null); if (!jedisClient.exists(redisPre)) { model.addAttribute("result", GMResult.build(402, "二维码已过期")); return "wx/loginStats"; } if ("NULL".equals(jedisClient.get(redisPre))) { jedisClient.set(redisPre, JsonUtils.objectToJson(wxMpUser)); jedisClient.expire(redisPre, LOGIN_EXPIRE); log.info(wxMpUser.toString()); model.addAttribute("result", GMResult.build(200, "登录成功")); return "wx/loginStats"; } } catch (WxErrorException e) { e.printStackTrace(); model.addAttribute("result", GMResult.build(401, "请勿重复登录")); return "wx/loginStats"; } model.addAttribute("result", GMResult.build(403, "二维码已使用")); System.out.println("登录失败"); return "wx/loginStats"; }
@PostMapping(value = "/checkLogin", produces = "application/json;charset=UTF-8") @ResponseBody public GMResult checkLogin(String uuid, HttpSession session) { String redisPre=REDIS_LOGIN_UUID_PRE + ":" + uuid + ":BASE"; if (!jedisClient.exists(redisPre)){ return GMResult.build(402, "二维码已过期"); } String json = jedisClient.get(redisPre); System.out.println("等待登录。。。。。json:" + json); if (!"NULL".equals(json)) { WxMpUser wxMpUser = JsonUtils.jsonToPojo(json, WxMpUser.class); System.out.println(wxMpUser.toString()); Buyer buyer=buyerService.wxLogin(wxMpUser); session.setAttribute("user", buyer); return GMResult.ok(); } return GMResult.build(400, "登录失败"); } }
|