From 1aca2f4b5d24fce0a60ac393732966863cc3a09f Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期二, 09 十二月 2025 18:47:38 +0800
Subject: [PATCH] 修改bug

---
 ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/staff/StaffLiveController.java |   83 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 83 insertions(+), 0 deletions(-)

diff --git a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/staff/StaffLiveController.java b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/staff/StaffLiveController.java
index ad947c4..840e11b 100644
--- a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/staff/StaffLiveController.java
+++ b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/controller/staff/StaffLiveController.java
@@ -1,19 +1,31 @@
 package com.ruoyi.goods.controller.staff;
 
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.http.HttpUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.redis.service.RedisService;
 import com.ruoyi.goods.domain.dto.XiaoeLiveQueryDto;
 import com.ruoyi.goods.domain.vo.XiaoeLiveVo;
 import com.ruoyi.goods.service.biz.XiaoeLiveService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestTemplate;
 
 import javax.validation.Valid;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author mitao
@@ -26,6 +38,9 @@
 public class StaffLiveController {
     private final XiaoeLiveService xiaoeLiveService;
 
+    @Autowired
+    private RedisService redisService;
+
     /**
      * 员工端直播列表
      * @param dto
@@ -36,4 +51,72 @@
     public R<Page<XiaoeLiveVo>> minePage(@Valid @RequestBody XiaoeLiveQueryDto dto) {
         return R.ok(xiaoeLiveService.getStaffLivePage(dto));
     }
+
+
+
+    @ApiOperation("获取直播ticket")
+    @PostMapping("/getLiveTicket")
+    public R<Map<String, String>> getLiveTicket(){
+        Object jsapi_ticket = redisService.getCacheObject("live:jsapi_ticket");
+        Object live_ticket = redisService.getCacheObject("live:ticket");
+        Map<String, String> map = new HashMap<>();
+        if(null != jsapi_ticket && null != live_ticket){
+            map.put("jsapi_ticket", jsapi_ticket.toString());
+            map.put("ticket", live_ticket.toString());
+            return R.ok(map);
+        }
+
+        Object ticket_access_token = redisService.getCacheObject("live:ticket_access_token");
+        String accessToken = "";
+        if(null == ticket_access_token){
+            HttpRequest get = HttpUtil.createGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww11400938eb1b91bc&corpsecret=-wuQ2EBxNT9BJa40LdpFqyxI_8RqrZTCUNiabzBasi8");
+            HttpResponse execute = get.execute();
+            int status = execute.getStatus();
+            if(200 == status){
+                JSONObject jsonObject = JSON.parseObject(execute.body());
+                Integer errcode = jsonObject.getInteger("errcode");
+                if(0 != errcode){
+                    return R.fail(jsonObject.getString("errcode"));
+                }
+                accessToken = jsonObject.getString("access_token");
+                Long expiresIn = jsonObject.getLong("expires_in");
+                redisService.setCacheObject("live:ticket_access_token", accessToken, expiresIn, TimeUnit.SECONDS);
+            }
+        }else{
+            accessToken = ticket_access_token.toString();
+        }
+
+        String jsapiTicket = "";
+        String ticket = "";
+        HttpRequest get = HttpUtil.createGet("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=" + accessToken);
+        HttpResponse execute = get.execute();
+        int status = execute.getStatus();
+        if(200 == status){
+            JSONObject jsonObject = JSON.parseObject(execute.body());
+            Integer errcode = jsonObject.getInteger("errcode");
+            if(0 != errcode){
+                return R.fail(jsonObject.getString("errcode"));
+            }
+            jsapiTicket = jsonObject.getString("ticket");
+            Long expiresIn = jsonObject.getLong("expires_in");
+            redisService.setCacheObject("live:jsapi_ticket", jsapiTicket, expiresIn, TimeUnit.SECONDS);
+        }
+
+        get = HttpUtil.createGet("https://qyapi.weixin.qq.com/cgi-bin/ticket/get?type=agent_config&access_token=" + accessToken);
+        execute = get.execute();
+        status = execute.getStatus();
+        if(200 == status){
+            JSONObject jsonObject = JSON.parseObject(execute.body());
+            Integer errcode = jsonObject.getInteger("errcode");
+            if(0 != errcode){
+                return R.fail(jsonObject.getString("errcode"));
+            }
+            ticket = jsonObject.getString("ticket");
+            Long expiresIn = jsonObject.getLong("expires_in");
+            redisService.setCacheObject("live:ticket", ticket, expiresIn, TimeUnit.SECONDS);
+        }
+        map.put("jsapi_ticket", jsapiTicket);
+        map.put("ticket", ticket);
+        return R.ok(map);
+    }
 }

--
Gitblit v1.7.1