|
|
@@ -948,6 +948,113 @@ public class FsCourseLinkServiceImpl implements IFsCourseLinkService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String getGotoWxAppLinkNew(String linkStr, String appId, Integer type) {
|
|
|
+ CloseableHttpClient client = null;
|
|
|
+ try {
|
|
|
+ client = HttpClients.createDefault();
|
|
|
+ String[] split = linkStr.split("\\?.*?=");
|
|
|
+ String key = linkStr.replaceAll(".*\\?(.*?)=.*", "$1");
|
|
|
+ if (split.length == 2 && split[0].length() > 0 && split[1].length() > 0) {
|
|
|
+ //处理页面路径
|
|
|
+ String pageUrl = split[0];
|
|
|
+ if (pageUrl.startsWith("/")) {
|
|
|
+ pageUrl = pageUrl.substring(1);
|
|
|
+ }
|
|
|
+ //处理参数
|
|
|
+ String query = split[1];
|
|
|
+ query = key + "=" + URLEncoder.encode(query, StandardCharsets.UTF_8.toString());
|
|
|
+ //获取微信token
|
|
|
+ final WxMaService wxService = WxMaConfiguration.getMaService(appId);
|
|
|
+ String token = wxService.getAccessToken();
|
|
|
+ log.info("小程序TOKEN值-------->刷新前TOKEN:{}", token);
|
|
|
+
|
|
|
+ JSONObject bodyObj = new JSONObject();
|
|
|
+ String apiUrl;
|
|
|
+ String responseField;
|
|
|
+ if ( type == null || type == 3) {
|
|
|
+ // 新链接:generatescheme
|
|
|
+ apiUrl = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" + token;
|
|
|
+ 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);
|
|
|
+ responseField = "openlink";
|
|
|
+ } else {
|
|
|
+ // 老链接:generate_urllink
|
|
|
+ apiUrl = "https://api.weixin.qq.com/wxa/generate_urllink?access_token=" + token;
|
|
|
+ bodyObj.put("path", pageUrl);
|
|
|
+ bodyObj.put("query", query);
|
|
|
+ responseField = "url_link";
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(apiUrl);
|
|
|
+ log.info("微信小程序请求参数打印:{}", bodyObj.toJSONString());
|
|
|
+ StringEntity entity = new StringEntity(bodyObj.toJSONString(), "UTF-8");
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ httpPost.setHeader("Content-type", "application/json");
|
|
|
+ httpPost.setHeader("cache-control", "max-age=0");
|
|
|
+ HttpEntity response = client.execute(httpPost).getEntity();
|
|
|
+ String responseString = EntityUtils.toString(response);
|
|
|
+ log.info("微信小程序接口响应数据:{}", responseString);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(responseString);
|
|
|
+
|
|
|
+ if (TOKEN_VALID_CODE.equals(jsonObject.getString("errcode"))) {
|
|
|
+ Integer curVersion = Integer.valueOf(version);
|
|
|
+ synchronized (TOKEN_VALID_CODE) {
|
|
|
+ if (curVersion.equals(version)) {
|
|
|
+ log.info("小程序TOKEN:40001进入强制刷新-------->刷新前TOKEN:{}", token);
|
|
|
+ wxService.getAccessToken(true);
|
|
|
+ version = version.equals(Integer.MAX_VALUE) ? 0 : curVersion + 1;
|
|
|
+ log.info("小程序TOKEN:40001进入强制刷新-------->刷新后TOKEN:{}", wxService.getAccessToken());
|
|
|
+ }
|
|
|
+ return getGotoWxAppLinkNew(linkStr, appId, type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (null != jsonObject && !jsonObject.isEmpty() && jsonObject.containsKey(responseField)) {
|
|
|
+ String link = jsonObject.getString(responseField);
|
|
|
+ // https://wxaurl.cn/HP0Hx7Fnjdm https://wxmpurl.cn/HP0Hx7Fnjdm
|
|
|
+ if(type!=null && type==1){
|
|
|
+ link = link.replace("wxmpurl.cn", "wxaurl.cn");
|
|
|
+ }
|
|
|
+ if(type!=null && type==2){
|
|
|
+ link = link.replace("wxaurl.cn", "wxmpurl.cn");
|
|
|
+ }
|
|
|
+ return link;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理微信错误响应
|
|
|
+ if (jsonObject != null && jsonObject.containsKey("errcode")) {
|
|
|
+ Integer errcode = jsonObject.getInteger("errcode");
|
|
|
+ String errmsg = jsonObject.getString("errmsg");
|
|
|
+ log.error("微信小程序生成链接失败,错误码:{},错误信息:{}", errcode, errmsg);
|
|
|
+ throw new RuntimeException("微信错误:" + errcode + "|" + errmsg);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 响应格式异常
|
|
|
+ log.error("微信小程序响应格式异常");
|
|
|
+ throw new RuntimeException("微信小程序响应格式异常");
|
|
|
+ } else {
|
|
|
+ return "页面链接错误,获取失败";
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ log.error("微信API调用异常", e);
|
|
|
+ throw new RuntimeException("微信API异常", e);
|
|
|
+ } catch (ClientProtocolException e) {
|
|
|
+ log.error("HTTP协议错误", e);
|
|
|
+ throw new RuntimeException("网络请求失败", e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("IO异常", e);
|
|
|
+ throw new RuntimeException("网络连接失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public R getWxaCodeGenerateScheme(String linkStr,String appId) {
|
|
|
CloseableHttpClient client = null;
|