奥雷迪尔

导航

12.18 微信模板消息推送

官方文档:https://mp.weixin.qq.com/wiki

模板消息推送文档,有固定的格式

准备工作:

项目配置文件:

1
2
3
wechat:
   templateId:
    orderStatus: e-Cqq67QxD6YNI41iRiqawEYdFavW_7pc7LyEMb-yeQ//设置模板ID

  配置文件

1
2
3
4
5
6
7
8
9
10
11
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
 
     
    /**
     * 微信模版id
     */
    private Map<String, String> templateId;
}

  

1.创建service

1
2
3
4
5
6
7
8
public interface PushMessageService {
 
    /**
     * 订单状态变更消息
     * @param orderDTO
     */
    void orderStatus(OrderDTO orderDTO);
}

  2.实现service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@Service
@Slf4j
public class PushMessageServiceImpl implements PushMessageService {
 
    @Autowired
    private WxMpService wxMpService;
 
    @Autowired
    private WechatAccountConfig accountConfig;
 
    @Override
    public void orderStatus(OrderDTO orderDTO) {
        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setTemplateId(accountConfig.getTemplateId().get("orderStatus"));
        templateMessage.setToUser(orderDTO.getBuyerOpenid());
 
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first""亲,请记得收货。"),
                new WxMpTemplateData("keyword1""微信点餐"),
                new WxMpTemplateData("keyword2""18868812345"),
                new WxMpTemplateData("keyword3", orderDTO.getOrderId()),
                new WxMpTemplateData("keyword4", orderDTO.getOrderStatusEnum().getMessage()),
                new WxMpTemplateData("keyword5""¥" + orderDTO.getOrderAmount()),
                new WxMpTemplateData("remark""欢迎再次光临!")
        );
        templateMessage.setData(data);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        }catch (WxErrorException e) {
            log.error("【微信模版消息】发送失败, {}", e);
        }
    }
}

  订单完结是发送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
 
    @Autowired
    private ProductService productService;
 
    @Autowired
    private OrderDetailRepository orderDetailRepository;
 
    @Autowired
    private OrderMasterRepository orderMasterRepository;
 
    @Autowired
    private PayService payService;
 
    @Autowired
    private PushMessageService pushMessageService;
@Override
    @Transactional
    public OrderDTO finish(OrderDTO orderDTO) {
        //判断订单状态
        if (!orderDTO.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
            log.error("【完结订单】订单状态不正确, orderId={}, orderStatus={}", orderDTO.getOrderId(), orderDTO.getOrderStatus());
            throw new SellException(ResultEnum.ORDER_STATUS_ERROR);
        }
 
        //修改订单状态
        orderDTO.setOrderStatus(OrderStatusEnum.FINISHED.getCode());
        OrderMaster orderMaster = new OrderMaster();
        BeanUtils.copyProperties(orderDTO, orderMaster);
        OrderMaster updateResult = orderMasterRepository.save(orderMaster);
        if (updateResult == null) {
            log.error("【完结订单】更新失败, orderMaster={}", orderMaster);
            throw new SellException(ResultEnum.ORDER_UPDATE_FAIL);
        }
 
        //推送微信模版消息
        pushMessageService.orderStatus(orderDTO);
 
        return orderDTO;
    }
}

posted on 2017-12-19 09:21  奥雷迪尔  阅读(103)  评论(0编辑  收藏  举报