chenjiahe
5 天以前 826b66207dafbce24f441cb83fed1b241a6fba27
提交 | 用户 | age
826b66 1 package com.hx.wx.gzh;
C 2
3 import com.hx.common.service.CommonService;
4 import com.hx.exception.TipsException;
5 import com.hx.util.HttpUtil;
6 import net.sf.json.JSONArray;
7 import net.sf.json.JSONObject;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11
12 /**
13  * 微信公众号工具类
14  * @author wzh
15  */
16 public class WxGzhUtil {
17
18     private static Logger logger = LoggerFactory.getLogger(WxGzhUtil.class);
19
20     public static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create";
21     public static final String DELETE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete";
22     public static final String GET_MENU_URL = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info";
23     public static final String GET_MATERIAL_URL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
24
25
26     /**
27      * 创建公众号自定义菜单
28      * @param token
29      * @param dataString
30      * @return
31      */
32     public static String createMenu (String token, String dataString) {
33         try {
34             return HttpUtil.post(CREATE_MENU_URL, token, dataString);
35         } catch (Exception e) {
36             logger.error("更新微信公众号自定义菜单失败:" + e);
37             throw new TipsException("更新失败");
38         }
39     }
40
41     /**
42      * 删除公众号自定义菜单
43      * @param token
44      * @param dataString
45      * @return
46      */
47     public static String deleteMenu (String token, String dataString) {
48         try {
49             return HttpUtil.post(DELETE_MENU_URL, token, dataString);
50         } catch (Exception e) {
51             logger.error("删除微信公众号自定义菜单失败:" + e);
52             throw new TipsException("删除失败");
53         }
54     }
55
56     /**
57      * 查询与拉取微信公众号自定义菜单
58      * @param token
59      * @return
60      */
61     public static String getMenuInfo (String token, CommonService commonService) {
62         try {
63             String result = HttpUtil.post(GET_MENU_URL, token, "");
64             JSONObject obj = JSONObject.fromObject(result);
65             JSONObject menuInfo = obj.getJSONObject("selfmenu_info");
66             if (menuInfo != null) {
67                 JSONArray button = menuInfo.getJSONArray("button");
68 //                if (button != null && button.size() > 0) {
69 //                    // 删除旧数据
70 //                    SqlSentence sqlSentence = new SqlSentence();
71 //                    Map<String,Object> sqlMap = new HashMap<>();
72 //                    sqlMap.put("isDel", BaseEntity.NO);
73 //                    sqlMap.put("del", BaseEntity.YES);
74 //                    sqlSentence.sqlSentence(" isDel = #{m.del} where isDel = #{m.isDel}", sqlMap);
75 //                    commonService.updateWhere(MenuMapper.class, sqlSentence);
76 //                    button.forEach(b -> {
77 //                        JSONObject j = JSONObject.fromObject(b);
78 //                        if (j != null) {
79 //                            getButton(j, commonService, "");
80 //                        }
81 //                    });
82 //                }
83             }
84             return result;
85         } catch (Exception e) {
86             logger.error("查询微信公众号自定义菜单失败:" + e);
87             throw new TipsException("查询失败");
88         }
89     }
90
91     /**
92      * 获取素材列表
93      * @param token
94      * @return
95      */
96     public static JSONObject materialList (String token, String request) {
97         try {
98             return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token="+token,request);
99         } catch (Exception e) {
100             logger.error("获取素材列表:" + e);
101             throw new TipsException("获取素材列表");
102         }
103     }
104
105     /**
106      * 获取素材列表
107      * @param token
108      * @return
109      */
110     public static JSONObject qrcodeCreate (String token, String request) {
111         try {
112             return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+token,request);
113         } catch (Exception e) {
114             logger.error("获取二维码信息:" + e);
115             throw new TipsException("获取二维码信息失败");
116         }
117     }
118
119     /**
120      * 获取模板列表
121      * @param token
122      * @return
123      */
124     public static JSONObject templateList (String token, String request) {
125         try {
126             return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token="+token,request);
127         } catch (Exception e) {
128             logger.error("获取模板列表:" + e);
129             throw new TipsException("获取模板列表失败");
130         }
131     }
132
133     /**
134      * 发送模板消息
135      * @param token
136      * @return
137      */
138     public static JSONObject templateSend (String token, String request) {
139         try {
140             return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token,request);
141         } catch (Exception e) {
142             logger.error("发送模板消息:" + e);
143             throw new TipsException("发送模板消息失败");
144         }
145     }
146
147     /**
148      * 获取永久素材
149      * @param token
150      * @return
151      */
152     public static JSONObject mediaGet (String token, String request) {
153         try {
154             return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/material/get_material?access_token="+token,request);
155         } catch (Exception e) {
156             logger.error("获取永久素材:" + e);
157             throw new TipsException("获取永久素材失败");
158         }
159     }
160
161     /**
162      * 批量获取用户基本信息
163      * @param token
164      * @return
165      */
166     public static JSONObject batchGetUserInfo (String token, String request) {
167         try {
168             return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token="+token,request);
169         } catch (Exception e) {
170             logger.error("批量获取用户基本信息:" + e);
171             throw new TipsException("批量获取用户基本信息");
172         }
173     }
174 }