| @@ -1,14 +1,12 @@ | |||
| package com.iformall.mq; | |||
| import com.iformall.domain.po.BaseMsg; | |||
| import com.iformall.domain.po.MailMsg; | |||
| import com.iformall.domain.po.WxMsg; | |||
| import com.iformall.domain.po.WxMsgRecord; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.enums.EnumMsgRecordStatus; | |||
| import com.iformall.enums.EnumMsgRecordType; | |||
| import com.iformall.mapper.WxMsgRecordMapper; | |||
| import com.iformall.service.impl.SendCallBackSmsServiceImpl; | |||
| import com.iformall.service.impl.SendEmailServiceImpl; | |||
| import com.iformall.service.impl.SendSmartAppMsgServiceImpl; | |||
| import com.iformall.service.impl.SendSmsServiceImpl; | |||
| import com.iformall.utils.JsonUtil; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| @@ -17,6 +15,9 @@ import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| /** | |||
| * 消息消费入口 | |||
| */ | |||
| @Slf4j | |||
| @Service | |||
| public class MqBaseConsumer { | |||
| @@ -29,6 +30,8 @@ public class MqBaseConsumer { | |||
| private SendCallBackSmsServiceImpl sendCallBackSmsService; | |||
| @Autowired | |||
| private SendEmailServiceImpl sendEmailService; | |||
| @Autowired | |||
| private SendSmartAppMsgServiceImpl sendSmartAppMsgService; | |||
| public void doMessage(String message) { | |||
| log.info("received message: {}", message); | |||
| @@ -44,11 +47,13 @@ public class MqBaseConsumer { | |||
| sendCallBackSmsService.send((WxMsg)JsonUtil.readValue(message,WxMsg.class)); | |||
| }else if(EnumMsgRecordType.EMAIL.getCode().equals(baseMsg.getMsgType())){ | |||
| sendEmailService.send((MailMsg)JsonUtil.readValue(message,MailMsg.class)); | |||
| }else if(EnumMsgRecordType.SMART_APP.getCode().equals(baseMsg.getMsgType())){ | |||
| sendSmartAppMsgService.send((SmartAppMsg)JsonUtil.readValue(message,SmartAppMsg.class)); | |||
| } | |||
| baseMsg.setMsgStatus(EnumMsgRecordStatus.CONSUME_SUCC.getCode()); | |||
| wxMsgRecordMapper.update(baseMsg); | |||
| }catch (Throwable e){ | |||
| }catch (Exception e){ | |||
| e.printStackTrace(); | |||
| if(null != baseMsg){ | |||
| baseMsg.setStatusMessage(e.getMessage()); | |||
| @@ -0,0 +1,68 @@ | |||
| package com.iformall.domain.po; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; | |||
| import java.util.List; | |||
| /** | |||
| * @author luozukai | |||
| * @date 2019/3/7 10:05 | |||
| */ | |||
| public class SmartAppMsg extends BaseMsg { | |||
| private static final long serialVersionUID = 223232132183760321L; | |||
| private String tenantId; | |||
| private String to; | |||
| private String from; | |||
| private String templateId; | |||
| private Integer templateType; | |||
| private List<WxMaTemplateData> templateDataList; | |||
| public String getTemplateId() { | |||
| return templateId; | |||
| } | |||
| public void setTemplateId(String templateId) { | |||
| this.templateId = templateId; | |||
| } | |||
| public String getTenantId() { | |||
| return tenantId; | |||
| } | |||
| public void setTenantId(String tenantId) { | |||
| this.tenantId = tenantId; | |||
| } | |||
| public String getTo() { | |||
| return to; | |||
| } | |||
| public void setTo(String to) { | |||
| this.to = to; | |||
| } | |||
| public String getFrom() { | |||
| return from; | |||
| } | |||
| public void setFrom(String from) { | |||
| this.from = from; | |||
| } | |||
| public Integer getTemplateType() { | |||
| return templateType; | |||
| } | |||
| public void setTemplateType(Integer templateType) { | |||
| this.templateType = templateType; | |||
| } | |||
| public List<WxMaTemplateData> getTemplateDataList() { | |||
| return templateDataList; | |||
| } | |||
| public void setTemplateDataList(List<WxMaTemplateData> templateDataList) { | |||
| this.templateDataList = templateDataList; | |||
| } | |||
| } | |||
| @@ -7,7 +7,7 @@ public enum EnumMsgRecordType { | |||
| SMS(1, "短信"), | |||
| SMS_CALLBACK(2, "业务短信"), | |||
| EMAIL(3, "邮件"), | |||
| SMART(4, "小程序"), | |||
| SMART_APP(4, "小程序"), | |||
| PUBLIC(5, "公众号"), | |||
| SYSTEM(6, "系统通知"), | |||
| ; | |||
| @@ -4,10 +4,14 @@ import com.iformall.common.CommonMapper; | |||
| import com.iformall.domain.po.BaseMsg; | |||
| import com.iformall.domain.po.WxMsgRecord; | |||
| import java.util.List; | |||
| public interface WxMsgRecordMapper extends CommonMapper<WxMsgRecord, String> { | |||
| void update(BaseMsg record); | |||
| List<WxMsgRecord> findList(WxMsgRecord record); | |||
| void update(WxMsgRecord record); | |||
| } | |||
| @@ -1,7 +1,13 @@ | |||
| package com.iformall.service; | |||
| import com.iformall.domain.po.BaseMsg; | |||
| import com.iformall.domain.po.WxMsgRecord; | |||
| import java.util.List; | |||
| public interface WxMsgRecordService { | |||
| void save(BaseMsg baseMsg); | |||
| List<WxMsgRecord> findList(WxMsgRecord record); | |||
| void update(WxMsgRecord record); | |||
| } | |||
| @@ -0,0 +1,63 @@ | |||
| package com.iformall.service.impl; | |||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.mapper.WxTemplateMsgMapper; | |||
| import com.iformall.service.MsgSendService; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import java.util.List; | |||
| /** | |||
| * 发送微信小程序消息 | |||
| * @author luozukai | |||
| * @date 2019/3/7 16:03 | |||
| */ | |||
| @Service | |||
| public class SendSmartAppMsgServiceImpl implements MsgSendService { | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| @Autowired | |||
| private WxTemplateMsgMapper wxTemplateMsgMapper; | |||
| @Autowired | |||
| private WxMaService wxMaService; | |||
| @Override | |||
| public void send(BaseMsg baseMsg) throws Exception{ | |||
| SmartAppMsg smartAppMsg = (SmartAppMsg)baseMsg; | |||
| WxTemplateMsg msg = null; | |||
| WxMaTemplateMessage templateMessage = null; | |||
| WxTemplateMsg msgQ = new WxTemplateMsg(); | |||
| msgQ.setTenantId(smartAppMsg.getTenantId()); | |||
| msgQ.setType(smartAppMsg.getTemplateType()); | |||
| List<WxTemplateMsg> list = wxTemplateMsgMapper.select(msgQ); | |||
| if (list.size() > 0) { | |||
| msg = list.get(0); | |||
| } | |||
| if (msg == null) { | |||
| logger.error("找不到模板Id"); | |||
| throw new MallinkException(ErrorCode.TEMPLATE_NOT_FOUND); | |||
| } | |||
| /** | |||
| * 核销失败 | |||
| * 商品名称 {{keyword1.DATA}} | |||
| * 核销时间 {{keyword2.DATA}} | |||
| * 失败原因 {{keyword3.DATA}} | |||
| * 备注 {{keyword4.DATA}} | |||
| */ | |||
| templateMessage = WxMaTemplateMessage.builder() | |||
| .toUser(smartAppMsg.getTo()) | |||
| .formId(smartAppMsg.getFrom()) // 微信支付的formId是prepayId | |||
| .page("pages/index/index") | |||
| .data(smartAppMsg.getTemplateDataList()) | |||
| .templateId(smartAppMsg.getTemplateId()) | |||
| .emphasisKeyword("keyword1.DATA") | |||
| .build(); | |||
| wxMaService.getMsgService().sendTemplateMsg(templateMessage); | |||
| } | |||
| } | |||
| @@ -4,10 +4,9 @@ import com.iformall.domain.po.*; | |||
| import com.iformall.mapper.*; | |||
| import com.iformall.service.WxMsgRecordService; | |||
| import com.iformall.utils.JsonUtil; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import java.util.List; | |||
| import java.util.UUID; | |||
| /** | |||
| @@ -21,18 +20,27 @@ public class WxMsgRecordServiceImpl implements WxMsgRecordService { | |||
| @Override | |||
| public void save(BaseMsg data) { | |||
| data.setUuid(UUID.randomUUID().toString()); | |||
| if(data instanceof WxMsgRecord){ | |||
| WxMsgRecord msgRecord = (WxMsgRecord)data; | |||
| msgRecord.setUuid(UUID.randomUUID().toString()); | |||
| if(msgRecord.getDynamicContentMap() != null && msgRecord.getDynamicContentMap().size() > 0) { | |||
| msgRecord.setDynamicContent(JsonUtil.obj2Json(msgRecord.getDynamicContentMap())); | |||
| } | |||
| wxMsgRecordMapper.insertSelective(msgRecord); | |||
| }else if(data instanceof WxMsg || data instanceof MailMsg){ | |||
| WxMsgRecord msgRecord = new WxMsgRecord(); | |||
| msgRecord.setUuid(UUID.randomUUID().toString()); | |||
| msgRecord.setMsgJson(JsonUtil.obj2Json(data)); | |||
| wxMsgRecordMapper.insertSelective(msgRecord); | |||
| } | |||
| } | |||
| @Override | |||
| public List<WxMsgRecord> findList(WxMsgRecord record) { | |||
| return wxMsgRecordMapper.findList(record); | |||
| } | |||
| @Override | |||
| public void update(WxMsgRecord record) { | |||
| wxMsgRecordMapper.update(record); | |||
| } | |||
| } | |||
| @@ -28,32 +28,56 @@ | |||
| </resultMap> | |||
| <sql id="allColumns"> | |||
| id,tenant_id,domain,msg_type,msg,send_time,model_id,sender,sender_user_id,sender_user_name,receiver,receiver_user_id, | |||
| receiver_user_name,msg_status,status_message,createtime,updatetime,uuid,signature,model_type,dynamic_content,msg_json | |||
| </sql> | |||
| <sql id="dynamicWhereConditions"> | |||
| where 1 = 1 | |||
| <if test=" null != id "> | |||
| <if test=" null != id "> | |||
| and `id` = #{id} | |||
| </if> | |||
| <if test=" null != tenantId "> | |||
| </if> | |||
| <if test=" null != tenantId "> | |||
| and `tenant_id` = #{tenantId} | |||
| </if> | |||
| </if> | |||
| <if test=" null != receiverUserId "> | |||
| and `receiver_user_id` = #{receiverUserId} | |||
| </if> | |||
| <if test=" null != receiver "> | |||
| and `receiver` = #{receiver} | |||
| </if> | |||
| <if test=" null != msgStatus "> | |||
| and `msg_status` = #{msgStatus} | |||
| </if> | |||
| <if test=" null != domain "> | |||
| and `domain` = #{domain} | |||
| </if> | |||
| <if test=" null != ids "> | |||
| and id in | |||
| <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")"> | |||
| #{idItem} | |||
| </foreach> | |||
| </if> | |||
| <if test=" null != sortColumns"> order by ${sortColumns} </if> | |||
| order by createtime desc | |||
| </sql> | |||
| <update id="update" parameterType="com.iformall.domain.po.BaseMsg"> | |||
| update wx_msg_record set updatetime = now() | |||
| <if test=" null != msgStatus ">,msg_status = #{msgStatus}</if> | |||
| <if test=" null != statusMessage ">,status_message = #{statusMessage}</if> | |||
| where uuid = #{uuid} | |||
| <where> | |||
| <if test=" null != uuid ">and uuid = #{uuid}</if> | |||
| <if test=" null != id ">and id = #{id}</if> | |||
| </where> | |||
| </update> | |||
| <select id="findList" parameterType="com.iformall.domain.po.WxMsgRecord" resultMap="BaseResultMap"> | |||
| select <include refid="allColumns" /> from wx_msg_model | |||
| <include refid="dynamicWhereConditions" /> | |||
| </select> | |||
| </mapper> | |||