diff --git a/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/auth/wx/AccessTokenUtil.java b/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/auth/wx/AccessTokenUtil.java index aa69016c..248d2eaf 100644 --- a/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/auth/wx/AccessTokenUtil.java +++ b/smart-switch-ruoyi/smart-switch-common/src/main/java/com/ruoyi/common/auth/wx/AccessTokenUtil.java @@ -8,17 +8,13 @@ import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.spring.SpringUtils; import lombok.SneakyThrows; -import org.springframework.beans.factory.annotation.Autowired; import java.util.concurrent.TimeUnit; public class AccessTokenUtil { - /** 缓存token */ - private static String cachedToken; - - /** token过期时间 */ - private static long tokenExpirationTime; + public static final String APPID = SpringUtils.getRequiredProperty("wx.appid"); + public static final String APP_SECRET = SpringUtils.getRequiredProperty("wx.appSecret"); @SneakyThrows public static String getToken() { @@ -26,12 +22,10 @@ public class AccessTokenUtil { String token = redisCache.getCacheObject(CacheConstants.WX_ACCESS_TOKEN); if (StringUtils.isBlank(token)) { - String APPID = SpringUtils.getRequiredProperty("wx.appid"); - String APPSECRET = SpringUtils.getRequiredProperty("wx.appSecret"); WxMaService wxMaService = new WxMaServiceImpl(); WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); config.setAppid(APPID); - config.setSecret(APPSECRET); + config.setSecret(APP_SECRET); wxMaService.setWxMaConfig(config); token = wxMaService.getAccessToken(); @@ -39,9 +33,4 @@ public class AccessTokenUtil { } return token; } - - /**判断token是否过期*/ - private static boolean isTokenExpired() { - return cachedToken == null || System.currentTimeMillis() > tokenExpirationTime; - } } diff --git a/smart-switch-ruoyi/smart-switch-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/smart-switch-ruoyi/smart-switch-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index d6f86c70..3f2fa8f3 100644 --- a/smart-switch-ruoyi/smart-switch-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/smart-switch-ruoyi/smart-switch-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -18,6 +18,7 @@ import com.ruoyi.ss.user.service.ISmUserService; import com.ruoyi.ss.account.service.AccountService; import com.ruoyi.ss.account.domain.enums.AccountType; import com.ruoyi.system.service.*; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AuthenticationManager; @@ -54,6 +55,7 @@ import java.util.Objects; * @author ruoyi */ @Component +@Slf4j public class SysLoginService { @Autowired @@ -267,6 +269,7 @@ public class SysLoginService // 通过授权码获取微信手机号 String phoneNumber = getWxPhoneNumber(body.getMobileCode()); + ServiceUtil.assertion(StringUtils.isBlank(phoneNumber), "获取授权手机号失败"); // 判断用户是否注册,未注册的自动注册 user = smUserService.selectUserByPhone(phoneNumber); @@ -341,11 +344,23 @@ public class SysLoginService JSONObject jsonObject = new JSONObject(); jsonObject.put("code", mobileCode); String post = HttpUtils.sendPost(url, jsonObject.toString()); - + log.info("获取手机号body:{}", post); // 获取微信用户手机号 - JSONObject jsonObject1 = JSONObject.parseObject(post); - String phoneInfo = jsonObject1.getString("phone_info"); + JSONObject body = JSONObject.parseObject(post); + if (body == null) { + return null; + } + + Integer errcode = body.getInteger("errcode"); + if (errcode == null || !errcode.equals(0)) { + throw new ServiceException("获取手机号失败:" + body.getString("errmsg")); + } + + String phoneInfo = body.getString("phone_info"); WxMaPhoneNumberInfo wxMaPhoneNumberInfo = JSONObject.parseObject(phoneInfo, WxMaPhoneNumberInfo.class); + if (wxMaPhoneNumberInfo == null) { + return null; + } return wxMaPhoneNumberInfo.getPhoneNumber(); }