fhx
2024-04-10 240ec6cefeda101e6cf5eff64c291b1b9eb94642
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
package com.hx.wx.gzh;
 
import com.hx.common.service.CommonService;
import com.hx.exception.TipsException;
import com.hx.util.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
 
/**
 * 微信公众号工具类
 * @author wzh
 */
public class WxGzhUtil {
 
    private static Logger logger = LoggerFactory.getLogger(WxGzhUtil.class);
 
    public static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create";
    public static final String DELETE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete";
    public static final String GET_MENU_URL = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info";
    public static final String GET_MATERIAL_URL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material";
 
 
    /**
     * 创建公众号自定义菜单
     * @param token
     * @param dataString
     * @return
     */
    public static String createMenu (String token, String dataString) {
        try {
            return HttpUtil.post(CREATE_MENU_URL, token, dataString);
        } catch (Exception e) {
            logger.error("更新微信公众号自定义菜单失败:" + e);
            throw new TipsException("更新失败");
        }
    }
 
    /**
     * 删除公众号自定义菜单
     * @param token
     * @param dataString
     * @return
     */
    public static String deleteMenu (String token, String dataString) {
        try {
            return HttpUtil.post(DELETE_MENU_URL, token, dataString);
        } catch (Exception e) {
            logger.error("删除微信公众号自定义菜单失败:" + e);
            throw new TipsException("删除失败");
        }
    }
 
    /**
     * 查询与拉取微信公众号自定义菜单
     * @param token
     * @return
     */
    public static String getMenuInfo (String token, CommonService commonService) {
        try {
            String result = HttpUtil.post(GET_MENU_URL, token, "");
            JSONObject obj = JSONObject.fromObject(result);
            JSONObject menuInfo = obj.getJSONObject("selfmenu_info");
            if (menuInfo != null) {
                JSONArray button = menuInfo.getJSONArray("button");
//                if (button != null && button.size() > 0) {
//                    // 删除旧数据
//                    SqlSentence sqlSentence = new SqlSentence();
//                    Map<String,Object> sqlMap = new HashMap<>();
//                    sqlMap.put("isDel", BaseEntity.NO);
//                    sqlMap.put("del", BaseEntity.YES);
//                    sqlSentence.sqlSentence(" isDel = #{m.del} where isDel = #{m.isDel}", sqlMap);
//                    commonService.updateWhere(MenuMapper.class, sqlSentence);
//                    button.forEach(b -> {
//                        JSONObject j = JSONObject.fromObject(b);
//                        if (j != null) {
//                            getButton(j, commonService, "");
//                        }
//                    });
//                }
            }
            return result;
        } catch (Exception e) {
            logger.error("查询微信公众号自定义菜单失败:" + e);
            throw new TipsException("查询失败");
        }
    }
 
    /**
     * 获取素材列表
     * @param token
     * @return
     */
    public static JSONObject materialList (String token, String request) {
        try {
            return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token="+token,request);
        } catch (Exception e) {
            logger.error("获取素材列表:" + e);
            throw new TipsException("获取素材列表");
        }
    }
 
    /**
     * 获取素材列表
     * @param token
     * @return
     */
    public static JSONObject qrcodeCreate (String token, String request) {
        try {
            return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+token,request);
        } catch (Exception e) {
            logger.error("获取二维码信息:" + e);
            throw new TipsException("获取二维码信息失败");
        }
    }
 
    /**
     * 获取模板列表
     * @param token
     * @return
     */
    public static JSONObject templateList (String token, String request) {
        try {
            return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token="+token,request);
        } catch (Exception e) {
            logger.error("获取模板列表:" + e);
            throw new TipsException("获取模板列表失败");
        }
    }
 
    /**
     * 发送模板消息
     * @param token
     * @return
     */
    public static JSONObject templateSend (String token, String request) {
        try {
            return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token,request);
        } catch (Exception e) {
            logger.error("发送模板消息:" + e);
            throw new TipsException("发送模板消息失败");
        }
    }
 
    /**
     * 获取永久素材
     * @param token
     * @return
     */
    public static JSONObject mediaGet (String token, String request) {
        try {
            return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/material/get_material?access_token="+token,request);
        } catch (Exception e) {
            logger.error("获取永久素材:" + e);
            throw new TipsException("获取永久素材失败");
        }
    }
 
    /**
     * 批量获取用户基本信息
     * @param token
     * @return
     */
    public static JSONObject batchGetUserInfo (String token, String request) {
        try {
            return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token="+token,request);
        } catch (Exception e) {
            logger.error("批量获取用户基本信息:" + e);
            throw new TipsException("批量获取用户基本信息");
        }
    }
}