VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Java教程 >
  • SpringBoot推送微信测试公众号信息

1、登陆微信公众平台测试号

image.png

2、扫码关注

image.png

3、新建模版

image.png
参数需以{{开头,以.DATA}}结尾 ,ex:{{msg.DATA}},代码里面替换就可以了

templateMessage.addData(new WxMpTemplateData("msg","测试测试","#FF69B4"));

4、导入依赖

     <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>4.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

5、配置文件及配置类编写

  • 配置文件
wechat:
  mpAppId: xxxxxxxxxxxxxxxxx
  mpAppSecret: xxxxxxxxxxxxxxxxx
  toUser: xxxxxxxxxxxxxxxxx
  templateId: xxxxxxxxxxxxxxxxxxxxx
  • 配置类
package com.wanqi.email2.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/10/2
 * @Author wandaren
 */
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
    private String mpAppId;
    private String mpAppSecret;

    private String toUser;

    private String templateId;

    public String getMpAppId() {
        return mpAppId;
    }

    public void setMpAppId(String mpAppId) {
        this.mpAppId = mpAppId;
    }

    public String getMpAppSecret() {
        return mpAppSecret;
    }

    public void setMpAppSecret(String mpAppSecret) {
        this.mpAppSecret = mpAppSecret;
    }

    public String getToUser() {
        return toUser;
    }

    public void setToUser(String toUser) {
        this.toUser = toUser;
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }
}

package com.wanqi.email2.config;
 
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WeChatMyConfig {
 
    @Autowired
    private WechatAccountConfig wechatAccountConfig;
 

 
    @Bean(name = "wxMpConfigStorage")
    public WxMpConfigStorage wxMpConfigStorage(){
        WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
        wxMpConfigStorage.setAppId(wechatAccountConfig.getMpAppId());
        wxMpConfigStorage.setSecret(wechatAccountConfig.getMpAppSecret());
        return wxMpConfigStorage;
    }

    @Bean(name = "wxMpService")
    public WxMpService wxMpService(@Qualifier(value = "wxMpConfigStorage") WxMpConfigStorage wxMpConfigStorage){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
        return wxMpService;
    }

    @Bean
    public WxMpTemplateMessage wxMpTemplateMessage(){
        return WxMpTemplateMessage.builder()
                //接收方微信openId
                .toUser(wechatAccountConfig.getToUser())
                //模板Id
                .templateId(wechatAccountConfig.getTemplateId())
                .build();
    }

}

6、消息推送

package com.wanqi.email2;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
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.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/9/28
 * @Author wandaren
 */
@Component
@RefreshScope
public class EmailScheduler {
    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private WxMpTemplateMessage wxMpTemplateMessage;


    private void push(String msg) {
        wxMpTemplateMessage.addData(new WxMpTemplateData("riqi", DateUtil.formatDateTime(new Date()), "#00BFFF"));
        wxMpTemplateMessage.addData(new WxMpTemplateData("msg", msg, "#e6b422"));
        wxMpTemplateMessage.addData(new WxMpTemplateData("url", "cookie过期修复清查看邮件"));
        wxMpTemplateMessage.addData(new WxMpTemplateData("beizhu", "京东E卡", "#f09199"));
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 


相关教程