首页 > 其他分享 >SpringBoot推送微信测试公众号信息

SpringBoot推送微信测试公众号信息

时间:2022-10-29 16:47:09浏览次数:87  
标签:SpringBoot weixin 微信 springframework import org 推送 public String

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

}

标签:SpringBoot,weixin,微信,springframework,import,org,推送,public,String
From: https://www.cnblogs.com/wandaren/p/16838671.html

相关文章

  • 更换微信小程序头像(一年内可申请修改5次)
    (1)电脑端登录“微信公众平台”,网址为“https://mp.weixin.qq.com/” (2)小程序管理员扫码登录,登录之后,点击左侧最下方的“设置” (3)点击“设置-基本设置-小程序头像-......
  • 安装Redis并在springboot项目中使用redis
    一、下载redis1.1官网下载 1.2将其添加到服务上,并使其开机自启动 二、下载redis可视化工具(下不下都行) 三、将redis集成到我们的springboot项目中3.1首先是引......
  • 第三方代开的微信小程序更换管理员
    (1)由于第三方代开小程序默认管理员是法人。首先使用法人微信搜索"小程序助手“小程序 (2)点击进入“小程序助手”,即可看到自己企业名下未更换管理员的小程序 ......
  • 微信公众号开发
    1.微信公众平台测试帐号1.1申请测试账号微信公众平台接口测试帐号:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login&token=399029368&lang=zh_CN1.2 ......
  • SpringBoot 解决跨域问题代码
     packagecom.example.demo.gs;importorg.springframework.context.annotation.Configuration;importjavax.servlet.*;importjavax.servlet.annotation.WebFilter......
  • springboot 临时关闭 springSecurity 权限认证
    Springboot项目有时候本地测试,但是有springsecurity的权限认证,很不方便测试。临时关闭方式启动类上去掉配置:@SpringBootApplication(exclude={SecurityAutoConfigur......
  • 2流高手速成记(之五):Springboot整合Shiro实现安全管理
    废话不多说,咱们直接接上回上一篇我们讲了如何使用Springboot框架整合Nosql,并于文章最后部分引入了服务端Session的概念而早在上上一篇中,我们则已经讲到了如何使用Springb......
  • Springboot错误:Unable to start embedded Tomcat server
    报错内容/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/bin/java-XX:TieredStopAtLevel=1-noverify-Dspring.output.ansi.enabled=always-javaagent:/......
  • SpringBoot Starter缘起
    SpringBoot通过SpringBootStarter零配置自动加载第三方模块,只需要引入模块的jar包不需要任何配置就可以启用模块,遵循约定大于配置的思想。那么如何编写一个SpringBootSt......
  • springboot启动默认连接数据库问题
    这里是给新手介绍的springboot默认启动是连接数据库的经常很多人蒙蔽,//无数据源启动@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,DataSo......