Authored by zhengwen.ge

广点通需求

  1 +package com.yoho.unions.common.enums;
  2 +
  3 +import org.apache.commons.lang3.StringUtils;
  4 +
  5 +/**
  6 + * 广点通返回信息枚举
  7 + * @author yoho
  8 + *
  9 + */
  10 +public enum MsgEnum {
  11 +
  12 + SUCCESS("0","成功"),
  13 + ILLEGAL_PARAMETER("-1","请求非法参数"),
  14 + PARSE_ARGUMENTS_FAIL("-2","参数解析失败"),
  15 + PARSE_DECODE_FAIL("-3","参数解码失败"),
  16 + GET_KEY_FAIL("-12","获取密钥失败"),
  17 + ILLEGAL_APPTYPE("-13","非法的应用类型"),
  18 + ILLEGAL_CONVTIME("-14","非法的转化时间"),
  19 + ILLEGAL_MUID("-15","非法的广点通移动设备标识"),
  20 + CONV_RULE_FAIL("-17","获取转化规则失败");
  21 +
  22 + private String code;
  23 + private String name;
  24 + public String getCode() {
  25 + return code;
  26 + }
  27 +
  28 + public String getName() {
  29 + return name;
  30 + }
  31 +
  32 + private MsgEnum(String code,String name){
  33 + this.code = code;
  34 + this.name = name;
  35 + }
  36 +
  37 + /**
  38 + * 根据code获取name
  39 + */
  40 + public static String getNameByCode(String code){
  41 + if (StringUtils.isEmpty(code)) {
  42 + return null;
  43 + }
  44 + for(MsgEnum e:values()){
  45 + if (code.equals(e.getCode())) {
  46 + return e.getName();
  47 + }
  48 + }
  49 + return null;
  50 + }
  51 +
  52 +}
