hejianhao
2026-08-19 e79bcd07eec1dab0f7feb6ee2906d3913c250bb9
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
package com.tcm.training.admin.app.home.service;
 
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.json.JSONObject;
import com.tcm.training.admin.app.home.vo.AppVzanLiveEntryVO;
import com.tcm.training.admin.live.service.LiveRoomSettingService;
import com.tcm.training.admin.live.vo.LiveRoomSettingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;
 
import java.net.URI;
import java.nio.charset.StandardCharsets;
 
@Slf4j
@Service
public class AppVzanLiveEntryService {
    // TODO 以下参数由微赞提供,联调前替换;正式环境应迁移至环境变量或配置中心。
//    private static final String VZAN_AUTH_SECRET = "e70ccf6a-1745-42b4-be41-aaed6f65700f";
    private static final String VZAN_AUTH_SECRET = "2b4fefe91d8cba50";
    private static final String VZAN_TUID = "9594";
    private static final String followPhone = "18912345680";
    private static final String VZAN_NICKNAME = "微赞测试用户";
    private static final String VZAN_AVATAR = "https://static3.njyqkj0ksyz.com/zhibo/images/defaultuser.jpg";
 
    private final LiveRoomSettingService liveRoomSettingService;
 
    public AppVzanLiveEntryService(LiveRoomSettingService liveRoomSettingService) {
        this.liveRoomSettingService = liveRoomSettingService;
    }
 
    public AppVzanLiveEntryVO createEntry() {
        validateStaticParameters();
        LiveRoomSettingVO setting = liveRoomSettingService.current();
        String liveRoomUrl = setting == null ? null : StrUtil.trim(setting.getLiveRoomUrl());
        validateLiveRoomUrl(liveRoomUrl);
 
        JSONObject payload = new JSONObject();
        payload.set("tuid", VZAN_TUID);
        payload.set("followPhone", followPhone);
        payload.set("third_ts", System.currentTimeMillis() + 5000);
        if (StrUtil.isNotBlank(VZAN_NICKNAME)) {
            payload.set("nickname", VZAN_NICKNAME);
        }
        if (StrUtil.isNotBlank(VZAN_AVATAR)) {
            payload.set("avatar", VZAN_AVATAR);
        }
 
        SymmetricCrypto sm4 = SmUtil.sm4(VZAN_AUTH_SECRET.getBytes(StandardCharsets.UTF_8));
        String encryptedData = sm4.encryptBase64(payload.toString(), StandardCharsets.UTF_8);
        String entryUrl = UriComponentsBuilder.fromHttpUrl(liveRoomUrl)
                .replaceQueryParam("data", encryptedData)
                .build()
                .encode(StandardCharsets.UTF_8)
                .toUriString();
        log.info("微赞直播生成URL: {}", entryUrl);
        return new AppVzanLiveEntryVO(entryUrl);
    }
 
    private void validateStaticParameters() {
        if (VZAN_AUTH_SECRET.getBytes(StandardCharsets.UTF_8).length != 16) {
            throw new IllegalStateException("微赞鉴权密钥必须为16字节");
        }
        if (StrUtil.isBlank(VZAN_TUID)) {
            throw new IllegalStateException("微赞用户标识暂未配置");
        }
    }
 
    private void validateLiveRoomUrl(String liveRoomUrl) {
        if (StrUtil.isBlank(liveRoomUrl)) {
            throw new IllegalArgumentException("直播间地址暂未配置");
        }
        try {
            URI uri = URI.create(liveRoomUrl);
            if (!"https".equalsIgnoreCase(uri.getScheme()) || StrUtil.isBlank(uri.getHost())) {
                throw new IllegalArgumentException("直播间地址必须为有效的HTTPS地址");
            }
        } catch (IllegalArgumentException ex) {
            throw new IllegalArgumentException("直播间地址必须为有效的HTTPS地址", ex);
        }
    }
}