|
|
|
@ -1,31 +1,20 @@ |
|
|
|
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 |
|
|
|
@ -40,56 +29,46 @@ public class WechatServiceImpl implements IWechatService { |
|
|
|
|
|
|
|
@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); |
|
|
|
JSONObject tokenInfo = null; |
|
|
|
try{ |
|
|
|
String url = wxConfig.getTokenUrl()+"?grant_type=client_credential&appid="+wxConfig.getAppId()+"&secret="+wxConfig.getSecret(); |
|
|
|
tokenInfo = restTemplate.getForObject(url, JSONObject.class); |
|
|
|
//调用成功,组装返回数据
|
|
|
|
if(tokenInfo != null){ |
|
|
|
WechatTokenVo result = new WechatTokenVo(); |
|
|
|
result.setAccessToken(tokenInfo.getString("access_token")); |
|
|
|
result.setExpiresIn(Integer.parseInt(tokenInfo.getString("expires_in"))); |
|
|
|
return result; |
|
|
|
}else { |
|
|
|
return null; |
|
|
|
} |
|
|
|
}else{ |
|
|
|
log.error("微信获取token接口调用失败,错误信息为:【查无数据】"); |
|
|
|
}catch (Exception e){ |
|
|
|
log.error("微信获取token接口调用失败,错误状态码为:【{}】,错误信息为:【{}】",tokenInfo.getString("errcode"),tokenInfo.getString("errmsg")); |
|
|
|
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){ |
|
|
|
JSONObject resultInfo = null; |
|
|
|
try{ |
|
|
|
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&access_token="+getWechatToken().getAccessToken()+"&page=1&page_size=1&start_date="+date+"&end_date="+date; |
|
|
|
resultInfo = restTemplate.getForObject(url, JSONObject.class); |
|
|
|
//调用成功,组装返回数据
|
|
|
|
if(resultInfo != null){ |
|
|
|
WechatAdIncomeVo result = new WechatAdIncomeVo(); |
|
|
|
result.setIncome(Integer.parseInt(incomeList.getJSONObject(0).getString("income"))); |
|
|
|
result.setDate(incomeList.getJSONObject(0).getString("date")); |
|
|
|
result.setIncome(Integer.parseInt(resultInfo.getJSONObject("summary").getString("income"))); |
|
|
|
result.setDate(date); |
|
|
|
return result; |
|
|
|
}else { |
|
|
|
return null; |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
log.error("微信获取广告收入接口调用失败,错误状态码为:【{}】,错误信息为:【{}】",resultInfo.getString("ret"),resultInfo.getString("err_msg")); |
|
|
|
throw new ServiceException("微信获取广告收入接口调用失败!",TiktokConstant.WECHAT_INCOME_ERROR); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|