瀏覽代碼

微信链接调用api更换

wangxy 2 周之前
父節點
當前提交
b7b25d87cb
共有 1 個文件被更改,包括 35 次插入30 次删除
  1. 35 30
      fs-service/src/main/java/com/fs/course/service/impl/FsCourseLinkServiceImpl.java

+ 35 - 30
fs-service/src/main/java/com/fs/course/service/impl/FsCourseLinkServiceImpl.java

@@ -828,14 +828,6 @@ public class FsCourseLinkServiceImpl implements IFsCourseLinkService
      */
     @Override
     public String getGotoWxAppLink(String linkStr, String appId) {
-        // URL Link缓存:避免重复生成导致85406访问限制
-        String cacheKey = buildUrlLinkCacheKey(linkStr);
-        String cached = redisCache.getCacheObject(cacheKey);
-        if (StringUtils.isNotEmpty(cached)) {
-            log.info("URL Link命中缓存,linkStr:{}", linkStr);
-            return cached;
-        }
-
         Long companyId = parseCompanyIdFromLink(linkStr);
 
         List<String> appIdList = getAppIdListByCompanyId(companyId);
@@ -849,6 +841,13 @@ public class FsCourseLinkServiceImpl implements IFsCourseLinkService
             currentIndex = appIdList.indexOf(appId);
         }
 
+        String cacheKey = buildUrlLinkCacheKey(linkStr, appIdList, appIdList.get(currentIndex));
+        String cached = redisCache.getCacheObject(cacheKey);
+        if (StringUtils.isNotEmpty(cached)) {
+            log.info("Scheme Link命中缓存,linkStr:{},appIdList:{}", linkStr, appIdList);
+            return cached;
+        }
+
         String result = generateWxAppLinkWithRetry(linkStr, appIdList, currentIndex);
         cacheUrlLinkIfSuccess(cacheKey, result);
         return result;
@@ -866,19 +865,19 @@ public class FsCourseLinkServiceImpl implements IFsCourseLinkService
     }
 
     /**
-     * 构建URL Link缓存key
+     * 构建Scheme Link缓存key
      */
-    private String buildUrlLinkCacheKey(String linkStr) {
-        return "wx:url_link:" + SecureUtil.md5(linkStr);
+    private String buildUrlLinkCacheKey(String linkStr, List<String> appIdList, String currentAppId) {
+        return "wx:scheme_link:" + SecureUtil.md5(linkStr + "|" + String.join(",", appIdList) + "|" + currentAppId);
     }
 
     /**
-     * 生成成功时缓存URL Link,有效期25天(微信URL Link有效期为30天)
+     * 生成成功时缓存Scheme Link,有效期25天(微信Scheme Link有效期为30天)
      */
     private void cacheUrlLinkIfSuccess(String cacheKey, String result) {
-        if (StringUtils.isNotEmpty(result) && result.contains("链接1:")) {
+        if (StringUtils.isNotEmpty(result) && result.startsWith("weixin://")) {
             redisCache.setCacheObject(cacheKey, result, 25, TimeUnit.DAYS);
-            log.info("URL Link已缓存,cacheKey:{}", cacheKey);
+            log.info("Scheme Link已缓存,cacheKey:{}", cacheKey);
         }
     }
 
@@ -1039,10 +1038,16 @@ public class FsCourseLinkServiceImpl implements IFsCourseLinkService
             final WxMaService wxService = WxMaConfiguration.getMaService(currentAppId);
             String token = wxService.getAccessToken();
             log.info("小程序TOKEN值-------->刷新前TOKEN:{}", token);
-            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/generate_urllink?access_token=" + token);
+            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/generatescheme?access_token=" + token);
             JSONObject bodyObj = new JSONObject();
-            bodyObj.put("path", pageUrl);
-            bodyObj.put("query", query);
+            JSONObject jumpWxa = new JSONObject();
+            jumpWxa.put("path", pageUrl);
+            jumpWxa.put("query", query);
+            jumpWxa.put("env_version", "release");
+            bodyObj.put("jump_wxa", jumpWxa);
+            bodyObj.put("is_expire", false);
+            bodyObj.put("expire_type", 1);
+            bodyObj.put("expire_interval", 30);
             log.info("微信小程序请求参数打印:{}", bodyObj.toJSONString());
             StringEntity entity = new StringEntity(bodyObj.toJSONString(),"UTF-8");
             httpPost.setEntity(entity);
@@ -1066,27 +1071,27 @@ public class FsCourseLinkServiceImpl implements IFsCourseLinkService
                 }
             }
 
-            if(null != jsonObject && !jsonObject.isEmpty() && jsonObject.containsKey("url_link")){
-                String urlLink = jsonObject.getString("url_link");
-                String mpUrlLink = urlLink.replace("https://wxaurl.cn/", "https://wxmpurl.cn/");
-                return "链接1:" + urlLink + "\n链接2:" + mpUrlLink;
+            if(null != jsonObject && !jsonObject.isEmpty() && jsonObject.containsKey("openlink")){
+                return jsonObject.getString("openlink");
             }
             
             if(jsonObject != null && jsonObject.containsKey("errcode")) {
                 Integer errcode = jsonObject.getInteger("errcode");
                 String errmsg = jsonObject.getString("errmsg");
-                log.error("微信小程序生成链接失败,appId:{},错误码:{},错误信息:{}", currentAppId, errcode, errmsg);
-                
-                if (currentIndex + 1 < appIdList.size()) {
-                    log.info("切换到下一个appId重试,下一个索引:{}", currentIndex + 1);
-                    return generateWxAppLinkWithRetry(linkStr, appIdList, currentIndex + 1);
+                if (errcode != null && errcode != 0) {
+                    log.error("微信小程序生成链接失败,appId:{},错误码:{},错误信息:{}", currentAppId, errcode, errmsg);
+                    
+                    if (currentIndex + 1 < appIdList.size()) {
+                        log.info("切换到下一个appId重试,下一个索引:{}", currentIndex + 1);
+                        return generateWxAppLinkWithRetry(linkStr, appIdList, currentIndex + 1);
+                    }
+                    
+                    return "微信错误:" + errcode + "|" + errmsg;
                 }
-                
-                return "微信错误:" + errcode + "|" + errmsg;
             }
 
-            log.error("微信小程序响应格式异常");
-            return "微信小程序响应格式异常";
+            log.error("微信小程序响应格式异常,未返回openlink");
+            return "微信小程序响应格式异常,未返回openlink";
 
         } catch (WxErrorException e) {
             log.error("微信API调用异常,appId:{}", currentAppId, e);