微信小程序消息推送至微信公众平台总结

微信小程序消息推送至微信公众平台总结

场景:由于业务需要,通过微信小程序给用户推消息(本篇文章使用的是使用openid直接推送的)


准备工作

1、注册公众号(需要服务号,订阅号不能推送消息)

2、完成微信认证(后面获取code及发送模板接口都需要拿到接口授权)

3、将开发好的小程序与公众号绑定,如下图示:
在这里插入图片描述

4、将开发者绑定到开发者工具下(成为一名公众平台开发者),如下图示:
在这里插入图片描述

5、公众号设置及基本配置,如下图:
公众号设置
基本配置
按要求配置好js接口安全域名及网页授权域名、配置IP白名单及获取appid和secret。

以上就是开发之前的准备工作啦(服务器部署及配置默认已配置完成)。


实现步骤

1.获取code

参考链接:

https://www.cnblogs.com/jobyym/p/15458796.html
微信官方文档

2.通过获取的code获取openid

前台代码如下(示例,仅供参考):

<view>
	<web-view :src="link"></web-view>
</view>


data() {
			return {
				openId: '',
				link:"https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=回调页面地址&response_type=code&scope=snsapi_base&state=1#wechat_redirect"
			}
		},
onLoad:function(e) {
			let that=this;
			console.log(e.code)
		    if(e.code != null){
				that.ajax(that.url.getOpenId,'GET',e.code,function(resp){
					that.openId=resp.data.openId;
					console.log(that.openId)
				});
		   }
		},

后台代码如下(仅供参考)

public String getOpenId(String code) {
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&code=" + code + "&secret="
                + secret + "&grant_type=authorization_code";
        String openId = "";
        // 发送请求
        JSONObject result = restTemplate.getForObject(url,JSONObject.class);
        log.info("返回数据:" + result);
        try {
            if (result != null){
                openId = (String) result.get("openid");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        log.info("获取的openID:" + openId);
        return openId;
    }

3.配置消息模板

在这里插入图片描述

4.调用微信接口发送模板消息

代码示例如下(仅后台):

1、调用接口获取token

public String getAccessToken(){
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret;
        String token= "";
        cn.hutool.json.JSONObject json = JSONUtil.parseObj(HttpUtil.get(url));
        token = (String) json.get("access_token");
        return token;
    }

2、调用接口发送模板消息

public String sendMsg(TemplateContentEntity request) {
        String result = null;
        if(StringUtils.isBlank(request.getTouser()) || StringUtils.isBlank(request.getTemplate_id())){
            throw new RuntimeException("入参有误,请联系管理员");
        }
        // 获取access_token
        String token = this.getAccessToken();
        String url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
        // 组织入参
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(request);
        String response= HttpUtil.post(url,jsonObject.toString());
        cn.hutool.json.JSONObject json = JSONUtil.parseObj(response);
        String errmsg = json.getStr("errmsg");
        System.out.println(errmsg);
        return result;
    }

总结

以上就是对于小程序推送消息至公众平台的记录,代码仅供参考,思路大概就是这个样子的,其实本人更推荐unionId的方式推送,可以参考官方链接:

unionid机制
有问题欢迎留言指教。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值