puzhibing
2023-02-26 8883d654f52171657b62aaec85027f29ef0a4dd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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();
        }
    }
 
 
}