9 changed files with 289 additions and 2 deletions
@ -0,0 +1,27 @@ |
|||||
|
package com.bnyer.system.config; |
||||
|
|
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.http.client.ClientHttpRequestFactory; |
||||
|
import org.springframework.http.client.SimpleClientHttpRequestFactory; |
||||
|
import org.springframework.web.client.RestTemplate; |
||||
|
|
||||
|
/** |
||||
|
* @author chengkun |
||||
|
* @date 2022/4/28 17:27 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
public class RestTemplateConfiguration { |
||||
|
@Bean |
||||
|
public RestTemplate restTemplate(ClientHttpRequestFactory factory) { |
||||
|
return new RestTemplate(factory); |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){ |
||||
|
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); |
||||
|
factory.setConnectTimeout(15000); |
||||
|
factory.setReadTimeout(5000); |
||||
|
return factory; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,49 @@ |
|||||
|
package com.bnyer.system.config; |
||||
|
|
||||
|
//import com.bnyer.img.handler.TokenCheckHandler;
|
||||
|
import lombok.Getter; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||
|
|
||||
|
/** |
||||
|
* 字节配置类 |
||||
|
* @author chengkun |
||||
|
* @date 2022/4/21 17:43 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
@ConfigurationProperties(prefix = "bnyer.system.tiktok") |
||||
|
@Getter |
||||
|
@RefreshScope |
||||
|
public class TiktokConfig implements WebMvcConfigurer { |
||||
|
|
||||
|
@Value("${bnyer.system.tiktok.appId}") |
||||
|
private String appId; |
||||
|
|
||||
|
@Value("${bnyer.system.tiktok.secret}") |
||||
|
private String secret; |
||||
|
|
||||
|
@Value("${bnyer.system.tiktok.adIncomeUrl}") |
||||
|
private String adIncomeUrl; |
||||
|
|
||||
|
@Value("${bnyer.system.tiktok.tokenUrl}") |
||||
|
private String tokenUrl; |
||||
|
|
||||
|
// @Resource
|
||||
|
// private TokenCheckHandler tokenCheckHandler;
|
||||
|
|
||||
|
/** 不需要拦截地址 */ |
||||
|
// public static final String[] excludeUrls = { "/mini/tiktok/loginTiktok", "/mini/tiktok/insertFeedback"};
|
||||
|
//
|
||||
|
// @Override
|
||||
|
// public void addInterceptors(InterceptorRegistry registry) {
|
||||
|
// // 注册Token拦截器
|
||||
|
// registry.addInterceptor(tokenCheckHandler)
|
||||
|
// .addPathPatterns("/**")
|
||||
|
// .excludePathPatterns(excludeUrls)
|
||||
|
// .order(-10);
|
||||
|
//
|
||||
|
// }
|
||||
|
} |
||||
@ -0,0 +1,39 @@ |
|||||
|
package com.bnyer.system.controller; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.bnyer.common.core.domain.Banner; |
||||
|
import com.bnyer.common.core.dto.BannerDto; |
||||
|
import com.bnyer.common.core.dto.BannerPageDto; |
||||
|
import com.bnyer.common.core.dto.StatusDto; |
||||
|
import com.bnyer.common.core.web.controller.BaseController; |
||||
|
import com.bnyer.common.core.web.domain.AjaxResult; |
||||
|
import com.bnyer.common.core.web.page.TableDataInfo; |
||||
|
import com.bnyer.system.service.IBannerService; |
||||
|
import com.bnyer.system.service.ITiktokService; |
||||
|
import com.github.pagehelper.PageHelper; |
||||
|
import io.swagger.annotations.Api; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import io.swagger.annotations.ApiParam; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.validation.annotation.Validated; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Api(value = "【图文平台】公共接口",tags = "【图文平台】公共接口") |
||||
|
@RestController |
||||
|
@RequestMapping("/system/common") |
||||
|
@Slf4j |
||||
|
public class CommonController extends BaseController { |
||||
|
|
||||
|
@Autowired |
||||
|
private ITiktokService tiktokService; |
||||
|
|
||||
|
//@RequiresPermissions("system:config:list")
|
||||
|
@ApiOperation(value="获取抖音广告首付详情") |
||||
|
@GetMapping(value = "/tiktokAdIncome") |
||||
|
public AjaxResult tiktokAdIncome(){ |
||||
|
return AjaxResult.success(tiktokService.getTiktokAdIncome()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
package com.bnyer.system.domain.vo; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonFormat; |
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Getter; |
||||
|
import lombok.Setter; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
|
||||
|
@Getter |
||||
|
@Setter |
||||
|
@ApiModel("抖音获取广告收益响应类") |
||||
|
public class TiktokAdIncomeVo implements Serializable { |
||||
|
|
||||
|
@ApiModelProperty(value="收入金额(单位分)") |
||||
|
private Integer income; |
||||
|
|
||||
|
|
||||
|
@JsonFormat(pattern = "yyyy-MM-dd") |
||||
|
@ApiModelProperty(value="收入日期") |
||||
|
private String date; |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
package com.bnyer.system.domain.vo; |
||||
|
|
||||
|
import com.alibaba.fastjson.annotation.JSONField; |
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Getter; |
||||
|
import lombok.Setter; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
|
||||
|
@Getter |
||||
|
@Setter |
||||
|
@ApiModel("抖音Token响应类") |
||||
|
public class TiktokTokenVo implements Serializable { |
||||
|
|
||||
|
@ApiModelProperty(value="token") |
||||
|
@JSONField(name = "access_token") |
||||
|
private String accessToken; |
||||
|
|
||||
|
@JSONField(name = "expires_in") |
||||
|
@ApiModelProperty(value="token有效时间") |
||||
|
private Integer expiresIn; |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package com.bnyer.system.service; |
||||
|
|
||||
|
import com.bnyer.system.domain.vo.TiktokAdIncomeVo; |
||||
|
import com.bnyer.system.domain.vo.TiktokTokenVo; |
||||
|
|
||||
|
public interface ITiktokService { |
||||
|
|
||||
|
/** |
||||
|
* 获取抖音token |
||||
|
* @return - |
||||
|
*/ |
||||
|
TiktokTokenVo getTiktokToken(); |
||||
|
|
||||
|
/** |
||||
|
* 查询抖音广告收入 |
||||
|
* @return - |
||||
|
*/ |
||||
|
TiktokAdIncomeVo getTiktokAdIncome(); |
||||
|
} |
||||
@ -0,0 +1,95 @@ |
|||||
|
package com.bnyer.system.service.impl; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.bnyer.common.core.constant.TiktokConstant; |
||||
|
import com.bnyer.common.core.exception.ServiceException; |
||||
|
import com.bnyer.system.config.TiktokConfig; |
||||
|
import com.bnyer.system.domain.vo.TiktokAdIncomeVo; |
||||
|
import com.bnyer.system.domain.vo.TiktokTokenVo; |
||||
|
import com.bnyer.system.service.ITiktokService; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.http.HttpEntity; |
||||
|
import org.springframework.http.HttpHeaders; |
||||
|
import org.springframework.http.HttpMethod; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.web.client.RestTemplate; |
||||
|
|
||||
|
import javax.annotation.Resource; |
||||
|
import java.text.SimpleDateFormat; |
||||
|
import java.util.Date; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Service |
||||
|
@Slf4j |
||||
|
public class TiktokServiceImpl implements ITiktokService { |
||||
|
|
||||
|
@Resource |
||||
|
private TiktokConfig tiktokConfig; |
||||
|
|
||||
|
@Autowired |
||||
|
private RestTemplate restTemplate; |
||||
|
|
||||
|
@Override |
||||
|
public TiktokTokenVo getTiktokToken() { |
||||
|
Map<String,String> map = new HashMap<>(); |
||||
|
map.put("appid",tiktokConfig.getAppId()); |
||||
|
map.put("secret", tiktokConfig.getSecret()); |
||||
|
map.put("grant_type", "client_credential"); |
||||
|
JSONObject tokenInfo = restTemplate.postForObject(tiktokConfig.getTokenUrl(), map, JSONObject.class); |
||||
|
if (tokenInfo != null) { |
||||
|
if(!tokenInfo.getString("err_no").equals(TiktokConstant.SUCCESS)){ |
||||
|
log.error("抖音获取token接口调用失败,错误状态码为:【{}】,错误信息为:【{}】",tokenInfo.getString("err_no"),tokenInfo.getString("err_tips")); |
||||
|
throw new ServiceException("抖音获取token接口调用失败!",TiktokConstant.TIKTOK_AUTH_ERROR); |
||||
|
} |
||||
|
}else{ |
||||
|
log.error("抖音获取token接口调用失败,错误信息为:【查无数据】"); |
||||
|
throw new ServiceException("抖音获取token接口调用失败!",TiktokConstant.TIKTOK_AUTH_ERROR); |
||||
|
} |
||||
|
//调用成功,组装返回数据
|
||||
|
JSONObject data = tokenInfo.getJSONObject("data"); |
||||
|
if(data != null){ |
||||
|
TiktokTokenVo result = new TiktokTokenVo(); |
||||
|
result.setAccessToken(data.getString("access_token")); |
||||
|
result.setExpiresIn(Integer.parseInt(data.getString("expires_in"))); |
||||
|
return result; |
||||
|
}else{ |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public TiktokAdIncomeVo getTiktokAdIncome() { |
||||
|
HttpHeaders headers = new HttpHeaders(); |
||||
|
headers.add("access-token",getTiktokToken().getAccessToken()); |
||||
|
Date yesterday = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24); |
||||
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
||||
|
String date = df.format(yesterday); |
||||
|
String url = tiktokConfig.getAdIncomeUrl() + "?start_date="+date+"&end_date="+date+"&host_name=douyin"; |
||||
|
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers); |
||||
|
JSONObject resultInfo = restTemplate.exchange(url, HttpMethod.GET, entity, JSONObject.class).getBody(); |
||||
|
if(resultInfo != null){ |
||||
|
if(!resultInfo.getString("err_no").equals(TiktokConstant.SUCCESS)){ |
||||
|
log.error("抖音获取广告收入接口调用失败,错误状态码为:【{}】,错误信息为:【{}】",resultInfo.getString("err_no"),resultInfo.getString("err_msg")); |
||||
|
throw new ServiceException("抖音获取广告收入接口调用失败!",TiktokConstant.TIKTOK_INCOME_ERROR); |
||||
|
} |
||||
|
}else{ |
||||
|
log.error("抖音获取广告收入接口调用失败,错误信息为:【查无数据】"); |
||||
|
throw new ServiceException("抖音获取广告收入接口调用失败!",TiktokConstant.TIKTOK_INCOME_ERROR); |
||||
|
} |
||||
|
//调用成功,组装返回数据
|
||||
|
JSONObject data = resultInfo.getJSONObject("data"); |
||||
|
if(data != null){ |
||||
|
JSONArray incomeList = data.getJSONArray("income_list"); |
||||
|
if(incomeList.size() > 0){ |
||||
|
TiktokAdIncomeVo result = new TiktokAdIncomeVo(); |
||||
|
result.setIncome(Integer.parseInt(incomeList.getJSONObject(0).getString("income"))); |
||||
|
result.setDate(incomeList.getJSONObject(0).getString("date")); |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue