```
feat(WxLoginController): 添加调试接口获取AccessToken并增强错误处理 新增调试端点 /debug/getAccessToken用于手动触发获取微信 AccessToken, 便于问题排查。同时增强微信 API 调用的错误处理逻辑,针对常见错误码(如 AppID、AppSecret 配置错误)抛出更明确的异常信息,提升调试效率 和问题定位准确性。 ```
This commit is contained in:
@@ -665,6 +665,32 @@ public class WxLoginController extends BaseController {
|
||||
return sample;
|
||||
}
|
||||
|
||||
@Operation(summary = "调试:获取AccessToken")
|
||||
@GetMapping("/debug/getAccessToken")
|
||||
public ApiResult<?> debugGetAccessToken() {
|
||||
try {
|
||||
// 获取当前线程的租户ID
|
||||
Integer tenantId = getTenantId();
|
||||
if (tenantId == null) {
|
||||
tenantId = 10550; // 默认租户
|
||||
}
|
||||
|
||||
System.out.println("=== 开始调试获取AccessToken,租户ID: " + tenantId + " ===");
|
||||
|
||||
// 手动调用获取AccessToken
|
||||
String accessToken = getAccessTokenForTenant(tenantId);
|
||||
|
||||
String result = "获取AccessToken成功: " + (accessToken != null ? accessToken.substring(0, Math.min(10, accessToken.length())) + "..." : "null");
|
||||
System.out.println("调试结果: " + result);
|
||||
|
||||
return success(result);
|
||||
} catch (Exception e) {
|
||||
System.err.println("调试获取AccessToken异常: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return fail("获取AccessToken失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从scene参数中提取租户ID
|
||||
* scene格式可能是: uid_33103 或其他包含用户ID的格式
|
||||
@@ -736,9 +762,25 @@ public class WxLoginController extends BaseController {
|
||||
|
||||
String apiUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
|
||||
System.out.println("调用微信API获取token - 租户ID: " + tenantId + ", AppID: " + (appId != null ? appId.substring(0, Math.min(8, appId.length())) + "..." : "null"));
|
||||
System.out.println("微信API请求URL: " + apiUrl.replaceAll("secret=[^&]*", "secret=***"));
|
||||
String result = HttpUtil.get(apiUrl);
|
||||
System.out.println("微信API响应: " + result);
|
||||
JSONObject json = JSON.parseObject(result);
|
||||
|
||||
// 检查是否有错误
|
||||
if (json.containsKey("errcode")) {
|
||||
Integer errcode = json.getInteger("errcode");
|
||||
String errmsg = json.getString("errmsg");
|
||||
System.err.println("微信API错误 - errcode: " + errcode + ", errmsg: " + errmsg);
|
||||
|
||||
if (errcode == 40125) {
|
||||
throw new RuntimeException("微信AppSecret配置错误,请检查并更新正确的AppSecret");
|
||||
} else if (errcode == 40013) {
|
||||
throw new RuntimeException("微信AppID配置错误,请检查并更新正确的AppID");
|
||||
} else {
|
||||
throw new RuntimeException("微信API调用失败: " + errmsg + " (errcode: " + errcode + ")");
|
||||
}
|
||||
}
|
||||
|
||||
if (json.containsKey("access_token")) {
|
||||
String accessToken = json.getString("access_token");
|
||||
|
||||
Reference in New Issue
Block a user