admin
2025-05-09 6159dc58f50d3e4680779b7989bbd4d49a76bad5
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("解析结果", new Gson().toJson(wxUserInfo));
                } catch (Exception e) {
                    wxLogger.error("解析出错", 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("解析结果", new Gson().toJson(wxUserInfo));
                    } catch (Exception e) {
                        wxLogger.error("解析出错", 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("从session读取到key:{}", 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("从session读取到key:{}", 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));
    }
}