|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
package com.yoho.activity.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.activity.common.ApiResponse;
|
|
|
import com.yoho.activity.common.bo.TencentMktActivityBO;
|
|
|
import com.yoho.activity.common.bo.TencentMktBO;
|
|
|
import com.yoho.activity.common.constatns.Constant;
|
|
|
import com.yoho.activity.common.convert.TencentMktActivityConvert;
|
|
|
import com.yoho.activity.common.helper.ClientSecretHelper;
|
|
|
import com.yoho.activity.common.redis.CacheKeyHelper;
|
|
|
import com.yoho.activity.common.utils.DateUtils;
|
|
|
import com.yoho.activity.common.utils.RandomUtil;
|
|
|
import com.yoho.activity.service.ITencentMktService;
|
|
|
import com.yoho.core.common.utils.AES;
|
|
|
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.core.rest.client.hystrix.AsyncFuture;
|
|
|
import com.yoho.coupon.dal.ITencentMktActivityDAO;
|
|
|
import com.yoho.coupon.dal.model.TencentMktActivity;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.service.model.promotion.request.ParamsConfigReq;
|
|
|
import com.yoho.service.model.promotion.response.EventConfigRsp;
|
|
|
import com.yoho.service.model.request.ProfileRequestBO;
|
|
|
import com.yoho.service.model.request.RegisterReqBO;
|
|
|
import com.yoho.service.model.response.ProfileInfoRsp;
|
|
|
import com.yoho.service.model.response.RegisterRspBO;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
@Service
|
|
|
public class TencentMktServiceImpl implements ITencentMktService {
|
|
|
|
|
|
static Logger log = LoggerFactory.getLogger(TencentMktServiceImpl.class);
|
|
|
|
|
|
static Logger registerLog = LoggerFactory.getLogger("tencentRegestLog");
|
|
|
|
|
|
@Resource
|
|
|
ClientSecretHelper clientSecretHelper;
|
|
|
@Value("${gateway.url}")
|
|
|
private String gatewayUrl;
|
|
|
|
|
|
@Value("${sendSMS.password}")
|
|
|
private String sendSMSPassword;
|
|
|
|
|
|
@Resource
|
|
|
private ITencentMktActivityDAO tencentMktActivityDAO;
|
|
|
|
|
|
@Resource
|
|
|
private ServiceCaller service;
|
|
|
|
|
|
@Resource(name = "yhValueOperations")
|
|
|
private YHValueOperations<String, String> yhValueOperations;
|
|
|
|
|
|
@Resource(name = "yhRedisTemplate")
|
|
|
private YHRedisTemplate<String, String> yhRedisTemplate;
|
|
|
|
|
|
@Value("${tencentmkt.shareUrl}")
|
|
|
private String shareUrl;
|
|
|
|
|
|
public static final String TENCENT_MKT__ACTIVITY_CACHEKEY = "yh:activity:tencentmkt:activitykey";
|
|
|
|
|
|
@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];
|
|
|
mobile = arr[1];
|
|
|
}
|
|
|
if (StringUtils.isEmpty(area)) {
|
|
|
area = "86";
|
|
|
}
|
|
|
|
|
|
// 验证新老用户
|
|
|
ProfileRequestBO bo = new ProfileRequestBO();
|
|
|
bo.setMobile(mobile);
|
|
|
bo.setArea(area);
|
|
|
bo.setCheckSSO(true);
|
|
|
ProfileInfoRsp result = service.call("users.getUserprofileByEmailOrMobile", bo, ProfileInfoRsp.class);
|
|
|
log.info("call users.getUserprofileByEmailOrMobile result is {}", result);
|
|
|
|
|
|
if(result != null && result.getUid() != 0){
|
|
|
log.warn("call getUserprofileByEmailOrMobile. user is exist. area={}, mobile={}", area, mobile);
|
|
|
return new ApiResponse(502, "该用户已经注册过了");
|
|
|
}
|
|
|
|
|
|
// 组装请求对象,并生成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的发送验证码请求
|
|
|
AsyncFuture<ApiResponse> response = service.get("sms.sendCode", gatewayUrl + "?" + param, null, ApiResponse.class, null);
|
|
|
return response.get();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
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);
|
|
|
response.setMessage("验证码错误");
|
|
|
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");
|
|
|
|
|
|
//发送优惠券
|
|
|
response = sendCoupon(uid);
|
|
|
if (Constant.CODE_SUCCESS != response.getCode()) {
|
|
|
log.warn("validCodeAndSendCode error sendCoupon error with code={}, area={}, mobile={}", code, area, mobile);
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
//如果是新用户,则发送提醒短信
|
|
|
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", "tencentmkt");
|
|
|
logJson.put("ip", "");
|
|
|
logJson.put("collect_ip", "");
|
|
|
logJson.put("isnew", newUser ? "Y" : "N");
|
|
|
registerLog.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);
|
|
|
return new ApiResponse(501, "验证码不能为空");
|
|
|
}
|
|
|
|
|
|
// 检查参数 :手机号码
|
|
|
if (StringUtils.isEmpty(mobile)) {
|
|
|
log.warn("validRegCode with regCode={}, area={}, mobile={}", regCode, area, mobile);
|
|
|
return new ApiResponse(501, "手机号码不能为空");
|
|
|
}
|
|
|
|
|
|
// 如果手机号中包含-符号,则说明是国际号
|
|
|
if (mobile.indexOf("-") > 0) {
|
|
|
String[] arr = mobile.split("-");
|
|
|
area = arr[0];
|
|
|
mobile = arr[1];
|
|
|
}
|
|
|
if (StringUtils.isEmpty(area)) {
|
|
|
area = "86";
|
|
|
}
|
|
|
|
|
|
// 组装请求对象,并生成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的发送验证码请求
|
|
|
AsyncFuture<ApiResponse> response = service.get("sms.validRegCode", gatewayUrl + "?" + param, null, ApiResponse.class, null);
|
|
|
return response.get();
|
|
|
}
|
|
|
|
|
|
public TencentMktActivityBO getActivityInfoByCode(String activityCode) {
|
|
|
TencentMktActivityBO activityBO = CacheKeyHelper.string2Value(yhValueOperations.get(TENCENT_MKT__ACTIVITY_CACHEKEY), TencentMktActivityBO.class);
|
|
|
if(activityBO != null) {
|
|
|
log.info("obtain tencent mkt activity from cache, activityId: {}, activityName: {}", activityBO.getId(), activityBO.getActivityName());
|
|
|
return activityBO;
|
|
|
}
|
|
|
|
|
|
//缓存不命中,从数据库获取
|
|
|
TencentMktActivity db = tencentMktActivityDAO.selectByActivityCode(activityCode);
|
|
|
if (db == null) {
|
|
|
log.error("no tencent mkt activity exists");
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
activityBO = TencentMktActivityConvert.db2bo(db);
|
|
|
yhValueOperations.set(TENCENT_MKT__ACTIVITY_CACHEKEY, CacheKeyHelper.value2String(activityBO));
|
|
|
yhRedisTemplate.longExpire(TENCENT_MKT__ACTIVITY_CACHEKEY, 1, TimeUnit.MINUTES);
|
|
|
|
|
|
log.debug("tencent mkt activity, activityId: {}, activityName: {}", activityBO.getId(), activityBO.getActivityName());
|
|
|
return activityBO;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public TencentMktBO getActivityInfo(String activityCode) {
|
|
|
|
|
|
log.info("getActivityInfo params is activityCode={}", activityCode);
|
|
|
TencentMktBO bo = new TencentMktBO();
|
|
|
// 参数检查
|
|
|
if(StringUtils.isBlank(activityCode)){
|
|
|
log.warn("there is not activity,params is activityCode is null");
|
|
|
bo.setFlag(0);
|
|
|
bo.setReturnMsg("入力参数不对");
|
|
|
return bo;
|
|
|
}
|
|
|
|
|
|
// 1、查询此活动
|
|
|
TencentMktActivityBO activitydb = getActivityInfoByCode(activityCode);
|
|
|
|
|
|
if (activitydb == null) {
|
|
|
// 活动不存在
|
|
|
log.warn("there is not activity,params is activityCode={}", activityCode);
|
|
|
bo.setFlag(2);
|
|
|
bo.setReturnMsg("不存在该活动");
|
|
|
return bo;
|
|
|
}
|
|
|
bo.setH5Title(activitydb.getH5Title());
|
|
|
bo.setActivityEndPic(activitydb.getActivityEndPic());
|
|
|
|
|
|
// activityStatus:1-活动未开始 2-活动中 3-活动已结束 4-活动不存在
|
|
|
int nowtime = DateUtils.getCurrentTimeSecond();
|
|
|
if (activitydb.getEndTime() < nowtime) {
|
|
|
// 活动已结束
|
|
|
log.warn("activity is over ,params is activityCode={}", activityCode);
|
|
|
bo.setFlag(3);
|
|
|
bo.setReturnMsg("本次活动已经结束");
|
|
|
return bo;
|
|
|
}
|
|
|
if (activitydb.getBeginTime() > nowtime) {
|
|
|
// 活动未开始
|
|
|
log.warn("activity could not started ,params is activityCode={}",activityCode);
|
|
|
bo.setFlag(4);
|
|
|
bo.setReturnMsg("本次活动未开始");
|
|
|
return bo;
|
|
|
}
|
|
|
|
|
|
|
|
|
bo = TencentMktActivityConvert.bo2boResponse(activitydb);
|
|
|
bo.setFlag(1);
|
|
|
bo.setShareUrl(shareUrl);
|
|
|
log.info("getActivityInfo result is {}", bo);
|
|
|
return bo;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
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("-");
|
|
|
area = arr[0];
|
|
|
mobile = arr[1];
|
|
|
}
|
|
|
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);
|
|
|
log.info("call users.getUserprofileByEmailOrMobile result is {}", result);
|
|
|
// 查询用户不存在
|
|
|
JSONObject json = new JSONObject();
|
|
|
if (result == null || result.getUid() == 0) {
|
|
|
log.info("user not exists with mobile={}", mobile);
|
|
|
// 2.调用注册
|
|
|
RegisterReqBO registerReqBO = new RegisterReqBO();
|
|
|
registerReqBO.setArea(area);
|
|
|
registerReqBO.setProfile(mobile);
|
|
|
registerReqBO.setPassword(RandomUtil.getCharAndNumr(6, 2));
|
|
|
registerReqBO.setClient_type(client_type);
|
|
|
RegisterRspBO model = null;
|
|
|
try {
|
|
|
model = service.call("users.register", registerReqBO, RegisterRspBO.class);
|
|
|
log.info("call register new user.result is {}", model);
|
|
|
json.put("password", registerReqBO.getPassword());
|
|
|
// 3.记录新注册用户 领取人数
|
|
|
} 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(603, "领取优惠券失败");
|
|
|
}
|
|
|
|
|
|
json.put("uid", model.getUid());
|
|
|
}
|
|
|
// 3.已注册用户,则返回,提示该用户已经注册过了
|
|
|
else {
|
|
|
log.warn("call getUserprofileByEmailOrMobile. result={}", result);
|
|
|
return new ApiResponse(502, "该用户已经注册过了");
|
|
|
}
|
|
|
return new ApiResponse(json);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse sendCoupon(int uid) throws ServiceException {
|
|
|
log.info("sendCoupon with uid={}", uid);
|
|
|
|
|
|
EventConfigRsp eventConfigRsp = null;
|
|
|
try {
|
|
|
ParamsConfigReq paramsConfigReq = new ParamsConfigReq();
|
|
|
paramsConfigReq.setUid(Integer.valueOf(uid));
|
|
|
// TODO
|
|
|
paramsConfigReq.setEventCode("SEND_COCACOLA_COUPON");
|
|
|
paramsConfigReq.setClientType("web");
|
|
|
eventConfigRsp = service.call("promotion.sendCouponByConfig", paramsConfigReq, EventConfigRsp.class);
|
|
|
} catch (Exception e) {
|
|
|
log.error("sendCoupon error with uid={}", uid, e);
|
|
|
return new ApiResponse(601, "对不起,您不符合领取条件!请查看活动说明。");
|
|
|
}
|
|
|
if (eventConfigRsp == null || eventConfigRsp.getFlag() != 1) {
|
|
|
log.warn("sendCoupon error with uid={}", uid);
|
|
|
return new ApiResponse(602, "对不起,您不符合领取条件!请查看活动说明。");
|
|
|
}
|
|
|
log.info("sendCoupon success result is {}", eventConfigRsp);
|
|
|
return new ApiResponse(200, "优惠券领取成功");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void sendNoticeSms(String mobile, String password) throws ServiceException {
|
|
|
log.info("sendNoticeSms with mobile={}", mobile);
|
|
|
String url = "http://www.ztsms.cn/sendSms.do?";
|
|
|
// TODO
|
|
|
String content = "【Yoho!Buy有货】恭喜您获得千元礼包,您的登录账户是"+ mobile +", 密码是"+ password +"(随机6\n" +
|
|
|
"位数字+2位字母);下载Yoho!Buy有货手机端http:"+"(最新地址),更多惊喜等着您!";
|
|
|
try {
|
|
|
String pwd = AES.decrypt("yoho96461qaz2wsx", this.sendSMSPassword);
|
|
|
url += "username=youhuo&password=" + MD5.md5(pwd) + "&mobile=" + mobile + "&content="+ content +"&productid=333333";
|
|
|
} catch (Exception e) {
|
|
|
log.error("AES.decrypt error with code={}", this.sendSMSPassword, e);
|
|
|
return;
|
|
|
}
|
|
|
// 调用接口发送短信请求
|
|
|
AsyncFuture<String> response = service.get("sms.sendSMS", url, null, String.class, null);
|
|
|
String result = response.get();
|
|
|
log.info("sendNoticeSms result is {}, mobile is {}", result, mobile);
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|