需求

多公众号发送模板消息

解决方案

思路:token
解决方案:复写框架底层实现方法,增加token参数(详情参考总纲
复写目标,底层源码TemplateMsgApi.java,复写后代码如下:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.kudesoft.duo.util;

import com.jfinal.kit.HttpKit;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.weixin.sdk.utils.JsonUtils;
import java.util.HashMap;
import java.util.Map;

public class TemplateMsgApi {
    private static String sendApiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
    private static String setIndustryUrl = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=";
    private static String getIndustryUrl = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=";
    private static String getTemplateIdUrl = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=";
    private static String getAllTemplateUrl = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=";
    private static String delTemplateUrl = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=";

    public TemplateMsgApi() {
    }

    public static ApiResult send(String jsonStr,String token) {
        String jsonResult = HttpUtils.post(sendApiUrl + token, jsonStr);
        return new ApiResult(jsonResult);
    }

    public static ApiResult setIndustry(String industry_id1, String industry_id2) {
        String url = setIndustryUrl + AccessTokenApi.getAccessTokenStr();
        Map<String, Object> params = new HashMap();
        params.put("industry_id1", industry_id1);
        params.put("industry_id2", industry_id2);
        String jsonResult = HttpUtils.post(url, JsonUtils.toJson(params));
        return new ApiResult(jsonResult);
    }

    public static ApiResult getIndustry() {
        return new ApiResult(HttpKit.get(getIndustryUrl + AccessTokenApi.getAccessTokenStr()));
    }

    public static ApiResult getTemplateId(String templateIdShort) {
        String url = getTemplateIdUrl + AccessTokenApi.getAccessTokenStr();
        Map<String, Object> params = new HashMap();
        params.put("template_id_short", templateIdShort);
        String json = HttpKit.post(url, JsonUtils.toJson(params));
        return new ApiResult(json);
    }

    public static ApiResult getAllTemplate() {
        return new ApiResult(HttpKit.get(getAllTemplateUrl + AccessTokenApi.getAccessTokenStr()));
    }

    public static ApiResult delTemplateById(String templateId) {
        String url = delTemplateUrl + AccessTokenApi.getAccessTokenStr();
        Map<String, Object> params = new HashMap();
        params.put("template_id", templateId);
        String json = HttpKit.post(url, JsonUtils.toJson(params));
        return new ApiResult(json);
    }
}

复写前调用:

ApiResult result = TemplateMsgApi.send(TemplateData.New()
                // 消息接收者
                .setTouser("oS48w1En1kcFfcA0Cdph5frpmxhA")
                // 模板id
                .setTemplate_id("g0xoVyf09q633kClOb73Z16AjBYbuJMMRcbv6gf8aPc")
                .setUrl("http://www.baidu.com")

                // 模板参数
                .add("first", "正式平台后台无法查看每个人opid,故随便获取了一个测试\n", "#999")
                .add("keyword1", "小王", "#999")
                .add("keyword2", "软件", "#999")
                .add("keyword3", String.valueOf(new Date()), "#999")
                .add("keyword4", "50", "#999")
                .add("remark", "\n55555!", "#999")
                .build());

        System.out.println(result);

复写后调用:

@Test
    public void send(){
         WXConfig wxConfig = wxConfigDao.getOne(3);
        String token = WXUtil.getAccessToken(wxConfig);
        ApiResult result = TemplateMsgApi.send(TemplateData.New()
                // 消息接收者
                .setTouser("oS48w1En1kcFfcA0Cdph5frpmxhA")
                // 模板id
                .setTemplate_id("g0xoVyf09q633kClOb73Z16AjBYbuJMMRcbv6gf8aPc")
                .setUrl("http://www.baidu.com")

                // 模板参数
                .add("first", "正式平台后台无法查看每个人opid,故随便获取了一个测试\n", "#999")
                .add("keyword1", "小王", "#999")
                .add("keyword2", "软件", "#999")
                .add("keyword3", String.valueOf(new Date()), "#999")
                .add("keyword4", "50", "#999")
                .add("remark", "\n55555!", "#999")
                .build(),token);

        System.out.println(result);

    }
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