Springboot集成微信平台发送模板消息

1、引入微信开发工具包:
wixin-java-tools这个开源的SDK。
下载地址
2、引入maven地址:jersey 是基于Java的一个轻量级RESTful风格的Web Services框架。

<dependency>
		<groupId>org.glassfish.jersey.core</groupId>
		<artifactId>jersey-server</artifactId>
	</dependency>
 <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>2.8.0</version>
  </dependency>

3、网页授权:
文档地址
第一步:我们需要配置一个回调的网址url。

  String url="http://localhost:8080/sell/wechat/userInfo";

第二步:调用sdk的方法

wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)

第三步:重定向到http://localhost:8080/sell/wechat/userInfo
在这里插入图片描述

在这里插入图片描述

@GetMapping("/authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl){
        //1. 配置
        //2.调用方法
        String url="http://localhost:8080/sell/wechat/userInfo";
        String redirectUrl=wxMpService.oauth2buildAuthorizationUrl(url,WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl));
        log.info("【微信网页授权】获取code,redirectUrl={}",redirectUrl);
        return "redirect:"+redirectUrl;
    }

注册服务接口:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: weixin

微信消息模板接口

概述
模板消息仅用于公众号向用户发送重要的服务通知,只能用于符合其要求的服务场景中,如信用卡刷卡通知,商品购买成功通知等。不支持广告等营销类消息以及其它所有可能对用户造成骚扰的消息。
参考网址: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277

@RestController
@RequestMapping("/weiXin")
public class WeiXinCatController {
	@Autowired
	private WxMpService wxService;

	@RequestMapping("/sendTemplate")
	public String createWeiXinCat(@RequestBody WxMpTemplateMessage wxMpTemplateMessage) throws WxErrorException {
		WxMpTemplateMsgService templateMsgService = wxService.getTemplateMsgService();
		return templateMsgService.sendTemplateMsg(wxMpTemplateMessage);
	}

}

参数
请求地址http://127.0.0.1:81/weiXin/sendTemplate

	{
	    "toUser": "okYSmt0cItTc4dZXFn43BbmYW8Rw",
	    "templateId": "eLozPgRNCPMEgOWGHwNW6cXkfrVqtsyDsyEwVabu0ww",
	    "url": "http://www.xxx.com",
	    "data": [
	        {
	            "name": "first",
	            "value": "2017年11月06日 23:32",
	            "color": "#173177"
	        },
	        {
	            "name": "keyword1",
	            "value": "30",
	            "color": "#173177"
	        },
	        {
	            "name": "keyword2",
	            "value": "201410515111522",
	            "color": "#173177"
	        }
	    ]
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知青先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值