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);
|
}
|
}
|
}
|