feat(admin): 添加声网服务和广告接口
- 新增 AdvertisementController,实现广告相关接口 - 添加 HttpClientConfig 配置类,配置 HTTP 客户端 - 新增 ShengWangChannelDTO、ShengWangChannelInfoDTO 和 ShengWangResponse 数据传输对象 - 实现 ShengWangService,提供声网服务相关功能
This commit is contained in:
parent
777b596836
commit
8ed6bdae9e
|
|
@ -0,0 +1,19 @@
|
|||
package com.nflg.mobilebroken.admin.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.net.http.HttpClient;
|
||||
import java.time.Duration;
|
||||
|
||||
@Configuration
|
||||
public class HttpClientConfig {
|
||||
|
||||
@Bean
|
||||
public HttpClient getHttpClient() {
|
||||
return HttpClient.newBuilder()
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
.connectTimeout(Duration.ofSeconds(10))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package com.nflg.mobilebroken.admin.pojo.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ShengWangChannelDTO {
|
||||
|
||||
/**
|
||||
* 频道列表
|
||||
*/
|
||||
private List<ShengWangChannelInfoDTO> channels;
|
||||
|
||||
/**
|
||||
* 频道总数
|
||||
*/
|
||||
@JsonProperty("total_size")
|
||||
private int TotalSize;
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.nflg.mobilebroken.admin.pojo.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ShengWangChannelInfoDTO {
|
||||
|
||||
/**
|
||||
* 频道名
|
||||
*/
|
||||
@JsonProperty("channel_name")
|
||||
private String ChannelName;
|
||||
|
||||
/**
|
||||
* 频道内的用户人数
|
||||
*/
|
||||
@JsonProperty("user_count")
|
||||
private Integer UserCount;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.nflg.mobilebroken.admin.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ShengWangResponse<T>{
|
||||
|
||||
private boolean success;
|
||||
|
||||
private T data;
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
package com.nflg.mobilebroken.admin.service;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nflg.mobilebroken.admin.pojo.dto.ShengWangChannelDTO;
|
||||
import com.nflg.mobilebroken.admin.pojo.dto.ShengWangResponse;
|
||||
import com.nflg.mobilebroken.common.util.VUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* 声网服务
|
||||
*/
|
||||
@Component
|
||||
@RefreshScope
|
||||
public class ShengWangService {
|
||||
|
||||
private static final String baseUrl="https://api.sd-rtn.com/dev";
|
||||
|
||||
@Resource
|
||||
private HttpClient httpClient;
|
||||
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Value("${shengwang.rtc.appId}")
|
||||
private String appId;
|
||||
|
||||
@Value("${shengwang.rtc.customerKey}")
|
||||
private String customerKey;
|
||||
|
||||
@Value("${shengwang.rtc.customerSecret}")
|
||||
private String customerSecret;
|
||||
|
||||
public ShengWangChannelDTO getAllChannels() throws Exception {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.GET()
|
||||
.header("Authorization",generateAuthorization(customerKey,customerSecret))
|
||||
.uri(URI.create(baseUrl+"/v1/channel/"+appId))
|
||||
.build();
|
||||
HttpResponse<String> response=httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
VUtils.trueThrowBusinessError(response.statusCode()!=200).throwMessage("");
|
||||
ShengWangResponse<ShengWangChannelDTO> result= objectMapper.readValue(response.body(), new TypeReference<>() {});
|
||||
VUtils.trueThrowBusinessError(!result.isSuccess()).throwMessage("获取频道信息失败");
|
||||
return result.getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成鉴权信息
|
||||
* @param customerKey 客户ID
|
||||
* @param customerSecret 客户密钥
|
||||
* @return 鉴权信息
|
||||
*/
|
||||
private String generateAuthorization(String customerKey,String customerSecret){
|
||||
// 拼接客户 ID 和客户密钥并使用 base64 编码
|
||||
String plainCredentials = customerKey + ":" + customerSecret;
|
||||
String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
|
||||
// 创建 authorization header
|
||||
return "Basic " + base64Credentials;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package com.nflg.mobilebroken.cfs.controller;
|
||||
|
||||
import com.nflg.mobilebroken.common.pojo.ApiResult;
|
||||
import com.nflg.mobilebroken.common.pojo.request.AdvertisementRequst;
|
||||
import com.nflg.mobilebroken.common.pojo.vo.AdvertisementVO;
|
||||
import com.nflg.mobilebroken.repository.service.IAdvertisementService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 广告相关接口
|
||||
* @author 曹鹏飞
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/ad")
|
||||
public class AdvertisementController {
|
||||
|
||||
@Resource
|
||||
private IAdvertisementService advertisementService;
|
||||
|
||||
/**
|
||||
* 根据类型获取广告
|
||||
* @param request 请求参数
|
||||
* @return 广告列表
|
||||
*/
|
||||
@GetMapping("getByType")
|
||||
public ApiResult<AdvertisementVO> getAdvertisement(@Valid @RequestBody @NotNull AdvertisementRequst request){
|
||||
return ApiResult.success(advertisementService.getByType(request.getType(),request.getPosition()));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue