From 6159dc58f50d3e4680779b7989bbd4d49a76bad5 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期五, 09 五月 2025 19:13:35 +0800 Subject: [PATCH] 添加日志 --- src/main/java/com/taoke/autopay/controller/WebApiController.java | 98 +++++++++++++++++++++++++++++------------------- 1 files changed, 59 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/taoke/autopay/controller/WebApiController.java b/src/main/java/com/taoke/autopay/controller/WebApiController.java index 8a8b616..c4fdc9a 100644 --- a/src/main/java/com/taoke/autopay/controller/WebApiController.java +++ b/src/main/java/com/taoke/autopay/controller/WebApiController.java @@ -20,7 +20,6 @@ import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.AntPathMatcher; import org.springframework.web.bind.annotation.RequestMapping; @@ -32,7 +31,10 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; -import java.util.*; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; @Controller @RequestMapping("webapi") @@ -238,16 +240,43 @@ } + private WxUserInfo wxLogin(String code, HttpSession session) throws Exception { + WXAppInfoDto wxApp = systemConfigService.getWxAppInfoCache(); + WxApiUtil.WXAccessTokenInfo tokenInfo = WxApiUtil.getAcessTokenInfo(code, wxApp); + if (tokenInfo != null && !StringUtil.isNullOrEmpty(tokenInfo.getOpenid())) { + WxApiUtil.WXUserInfo wxUserInfo = null; + if (tokenInfo.getScope() != null && tokenInfo.getScope().contains("snsapi_userinfo")) { + try { + wxUserInfo = WxApiUtil.getUserInfo(tokenInfo.getAccess_token(), tokenInfo.getOpenid()); + wxLogger.info("瑙f瀽缁撴灉", new Gson().toJson(wxUserInfo)); + } catch (Exception e) { + wxLogger.error("瑙f瀽鍑洪敊", e); + } + } + if (wxUserInfo == null) { + wxUserInfo = new WxApiUtil.WXUserInfo(); + wxUserInfo.setOpenid(tokenInfo.getOpenid()); + } + WxUserInfo user = wxUserService.login(wxUserInfo); + session.setAttribute(Constant.SESSION_KEY_USER, user); + wxLogger.info("寰俊淇濆瓨鐢ㄦ埛淇℃伅锛歿} id-{}", session.getId(), user.getId()); + return user; + } + throw new Exception("鑾峰彇鎺堟潈淇℃伅寮傚父"); + } + @RequestMapping(value = "wxLogin") - public void wxLogin(String code, String state, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException { + public void wxLogin(String code, String state, HttpServletRequest request, HttpServletResponse + response, HttpSession session) throws IOException { // 鏍规嵁code鑾峰彇openid SubmitKeyInfo alipayKeyInfo = (SubmitKeyInfo) session.getAttribute(Constant.SESSION_KEY_TEMP_ALIPAY_KEY); - wxLogger.info("寰俊鎺堟潈鍥炶皟锛歿} code-{} referer-{}", session.getId(), code, alipayKeyInfo.getReferer()); + if (alipayKeyInfo != null) { + wxLogger.info("寰俊鎺堟潈鍥炶皟锛歿} code-{} referer-{}", session.getId(), code, alipayKeyInfo.getReferer()); + } String failLink = systemConfigService.getValueCache(SystemConfigKeyEnum.WX_LOGIN_FAIL_LINK); - String referer = alipayKeyInfo.getReferer(); + String referer = alipayKeyInfo != null ? alipayKeyInfo.getReferer() : ""; try { - WXAppInfoDto wxApp = systemConfigService.getWxAppInfoCache(); String successLink = systemConfigService.getValueCache(SystemConfigKeyEnum.WX_LOGIN_SUCCESS_LINK); if (!StringUtil.isNullOrEmpty(referer)) { Map<String, String> params = HttpUtil.getPramsFromUrl(referer); @@ -255,41 +284,31 @@ successLink = HttpUtil.getWholeUrl(HttpUtil.getUrlWithoutParams(referer), params); } - WxApiUtil.WXAccessTokenInfo tokenInfo = WxApiUtil.getAcessTokenInfo(code, wxApp); - if (tokenInfo != null && !StringUtil.isNullOrEmpty(tokenInfo.getOpenid())) { - WxApiUtil.WXUserInfo wxUserInfo = null; - if (tokenInfo.getScope() != null && tokenInfo.getScope().contains("snsapi_userinfo")) { - try { - wxUserInfo = WxApiUtil.getUserInfo(tokenInfo.getAccess_token(), tokenInfo.getOpenid()); - wxLogger.info("瑙f瀽缁撴灉", new Gson().toJson(wxUserInfo)); - } catch (Exception e) { - wxLogger.error("瑙f瀽鍑洪敊", e); - } - } - if (wxUserInfo == null) { - wxUserInfo = new WxApiUtil.WXUserInfo(); - wxUserInfo.setOpenid(tokenInfo.getOpenid()); - } - WxUserInfo user = wxUserService.login(wxUserInfo); - session.setAttribute(Constant.SESSION_KEY_USER, user); - wxLogger.info("寰俊淇濆瓨鐢ㄦ埛淇℃伅锛歿} id-{}", session.getId(), user.getId()); - - wxLogger.info("浠巗ession璇诲彇鍒発ey锛歿}", alipayKeyInfo); - if (alipayKeyInfo != null) { - if (!ipInfoMap.containsKey(alipayKeyInfo.getIp())) { - try { - IPUtil.IPInfo ipInfo = IPUtil.getLocalIPInfo(alipayKeyInfo.getIp()); - ipInfoMap.put(alipayKeyInfo.getIp(), ipInfo); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - addKey(alipayKeyInfo, user.getId()); - } - response.sendRedirect(successLink); + WxUserInfo user = wxLogin(code, session); + if(alipayKeyInfo==null){ + // 鏅�氱櫥褰� + wxLogger.info("鏅�氱櫥褰曟垚鍔�"); + response.sendRedirect("/credit/index.html?state=SUCCESS"); return; } - } catch (Exception e) { + + wxLogger.info("浠巗ession璇诲彇鍒発ey锛歿}", alipayKeyInfo); + if (alipayKeyInfo != null) { + if (!ipInfoMap.containsKey(alipayKeyInfo.getIp())) { + try { + IPUtil.IPInfo ipInfo = IPUtil.getLocalIPInfo(alipayKeyInfo.getIp()); + ipInfoMap.put(alipayKeyInfo.getIp(), ipInfo); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + addKey(alipayKeyInfo, user.getId()); + } + response.sendRedirect(successLink); + return; + + } catch ( + Exception e) { wxLogger.error("鎺堟潈澶辫触锛歿}", e.getMessage()); if (!StringUtil.isNullOrEmpty(referer)) { Map<String, String> params = HttpUtil.getPramsFromUrl(referer); @@ -360,4 +379,5 @@ return JsonUtil.loadTrueResult(new Gson().toJson(map)); } + } -- Gitblit v1.8.0