|
|
@@ -789,6 +789,102 @@ public class FsCourseLinkServiceImpl implements IFsCourseLinkService
|
|
|
// String miniprogramAppid = config.getMiniprogramAppid();
|
|
|
// if (StringUtils.isBlank(miniprogramAppid)) {
|
|
|
// return "未配置点播小程序id";
|
|
|
+// }
|
|
|
+ //获取微信token
|
|
|
+ final WxMaService wxService = WxMaConfiguration.getMaService(appId);
|
|
|
+ String token = wxService.getAccessToken();
|
|
|
+ log.info("小程序TOKEN值-------->刷新前TOKEN:{}", token);
|
|
|
+ HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/generatescheme?access_token=" + token);
|
|
|
+ JSONObject bodyObj = new JSONObject();
|
|
|
+ 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);
|
|
|
+ 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 getGotoWxAppLink(linkStr,appId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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("微信小程序生成链接失败,错误码:{},错误信息:{}", 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取跳转微信小程序的链接地址 老链接方法
|
|
|
+ * @param linkStr
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getGotoWxAppLinkOld(String linkStr,String appId) {
|
|
|
+ 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());
|
|
|
+// String json = configService.selectConfigByKey("course.config");
|
|
|
+// CourseConfig config = JSON.parseObject(json, CourseConfig.class);
|
|
|
+// String miniprogramAppid = config.getMiniprogramAppid();
|
|
|
+// if (StringUtils.isBlank(miniprogramAppid)) {
|
|
|
+// return "未配置点播小程序id";
|
|
|
// }
|
|
|
//获取微信token
|
|
|
final WxMaService wxService = WxMaConfiguration.getMaService(appId);
|
|
|
@@ -824,7 +920,7 @@ public class FsCourseLinkServiceImpl implements IFsCourseLinkService
|
|
|
if(null != jsonObject && !jsonObject.isEmpty() && jsonObject.containsKey("url_link")){
|
|
|
return jsonObject.getString("url_link");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 处理微信错误响应
|
|
|
if(jsonObject != null && jsonObject.containsKey("errcode")) {
|
|
|
Integer errcode = jsonObject.getInteger("errcode");
|
|
|
@@ -832,7 +928,7 @@ public class FsCourseLinkServiceImpl implements IFsCourseLinkService
|
|
|
log.error("微信小程序生成链接失败,错误码:{},错误信息:{}", errcode, errmsg);
|
|
|
throw new RuntimeException("微信错误:" + errcode + "|" + errmsg);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 响应格式异常
|
|
|
log.error("微信小程序响应格式异常");
|
|
|
throw new RuntimeException("微信小程序响应格式异常");
|