...
|
...
|
@@ -17,15 +17,17 @@ import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.activity.common.ApiResponse;
|
|
|
import com.yoho.activity.common.constatns.Constant;
|
|
|
import com.yoho.activity.common.helper.ClientSecretHelper;
|
|
|
import com.yoho.activity.common.utils.DateUtils;
|
|
|
import com.yoho.activity.common.utils.RandomUtil;
|
|
|
import com.yoho.activity.service.ICocacolaService;
|
|
|
import com.yoho.core.common.utils.MD5;
|
|
|
import com.yoho.core.redis.YHRedisTemplate;
|
|
|
import com.yoho.core.redis.YHValueOperations;
|
|
|
import com.yoho.core.rest.client.ServiceCaller;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.service.model.promotion.request.ParamsConfigReq;
|
|
|
import com.yoho.service.model.promotion.response.EventConfigRsp;
|
...
|
...
|
@@ -34,45 +36,43 @@ import com.yoho.service.model.request.RegisterReqBO; |
|
|
import com.yoho.service.model.response.ProfileInfoRsp;
|
|
|
import com.yoho.service.model.response.RegisterRspBO;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 描述:
|
|
|
*
|
|
|
* @author ping.huang
|
|
|
* 2016年4月1日
|
|
|
* @author ping.huang 2016年4月1日
|
|
|
*/
|
|
|
@Service
|
|
|
public class CocacolaServiceImpl implements ICocacolaService {
|
|
|
|
|
|
static Logger log = LoggerFactory.getLogger(CocacolaServiceImpl.class);
|
|
|
|
|
|
static Logger cocacolaRegestLog = LoggerFactory.getLogger("cocacolaRegestLog");
|
|
|
@Resource
|
|
|
RestTemplate restTemplate;
|
|
|
@Resource
|
|
|
ClientSecretHelper clientSecretHelper;
|
|
|
@Value("${gateway.url}")
|
|
|
private String gatewayUrl;
|
|
|
|
|
|
private String gatewayUrl;
|
|
|
|
|
|
@Resource
|
|
|
private ServiceCaller service;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private YHValueOperations<String, String> yhValueOperations;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private YHRedisTemplate<String, String> yhRedisTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse sendSms(String area, String mobile) throws ServiceException {
|
|
|
log.debug("sendSms with area={}, mobile={}", area, mobile);
|
|
|
//检查参数
|
|
|
// 检查参数
|
|
|
if (StringUtils.isEmpty(mobile)) {
|
|
|
log.warn("sendSms error mobile is empty with area={}, mobile={}", area, mobile);
|
|
|
return new ApiResponse(501, "手机号不能为空");
|
|
|
}
|
|
|
|
|
|
//如果手机号中包含-符号,则说明是国际号
|
|
|
|
|
|
// 如果手机号中包含-符号,则说明是国际号
|
|
|
if (mobile.indexOf("-") > 0) {
|
|
|
String[] arr = mobile.split("-");
|
|
|
area = arr[0];
|
...
|
...
|
@@ -81,33 +81,94 @@ public class CocacolaServiceImpl implements ICocacolaService { |
|
|
if (StringUtils.isEmpty(area)) {
|
|
|
area = "86";
|
|
|
}
|
|
|
|
|
|
//组装请求对象,并生成client_secret
|
|
|
|
|
|
// 组装请求对象,并生成client_secret
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
map.put("method", "app.passport.smsbind");
|
|
|
map.put("mobile", mobile);
|
|
|
map.put("area", area);
|
|
|
String param = clientSecretHelper.createClientSecret(map);
|
|
|
|
|
|
//调用gateway的发送验证码请求
|
|
|
|
|
|
// 调用gateway的发送验证码请求
|
|
|
return restTemplate.getForObject(gatewayUrl + "?" + param, ApiResponse.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse validRegCode(String regCode, String area, String mobile)throws ServiceException {
|
|
|
public ApiResponse validCodeAndSendCode(String code, String area, String mobile, String client_id) throws ServiceException {
|
|
|
log.debug("validRegCodeAndSendCode with code={}, area={}, mobile={}", code, area, mobile);
|
|
|
|
|
|
// 验证验证码
|
|
|
ApiResponse response = validRegCode(code, area, mobile);
|
|
|
if (Constant.CODE_SUCCESS != response.getCode()) {
|
|
|
log.warn("validCodeAndSendCode error validCode is error with code={}, area={}, mobile={}", code, area, mobile);
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
// 判断用户是否已经注册过,如果未注册,则调用注册接口
|
|
|
response = register(area, mobile, "web");
|
|
|
if (Constant.CODE_SUCCESS != response.getCode()) {
|
|
|
log.warn("validCodeAndSendCode error register error with code={}, area={}, mobile={}", code, area, mobile);
|
|
|
return response;
|
|
|
}
|
|
|
JSONObject registerJSON = (JSONObject) response.getData();
|
|
|
boolean newUser = registerJSON.getBooleanValue("newUser");
|
|
|
int uid = registerJSON.getIntValue("uid");
|
|
|
String password = registerJSON.getString("password");
|
|
|
|
|
|
// 调用接口,查询该用户是否已经领取过优惠券
|
|
|
// TODO
|
|
|
// ParamsConfigReq paramsConfigReq = new ParamsConfigReq();
|
|
|
// paramsConfigReq.setUid(Integer.valueOf(uid));
|
|
|
// paramsConfigReq.setEventCode("SEND_REGISTER_COUPON");
|
|
|
// paramsConfigReq.setClientType("web");
|
|
|
// paramsConfigReq.setType(1);
|
|
|
// boolean isSend = service.call("promotion.XXXXXXX", paramsConfigReq, Boolean.class);
|
|
|
// if (isSend) {
|
|
|
// log.warn("user has get coupon with area={}, mobile={}", area, mobile);
|
|
|
// return new ApiResponse(600, "对不起,您已经领过,请查看活动说明");
|
|
|
// }
|
|
|
|
|
|
//发送优惠券
|
|
|
response = sendCoupon(uid);
|
|
|
if (Constant.CODE_SUCCESS != response.getCode()) {
|
|
|
log.warn("validCodeAndSendCode error sendCoupon error with code={}, area={}, mobile={}", code, area, mobile);
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
//如果是新用户,则发送提醒短信
|
|
|
if (newUser) {
|
|
|
sendNoticeSms(mobile, password);
|
|
|
}
|
|
|
|
|
|
//调用成功,记录日志
|
|
|
JSONObject logJson = new JSONObject();
|
|
|
logJson.put("uid", uid);
|
|
|
logJson.put("client_id", client_id);
|
|
|
logJson.put("create_time", DateUtils.getcurrentDateTime());
|
|
|
logJson.put("mobile", mobile);
|
|
|
logJson.put("source", "kekoukele");
|
|
|
logJson.put("ip", "");
|
|
|
logJson.put("collect_ip", "");
|
|
|
logJson.put("isnew", newUser ? "Y" : "N");
|
|
|
cocacolaRegestLog.info(logJson.toString());
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
private ApiResponse validRegCode(String regCode, String area, String mobile) throws ServiceException {
|
|
|
log.debug("validRegCode with regCode={}, area={}, mobile={}", regCode, area, mobile);
|
|
|
|
|
|
// 检查参数 :验证码
|
|
|
if (StringUtils.isEmpty(regCode)) {
|
|
|
log.warn("validRegCode with regCode={}, area={}, mobile={}",regCode, area, mobile);
|
|
|
log.warn("validRegCode with regCode={}, area={}, mobile={}", regCode, area, mobile);
|
|
|
return new ApiResponse(501, "验证码不能为空");
|
|
|
}
|
|
|
|
|
|
|
|
|
// 检查参数 :手机号码
|
|
|
if (StringUtils.isEmpty(mobile)) {
|
|
|
log.warn("validRegCode with regCode={}, area={}, mobile={}",regCode, area, mobile);
|
|
|
log.warn("validRegCode with regCode={}, area={}, mobile={}", regCode, area, mobile);
|
|
|
return new ApiResponse(501, "手机号码不能为空");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 如果手机号中包含-符号,则说明是国际号
|
|
|
if (mobile.indexOf("-") > 0) {
|
...
|
...
|
@@ -119,29 +180,29 @@ public class CocacolaServiceImpl implements ICocacolaService { |
|
|
area = "86";
|
|
|
}
|
|
|
|
|
|
//组装请求对象,并生成client_secret
|
|
|
// 组装请求对象,并生成client_secret
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
map.put("method", "app.register.validRegCode");
|
|
|
map.put("code", regCode);
|
|
|
map.put("mobile", mobile);
|
|
|
map.put("area", area);
|
|
|
String param = clientSecretHelper.createClientSecret(map);
|
|
|
|
|
|
//调用gateway的发送验证码请求
|
|
|
|
|
|
// 调用gateway的发送验证码请求
|
|
|
return restTemplate.getForObject(gatewayUrl + "?" + param, ApiResponse.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse register(String area, String mobile,String client_type) throws ServiceException {
|
|
|
|
|
|
public ApiResponse register(String area, String mobile, String client_type) throws ServiceException {
|
|
|
|
|
|
log.debug("register with area={}, mobile={},client_type={}", area, mobile, client_type);
|
|
|
|
|
|
|
|
|
// 检查参数 :手机号码
|
|
|
if (StringUtils.isEmpty(mobile)) {
|
|
|
log.warn("register with area={}, mobile={}, client_type={}", area, mobile, client_type);
|
|
|
return new ApiResponse(501, "手机号码不能为空");
|
|
|
}
|
|
|
|
|
|
|
|
|
// 如果手机号中包含-符号,则说明是国际号
|
|
|
if (mobile.indexOf("-") > 0) {
|
|
|
String[] arr = mobile.split("-");
|
...
|
...
|
@@ -151,17 +212,18 @@ public class CocacolaServiceImpl implements ICocacolaService { |
|
|
if (StringUtils.isEmpty(area)) {
|
|
|
area = "86";
|
|
|
}
|
|
|
|
|
|
|
|
|
// 1.根据手机号码查询,用户是否注册
|
|
|
|
|
|
|
|
|
ProfileRequestBO bo = new ProfileRequestBO();
|
|
|
bo.setMobile(mobile);
|
|
|
bo.setArea(area);
|
|
|
bo.setCheckSSO(true);
|
|
|
ProfileInfoRsp result = service.call("users.getUserprofileByEmailOrMobile", bo, ProfileInfoRsp.class);
|
|
|
// 查询用户不存在
|
|
|
JSONObject json = new JSONObject();
|
|
|
if (result == null || result.getUid() == 0) {
|
|
|
|
|
|
json.put("newUser", true);
|
|
|
// 2.调用注册
|
|
|
RegisterReqBO registerReqBO = new RegisterReqBO();
|
|
|
registerReqBO.setArea(area);
|
...
|
...
|
@@ -169,69 +231,63 @@ public class CocacolaServiceImpl implements ICocacolaService { |
|
|
registerReqBO.setPassword(RandomUtil.autoGetPassword());
|
|
|
registerReqBO.setClient_type(client_type);
|
|
|
RegisterRspBO model = service.call("users.register", registerReqBO, RegisterRspBO.class);
|
|
|
|
|
|
json.put("password", registerReqBO.getPassword());
|
|
|
// 3.记录新注册用户 领取人数
|
|
|
try{
|
|
|
try {
|
|
|
yhValueOperations.increment(Constant.USED_REGISTER_GET_TIME_MEM_KEY + mobile, 1);
|
|
|
yhRedisTemplate.longExpire(Constant.USED_REGISTER_GET_TIME_MEM_KEY + mobile, 30, TimeUnit.DAYS);
|
|
|
|
|
|
}catch (Exception e) {
|
|
|
log.warn("Redis exception. check get times. area is {}, mobile is {}, client_type={},exception is {}", area, mobile,client_type, e.getMessage());
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
log.warn("Redis exception. check get times. area is {}, mobile is {}, client_type={},exception is {}", area, mobile, client_type, e.getMessage());
|
|
|
}
|
|
|
return new ApiResponse(model);
|
|
|
json.put("uid", model.getUid());
|
|
|
}
|
|
|
// 3.已注册用户,redis记录领取人数
|
|
|
else{
|
|
|
|
|
|
try{
|
|
|
else {
|
|
|
json.put("newUser", false);
|
|
|
try {
|
|
|
yhValueOperations.increment(Constant.USED_REGISTER_GET_TIME_MEM_KEY + mobile, 1);
|
|
|
yhRedisTemplate.longExpire(Constant.USED_REGISTER_GET_TIME_MEM_KEY + mobile, 30, TimeUnit.DAYS);
|
|
|
|
|
|
}catch (Exception e) {
|
|
|
} catch (Exception e) {
|
|
|
log.warn("Redis exception. check get times. area is {}, mobile is {}, client_type={} exception is {}", area, mobile, client_type, e.getMessage());
|
|
|
}
|
|
|
|
|
|
return new ApiResponse();
|
|
|
json.put("uid", result.getUid());
|
|
|
}
|
|
|
|
|
|
return new ApiResponse(json);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse sendCoupon(String uid) throws ServiceException{
|
|
|
public ApiResponse sendCoupon(int uid) throws ServiceException {
|
|
|
log.info("sendCoupon with uid={}", uid);
|
|
|
|
|
|
// try {
|
|
|
ParamsConfigReq paramsConfigReq = new ParamsConfigReq();
|
|
|
paramsConfigReq.setUid(Integer.valueOf(uid));
|
|
|
paramsConfigReq.setEventCode("SEND_REGISTER_COUPON");
|
|
|
paramsConfigReq.setClientType("web");
|
|
|
paramsConfigReq.setType(1);
|
|
|
EventConfigRsp eventConfigRsp = service.call("promotion.sendCouponByConfig", paramsConfigReq, EventConfigRsp.class);
|
|
|
log.info("sendCoupon result is {}", eventConfigRsp);
|
|
|
// } catch (Exception e) {
|
|
|
// throw new ServiceException(ServiceError.PROMOTION_COUPON_SEND_FAIL);
|
|
|
// }
|
|
|
if(eventConfigRsp.getFlag()!=1){
|
|
|
throw new ServiceException(ServiceError.PROMOTION_COUPON_SEND_FAIL);
|
|
|
}else{
|
|
|
return new ApiResponse(200,"优惠券发送成功");
|
|
|
ParamsConfigReq paramsConfigReq = new ParamsConfigReq();
|
|
|
paramsConfigReq.setUid(Integer.valueOf(uid));
|
|
|
paramsConfigReq.setEventCode("SEND_COCACOLA_COUPON");
|
|
|
paramsConfigReq.setClientType("web");
|
|
|
paramsConfigReq.setType(1);
|
|
|
EventConfigRsp eventConfigRsp = service.call("promotion.sendCouponByConfig", paramsConfigReq, EventConfigRsp.class);
|
|
|
log.info("sendCoupon result is {}", eventConfigRsp);
|
|
|
if (eventConfigRsp.getFlag() != 1) {
|
|
|
log.warn("sendCoupon error with uid={}", uid);
|
|
|
return new ApiResponse(601, "优惠券领取失败");
|
|
|
} else {
|
|
|
return new ApiResponse(200, "优惠券领取成功");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse sendNoticeSms(String mobile,String password) throws ServiceException{
|
|
|
log.info("sendCoupon with mobile={},password={}", mobile,password);
|
|
|
//组装请求对象,并生成client_secret
|
|
|
public void sendNoticeSms(String mobile, String password) throws ServiceException {
|
|
|
log.info("sendCoupon with mobile={}", mobile);
|
|
|
// 组装请求对象,并生成client_secret
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
map.put("method", "app.message.sendMsg");
|
|
|
map.put("username", "youhuo");
|
|
|
map.put("password", MD5.md5("I8vX4MtK"));
|
|
|
map.put("mobile", mobile);
|
|
|
map.put("codes", "123,"+mobile+","+password+"");
|
|
|
map.put("template", "mobile_coupon");
|
|
|
String param = clientSecretHelper.createClientSecret(map);
|
|
|
|
|
|
//调用gateway的发送短信请求
|
|
|
return restTemplate.getForObject(gatewayUrl + "?" + param, ApiResponse.class);
|
|
|
map.put("content", "【 Yoho!Buy有货】恭喜您获得一张……(优惠券名称),您的登录账号是 "+ mobile +", 密码是"+ password +";下载Yoho!Buy有货手机端 http://t.cn,更多惊喜等着您!【有货】");
|
|
|
map.put("productid", "333333");
|
|
|
|
|
|
// 调用接口发送短信请求
|
|
|
restTemplate.postForObject("http://www.ztsms.cn/sendSms.do", map, String.class);
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|