From 06a17c236d4cb9b8da75fce43af938cb7ea510bf Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期一, 15 十二月 2025 09:50:12 +0800
Subject: [PATCH] feat: 优化企业微信判断,优化gps分断处理
---
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatLoginServiceImpl.java | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatLoginServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatLoginServiceImpl.java
index cfbed27..50457cc 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatLoginServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WechatLoginServiceImpl.java
@@ -11,6 +11,9 @@
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
+import com.ruoyi.system.domain.SysConfig;
+import com.ruoyi.system.mapper.SysConfigMapper;
+import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.IWechatLoginService;
import com.ruoyi.system.service.ISysUserService;
@@ -30,6 +33,12 @@
@Autowired
private ISysUserService userService;
+ @Autowired
+ private ISysConfigService configService;
+
+ @Autowired
+ private SysConfigMapper configMapper;
+
/**
* 寰俊API - code2Session
*/
@@ -40,10 +49,28 @@
*/
private static final String GET_PHONE_NUMBER_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";
+ private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
+
/**
* 寰俊API - 鑾峰彇access_token
*/
- private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
+ /**
+ * 鏍规嵁configKey鍐欏叆鎴栨洿鏂皊ys_config
+ */
+ private void upsertConfig(String key, String value) {
+ SysConfig exist = configMapper.checkConfigKeyUnique(key);
+ if (exist != null && exist.getConfigId() != null) {
+ exist.setConfigValue(value);
+ configMapper.updateConfig(exist);
+ } else {
+ SysConfig cfg = new SysConfig();
+ cfg.setConfigKey(key);
+ cfg.setConfigName(key);
+ cfg.setConfigValue(value);
+ cfg.setConfigType("Y");
+ configMapper.insertConfig(cfg);
+ }
+ }
/**
* 閫氳繃寰俊code鑾峰彇openid鍜宻ession_key
@@ -154,6 +181,9 @@
return result;
}
+ // 浼扮畻杩囨湡鏃堕棿(寰俊access_token榛樿7200绉掓湁鏁�)
+ java.util.Date accessTokenExpiresAt = new java.util.Date(System.currentTimeMillis() + 7200L * 1000L);
+
// 鏋勫缓璇锋眰URL
String url = GET_PHONE_NUMBER_URL + "?access_token=" + accessToken;
@@ -186,6 +216,9 @@
result.put("phoneNumber", phoneInfo.getString("phoneNumber"));
result.put("purePhoneNumber", phoneInfo.getString("purePhoneNumber"));
result.put("countryCode", phoneInfo.getString("countryCode"));
+ // 杩斿洖褰撳墠浣跨敤鐨刟ccess_token鍙婃湁鏁堟湡锛屼究浜庤皟鐢ㄦ柟瀛樺偍
+ result.put("accessToken", accessToken);
+ result.put("accessTokenExpiresAt", accessTokenExpiresAt);
return result;
}
@@ -260,7 +293,7 @@
return result;
}
- // 5. 鏇存柊鐢ㄦ埛鐨勫井淇′俊鎭�
+ // 5. 鏇存柊鐢ㄦ埛鐨勫井淇′俊鎭�(鍖呮嫭access_token涓庤繃鏈熸椂闂�)
SysUser updateUser = new SysUser();
updateUser.setUserId(user.getUserId());
updateUser.setOpenId(openId);
@@ -268,6 +301,17 @@
{
updateUser.setUnionId(unionId);
}
+ // 淇濆瓨鏈璋冪敤浣跨敤鐨刟ccess_token鍙婅繃鏈熸椂闂村埌搴旂敤绾ч厤缃紙渚夸簬鍚庣画澶嶇敤锛�
+ Object at = phoneResult.get("accessToken");
+ Object atExp = phoneResult.get("accessTokenExpiresAt");
+ if (at != null) {
+ String appId = wechatConfig.getAppId();
+ String tokenKey = "weixin.access_token." + appId;
+ String expireKey = "weixin.access_token_expires." + appId;
+ upsertConfig(tokenKey, at.toString());
+ long expireTs = (atExp instanceof java.util.Date) ? ((java.util.Date) atExp).getTime() : (System.currentTimeMillis() + 7200L * 1000L);
+ upsertConfig(expireKey, String.valueOf(expireTs));
+ }
userService.updateUser(updateUser);
log.info("鐢ㄦ埛{}寰俊淇℃伅鏇存柊鎴愬姛", user.getUserName());
--
Gitblit v1.9.1