当前位置:   article > 正文

微信订阅消息-完整版_微信订阅消息开发文档

微信订阅消息开发文档

微信订阅消息推送

  • 官方文档地址:

    https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/uniform-message/uniformMessage.send.html
    
    • 1
  • 微信消息推送对接前提

    • 需要前台对接微信订阅消息授权
  • 消息推送对接流程:

    • 根据官方文档提示需要获取access_token
      • 官方文档地址: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
    • 调用推送接口
      • 请求地址:
        POST https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN
        
        • 1
        注意点:access_token 请求参数必须在请求路径上 其余参数都在body中

所需代码:

  1. 工具类
package com.zmkj.springcloud.pass.common.entity.wx.mesPushData;

public class TemplateData {
    private String value;//

    public TemplateData(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
package com.zmkj.springcloud.pass.common.entity.wx.mesPushData;

import java.util.Map;

/*
 * 小程序推送所需数据
 * */
public class WxMssVo {
    private String touser;//用户openid
    private String template_id;//订阅消息模版id
    private Map<String, TemplateData> data;//推送文字

    public String getTouser() {
        return touser;
    }

    public void setTouser(String touser) {
        this.touser = touser;
    }

    public String getTemplate_id() {
        return template_id;
    }

    public void setTemplate_id(String template_id) {
        this.template_id = template_id;
    }

    public Map<String, TemplateData> getData() {
        return data;
    }

    public void setData(Map<String, TemplateData> data) {
        this.data = data;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
package com.zmkj.springcloud.pass.common.util;

import com.alibaba.fastjson.JSON;
import com.zmkj.springcloud.pass.common.entity.wx.mesPushData.TemplateData;
import com.zmkj.springcloud.pass.common.entity.wx.mesPushData.WxMssVo;
import com.zmkj.springcloud.pass.common.vo.Result;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/*
 * 微信消息推送
 * */
public class WxMesgPushUtil {

    private static RestTemplate restTemplate;

    static {
//        构建请求对象
        restTemplate = new RestTemplate();
    }

    //    推送
    public static Result<Object> wxPush(String appId, String secret, String oppenId, String TemplateId, Map<String, TemplateData> hashMap) {
        String accessToken = "";
//        微信 获取 token
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={1}&secret={2}";
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class, appId, secret);
        String code = forEntity.getStatusCode().toString();
        if ("200".equals(code)) {
            String body = forEntity.getBody();
            Map map = JSON.parseObject(body, Map.class);
            accessToken = map.get("access_token").toString();
        }

//        定义消息推送请求
        String postUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;

//        创建消息推送对象
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(oppenId);
        wxMssVo.setTemplate_id(TemplateId);
        wxMssVo.setData(hashMap);

//        发送请求
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(postUrl, wxMssVo, String.class);
//         推送成功格式:     {"errcode":0,"errmsg":"ok"}
        String wxCode = responseEntity.getStatusCode().toString();
        if ("200".equals(wxCode)) {
//            判断是否报错
            String body = responseEntity.getBody();
            Map<String, String> map = JSON.parseObject(body, Map.class);
            String errmsg = map.get("errmsg");
            if ("ok".equals(errmsg)) {
//                返回成功
                Result<Object> result = new Result<>(StatusMagic.SUCCESS);
                return result;
            } else {
//                返回失败
                Result<Object> result = new Result<>(StatusMagic.ERROR);
                result.setMsg(errmsg);
                return result;
            }
        } else {
            Result<Object> result = new Result<>(StatusMagic.ERROR);
            result.setMsg(wxCode);
            return result;
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
package com.zmkj.springcloud.pass.common.vo;


import com.alibaba.fastjson.JSONObject;
import com.zmkj.springcloud.pass.common.util.StatusMagic;

public class Result<T> {

    public Integer code;

    public String msg;

    public T data;

    public Result() {

    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(StatusMagic sm) {
//        this.data = new JSONObject();
        this.code = sm.getCode();
        this.msg = sm.getMessage();
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/849561
推荐阅读
相关标签
  

闽ICP备14008679号