本篇文章的主题是 如何通过springboot来实现微信的模版消息推送
实现效果:

在当今的信息化时代,微信作为国人最为常用的通讯工具之一,已经不仅仅是一个简单的社交应用,更是连接人与服务、人与信息的桥梁。企业微信模板消息作为一种高效、便捷的信息传递方式,被广泛应用于各类业务场景中,如订单通知、会议提醒、活动推送等。
通过本教程的学习,您将掌握如何在Spring Boot项目中集成微信SDK,如何编写代码发送微信模板消息,并了解整个推送的过程。
简要说明:
由于发送模版消息需要微信的服务号,申请服务号的话需要营业执照,个人是没有办法申请的,但是微信为了给开发者们提供测试特意开放了公众平台测试账号号,大家可以申请测试号来进行模版推送的开发和测试
开发步骤:
大家首先访问微信公众平台地址:https://mp.weixin.qq.com/
如果还没有注册账号的可以申请一个个人订阅号,这个教程大家网上自行查阅,很简单~
登录成功之后选择 开发者工具 --> 公众平台测试账号

测试号管理页面如下:

获取你的测试号信息

使用你的微信扫描这个测试公众号的二维码并关注
然后会得到你的**微信号(openId)**这个后面会用到

点击新增测试模版

添加模板信息
一定要按照注意事项填写 参数需以{{开头,以.DATA}}结尾

我这边创建了两个模版,这个模版id后面也会用到

com.github.binarywang weixin-java-mp 3.0.0 
appId、appSecret和orderTemplateId就是上面微信公众平台测试号管理中我们获取到的几个参数,现在把这三个参数配置到我们的项目中。
callBackUrl暂时先不用管

创建配置类
@ConfigurationProperties(prefix = "wechat.public")
@Component
@Data
@RefreshScope
public class WeChatProperties {
    private String appId;
    private String appSecret;
    private String callBackUrl;
    private String orderTemplateId;
}
 
package com.mdx.user.manager;
import com.mdx.user.config.WeChatProperties;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
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.Component;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
 * @author : jiagang
 * @date : Created in 2022/7/26 10:42
 */
@Component
@Slf4j
public class WxMessagesManager {
    @Autowired
    private WeChatProperties weChatProperties;
    public void sendOrderMsg(String openId, String orderId, String serviceName){
        String templateId = weChatProperties.getOrderTemplateId();
        // 订单时间
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm");
        Date date = new Date();
        String timeNow = sdf.format(date);
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(weChatProperties.getAppId());
        wxStorage.setSecret(weChatProperties.getAppSecret());
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        // 此处的 key/value 需和模板消息对应
        List wxMpTemplateDataList = Arrays.asList(
                new WxMpTemplateData("first", "您有一个新的订货单", "#FF0000"),
                new WxMpTemplateData("keyword1", orderId),
                new WxMpTemplateData("keyword2", serviceName),
                new WxMpTemplateData("keyword3", timeNow),
                new WxMpTemplateData("remark", "请登录系统查看订单详情并及时配货")
        );
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId) // openId为1.3步骤中得到的微信号
                .templateId(templateId)
                .data(wxMpTemplateDataList)
                .url("https://blog.csdn.net/qq_38374397?type=blog")  // 跳转详情地址
                .build();
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("消息模版发送成功~");
        } catch (Exception e) {
            log.error("推送失败:" + e.getMessage());
        }
    }
}
  
openId为1.3步骤中得到的微信号,其余参数可自定义


移动端:

点击详情:

PC端:

最后送大家一句话白驹过隙,沧海桑田
文章持续更新,可以关注下方公众号或者微信搜一搜「 迷迭香编程 」获取项目源码、干货笔记、面试题集,第一时间阅读,获取更完整的链路资料。