下方代码包含以下操作方法:
-
获取 ACCESS_TOKEN
-
获取个人模板列表
-
删除消息模板根据id
-
获取类目
-
获取关键词列表
-
获取所属类目下的公共模板
-
发送订阅消息
-
添加模板
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;
}
}