|
|
@ -44,6 +44,7 @@ import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
import static com.gxwebsoft.common.core.constants.PlatformConstants.MP_WEIXIN; |
|
|
|
import static com.gxwebsoft.common.core.constants.RedisConstants.ACCESS_TOKEN_KEY; |
|
|
|
import static com.gxwebsoft.common.core.constants.RedisConstants.MP_WX_KEY; |
|
|
|
|
|
|
|
@RestController |
|
|
|
@RequestMapping("/api/wx-login") |
|
|
@ -224,8 +225,8 @@ public class WxLoginController extends BaseController { |
|
|
|
|
|
|
|
// 获取openid
|
|
|
|
private JSONObject getOpenIdByCode(UserParam userParam) { |
|
|
|
// 获取微信小程序配置信息
|
|
|
|
JSONObject setting = settingService.getBySettingKey("mp-weixin", getTenantId()); |
|
|
|
// 从缓存获取微信小程序配置信息
|
|
|
|
JSONObject setting = getWxConfigFromCache(getTenantId()); |
|
|
|
// 获取openId
|
|
|
|
String apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + setting.getString("appId") + "&secret=" + setting.getString("appSecret") + "&js_code=" + userParam.getCode() + "&grant_type=authorization_code"; |
|
|
|
// 执行get请求
|
|
|
@ -282,8 +283,8 @@ public class WxLoginController extends BaseController { |
|
|
|
Integer tenantId = getTenantId(); |
|
|
|
String key = ACCESS_TOKEN_KEY.concat(":").concat(tenantId.toString()); |
|
|
|
|
|
|
|
// 使用跨租户方式获取微信小程序配置信息
|
|
|
|
JSONObject setting = settingService.getBySettingKeyIgnoreTenant("mp-weixin", tenantId); |
|
|
|
// 从缓存获取微信小程序配置信息
|
|
|
|
JSONObject setting = getWxConfigFromCache(tenantId); |
|
|
|
if (setting == null) { |
|
|
|
throw new BusinessException("请先配置小程序"); |
|
|
|
} |
|
|
@ -332,7 +333,7 @@ public class WxLoginController extends BaseController { |
|
|
|
// 请求微信接口获取openid
|
|
|
|
String apiUrl = "https://api.weixin.qq.com/sns/jscode2session"; |
|
|
|
final HashMap<String, Object> map = new HashMap<>(); |
|
|
|
final JSONObject setting = settingService.getBySettingKey("mp-weixin", getTenantId()); |
|
|
|
final JSONObject setting = getWxConfigFromCache(getTenantId()); |
|
|
|
final String appId = setting.getString("appId"); |
|
|
|
final String appSecret = setting.getString("appSecret"); |
|
|
|
map.put("appid", appId); |
|
|
@ -360,7 +361,7 @@ public class WxLoginController extends BaseController { |
|
|
|
|
|
|
|
String apiUrl = "https://api.weixin.qq.com/sns/jscode2session"; |
|
|
|
final HashMap<String, Object> map = new HashMap<>(); |
|
|
|
final JSONObject setting = settingService.getBySettingKey("mp-weixin", getTenantId()); |
|
|
|
final JSONObject setting = getWxConfigFromCache(getTenantId()); |
|
|
|
final String appId = setting.getString("appId"); |
|
|
|
final String appSecret = setting.getString("appSecret"); |
|
|
|
map.put("appid", appId); |
|
|
@ -548,9 +549,9 @@ public class WxLoginController extends BaseController { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 使用跨租户方式获取微信小程序配置信息
|
|
|
|
// 从缓存获取微信小程序配置信息
|
|
|
|
Integer tenantId = getTenantId(); |
|
|
|
JSONObject setting = settingService.getBySettingKeyIgnoreTenant("mp-weixin", tenantId); |
|
|
|
JSONObject setting = getWxConfigFromCache(tenantId); |
|
|
|
if (setting == null) { |
|
|
|
throw new IOException("请先配置小程序"); |
|
|
|
} |
|
|
@ -576,7 +577,7 @@ public class WxLoginController extends BaseController { |
|
|
|
if (json.has("access_token")) { |
|
|
|
String token = json.get("access_token").asText(); |
|
|
|
long expiresIn = json.get("expires_in").asInt(7200); |
|
|
|
|
|
|
|
|
|
|
|
// 缓存完整的JSON响应,与其他方法保持一致
|
|
|
|
redisUtil.set(key, body, expiresIn, TimeUnit.SECONDS); |
|
|
|
tokenExpireEpoch = now + expiresIn; |
|
|
@ -603,18 +604,20 @@ public class WxLoginController extends BaseController { |
|
|
|
result.put("tenantId", tenantId); |
|
|
|
|
|
|
|
try { |
|
|
|
// 尝试获取配置
|
|
|
|
JSONObject setting = settingService.getBySettingKeyIgnoreTenant("mp-weixin", tenantId); |
|
|
|
// 尝试从缓存获取配置
|
|
|
|
JSONObject setting = getWxConfigFromCache(tenantId); |
|
|
|
result.put("hasConfig", true); |
|
|
|
result.put("config", setting); |
|
|
|
result.put("cacheKey", MP_WX_KEY + tenantId); |
|
|
|
} catch (Exception e) { |
|
|
|
result.put("hasConfig", false); |
|
|
|
result.put("error", e.getMessage()); |
|
|
|
result.put("cacheKey", MP_WX_KEY + tenantId); |
|
|
|
|
|
|
|
// 提供创建配置的建议
|
|
|
|
Map<String, Object> suggestion = new HashMap<>(); |
|
|
|
suggestion.put("message", "请在系统设置中创建微信小程序配置"); |
|
|
|
suggestion.put("configKey", "mp-weixin"); |
|
|
|
suggestion.put("message", "请在Redis中创建微信小程序配置"); |
|
|
|
suggestion.put("cacheKey", MP_WX_KEY + tenantId); |
|
|
|
suggestion.put("tenantId", tenantId); |
|
|
|
suggestion.put("sampleConfig", createSampleWxConfig()); |
|
|
|
result.put("suggestion", suggestion); |
|
|
@ -636,23 +639,21 @@ public class WxLoginController extends BaseController { |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
// 创建配置对象
|
|
|
|
Setting setting = new Setting(); |
|
|
|
setting.setSettingKey("mp-weixin"); |
|
|
|
setting.setTenantId(tenantId); |
|
|
|
|
|
|
|
// 直接在Redis中创建配置
|
|
|
|
String key = MP_WX_KEY + tenantId; |
|
|
|
|
|
|
|
// 创建配置内容
|
|
|
|
Map<String, String> config = new HashMap<>(); |
|
|
|
config.put("appId", appId); |
|
|
|
config.put("appSecret", appSecret); |
|
|
|
setting.setContent(JSON.toJSONString(config)); |
|
|
|
setting.setComments("微信小程序配置"); |
|
|
|
setting.setSortNumber(1); |
|
|
|
|
|
|
|
// 保存配置
|
|
|
|
settingService.save(setting); |
|
|
|
config.put("tenantId", tenantId.toString()); |
|
|
|
config.put("settingKey", "mp-weixin"); |
|
|
|
config.put("settingId", "301"); |
|
|
|
|
|
|
|
// 保存到Redis缓存
|
|
|
|
redisUtil.set(key, JSON.toJSONString(config)); |
|
|
|
|
|
|
|
return success("微信小程序配置创建成功", setting); |
|
|
|
return success("微信小程序配置创建成功", config); |
|
|
|
} catch (Exception e) { |
|
|
|
return fail("创建配置失败: " + e.getMessage(), null); |
|
|
|
} |
|
|
@ -674,15 +675,15 @@ public class WxLoginController extends BaseController { |
|
|
|
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()); |
|
|
@ -691,6 +692,24 @@ public class WxLoginController extends BaseController { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 从Redis缓存中获取微信小程序配置 |
|
|
|
* @param tenantId 租户ID |
|
|
|
* @return 微信配置信息 |
|
|
|
*/ |
|
|
|
private JSONObject getWxConfigFromCache(Integer tenantId) { |
|
|
|
String key = MP_WX_KEY + tenantId; |
|
|
|
String cacheValue = redisUtil.get(key); |
|
|
|
if (StrUtil.isBlank(cacheValue)) { |
|
|
|
throw new BusinessException("未找到微信小程序配置,请检查缓存key: " + key); |
|
|
|
} |
|
|
|
try { |
|
|
|
return JSON.parseObject(cacheValue); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new BusinessException("微信小程序配置格式错误: " + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 从scene参数中提取租户ID |
|
|
|
* scene格式可能是: uid_33103 或其他包含用户ID的格式 |
|
|
@ -732,12 +751,6 @@ public class WxLoginController extends BaseController { |
|
|
|
try { |
|
|
|
String key = ACCESS_TOKEN_KEY.concat(":").concat(tenantId.toString()); |
|
|
|
|
|
|
|
// 使用跨租户方式获取微信小程序配置信息
|
|
|
|
JSONObject setting = settingService.getBySettingKeyIgnoreTenant("mp-weixin", tenantId); |
|
|
|
if (setting == null) { |
|
|
|
throw new RuntimeException("租户 " + tenantId + " 的小程序未配置"); |
|
|
|
} |
|
|
|
|
|
|
|
// 从缓存获取access_token
|
|
|
|
String value = redisUtil.get(key); |
|
|
|
if (value != null) { |
|
|
@ -757,8 +770,9 @@ public class WxLoginController extends BaseController { |
|
|
|
} |
|
|
|
|
|
|
|
// 缓存中没有,重新获取
|
|
|
|
String appId = setting.getString("appId"); |
|
|
|
String appSecret = setting.getString("appSecret"); |
|
|
|
JSONObject wxConfig = getWxConfigFromCache(tenantId); |
|
|
|
String appId = wxConfig.getString("appId"); |
|
|
|
String appSecret = wxConfig.getString("appSecret"); |
|
|
|
|
|
|
|
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")); |
|
|
@ -766,13 +780,13 @@ public class WxLoginController extends BaseController { |
|
|
|
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) { |
|
|
|