spring boot項目之微信模板消息推送


一、打開微信公眾平台的消息管理-->模板消息接口

 

發送模板消息(需要傳模板ID:template_id【在公眾平台-->模板信息-->我的模板】)

 

 

接口調用請求說明

http請求方式: POST
https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

POST數據說明

POST數據示例如下:

      {
"touser":"OPENID",
"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
"url":"http://weixin.qq.com/download",
"miniprogram":{
"appid":"xiaochengxuappid12345",
"pagepath":"index?foo=bar"
},
"data":{
"first": {
"value":"恭喜你購買成功!",
"color":"#173177"
},
"keyword1":{
"value":"巧克力",
"color":"#173177"
},
"keyword2": {
"value":"39.8元",
"color":"#173177"
},
"keyword3": {
"value":"2014年9月22日",
"color":"#173177"
},
"remark":{
"value":"歡迎再次購買!",
"color":"#173177"
}
}
}

參數說明

參數 是否必填 說明
touser 接收者openid
template_id 模板ID
url 模板跳轉鏈接
miniprogram 跳小程序所需數據,不需跳小程序可不用傳該數據
appid 所需跳轉到的小程序appid(該小程序appid必須與發模板消息的公眾號是綁定關聯關系,暫不支持小游戲)
pagepath 所需跳轉到小程序的具體頁面路徑,支持帶參數,(示例index?foo=bar),暫不支持小游戲
data 模板數據
color 模板內容字體顏色,不填默認為黑色

注:url和miniprogram都是非必填字段,若都不傳則模板無跳轉;若都傳,會優先跳轉至小程序。開發者可根據實際需要選擇其中一種跳轉方式即可。當用戶的微信客戶端版本不支持跳小程序時,將會跳轉至url。

返回碼說明

在調用模板消息接口后,會返回JSON數據包。正常時的返回JSON數據包示例:

    {
"errcode":0,
"errmsg":"ok",
"msgid":200228332
}

使用效果

    

 二、打開項目,單獨建立一個service——PushMessage,用來做微信推送(主要推送的是訂單狀態變更消息)

實現PushMessageService接口的實現類PushMessageServiceImpl(WxMpService已經在WxchatConfig類上被注入,模板id也被注冊)

package com.imooc.service.impl;

import com.imooc.config.WechatAccountConfig;
import com.imooc.dto.OrderDTO;
import com.imooc.service.PushMessageService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@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);
}
}
}

 

三、在訂單完結時推送這個消息,故在OrderServiceImpl上的finish方法上推送微信模板消息

注意:在PushMessageServiceImpl上如果消息推送發生錯誤,只是用log標記一下而已,並沒有拋出異常,這個因為它在OrderServiceImpl.finish

上被引用,如果拋出異常,那么整個方法就會因為@Transactial注解不通過。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM