微信登录debug

This commit is contained in:
墨大叔 2024-09-23 19:41:01 +08:00
parent 4bb1962255
commit 04a6ab76a2
3 changed files with 37 additions and 23 deletions

View File

@ -3,6 +3,9 @@ package com.ruoyi.common.auth.wx;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@ -10,33 +13,24 @@ import java.util.concurrent.TimeUnit;
public class AccessTokenUtil { public class AccessTokenUtil {
/** 缓存token */ public static final String APPID = SpringUtils.getRequiredProperty("wx.appid");
private static String cachedToken; public static final String APP_SECRET = SpringUtils.getRequiredProperty("wx.appSecret");
/** token过期时间 */
private static long tokenExpirationTime;
@SneakyThrows @SneakyThrows
public static String getToken() { public static String getToken() {
if (isTokenExpired()) { RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
String APPID = SpringUtils.getRequiredProperty("wx.appid"); String token = redisCache.getCacheObject(CacheConstants.WX_ACCESS_TOKEN);
String APPSECRET = SpringUtils.getRequiredProperty("wx.appSecret");
if (StringUtils.isBlank(token)) {
WxMaService wxMaService = new WxMaServiceImpl(); WxMaService wxMaService = new WxMaServiceImpl();
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid(APPID); config.setAppid(APPID);
config.setSecret(APPSECRET); config.setSecret(APP_SECRET);
wxMaService.setWxMaConfig(config); wxMaService.setWxMaConfig(config);
String accessToken = wxMaService.getAccessToken(); token = wxMaService.getAccessToken();
cachedToken = accessToken;
// 更新 token 过期时间
tokenExpirationTime = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(7200L);
return cachedToken;
}
return cachedToken;
}
/**判断token是否过期*/ redisCache.setCacheObject(CacheConstants.WX_ACCESS_TOKEN, token, 5400, TimeUnit.SECONDS);
private static boolean isTokenExpired() { }
return cachedToken == null || System.currentTimeMillis() > tokenExpirationTime; return token;
} }
} }

View File

@ -55,4 +55,9 @@ public class CacheConstants
* 舆情分析 * 舆情分析
*/ */
public static final String BRIEF = DASHBOARD_PREFIX + "brief"; public static final String BRIEF = DASHBOARD_PREFIX + "brief";
/**
* 微信access_token
*/
public static final String WX_ACCESS_TOKEN = "wx_access_token";
} }

View File

@ -18,6 +18,7 @@ import com.ruoyi.ss.user.service.ISmUserService;
import com.ruoyi.ss.account.service.AccountService; import com.ruoyi.ss.account.service.AccountService;
import com.ruoyi.ss.account.domain.enums.AccountType; import com.ruoyi.ss.account.domain.enums.AccountType;
import com.ruoyi.system.service.*; import com.ruoyi.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
@ -54,6 +55,7 @@ import java.util.Objects;
* @author ruoyi * @author ruoyi
*/ */
@Component @Component
@Slf4j
public class SysLoginService public class SysLoginService
{ {
@Autowired @Autowired
@ -267,6 +269,7 @@ public class SysLoginService
// 通过授权码获取微信手机号 // 通过授权码获取微信手机号
String phoneNumber = getWxPhoneNumber(body.getMobileCode()); String phoneNumber = getWxPhoneNumber(body.getMobileCode());
ServiceUtil.assertion(StringUtils.isBlank(phoneNumber), "获取授权手机号失败");
// 判断用户是否注册未注册的自动注册 // 判断用户是否注册未注册的自动注册
user = smUserService.selectUserByPhone(phoneNumber); user = smUserService.selectUserByPhone(phoneNumber);
@ -341,11 +344,23 @@ public class SysLoginService
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("code", mobileCode); jsonObject.put("code", mobileCode);
String post = HttpUtils.sendPost(url, jsonObject.toString()); String post = HttpUtils.sendPost(url, jsonObject.toString());
log.info("获取手机号body:{}", post);
// 获取微信用户手机号 // 获取微信用户手机号
JSONObject jsonObject1 = JSONObject.parseObject(post); JSONObject body = JSONObject.parseObject(post);
String phoneInfo = jsonObject1.getString("phone_info"); 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); WxMaPhoneNumberInfo wxMaPhoneNumberInfo = JSONObject.parseObject(phoneInfo, WxMaPhoneNumberInfo.class);
if (wxMaPhoneNumberInfo == null) {
return null;
}
return wxMaPhoneNumberInfo.getPhoneNumber(); return wxMaPhoneNumberInfo.getPhoneNumber();
} }