1 -package com.yoho.unions.server.service.impl;  
2 -  
3 -import javax.annotation.Resource;  
4 -  
5 -import org.apache.commons.lang.StringUtils;  
6 -import org.apache.commons.lang3.tuple.Pair;  
7 -import org.apache.http.HttpStatus;  
8 -import org.slf4j.Logger;  
9 -import org.slf4j.LoggerFactory;  
10 -import org.springframework.stereotype.Service;  
11 -  
12 -import com.alibaba.fastjson.JSONObject;  
13 -import com.netflix.config.DynamicIntProperty;  
14 -import com.netflix.config.DynamicPropertyFactory;  
15 -import com.yoho.core.cache.CacheClient;  
16 -import com.yoho.service.model.union.request.ActiveUnionRequestBO;  
17 -import com.yoho.service.model.union.request.AddUnionRequestBO;  
18 -import com.yoho.service.model.union.response.UnionResponseBO;  
19 -import com.yoho.unions.common.enums.SourceEnum;  
20 -import com.yoho.unions.common.utils.DateUtil;  
21 -import com.yoho.unions.common.utils.HttpUtils;  
22 -import com.yoho.unions.dal.IUnionsActiveRecordDAO;  
23 -import com.yoho.unions.dal.model.UnionsActiveRecord;  
24 -import com.yoho.unions.server.service.DingdangService;  
25 -  
26 -@Service  
27 -public class DingdangServiceImpl_OLD implements DingdangService {  
28 -  
29 - static Logger log = LoggerFactory.getLogger(DingdangServiceImpl_OLD.class);  
30 -  
31 - static Logger addDingdang = LoggerFactory.getLogger("addDingdang");  
32 - static Logger activeDingdang = LoggerFactory.getLogger("activeDingdang");  
33 -  
34 - @Resource  
35 - CacheClient cacheClient;  
36 -  
37 - @Resource  
38 - IUnionsActiveRecordDAO unionsActiveRecordDAO;  
39 -  
40 - private static final String unions_KEY = "yh:unions:dingdang_";  
41 -  
42 - @Override  
43 - public UnionResponseBO addUnion(AddUnionRequestBO request) throws Exception {  
44 - log.debug("addunions with param is {}", request);  
45 - UnionResponseBO bo = new UnionResponseBO();  
46 - if (StringUtils.isEmpty(request.getApp())) {  
47 - log.warn("addunions error app is null with param is {}", request);  
48 - bo.setIsSuccess(false);  
49 - bo.setMsg("APP IS NULL");  
50 - return bo;  
51 -// throw new ServiceException(ServiceError.APP_IS_NULL);  
52 - }  
53 - if (StringUtils.isEmpty(request.getUdid())) {  
54 - log.warn("addunions error udid is null with param is {}", request);  
55 - bo.setIsSuccess(false);  
56 - bo.setMsg("UDID IS NULL");  
57 - return bo;  
58 -// throw new ServiceException(ServiceError.UDID_IS_NULL);  
59 - }  
60 - if (StringUtils.isEmpty(request.getCallbackurl())) {  
61 - log.warn("addunions error callbackurl is null with param is {}", request);  
62 - bo.setIsSuccess(false);  
63 - bo.setMsg("CALLBACKURL IS NULL");  
64 - return bo;  
65 -// throw new ServiceException(ServiceError.CALLBACKURL_IS_NULL);  
66 - }  
67 -  
68 - //检查memcached中是否已经有该udid  
69 - String cacheUdid = cacheClient.get(unions_KEY + request.getApp() + "_" + request.getUdid(), String.class);  
70 - log.info("addunions get cache key={}, cacheUdid={}", unions_KEY + request.getApp() + "_" + request.getUdid(), cacheUdid);  
71 - if (StringUtils.isNotEmpty(cacheUdid)) {  
72 - log.warn("addunions error app and udid is added with param is {}", request);  
73 - bo.setIsSuccess(false);  
74 - bo.setMsg("APP UDID IS EXISTS");  
75 - return bo;  
76 -// throw new ServiceException(ServiceError.APP_UDID_IS_EXISTS);  
77 - }  
78 -  
79 - //保存到memcached,时间,一个小时  
80 - DynamicIntProperty activeTime = DynamicPropertyFactory.getInstance().getIntProperty("activeTime", 60 * 60);  
81 - cacheClient.set(unions_KEY + request.getApp() + "_" + request.getUdid(), activeTime.get(), request.getUdid());  
82 - log.debug("addunions set cache success");  
83 - //插入数据库  
84 - try {  
85 - UnionsActiveRecord unions = new UnionsActiveRecord();  
86 - unions.setApp(request.getApp());  
87 - unions.setUdid(request.getUdid());  
88 - unions.setCallbackurl(request.getCallbackurl());  
89 - unions.setType(request.getType());  
90 - log.debug("add to unionsLog db with param is {}", unions);  
91 - unionsActiveRecordDAO.insert(unions);  
92 - } catch (Exception e) {  
93 - log.error("", e);  
94 - }  
95 - log.info("addunions success with param is {}", request);  
96 - addDingdang.info("addunions success with param is {}", request);  
97 - bo.setMsg("成功");  
98 - bo.setIsSuccess(true);  
99 - return bo;  
100 - }  
101 -  
102 - @Override  
103 - public UnionResponseBO activeUnion(ActiveUnionRequestBO request) throws Exception {  
104 - log.debug("activeunions with param is {}", request);  
105 - UnionResponseBO bo = new UnionResponseBO();  
106 - if (StringUtils.isEmpty(request.getAppid())) {  
107 - log.warn("activeunions error app is null with param is {}", request);  
108 - bo.setIsSuccess(false);  
109 - bo.setMsg("APP IS NULL");  
110 - return bo;  
111 - //throw new ServiceException(500, "APP_IS_NULL");  
112 - }  
113 - if (StringUtils.isEmpty(request.getUdid())) {  
114 - log.warn("activeunions error udid is null with param is {}", request);  
115 - bo.setIsSuccess(false);  
116 - bo.setMsg("UDID IS NULL");  
117 - return bo;  
118 -// throw new ServiceException(500, "UDID_IS_NULL");  
119 - }  
120 -  
121 - String memKey = unions_KEY + request.getAppid() + "_" + request.getUdid();  
122 -  
123 - //检查memcached中是否已经有该udid  
124 - String cacheUdid = cacheClient.get(memKey, String.class);  
125 - log.info("activeUnion get cache key={}, cacheUdid={}", memKey, cacheUdid);  
126 - if (StringUtils.isEmpty(cacheUdid)) {  
127 - log.warn("activeunions error app and udid is not exists with param is {}", request);  
128 - bo.setIsSuccess(false);  
129 - bo.setMsg("APP UDID IS NOT EXISTS IN CACHE");  
130 - return bo;  
131 - }  
132 -  
133 - //检查该app和udid是否已经激活  
134 - UnionsActiveRecord unions = new UnionsActiveRecord();  
135 - unions.setApp(request.getAppid());  
136 - unions.setUdid(request.getUdid());  
137 - unions.setType(request.getApptype());  
138 - UnionsActiveRecord u = unionsActiveRecordDAO.selectByUdidAndApp(unions);  
139 -  
140 - if (u == null) {  
141 - log.warn("activeunions error app and udid is not exists with param is {}", request);  
142 - bo.setIsSuccess(false);  
143 - bo.setMsg("database has not exists this udid but cache exists");  
144 - return bo;  
145 - }  
146 -  
147 - //已经激活  
148 - if ("1".equals(u.getIsActive())) {  
149 - log.warn("activeunions error app is actived with param is {}", request);  
150 - bo.setIsSuccess(false);  
151 - bo.setMsg("app udid had actived");  
152 - return bo;  
153 - }  
154 -  
155 - //调用联盟激活接口  
156 - int repeatCount = 0;  
157 - Pair<Integer, String> pair = null;  
158 - JSONObject json = null;  
159 - boolean isSuccess = false;  
160 - String url = "";  
161 - while (true) {  
162 - //如果已经成功,或者重试次数大于3次,则不再调用  
163 - if (isSuccess || repeatCount >= 3) {  
164 - break;  
165 - }  
166 - try {  
167 - url = u.getCallbackurl();  
168 - pair = HttpUtils.httpGet(url);  
169 - if (pair.getLeft() == HttpStatus.SC_OK) {  
170 - log.debug("call " + u.getCallbackurl() + " url success return message is {}", pair.getRight());  
171 - json = JSONObject.parseObject(pair.getRight());  
172 - isSuccess = json.getBooleanValue("isSuccess");  
173 - }  
174 - } catch (Exception e) {  
175 - log.error("", e);  
176 - }  
177 - repeatCount++;  
178 - }  
179 - log.info("call " + u.getCallbackurl() + " url return message is {}", pair.getRight());  
180 -  
181 - //更新数据库  
182 - if (isSuccess) {  
183 - unions.setIsActive("1");  
184 - unions.setId(u.getId());  
185 - unionsActiveRecordDAO.updateByPrimaryKey(unions);  
186 - //激活成功,从memcached中删除  
187 - cacheClient.delete(memKey);  
188 - log.debug("activeUnion delete cache success key={}", memKey);  
189 - bo.setMsg("激活成功");  
190 - bo.setIsSuccess(true);  
191 - } else {  
192 - unions.setIsActive("0");  
193 - unions.setId(u.getId());  
194 - unionsActiveRecordDAO.updateByPrimaryKey(unions);  
195 - bo.setMsg("激活失败");  
196 - bo.setIsSuccess(false);  
197 - }  
198 -  
199 - try {  
200 - //组装大数据分析的日志  
201 - JSONObject j = new JSONObject();  
202 - j.put("apptype", request.getApptype());  
203 - j.put("appid", request.getAppid());  
204 - j.put("udid", request.getUdid());  
205 - j.put("dateid", DateUtil.getcurrentDateTime());  
206 - SourceEnum e = SourceEnum.getSourceEnumByValue(u.getType());  
207 - j.put("source", e == null ? "" : e.getName());  
208 - j.put("ip", request.getClientIP());  
209 - j.put("collect_ip", "");  
210 - activeDingdang.info(j.toString());  
211 - } catch (Exception e) {  
212 - log.error("", e);  
213 - }  
214 - return bo;  
215 - }  
216 -  
217 -}  
@@ -23,6 +23,7 @@ import com.yoho.service.model.union.request.MainUnionRequestBO; @@ -23,6 +23,7 @@ import com.yoho.service.model.union.request.MainUnionRequestBO;
23 import com.yoho.service.model.union.response.ActivateDingdangResponseBO; 23 import com.yoho.service.model.union.response.ActivateDingdangResponseBO;
24 import com.yoho.service.model.union.response.ActivateGDTResponseBO; 24 import com.yoho.service.model.union.response.ActivateGDTResponseBO;
25 import com.yoho.service.model.union.response.MainUnionResponseBO; 25 import com.yoho.service.model.union.response.MainUnionResponseBO;
  26 +import com.yoho.unions.common.enums.MsgEnum;
