7 changed files with 210 additions and 1 deletions
@ -0,0 +1,32 @@ |
|||
package com.bnyer.system.config; |
|||
|
|||
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; |
|||
|
|||
/** |
|||
* 微信配置类 |
|||
* @author chengkun |
|||
* @date 2022/4/21 17:43 |
|||
*/ |
|||
@Configuration |
|||
@ConfigurationProperties(prefix = "bnyer.system.wechat") |
|||
@Getter |
|||
@RefreshScope |
|||
public class WxConfig { |
|||
|
|||
@Value("${bnyer.system.wechat.appId}") |
|||
private String appId; |
|||
|
|||
@Value("${bnyer.system.wechat.secret}") |
|||
private String secret; |
|||
|
|||
@Value("${bnyer.system.wechat.adIncomeUrl}") |
|||
private String adIncomeUrl; |
|||
|
|||
@Value("${bnyer.system.wechat.tokenUrl}") |
|||
private String tokenUrl; |
|||
|
|||
} |
|||
@ -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 WechatAdIncomeVo 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 WechatTokenVo 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,21 @@ |
|||
package com.bnyer.system.service; |
|||
|
|||
import com.bnyer.system.domain.vo.TiktokAdIncomeVo; |
|||
import com.bnyer.system.domain.vo.TiktokTokenVo; |
|||
import com.bnyer.system.domain.vo.WechatAdIncomeVo; |
|||
import com.bnyer.system.domain.vo.WechatTokenVo; |
|||
|
|||
public interface IWechatService { |
|||
|
|||
/** |
|||
* 获取微信token |
|||
* @return - |
|||
*/ |
|||
WechatTokenVo getWechatToken(); |
|||
|
|||
/** |
|||
* 查询微信广告收入 |
|||
* @return - |
|||
*/ |
|||
WechatAdIncomeVo getWechatAdIncome(); |
|||
} |
|||
@ -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.config.WxConfig; |
|||
import com.bnyer.system.domain.vo.TiktokAdIncomeVo; |
|||
import com.bnyer.system.domain.vo.TiktokTokenVo; |
|||
import com.bnyer.system.domain.vo.WechatAdIncomeVo; |
|||
import com.bnyer.system.domain.vo.WechatTokenVo; |
|||
import com.bnyer.system.service.ITiktokService; |
|||
import com.bnyer.system.service.IWechatService; |
|||
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.http.ResponseEntity; |
|||
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 WechatServiceImpl implements IWechatService { |
|||
|
|||
@Resource |
|||
private WxConfig wxConfig; |
|||
|
|||
@Autowired |
|||
private RestTemplate restTemplate; |
|||
|
|||
|
|||
@Override |
|||
public WechatTokenVo getWechatToken() { |
|||
String url = wxConfig.getTokenUrl()+"?grant_type=client_credential&appid="+wxConfig.getAppId()+"&secret="+wxConfig.getSecret(); |
|||
JSONObject tokenInfo = restTemplate.getForEntity(url, JSONObject.class).getBody(); |
|||
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.WECHAT_AUTH_ERROR); |
|||
} |
|||
}else{ |
|||
log.error("微信获取token接口调用失败,错误信息为:【查无数据】"); |
|||
throw new ServiceException("微信获取token接口调用失败!",TiktokConstant.WECHAT_AUTH_ERROR); |
|||
} |
|||
//调用成功,组装返回数据
|
|||
JSONObject data = tokenInfo.getJSONObject("data"); |
|||
if(data != null){ |
|||
WechatTokenVo result = new WechatTokenVo(); |
|||
result.setAccessToken(data.getString("access_token")); |
|||
result.setExpiresIn(Integer.parseInt(data.getString("expires_in"))); |
|||
return result; |
|||
}else{ |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public WechatAdIncomeVo getWechatAdIncome() { |
|||
Date yesterday = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24); |
|||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
|||
String date = df.format(yesterday); |
|||
String url = wxConfig.getAdIncomeUrl() + "?action=publisher_adpos_general&"+getWechatToken().getAccessToken()+"&page=1&page_size=1&start_date="+date+"&end_date="+date; |
|||
JSONObject resultInfo = restTemplate.getForObject(url,JSONObject.class); |
|||
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){ |
|||
WechatAdIncomeVo result = new WechatAdIncomeVo(); |
|||
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