|
|
package com.yoho.unions.server.task;
|
|
|
|
|
|
import com.yoho.service.model.union.response.UnionResponse;
|
|
|
import com.yoho.unions.common.enums.ClientTypeEnum;
|
|
|
import com.yoho.unions.common.utils.DateUtil;
|
|
|
import com.yoho.unions.common.utils.HttpUtils;
|
|
|
import com.yoho.unions.dal.IUnionActivityDAO;
|
|
|
import com.yoho.unions.dal.IUnionConfigDAO;
|
|
|
import com.yoho.unions.dal.model.UnionActivity;
|
|
|
import com.yoho.unions.dal.model.UnionConfig;
|
|
|
import com.yoho.unions.server.service.IUnionOrderService;
|
|
|
import com.yoho.core.common.utils.MD5;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
import com.yoho.unions.vo.TransInfo;
|
|
|
import com.yoho.unions.vo.UnionOrderReqVO;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 广点通转化数据上报,定时从大数据抽取数据,然后联盟拼接,上报给广点通
|
|
|
* Created by yoho on 2017/2/22.
|
|
|
*/
|
|
|
@Component
|
|
|
public class GdtTransTask {
|
|
|
|
|
|
static Logger log = LoggerFactory.getLogger(GdtTransTask.class);
|
|
|
|
|
|
@Autowired
|
|
|
private IUnionOrderService unionOrderService;
|
|
|
|
|
|
@Resource
|
|
|
IUnionActivityDAO unionActivityDAO;
|
|
|
|
|
|
@Resource
|
|
|
IUnionConfigDAO unionConfigDAO;
|
|
|
|
|
|
@Scheduled(cron = "* 0/5 * * * ?")
|
|
|
public void run() {
|
|
|
UnionOrderReqVO reqVO = new UnionOrderReqVO();
|
|
|
reqVO.setLimit(20);
|
|
|
this.execute(reqVO);
|
|
|
}
|
|
|
|
|
|
public void execute(UnionOrderReqVO reqVO){
|
|
|
//从大数据中取出联盟需要的广点通带来转化的设备号,和金额
|
|
|
|
|
|
List<TransInfo> transInfoList = unionOrderService.getTrandInfo(reqVO);
|
|
|
if (CollectionUtils.isEmpty(transInfoList)) {
|
|
|
return;
|
|
|
}
|
|
|
//将取出来的数据进行拼接
|
|
|
for(TransInfo transInfo:transInfoList){
|
|
|
log.info("enter trans udid is {},ordercode is {}",transInfo.getUdid(),transInfo.getOrdercode());
|
|
|
String udid = transInfo.getUdid();
|
|
|
//利用udid去表中查出是两个广点通账号带来的
|
|
|
UnionActivity unionActivity = unionActivityDAO.selectByUdid(udid);
|
|
|
|
|
|
if(unionActivity==null){
|
|
|
return;
|
|
|
}
|
|
|
//按照广点通要求拼接数据
|
|
|
String url = urlEode(unionActivity,transInfo);
|
|
|
//改成httpclient方式调用
|
|
|
Pair<Integer, String> pair = HttpUtils.httpGet(url);
|
|
|
log.info("GdtTransTask call union success url={}, and result={}", url, pair);
|
|
|
if (pair.getLeft() != 200) {
|
|
|
log.warn("callback error with request={}", url);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public String urlEode(UnionActivity requestBO,TransInfo transInfo) {
|
|
|
log.info("enter GdtTransTask.urlEode requestBO is {}, transInfo is {} ", requestBO,transInfo);
|
|
|
// 根据不同的应用类型,获取不同的加密密钥和签名密钥
|
|
|
String encryptKey = "BAAAAAAAAAAABZJQ";
|
|
|
String signKey = "1461f1237d22dfc7";
|
|
|
String muid4MD5 = requestBO.getMuid();
|
|
|
String url = "http://t.gdt.qq.com/conv/app/";
|
|
|
//有货在智慧推的账号id
|
|
|
String uid = "365136";
|
|
|
if(StringUtils.isNotEmpty(requestBO.getAdvertiserId())){
|
|
|
uid = requestBO.getAdvertiserId();
|
|
|
}
|
|
|
String clientType = ClientTypeEnum.IOS.getName();
|
|
|
if(StringUtils.isNotEmpty(requestBO.getClientType())){
|
|
|
clientType = requestBO.getClientType().toLowerCase();
|
|
|
}
|
|
|
UnionConfig unionConfig = unionConfigDAO.selectByUidAndClientType(Integer.valueOf(uid),clientType);
|
|
|
if(null!=unionConfig){
|
|
|
encryptKey = unionConfig.getEncryptKey();
|
|
|
signKey = unionConfig.getSignKey();
|
|
|
url = unionConfig.getUrl();
|
|
|
}
|
|
|
//APPID可以固定根据clientType判断
|
|
|
String appId = "490655927";
|
|
|
if(clientType.equals(ClientTypeEnum.ANDROID.getName())){
|
|
|
appId = "100898104";
|
|
|
}
|
|
|
int conv_time = DateUtil.getCurrentTimeSecond();
|
|
|
String page = null;
|
|
|
String query_string = null;
|
|
|
String clickId = requestBO.getClickId();
|
|
|
StringBuffer bf = new StringBuffer();
|
|
|
StringBuffer sf = new StringBuffer();
|
|
|
page = bf.append(url).append(appId).append("/conv?click_id=").append(clickId).append("&muid=").append(muid4MD5).append("&conv_time=").append(conv_time).append("&value=").append(transInfo.getOrderAmount().multiply(new BigDecimal(100))).toString();
|
|
|
query_string = sf.append("click_id=").append(clickId).append("&muid=").append(muid4MD5).append("&conv_time=").append(conv_time).append("&value=").append(transInfo.getOrderAmount().multiply(new BigDecimal(100))).toString();
|
|
|
log.info("enter GdtTransTask.urlEode page is {}",page);
|
|
|
log.info("enter GdtTransTask.urlEode query_string is {}",query_string);
|
|
|
// 对url进行加密
|
|
|
String encode_page = null;
|
|
|
try {
|
|
|
encode_page = URLEncoder.encode(page, "UTF-8");
|
|
|
log.info("activateZhiHuitui urlEncoder is{} ", encode_page);
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
log.error("encode exception.", e);
|
|
|
}
|
|
|
// 将签名密钥和提交方式与加密后的url进行拼接
|
|
|
StringBuffer urlBuffer = new StringBuffer();
|
|
|
|
|
|
String property = urlBuffer.append(signKey).append("&GET&").append(encode_page).toString();
|
|
|
String signature = MD5.md5(property);
|
|
|
StringBuffer base = new StringBuffer();
|
|
|
String base_data = base.append(query_string).append("&sign=").append(signature).toString();
|
|
|
|
|
|
// 简单异或处理
|
|
|
String data = simpleXorByGdt(base_data, encryptKey);
|
|
|
// base64编码
|
|
|
String ret = Base64.encodeBase64String(data.getBytes());
|
|
|
// 64位处理之后,防止里面有换行符
|
|
|
String retRep = ret.replaceAll("\n", "");
|
|
|
// 将64位处理之后的值,按照url加密的方式进行加密
|
|
|
try {
|
|
|
retRep = URLEncoder.encode(retRep, "UTF-8");
|
|
|
log.info("activateUnion retRep is{}", retRep);
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
log.error("encode exception.", e);
|
|
|
}
|
|
|
// conv_type,,现在只有移动应用激活类型(MOBILEAPP_ACTIVITE);
|
|
|
String convType = "MOBILEAPP_COST";
|
|
|
if(transInfo.getType().equals("1")){
|
|
|
convType = "MOBILEAPP_ADDTOCART";
|
|
|
}
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
url = sb.append(url).append(appId).append("/conv?v=").append(retRep).append("&conv_type=").append(convType).append("&app_type=").append(clientTypeConver(clientType)).append("&advertiser_id=").append(uid).toString();
|
|
|
log.info("GdtTransTask url is {}", url);
|
|
|
return url;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 简单异或
|
|
|
*
|
|
|
* @param source
|
|
|
* @param key
|
|
|
* @return
|
|
|
*/
|
|
|
private String simpleXorByGdt(String source, String key) {
|
|
|
String result = "";
|
|
|
int j = 0;
|
|
|
for (int i = 0; i < source.length(); i++) {
|
|
|
int c1 = source.charAt(i);
|
|
|
int c2 = key.charAt(j);
|
|
|
|
|
|
result = result + (char) (c1 ^ c2);
|
|
|
j = j + 1;
|
|
|
j = j % (key.length());
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private String clientTypeConver(String clientType) {
|
|
|
if (StringUtils.isEmpty(clientType)) {
|
|
|
return null;
|
|
|
}
|
|
|
if (ClientTypeEnum.ANDROID.getName().equalsIgnoreCase(clientType)) {
|
|
|
return clientType.toUpperCase();
|
|
|
} else {
|
|
|
return "IOS";
|
|
|
}
|
|
|
|
|
|
}
|
|
|
} |
...
|
...
|
|