Java操作微信小程序消息订阅

下方代码包含以下操作方法:

  1. 获取 ACCESS_TOKEN

  2. 获取个人模板列表

  3. 删除消息模板根据id

  4. 获取类目

  5. 获取关键词列表

  6. 获取所属类目下的公共模板

  7. 发送订阅消息

  8. 添加模板

package com.tswl.sch.commen;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信小程序模板公共类
 *
 * @author : xufan_yang
 * @date : 2023/4/12 14:11
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/wechatmsgtemplate")
public class WeChatMsgTemplate {

    /** 获取 token url 地址 */
    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/stable_token";
    /** 获取个人模板列表 url 地址 */
    private static final String GET_TEMPLATE_LIST_URL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=ACCESS_TOKEN";
    /** 删除消息模板根据id url 地址 */
    private static final String DELETE_TEMPLATE_URL = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token=ACCESS_TOKEN ";
    /** 获取模板类目 url 地址 */
    private static final String GET_CATEGORY_URL = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token=ACCESS_TOKEN ";
    /** 获取模板类目 url 地址 */
    private static final String GET_PUB_TEMPLATEKEYWORDS_BYID = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token=ACCESS_TOKEN&tid=TID";
    /** 获取模板类目 url 地址 */
    private static final String GET_PUB_TEMPLATE_TITLELIST = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token=ACCESS_TOKEN&ids=IDS&start=START&limit=LIMIT";
    /** 发送订阅消息 url 地址 */
    private static final String SEND_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
    /** 添加模板 url 地址 */
    private static final String ADD_MESSAGE_TEMPLATE = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token=ACCESS_TOKEN ";

    /**
     * 获取 ACCESS_TOKEN
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/getstableaccesstoken")
    public JSONObject getStableAccessToken(@RequestBody JSONObject jsonObject) {
        String appId = jsonObject.getString("appId");
        String appSecret = jsonObject.getString("appSecret");
        String paream = "{\"grant_type\": \"client_credential\",\"appid\": \"" + appId + "\",\"secret\": \"" + appSecret + "\"}";
        String post = HttpUtil.post(ACCESS_TOKEN_URL, paream);
        return JSON.parseObject(post);
    }

    /**
     * 获取个人模板列表
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/getmessagetemplatelist")
    public JSONObject getMessageTemplateList(@RequestBody JSONObject jsonObject) {
        String access_token = jsonObject.getString("access_token");
        String url = GET_TEMPLATE_LIST_URL.replace("ACCESS_TOKEN", access_token);
        String templateListResult = HttpUtil.get(url);
        JSONObject responseJson = JSONObject.parseObject(templateListResult);
        System.out.println(responseJson);
        return responseJson;
    }

    /**
     * 删除消息模板根据id
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/deletemessagetemplate")
    public JSONObject deleteMessageTemplate(@RequestBody JSONObject jsonObject) {
        String access_token = jsonObject.getString("access_token");
        // 要删除的模板id
        String priTmplId = jsonObject.getString("priTmplId");
        JSONObject paream = new JSONObject();
        paream.put("priTmplId", priTmplId);
        String url = DELETE_TEMPLATE_URL.replace("ACCESS_TOKEN", access_token);
        String body = HttpRequest.post(url).body(JSONUtil.toJsonStr(paream)).execute().body();
        JSONObject result = JSONObject.parseObject(body);
        return result;
    }

    /**
     * 获取类目
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/getcategory")
    public JSONObject getCategory(@RequestBody JSONObject jsonObject) {
        String access_token = jsonObject.getString("access_token");
        String url = GET_CATEGORY_URL.replace("ACCESS_TOKEN", access_token);
        String deleteResult = HttpUtil.get(url);
        JSONObject result = JSONObject.parseObject(deleteResult);
        return result;
    }

    /**
     * 获取关键词列表
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/getpubtemplatekeywordsbyid")
    public JSONObject getPubTemplateKeyWordsById(@RequestBody JSONObject jsonObject) {
        String access_token = jsonObject.getString("access_token");
        // 模板标题 id
        String tid = jsonObject.getString("tid");
        String url = GET_PUB_TEMPLATEKEYWORDS_BYID.replace("ACCESS_TOKEN", access_token).replace("TID", tid);
        String deleteResult = HttpUtil.get(url);
        JSONObject result = JSONObject.parseObject(deleteResult);
        return result;
    }

    /**
     * 获取所属类目下的公共模板
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/getpubtemplatetitlelist")
    public JSONObject getPubTemplateTitleList(@RequestBody JSONObject jsonObject) {
        String access_token = jsonObject.getString("access_token");
        // 类目 id,多个用逗号隔开
        String IDS = jsonObject.getString("ids");
        // 用于分页,表示从 start 开始。从 0 开始计数
        String START = jsonObject.getString("start");
        // 用于分页,表示拉取 limit 条记录。最大为 30
        String LIMIT = jsonObject.getString("limit");
        String url = GET_PUB_TEMPLATE_TITLELIST.replace("ACCESS_TOKEN", access_token)
                .replace("IDS", IDS).replace("START", START).replace("LIMIT", LIMIT);
        String deleteResult = HttpUtil.get(url);
        JSONObject result = JSONObject.parseObject(deleteResult);
        return result;
    }

    /**
     * 发送订阅消息
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/sendmessage")
    public JSONObject sendMessage(@RequestBody JSONObject jsonObject) {
        String access_token = jsonObject.getString("access_token");
        // 接收者(用户)的 openid
        String touser = jsonObject.getString("touser");
        // 所需下发的订阅模板id
        String templateId = jsonObject.getString("templateId");
        // 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
        String lang = jsonObject.getString("lang");
        // 点击模板卡片后的跳转页面 该字段不填则模板无跳转
        String page = jsonObject.getString("page");
        // 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
        String miniprogramState = jsonObject.getString("miniprogramState");
        // 模板内容
        JSONObject data = jsonObject.getJSONObject("data");
        System.out.println(data);
        JSONObject paream = new JSONObject();
        paream.put("touser",touser);
        paream.put("templateId",templateId);
        paream.put("page",page);
        paream.put("lang",lang);
        paream.put("miniprogramState",miniprogramState);
        paream.put("data",data);
        String url = SEND_MESSAGE.replace("ACCESS_TOKEN", access_token);
        String body = HttpRequest.post(url).body(JSONUtil.toJsonStr(paream)).execute().body();
        JSONObject result = JSONObject.parseObject(body);
        return result;
    }

    /**
     * 添加模板
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/addmessagetemplate")
    public JSONObject addMessageTemplate(@RequestBody JSONObject jsonObject) {
        String access_token = jsonObject.getString("access_token");
        // 模板标题 id
        String tid = jsonObject.getString("tid");
        // 开发者自行组合好的模板关键词列表
        List<Integer> kidLists = jsonObject.getObject("kidList", List.class);
        // 服务场景描述
        String sceneDesc = jsonObject.getString("sceneDesc");
        Map<String,Object> paream = new HashMap<>();
        paream.put("tid",tid);
        paream.put("kidList",kidLists);
        paream.put("sceneDesc",sceneDesc);
        String url = ADD_MESSAGE_TEMPLATE.replace("ACCESS_TOKEN", access_token);
        String body = HttpRequest.post(url).body(JSONUtil.toJsonStr(paream)).execute().body();
        JSONObject result = JSONObject.parseObject(body);
        return result;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杨同学_丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值