package com.supersavedriving.user.modular.system.util.weChat;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.stylefeng.guns.modular.system.util.RedisUtil;
|
import com.stylefeng.guns.modular.system.util.ResultUtil;
|
import com.stylefeng.guns.modular.system.util.httpClinet.HttpClientUtil;
|
import com.stylefeng.guns.modular.system.util.httpClinet.HttpResult;
|
import com.stylefeng.guns.modular.system.util.weChat.model.Category;
|
import com.stylefeng.guns.modular.system.util.weChat.model.MessageTemplate;
|
import com.stylefeng.guns.modular.system.util.weChat.model.PubTemplateKeywords;
|
import com.stylefeng.guns.modular.system.util.weChat.model.PubTemplatetitles;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 订阅消息
|
*/
|
@Component
|
public class SubscribeMessageUtil {
|
|
@Value("${wx.appletsAppid}")
|
private String wxAppletsAppid;
|
|
@Autowired
|
private RedisUtil redisUtil;
|
|
@Autowired
|
private HttpClientUtil httpClientUtil;
|
|
|
/**
|
* 获取消息类目
|
* @return
|
*/
|
public ResultUtil<List<Category>> getcategory(){
|
try {
|
String accessToken = redisUtil.getValue("wxAppletsAccessToken");
|
String url = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token=" + accessToken;
|
HttpResult httpResult = httpClientUtil.pushHttpRequset("GET", url, null, null, "form");
|
if(httpResult.getCode() != 200){
|
return ResultUtil.error("获取消息类目失败");
|
}
|
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
|
Integer errcode = jsonObject.getInteger("errcode");
|
if(0 != errcode){
|
return ResultUtil.error(jsonObject.getString("errmsg"));
|
}
|
JSONArray data = jsonObject.getJSONArray("data");
|
List<Category> categories = data.toJavaList(Category.class);
|
return ResultUtil.success(categories);
|
}catch (Exception e){
|
e.printStackTrace();
|
return ResultUtil.runErr();
|
}
|
}
|
|
|
/**
|
* 获取所属类目下的公共模板
|
* @param ids
|
* @return
|
*/
|
public ResultUtil<List<PubTemplatetitles>> getpubtemplatetitles(String ids){
|
List<PubTemplatetitles> pubTemplatetitles = new ArrayList<>();
|
try {
|
Integer start = 0;
|
while (true){
|
String accessToken = redisUtil.getValue("wxAppletsAccessToken");
|
String url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles?access_token=" + accessToken;
|
Map<String, Object> params = new HashMap<>();
|
params.put("ids", ids);
|
params.put("start", start + "");
|
params.put("limit", 30);
|
HttpResult httpResult = httpClientUtil.pushHttpRequset("GET", url, params, null, "form");
|
if(httpResult.getCode() != 200){
|
return ResultUtil.error("获取消息类目失败");
|
}
|
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
|
Integer errcode = jsonObject.getInteger("errcode");
|
if(0 != errcode){
|
return ResultUtil.error(jsonObject.getString("errmsg"));
|
}
|
JSONArray data = jsonObject.getJSONArray("data");
|
List<PubTemplatetitles> pubTemplatetitles1 = data.toJavaList(PubTemplatetitles.class);
|
if(pubTemplatetitles1.size() == 0){
|
break;
|
}
|
pubTemplatetitles.addAll(pubTemplatetitles1);
|
start += 30;
|
}
|
return ResultUtil.success(pubTemplatetitles);
|
}catch (Exception e){
|
e.printStackTrace();
|
return ResultUtil.runErr();
|
}
|
}
|
|
|
/**
|
* 获取关键词列表
|
* @param tid
|
* @return
|
*/
|
public ResultUtil<List<PubTemplateKeywords>> getpubtemplatekeywords(String tid){
|
try {
|
String accessToken = redisUtil.getValue("wxAppletsAccessToken");
|
String url = "https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords?access_token=" + accessToken;
|
Map<String, Object> params = new HashMap<>();
|
params.put("tid", tid);
|
HttpResult httpResult = httpClientUtil.pushHttpRequset("GET", url, params, null, "form");
|
if(httpResult.getCode() != 200){
|
return ResultUtil.error("获取消息类目失败");
|
}
|
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
|
Integer errcode = jsonObject.getInteger("errcode");
|
if(0 != errcode){
|
return ResultUtil.error(jsonObject.getString("errmsg"));
|
}
|
JSONArray data = jsonObject.getJSONArray("data");
|
List<PubTemplateKeywords> pubTemplateKeywords = data.toJavaList(PubTemplateKeywords.class);
|
return ResultUtil.success(pubTemplateKeywords);
|
}catch (Exception e){
|
e.printStackTrace();
|
return ResultUtil.runErr();
|
}
|
}
|
|
|
/**
|
* 获取个人模板列表
|
* @return
|
*/
|
public ResultUtil<List<MessageTemplate>> getMessageTemplateList(){
|
try {
|
String accessToken = redisUtil.getValue("wxAppletsAccessToken");
|
String url = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=" + accessToken;
|
HttpResult httpResult = httpClientUtil.pushHttpRequset("GET", url, null, null, "form");
|
if(httpResult.getCode() != 200){
|
return ResultUtil.error("获取消息模板失败");
|
}
|
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
|
Integer errcode = jsonObject.getInteger("errcode");
|
if(0 != errcode){
|
return ResultUtil.error(jsonObject.getString("errmsg"));
|
}
|
JSONArray data = jsonObject.getJSONArray("data");
|
List<MessageTemplate> messageTemplates = data.toJavaList(MessageTemplate.class);
|
return ResultUtil.success(messageTemplates);
|
}catch (Exception e){
|
e.printStackTrace();
|
return ResultUtil.runErr();
|
}
|
}
|
|
|
/**
|
* 添加消息模板
|
* @param tid 模板标题 id
|
* @param kidList 开发者自行组合好的模板关键词列表,关键词顺序可以自由搭配(例如 [3,5,4] 或 [4,5,3]),最多支持5个,最少2个关键词组合
|
* @param sceneDesc 服务场景描述,15个字以内
|
* @return
|
*/
|
public ResultUtil<String> addtemplate(String tid, String[] kidList, String sceneDesc){
|
try {
|
String accessToken = redisUtil.getValue("wxAppletsAccessToken");
|
String url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token=" + accessToken;
|
Map<String, Object> params = new HashMap<>();
|
params.put("tid", tid);
|
params.put("kidList", kidList);
|
params.put("sceneDesc", sceneDesc);
|
HttpResult httpResult = httpClientUtil.pushHttpRequset("POST", url, params, null, "json");
|
if(httpResult.getCode() != 200){
|
return ResultUtil.error("获取消息模板失败");
|
}
|
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
|
Integer errcode = jsonObject.getInteger("errcode");
|
if(0 != errcode){
|
return ResultUtil.error(jsonObject.getString("errmsg"));
|
}
|
String priTmplId = jsonObject.getString("priTmplId");
|
return ResultUtil.success(priTmplId);
|
}catch (Exception e){
|
e.printStackTrace();
|
return ResultUtil.runErr();
|
}
|
|
}
|
|
|
/**
|
* 删除模板
|
* @param priTmplId 模板id
|
* @return
|
*/
|
public ResultUtil deltemplate(String priTmplId){
|
try {
|
String accessToken = redisUtil.getValue("wxAppletsAccessToken");
|
String url = "https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate?access_token=" + accessToken;
|
Map<String, Object> params = new HashMap<>();
|
params.put("priTmplId", priTmplId);
|
HttpResult httpResult = httpClientUtil.pushHttpRequset("POST", url, params, null, "json");
|
if(httpResult.getCode() != 200){
|
return ResultUtil.error("获取消息模板失败");
|
}
|
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
|
Integer errcode = jsonObject.getInteger("errcode");
|
if(0 != errcode){
|
return ResultUtil.error(jsonObject.getString("errmsg"));
|
}
|
return ResultUtil.success();
|
}catch (Exception e){
|
e.printStackTrace();
|
return ResultUtil.runErr();
|
}
|
}
|
|
|
/**
|
* 发送消息
|
* @param template_id 所需下发的订阅模板id
|
* @param page 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转
|
* @param touser 接收者(用户)的 openid
|
* @param data 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }的object
|
* @param miniprogram_state 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
* @param lang 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
|
* @return
|
*/
|
public ResultUtil send(String template_id, String page, String touser, String data, String miniprogram_state, String lang){
|
try {
|
String accessToken = redisUtil.getValue("wxAppletsAccessToken");
|
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
|
Map<String, Object> params = new HashMap<>();
|
params.put("template_id", template_id);
|
params.put("page", page);
|
params.put("touser", touser);
|
params.put("data", JSON.parseObject(data));
|
params.put("miniprogram_state", miniprogram_state);
|
params.put("lang", lang);
|
HttpResult httpResult = httpClientUtil.pushHttpRequset("POST", url, params, null, "json");
|
if(httpResult.getCode() != 200){
|
return ResultUtil.error("获取消息模板失败");
|
}
|
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
|
Integer errcode = jsonObject.getInteger("errcode");
|
if(0 != errcode){
|
return ResultUtil.error(jsonObject.getString("errmsg"));
|
}
|
return ResultUtil.success();
|
}catch (Exception e){
|
e.printStackTrace();
|
return ResultUtil.runErr();
|
}
|
}
|
|
|
}
|