Quellcode durchsuchen

生成短链接口

yuhongqi vor 2 Tagen
Ursprung
Commit
dae714ab72

+ 13 - 0
fs-company-app/src/main/java/com/fs/app/controller/FsUserCourseVideoController.java

@@ -31,6 +31,7 @@ import com.fs.im.service.IFsImMsgSendDetailService;
 import com.fs.im.service.IFsImMsgSendLogService;
 import com.fs.im.service.OpenIMService;
 import com.fs.im.vo.FsImMsgSendLogVO;
+import com.fs.live.service.ILiveService;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import io.swagger.annotations.Api;
@@ -76,6 +77,8 @@ public class FsUserCourseVideoController extends AppBaseController {
 
     @Autowired
     private IFsImMsgSendLogService imMsgSendLogService;
+    @Autowired
+    private ILiveService liveService;
 
     @Login
     @GetMapping("/pageList")
@@ -270,6 +273,16 @@ public class FsUserCourseVideoController extends AppBaseController {
         return ResponseResult.ok(courseLinkService.getGotoWxAppLink(linkStr,appid));
     }
 
+    /**
+     * 获取跳转微信小程序的链接地址
+     */
+    @Login
+    @GetMapping("/getGotoWxAppLiveLink")
+    @ApiOperation("获取跳转微信小程序直播的链接地址")
+    public ResponseResult<String> getGotoWxAppLiveLink(String linkStr,String appid) {
+        return ResponseResult.ok(liveService.getGotoWxAppLiveLink(linkStr,appid));
+    }
+
     @ApiOperation("会员批量发送课程消息")
     @PostMapping("/batchSendCourse")
     public OpenImResponseDTO batchSendCourse(@RequestBody BatchSendCourseDTO batchSendCourseDTO) throws JsonProcessingException {

+ 2 - 0
fs-service/src/main/java/com/fs/live/service/ILiveService.java

@@ -187,4 +187,6 @@ public interface ILiveService
     int updateLiveEntity(Live live);
 
     void updateGlobalVisible(long liveId, Integer status);
+
+    String getGotoWxAppLiveLink(String linkStr, String appid);
 }

+ 85 - 0
fs-service/src/main/java/com/fs/live/service/impl/LiveServiceImpl.java

@@ -1,5 +1,6 @@
 package com.fs.live.service.impl;
 
+import cn.binarywang.wx.miniapp.api.WxMaService;
 import cn.hutool.core.thread.ThreadUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.alibaba.fastjson.JSON;
@@ -7,6 +8,7 @@ import com.alibaba.fastjson.JSONObject;
 
 import com.fs.common.exception.base.BaseException;
 import com.fs.company.mapper.CompanyMapper;
+import com.fs.core.config.WxMaConfiguration;
 import com.fs.his.domain.FsStoreProduct;
 import com.fs.his.domain.FsUser;
 import com.fs.his.mapper.FsUserMapper;
@@ -31,16 +33,26 @@ import com.fs.system.domain.SysConfig;
 import com.fs.system.service.ISysConfigService;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
 import okhttp3.FormBody;
 import okhttp3.OkHttpClient;
 import okhttp3.Request;
 import okhttp3.Response;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import com.fs.common.utils.sign.Md5Utils;
 
 import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.time.temporal.ChronoUnit;
@@ -101,6 +113,10 @@ public class LiveServiceImpl implements ILiveService
     @Autowired
     private LiveCouponMapper liveCouponMapper;
 
+    private static String TOKEN_VALID_CODE = "40001";
+
+    private volatile Integer version = 0;
+
 
 
     /**
@@ -363,6 +379,75 @@ public class LiveServiceImpl implements ILiveService
         baseMapper.updateGlobalVisible(liveId, status);
     }
 
+    @Override
+    public String getGotoWxAppLiveLink(String linkStr, String appId) {
+        CloseableHttpClient client = null;
+        try {
+            client = HttpClients.createDefault();
+            String[] split = linkStr.split("\\?");
+            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 = 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);
+                String token = wxService.getAccessToken();
+                log.info("小程序TOKEN值-------->刷新前TOKEN:{}", token);
+                HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/generate_urllink?access_token=" + token);
+                JSONObject bodyObj = new JSONObject();
+                bodyObj.put("path", pageUrl);
+                bodyObj.put("query", query);
+                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 getGotoWxAppLiveLink(linkStr,appId);
+                    }
+                }
+
+                if(null != jsonObject && !jsonObject.isEmpty() && jsonObject.containsKey("url_link")){
+                    return jsonObject.getString("url_link");
+                }
+            } else {
+                return "页面链接错误,获取失败";
+            }
+
+        } catch (WxErrorException e) {
+            throw new RuntimeException(e);
+        } catch (ClientProtocolException e) {
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        return "";
+    }
+
     /**
      * 修改直播
      *