Authored by 匡佳华

小程序模板消息改为订阅消息

package com.yoho.message.dal;
import com.yoho.message.dal.model.MiniSubscribe;
import org.apache.ibatis.annotations.Param;
public interface IMiniSubscribeDAO {
MiniSubscribe selectAvailableTemplateId(@Param("tableName") String tableName, @Param("uid") Integer uid, @Param("templateId") String templateId);
void updateMiniSubscribe(@Param("tableName") String tableName, @Param("miniSubscribe") MiniSubscribe miniSubscribe);
}
... ...
package com.yoho.message.dal.model;
public class MiniSubscribe {
private Integer id;
private Integer uid;
private String openId;
private String templateId;
private Integer availableTimes;
private Integer miniAppType;
private Integer createTime;
private Integer updateTime;
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public Integer getAvailableTimes() {
return availableTimes;
}
public void setAvailableTimes(Integer availableTimes) {
this.availableTimes = availableTimes;
}
public Integer getMiniAppType() {
return miniAppType;
}
public void setMiniAppType(Integer miniAppType) {
this.miniAppType = miniAppType;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getCreateTime() {
return createTime;
}
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
public Integer getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Integer updateTime) {
this.updateTime = updateTime;
}
}
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yoho.message.dal.IMiniSubscribeDAO" >
<resultMap id="BaseResultMap" type="com.yoho.message.dal.model.MiniSubscribe" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="uid" property="uid" jdbcType="INTEGER" />
<result column="open_id" property="openId" jdbcType="VARCHAR" />
<result column="template_id" property="templateId" jdbcType="VARCHAR" />
<result column="available_time" property="availableTimes" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
<result column="update_time" property="updateTime" jdbcType="INTEGER" />
</resultMap>
<select id="selectAvailableTemplateId" resultMap="BaseResultMap">
select uid, open_id, template_id, available_time, create_time, update_time
from ${tableName}
where uid = #{uid} and template_id = #{templateId} and available_time > 0
limit 1
</select>
<update id="updateMiniSubscribe" parameterType="com.yoho.message.dal.model.MiniSubscribe">
update ${tableName} set available_time = available_time - 1
where uid = #{miniSubscribe.uid} and template_id = #{miniSubscribe.templateId}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -25,5 +25,15 @@ datasources:
daos:
- com.yoho.message.dal.IUserProfileDao
- com.yoho.message.dal.IWechatTemplateDao
yh_wechat:
servers:
- 192.168.102.219:3306
- 192.168.102.219:3306
username: yh_test
password: 9nm0icOwt6bMHjMusIfMLw==
maxConnections: 50
daos:
- com.yoho.message.dal.IMiniSubscribeDAO
readOnlyInSlave: true
\ No newline at end of file
... ...
consumer:
- address: 172.16.6.54:5672
- address: 192.168.102.211:5672
username: yoho
password: yoho
consumers:
... ... @@ -23,7 +23,7 @@ consumer:
producer:
- address: 172.16.6.54:5672
- address: 192.168.102.211:5672
username: yoho
password: yoho
producers:
... ...
... ... @@ -26,4 +26,14 @@ datasources:
- com.yoho.message.dal.IUserProfileDao
- com.yoho.message.dal.IWechatTemplateDao
yh_wechat:
username: ${jdbc.mysql.cobar.username}
password: ${jdbc.mysql.cobar.password}
cobar: true
maxConnections: 80
minIdle: 20
maxIdle: 40
daos:
- com.yoho.message.dal.IMiniSubscribeDAO
readOnlyInSlave: ${readOnlyInSlave}
\ No newline at end of file
... ...
package com.yoho.yhmessage.wechat.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.service.model.msgcenter.wechat.MiniappMsgReqBO;
import com.yoho.service.model.resource.response.CommonRspBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/sendMiniMsgController")
public class SendMiniMsgController {
@Autowired
IMiniAppService miniAppService;
@RequestMapping("/testSendMiniMsg")
public CommonRspBO collectFormId(@RequestBody MiniappMsgReqBO reqBO){
String data = "{\"keyword1\": { \"value\": \"订单未支付\" }, \"keyword2\": { \"value\": \"${orderCode}\" },\"keyword3\": { \"value\": \"${productName}\" }, \"keyword4\": { \"value\": \"${orderAmount}\" },\"keyword5\": { \"value\":\"您有订单尚未支付!时间所剩不多啦,快来支付吧,更多潮品尽在Yoho!Buy有货\" } }";
JSONObject dataJson = JSON.parseObject(data);
reqBO.setData(dataJson);
miniAppService.sendMiniappMsg(reqBO);
return new CommonRspBO();
}
}
... ...
... ... @@ -6,7 +6,9 @@ import com.yoho.core.common.utils.DateUtil;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yoho.message.dal.IMiniSubscribeDAO;
import com.yoho.message.dal.IMiniappMsgFormDAO;
import com.yoho.message.dal.model.MiniSubscribe;
import com.yoho.message.dal.model.MiniappMsgForm;
import com.yoho.service.model.msgcenter.wechat.MiniappMsgReqBO;
import com.yoho.yhmessage.constants.RedisCacheKeyConstant;
... ... @@ -42,6 +44,8 @@ public class MiniServiceAppImpl implements IMiniAppService {
@Value("${miniapp.send.tempalte:/cgi-bin/message/wxopen/template/send?}")
private String sendMsgTemplate;
private static final String sendSubscribeMsg = "/cgi-bin/message/subscribe/send?";
@Autowired
private IMiniappMsgFormDAO miniappMsgFormDAO;
... ... @@ -49,7 +53,7 @@ public class MiniServiceAppImpl implements IMiniAppService {
private IMiniAppAccessTokenService miniAppAccessTokenService;
public String getMsgSendUrl(){
return wechatHost + sendMsgTemplate;
return wechatHost + sendSubscribeMsg;
}
@Autowired
... ... @@ -64,6 +68,9 @@ public class MiniServiceAppImpl implements IMiniAppService {
@Autowired
private IFilterSensitiveWordService filterSensitiveWordService;
@Autowired
private IMiniSubscribeDAO miniSubscribeDAO;
/**
* 发送小程序模板消息
* @param reqBO
... ... @@ -78,10 +85,7 @@ public class MiniServiceAppImpl implements IMiniAppService {
}
//过滤敏感词目前只过滤了‘测试’相关字眼,以后若要加敏感词,则改为该方法过滤 将敏感词维护在 q_msg_system.message_sensitive_words 表中
/*if(filterSensitiveWordService.isContainSensitiveWord(reqBO.getData().toJSONString())){
logger.warn("sendMiniappMsg with sensitiveWord exist,reqBO is {}",reqBO);
return new JSONObject();
}*/
if(filterSensitiveWordService.checkTestWord(reqBO.getData().toJSONString())){
logger.warn("sendMiniappMsg with sensitiveWord exist,uid is {},reqBO is {}",reqBO.getUid(),reqBO);
return new JSONObject();
... ... @@ -92,33 +96,40 @@ public class MiniServiceAppImpl implements IMiniAppService {
throw new ServiceException(ServiceError.WECHAT_PARAM_ISNULL);
}
// 获取有效的formid
//判断该模板用户是否已经订阅
MiniSubscribe miniSubscribe = miniSubscribeDAO.selectAvailableTemplateId("mini_subscribe_"+reqBO.getMiniappType(), Integer.valueOf(reqBO.getUid()), reqBO.getTemplateId());
if(miniSubscribe == null){
return new JSONObject();
}
/*// 获取有效的formid
MiniappMsgForm miniappMsgForm = miniappMsgFormDAO.selectLastedAvailabByUid(reqBO.getUid(), reqBO.getMiniappType());
if (miniappMsgForm == null) {
logger.warn("validate form not exist, uid is {},reqBO is {}",reqBO.getUid(),reqBO);
return new JSONObject();
}
}*/
// 新获取的formid
/* // 新获取的formid
if (!reqBO.getFormId().equals(miniappMsgForm.getFormId())){
logger.info("reset form id, uid is {},old formid is {}, new formid is {}", reqBO.getUid(),reqBO.getFormId(), miniappMsgForm.getFormId());
reqBO.setFormId(miniappMsgForm.getFormId());
}
}*/
// 发送消息
JSONObject result = sendMsgUpdateDB(reqBO, miniappMsgForm);
JSONObject result = sendMsgUpdateDB(reqBO, miniSubscribe);
// token过期重新获取
if (result == null || result.getInteger("errcode") == 40001){
logger.warn("retry send msg because error is {}, uid is {},reqBO is {}", result != null ? result.toJSONString() : null, reqBO.getUid(), reqBO);
if (result == null || (Integer)result.get("errcode") == 40001){
logger.warn("retry send msg because error is {}, uid is {},reqBO is {}", result != null ? result.toString() : null, reqBO.getUid(), reqBO);
miniAppAccessTokenService.deletAccessToken(reqBO.getMiniappType());
return sendMsgUpdateDB(reqBO, miniappMsgForm);
return sendMsgUpdateDB(reqBO, miniSubscribe);
}
// 过期的formId 和 不合法的formid
else if(result.getInteger("errcode") == 41028 || result.getInteger("errcode")== 41029){
logger.info("retry send msg because error is {}, uid is {},reqBO is {}", result.toJSONString(),reqBO.getUid(),reqBO);
miniappMsgForm.setAvailableTimes(0);
miniappMsgFormDAO.updateByPrimaryKey(miniappMsgForm);
// TODO用户拒绝订阅需清空订阅次数
/*// 过期的formId 和 不合法的formid
else if((Integer)result.get("errcode") == 41028 || (Integer)result.get("errcode")== 41029){
logger.info("retry send msg because error is {}, uid is {},reqBO is {}", result.toString(),reqBO.getUid(),reqBO);
miniSubscribe.setAvailableTimes(0);
miniappMsgFormDAO.updateByPrimaryKey(miniSubscribe);
MiniappMsgForm miniappMsgFormNew = miniappMsgFormDAO.selectLastedAvailabByUid(reqBO.getUid(), reqBO.getMiniappType());
... ... @@ -130,19 +141,20 @@ public class MiniServiceAppImpl implements IMiniAppService {
}
return sendMsgUpdateDB(reqBO, miniappMsgFormNew);
}
}*/
return result;
}
private JSONObject sendMsgUpdateDB(MiniappMsgReqBO reqBO, MiniappMsgForm miniappMsgForm) {
private JSONObject sendMsgUpdateDB(MiniappMsgReqBO reqBO, MiniSubscribe miniSubscribe) {
reqBO.setTouser(miniSubscribe.getOpenId());
JSONObject result = sendTemplateMsg(reqBO);
if (result != null && result.getInteger("errcode") == 0){
miniappMsgForm.setAvailableTimes(miniappMsgForm.getAvailableTimes()-1);
miniappMsgFormDAO.updateByPrimaryKey(miniappMsgForm);
if (result != null && (Integer)result.get("errcode") == 0){
miniSubscribe.setAvailableTimes(miniSubscribe.getAvailableTimes()-1);
miniSubscribeDAO.updateMiniSubscribe("mini_subscribe_" + reqBO.getMiniappType(), miniSubscribe);
//miniappMsgFormDAO.updateByPrimaryKey(miniappMsgForm);
//发送成功,记入发送数至 redis
saveSendRedis(reqBO.getPage());
}
... ... @@ -163,6 +175,7 @@ public class MiniServiceAppImpl implements IMiniAppService {
String msgId = page.substring(index + MSG_ID_INDEX);
//组装key:日期,value部分为 以场景id为key的hash结构 设置超时时间2天
RedisKeyBuilder redisKey = RedisKeyBuilder.newInstance().appendFixed(RedisCacheKeyConstant.MINI_SEND_NUM_KEY).appendVar(DateUtil.getToday("yyyyMMdd"));
//todo 若是
long result = pushRedisService.hashIncrement(redisKey,msgId,1,TWO_DAYS_HOURS);
logger.info("saveSendRedis end with msgId is {},result is {}",msgId,result);
}catch (Exception e){
... ... @@ -196,7 +209,6 @@ public class MiniServiceAppImpl implements IMiniAppService {
bodyParam.put("touser", reqBO.getTouser());
bodyParam.put("template_id", reqBO.getTemplateId());
bodyParam.put("page", reqBO.getPage());
bodyParam.put("form_id", reqBO.getFormId());
bodyParam.put("data", reqBO.getData());
if (StringUtils.isNotBlank(reqBO.getEmphasisKeyword())){
... ...