[Java]最新订阅消息的发送方法.(2020-9-21)

最新订阅消息的发送方法.线上正常使用中.
为了省事,并没有加入查询,和添加个人模板申请的接口.微信公众平台申请.
拿去即用.
获取token,调用微信api必备.建议缓存到redis…因为微信有调用次数限制.
  public void getAccessToken() {
        String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" +
                appId + "&secret=" + appSecret;
        String response = HttpsUtils.get(tokenUrl);
    	JSONObject object = JSON.parseObject(response);
        logger.info("objData:" + object);
        token= object.getString("access_token");
      //  redisCacheManger.setValue("wx:access_token", map.get("access_token"), 7000);
    }
发送
 public Integer pushWx(String pushData, int type, Integer token) {
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + token;
        logger.info("微信开始推送发送参数:" + pushData);
        try {
            String response = HttpsUtils.postJson(url, pushData);
            logger.info("微信开始推送返回参数:" + response);
            JSONObject parseObject = JSONObject.parseObject(response);
            return parseObject.getInteger("errcode");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
业务逻辑,封装数据为json…发送给微信的订阅推送api
 public void wxPush301() {   //加一个循环   确保 所有的头被弹出来
        while (true) {
            String str= "s1+s2+s3+s4+s5";//要推送的数据
            if (value == null) {
                break;
            }
    
            WxConfigPush wxConfigPush =getWxConfigPush(1);//通过id 获取模板ID已经5 微信那边的字段名.{}
            //以下为推送配置
            String toUser = "xxxxxx" ;//用户的openid

            String[] arr = str.split("=");  //分割得到5个字符串.
            if (toUser == null) return;
            WxTemplate wxTemplate = new WxTemplate();
            String page = "pages/user/order/index";   //前端用于打开的小程序页面 路径
            wxTemplate.setTouser(toUser);
            wxTemplate.setTemplate_id(wxConfigPush.getTempalteId());  //获取到的订阅模板 ID..wxConfigPush 这个实体类可以用下方的sql 语句.逆向工程生成.
            wxTemplate.setPage(page); 
            wxTemplate.setData(putValueToData(wxConfigPush, arr[0], arr[1], arr[2], arr[3], arr[4]));
            try {
                String s = sendWxPush(wxTemplate, 301);
                Map<String, String> resultMap = Json2Object.json2Map(s);
                logger.debug(JSON.toJSONString(wxTemplate));
                if (resultMap.get("errcode").equals("0")) {
                    logger.info("订阅_推送成功~");
                } else {
                    logger.info("订阅_推送失败:" + s);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

循环插入keyWords,把对应的值放到map中.可以为null.

    static Map<String, WxValue> putValueToData(WeixiConfigPush configPush, String keyWord1, String keyWord2, String keyWord3, String keyWord4, String keyWord5) {
        Map<String, WxValue> data = new HashMap<>();
        if (configPush.getS1() != null && configPush.getS1() != "" && keyWord2 != null) {
            data.put(removeBracker(configPush.getS1()), new WxValue(keyWord1));
        }
        if (configPush.getS2() != null && configPush.getS1() != "" && keyWord2 != null) {
            data.put(removeBracker(configPush.getS2()), new WxValue(keyWord2));
        }
        if (configPush.getS3() != null && configPush.getS3() != "" && keyWord3 != null) {
            data.put(removeBracker(configPush.getS3()), new WxValue(keyWord3));
        }
        if (configPush.getS4() != null && configPush.getS4() != "" && keyWord4 != null) {
            data.put(removeBracker(configPush.getS4()), new WxValue(keyWord4));
        }
        if (configPush.getS5() != null && configPush.getS5() != "" && keyWord2 != null) {
            data.put(removeBracker(configPush.getS5()), new WxValue(keyWord5));
        }

        return data;

    }
去除括号{{}}
static String removeBracker(String s) {
        if (s != null) {
            s = s.substring(2, s.length() - 7);
        }
        return s;
    }

#####数据库 表 …推送配置表 自己生成下实体类.以及对应的mapper.dao

CREATE TABLE `wx_config_push` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `app_type` int(11) DEFAULT NULL COMMENT '1商铺,2顾客3,管理员',
  `type` int(11) DEFAULT NULL COMMENT '模板类型:0,下单,1退款申请,2,撤销,3,退款成功,4,预约',
  `tempalte_id` varchar(64) DEFAULT NULL COMMENT '微信推送模板id',
  `s1` varchar(32) DEFAULT NULL COMMENT '自定义变量名',  如{{character_string4.DATA}}
  `s2` varchar(32) DEFAULT NULL,
  `s3` varchar(32) DEFAULT NULL,
  `s4` varchar(32) DEFAULT NULL,
  `s5` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 COMMENT='微信推送的模板id  以及 自定义变量名'
两个封装类,分别封装数据.满足微信那边的格式要求.
public class WxTemplate {
//第一层
    private String touser;
    private String template_id;
    private Map<String,WxValue> data;
    private String page;
    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, WxValue> getData() {
        return data;
    }
    public void setData(Map<String, WxValue> data) {
        this.data = data;
    }
    public String getPage() {
        return page;
    }
    public void setPage(String page) {
        this.page = page;
    }
}
public class WxValue implements Serializable {
    //第二层
    public WxValue(String value) {
        this.value = value;
    }
    private String value;
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
}

对应着填入数据库.模板信息…再对着传入参数…发送拉倒…

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值