Pu Zhibing
2025-12-16 e00543d8fb4eb06cf5f4272fa1df432e5d8f953f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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 com.ruoyi.goods.utils.XiaoeUtils;
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
 * @date 2025/1/15
 */
@Api(tags = {"员工端直播相关接口"})
@RestController
@RequestMapping("/staff/live")
@RequiredArgsConstructor
public class StaffLiveController {
    private final XiaoeLiveService xiaoeLiveService;
 
    @Autowired
    private RedisService redisService;
 
    @Autowired
    private XiaoeUtils xiaoeUtils;
 
    /**
     * 员工端直播列表
     * @param dto
     * @return
     */
    @ApiOperation("直播列表")
    @PostMapping("/page")
    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);
    }
 
 
    @ApiOperation("获取开播口令")
    @PostMapping("/createLivePassword")
    public R<com.alibaba.fastjson2.JSONObject> createLivePassword(String id){
        com.alibaba.fastjson2.JSONObject livePassword = xiaoeUtils.createLivePassword(id);
        return R.ok(livePassword);
    }
}