SpringBoot微信小程序发送公众号模板消息

实现效果:

        

准备工作:

        1.微信公众号和小程序是同一认证主题,并且已关联

        2.微信公众号获得模板消息能力,设置行业类别并选择相应的模板

实现思路:

        1.封装请求参数

        2.获取access_token

        3.发送

代码实现:

        

package com.qupaiji.api.weixin.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qupaiji.api.weixin.AccessTokenService;
import com.qupaiji.config.AccountConfiguration;
import com.qupaiji.utils.HttpClientUtil;
import com.qupaiji.utils.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Component
@Service
public class MessageService {

    final static Logger log = LoggerFactory.getLogger(MessageService.class);

    //模板消息模板ID
    final static String template_id = "";
    //公众号appid
    final static String appid = "";

    @Autowired
    private AccessTokenService accessTokenService;
    @Autowired
    private AccountConfiguration accountConfiguration;

    /**
     * 发送统一服务消息
     */
    @Async
    public void sendMessage(String openid, String to_number, String express_name, String article_number, String mtc_address, String mtc_phone, String mtc_receive_time) {
        log.info("发送");
        try {
            JSONObject obj = new JSONObject();
            JSONObject mp_template_msg = new JSONObject();
            JSONObject miniprogram = new JSONObject();
            JSONObject data = new JSONObject();

            //封装请求参数
            obj.put("touser", openid);

            //封装mp_template_msg
            mp_template_msg.put("appid", appid);    //微信公众号appid
            mp_template_msg.put("template_id", template_id);
            mp_template_msg.put("url", "");

            //封装miniprogram
            miniprogram.put("appid", accountConfiguration.getAppId());  //小程序appid
            miniprogram.put("pagepath", "pages/index/index?foo=bar");
            mp_template_msg.put("miniprogram", miniprogram);

            //封装data
            data.put("first", new TemplateData("【取派寄】您的快递已到站。", "#173177"));
            data.put("keyword1", new TemplateData(article_number, "#173177"));    //取货码
            data.put("keyword2", new TemplateData(express_name, "#173177"));  //快递公司
            data.put("keyword3", new TemplateData(to_number, "#173177")); //快递单号
            data.put("keyword4", new TemplateData(mtc_phone, "#173177"));   //联系电话
            data.put("keyword5", new TemplateData(mtc_address, "#173177"));   //取件地址
            data.put("remark", new TemplateData("请尽快取走您的包裹,详情可进入【取派寄】小程序查看。", "#173177"));
            mp_template_msg.put("data", data);

            obj.put("mp_template_msg", mp_template_msg);

            RestTemplate restTemplate = new RestTemplate();
            //统一服务消息
            String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" + accessTokenService.getAccessToken();
            log.info("url:"+url);
            log.info("请求的内容:" + obj);
            Strin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值