SpringBoot 实现微信推送模板的示例代码

网友投稿 591 2022-11-17

SpringBoot 实现微信推送模板的示例代码

SpringBoot 实现微信推送模板的示例代码

导读

添加依赖

在SpringBoot项目中添加依赖

com.github.binarywang

weixin-java-mp

3.3.0

控制层代码

package com.ybchen.springbootwechart.controller;

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.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @ClassName:PushController

* @Description:微信推送

* @Author:chenyb

* @Date:2020/11/27 10:33 上午

* @Versiion:1.0

*/

@RestController

public class PushController {

/*

* 微信测试账号推送

* */

@GetMapping("/push")

public String push() {

//1,配置

WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();

wxStorage.setAppId("AppId");

wxStorage.setSecret("Secret");

WxMpService wxMpService = new WxMpServiceImpl();

wxMpService.setWxMpConfigStorage(wxStorage);

//2,推送消息

WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()

.toUser("ojPPk54RcFkCgGVP3m66v1RM2mvA")//要推送的用户openid

.templateId("a7RPsASc7fw33zFo7zEfWKE0vrPnUo7VZ82fX3tTfMg")//模版id

.url("https://cnblogs.com/chenyanbin/")//点击模版消息要访问的网址

.build();

//3,如果是正式版发送模版消息,这里需要配置你的信息

// templateMessage.addData(new WxMpTemplatEiqSprumyeData("name", "value", "#FF00FF"));

// templateMessage.addData(new WxMpTemplateData(name2, value2, color2));

try {

wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);

return "推送成功";

} catch (Exception e) {

System.out.println("推送失败:" + e.getMessage());

e.printStackTrace();

return "推送失败";

}

}

}

去微信公众平台注册一个开发测试账户

测试

替换模板内容在微信公众平台创建模板

语法:{{变量名.DATA}}

姓名:{{user_name.DATA}}

性别:{{sex.DATA}}

手机号:{{phone.DATA}}

邮箱:{{email.DATA}}

控制层修改

package com.ybchen.springbootwechart.controller;

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.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

import java.util.Map;

import java.util.Objects;

/**

* @ClassName:PushController

* @Description:微信推送

* @Author:chenyb

* @Date:2020/11/27 10:33 上午

* @Versiion:1.0

*/

@RestController

public class PushController {

/*

* 微信测试账号推送

* */

@GetMapping("/push")

public String push() {

//1,配置

WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();

wxStorage.setAppId("wx12db1518efd2302c");

wxStorage.setSecret("056f31d80a5a22cc0c418cc08f5657ad");

WxMpService wxMpService = new WxMpServiceImpl();

wxMpService.setWxMpConfigStorage(wxStorage);

//2,推送消息

WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()

.toUser("ojPPk54RcFkCgGVP3m66v1RM2mvA")//要推送的用户openid

.templateId("O0t0lPP7xRqbNz0-OwPzliSplzGFrkr4-au-OIGhiOE")//模版id

.url("https://cnblogs.com/chenyanbin/")//点击模版消息要访问的网址

.build();

templateMessage.addData(new WxMpTemplateData("user_name", "陈彦斌", "#CCCCFF"));

templateMessage.addData(new WxMpTemplateData("sex", "男", "#FF00FF"));

templateMessage.addData(new WxMpTemplateData("phone", "188888888888", "#CCFF99"));

templateMessage.addData(new WxMpTemplateData("email", "543210188@qq.com", "#FF0033"));

try {

wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);

return "推送成功";

} catch (Exception e) {

System.out.println("推送失败:" + e.getMessage());

e.printStackTrace();

return "推送失败";

}

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Docker 入门
下一篇:年俗
相关文章

 发表评论

暂时没有评论,来抢沙发吧~