26 import com.yoho.unions.common.utils.DateUtil; 27 import com.yoho.unions.common.utils.DateUtil;
27 import com.yoho.unions.common.utils.HttpUtils; 28 import com.yoho.unions.common.utils.HttpUtils;
28 import com.yoho.unions.dal.IUnionLogsDAO; 29 import com.yoho.unions.dal.IUnionLogsDAO;
@@ -103,7 +104,8 @@ public class GDTServiceImpl implements MainUnionService { @@ -103,7 +104,8 @@ public class GDTServiceImpl implements MainUnionService {
103 log.debug("url return message is {}",pair.getRight()); 104 log.debug("url return message is {}",pair.getRight());
104 JSONObject json = JSONObject.parseObject(pair.getRight()); 105 JSONObject json = JSONObject.parseObject(pair.getRight());
105 ret = json.getString("ret"); 106 ret = json.getString("ret");
106 - msg = json.getString("msg"); 107 +// msg = json.getString("msg");
  108 + msg = MsgEnum.getNameByCode(ret);
107 //广点通返回成功 109 //广点通返回成功
108 if(StringUtils.isNotEmpty(ret)&&"0".equals(ret)){ 110 if(StringUtils.isNotEmpty(ret)&&"0".equals(ret)){
109 unionLogs.setIsActivate((byte) 1); 111 unionLogs.setIsActivate((byte) 1);