java 公众号根据openid发送模板消息

直接看代码,下面是用到的所有类直接复制使用即可

项目目录:

1.Data_keyword:
package com.example.medical.wechatMiniapps.webchatMessage;

public class Data_keyword {
    private Data_style first;

    private Data_style keyword1;

    private Data_style keyword2;

    public Data_style getKeyword3() {
        return keyword3;
    }

    public void setKeyword3(Data_style keyword3) {
        this.keyword3 = keyword3;
    }

    private Data_style keyword3;

    private Data_style remark;

    public Data_style getFirst() {
        return first;
    }

    public void setFirst(Data_style first) {
        this.first = first;
    }

    public Data_style getKeyword1() {
        return keyword1;
    }

    public void setKeyword1(Data_style keyword1) {
        this.keyword1 = keyword1;
    }

    public Data_style getKeyword2() {
        return keyword2;
    }

    public void setKeyword2(Data_style keyword2) {
        this.keyword2 = keyword2;
    }

    public Data_style getRemark() {
        return remark;
    }

    public void setRemark(Data_style remark) {
        this.remark = remark;
    }
}
2.Data_style
package com.example.medical.wechatMiniapps.webchatMessage;

public class Data_style {
    private String value;

    private String color;

    public String getValue() {
        return value;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
3.Miniprogram
package com.example.medical.wechatMiniapps.webchatMessage;

/**
 * 发送模板消息后点击需要跳到的小程序页面
 */
public class Miniprogram {
    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getPagepath() {
        return pagepath;
    }

    public void setPagepath(String pagepath) {
        this.pagepath = pagepath;
    }

    private String appid;

    private String pagepath;
}
4.WxTemplate
package com.example.medical.wechatMiniapps.webchatMessage;

public class WxTemplate {
    private String touser; //用户OpenID

    private String template_id; //模板消息ID

    private String url; //URL置空,则在发送后,点击模板消息会进入一个空白页面(ios),或无法点击(android)。

    public void setMiniprogram(Miniprogram miniprogram) {
        this.miniprogram = miniprogram;
    }

    public Miniprogram getMiniprogram() {
        return miniprogram;
    }

    //    所需跳转到小程序的具体页面路径,支持带参数,(示例pages/index/index?foo=bar),要求该小程序已发布,暂不支持小游戏 private Miniprogram miniprogram; private String topcolor; //标题颜色 private Data_keyword 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 templateId) { template_id = templateId; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getTopcolor() { return topcolor; } public void setTopcolor(String topcolor) { this.topcolor = topcolor; } public Data_keyword getData() { return data; } public void setData(Data_keyword data) { this.data = data; } }
5.Template
package com.example.medical.wechatMiniapps.config;


import com.alibaba.fastjson.JSONObject;
import com.example.medical.config.AccountDate;
import com.example.medical.config.HttpClientUtil;
import com.example.medical.mapper.Template.MedicalTemplateListMapper;
import com.example.medical.mapper.Template.MedicalTemplateParticularsMapper;
import com.example.medical.utils.RedisClient;
import com.example.medical.wechat.message.model.MedicalTemplateList;
import com.example.medical.wechat.message.model.MedicalTemplateParticulars;
import com.example.medical.wechatMiniapps.models.TemplateData;
import com.example.medical.wechatMiniapps.webchatMessage.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


import java.text.SimpleDateFormat;
import java.util.List;

@Component
public class Template {
    @Autowired
    private MedicalTemplateListMapper medicalTemplateListMapper;
    @Autowired
    private MedicalTemplateParticularsMapper medicalTemplateParticularsMapper;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private WeChatTemplateSendConfig wechatTemplateSendConfig;
/**
     * 发送模板消息
     * appId 公众账号的唯一标识
     * appSecret 公众账号的密钥
     * openId 用户标识
     *
     * @return
     */
    public boolean send_template_message(String name,String addType, String openId) {
        //我这里从redis里面取的access_token(下面有获取access_token代码)
        String url = wechatTemplateSendConfig.getSend().replaceAll("ACCESS_TOKEN", redisClient.get("wechat_access_token"));
        String templateId = "lwda9";//模板id
        //填写接收模板消息之后,你想要跳转的url页面。
        String goUrl = "";

        Miniprogram miniprogram=new Miniprogram();
        miniprogram.setAppid("这是需要跳到的小程序appid");
        miniprogram.setPagepath("pages/index/index");


        Data_style first = new Data_style();
        Data_style keyword1 = new Data_style();
        Data_style keyword2 = new Data_style();
        Data_style keyword3 = new Data_style();
        Data_style remark = new Data_style();

        WxTemplate temp = new WxTemplate();
        Data_keyword data = new Data_keyword();

        first.setValue("申请通知");
        first.setColor("#173177");

        keyword1.setValue(name);
        keyword1.setColor("#173177");

        keyword2.setValue(AccountDate.getNowTime());
        keyword2.setColor("#173177");

        keyword3.setValue(addType);
        keyword3.setColor("#173177");

        remark.setValue("");
        remark.setColor("#173177");

        data.setFirst(first);
        data.setKeyword1(keyword1);
        data.setKeyword2(keyword2);
        data.setKeyword3(keyword3);
        data.setRemark(remark);

        temp.setTouser(openId);
        temp.setTemplate_id(templateId);
        temp.setUrl(goUrl);
        temp.setMiniprogram(miniprogram);
        temp.setTopcolor("#173177");
        temp.setData(data);

        String jsonString = net.sf.json.JSONObject.fromObject(temp).toString().replace("day", "Day");
        JSONObject jsonObjects = JSONObject.parseObject(HttpClientUtil.doPostJson(url, jsonString));
        if ("ok".equals(jsonObjects.getString("errmsg"))) {
            return true;
        } else {
            return false;
        }
    }

}
6.GetAccessToken
package com.example.medical.wechat.auth.GetAccessToken;

import com.alibaba.fastjson.JSONObject;
import com.example.medical.config.HttpClientUtil;
import com.example.medical.config.JwtProperty;
import com.example.medical.utils.RedisClient;
import com.example.medical.wechat.modules.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @program: medical
 * @description: 获取AccessToken
 **/
@Slf4j
@Service
public class GetAccessToken {
    @Autowired
    private RedisClient redisClient;

    /**
     * @Description: 程序启动后每90分钟获取一次accessToken
     */
    @Scheduled(fixedDelay = 2 * 2700 * 1000)
    private void getToken() {
        try {
            String url = JwtProperty.getTokenUrlq().replaceAll("APPID", JwtProperty.getAppId())
                    .replaceAll("APPSECRET", JwtProperty.getAppSecret());
            String tokenSer = HttpUtil.doGetNew(url);
            JSONObject jsonObject = JSONObject.parseObject(tokenSer);
            if (jsonObject != null) {
                if (jsonObject.containsKey("access_token")) {
                    String accessToken = jsonObject.getString("access_token");
                    redisClient.set("wechat_access_token", accessToken);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
调用
 template.send_template_message('测试','申请通知',"o_ibXt_yEbryWndMDF_iXD_ZQI54");

注意: APPID和APPSECRET 我是在AccessToken类里面从配置文件读出来的

效果:

人生的成功不在于拿到一副好牌,而是怎样将坏牌打好。
原文地址:https://www.cnblogs.com/bin521/p/14434368.html