admin
2018-12-19 b91b38942de4a865f105c4ca1e7d5ffce1b60b66
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
package org.yeshi.utils.wx;
 
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
 
import javax.servlet.http.HttpServletRequest;
 
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.StringUtil;
 
import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;
 
import net.sf.json.JSONObject;
 
public class WXUtil {
 
    public static String getContent(HttpServletRequest req) {
        try {
            byte[] buffer = new byte[2048];
            int readBytes = 0;
            StringBuilder stringBuilder = new StringBuilder();
            while ((readBytes = req.getInputStream().read(buffer)) > 0) {
                stringBuilder.append(new String(buffer, 0, readBytes));
            }
            return stringBuilder.toString();
        } catch (Exception e) {
 
        }
        return null;
    }
 
    @SuppressWarnings("unchecked")
    public static Map<String, String> parseXML(HttpServletRequest req) {
 
        SAXReader reader = new SAXReader();
        try {
            byte[] buffer = new byte[2048];
            int readBytes = 0;
            StringBuilder stringBuilder = new StringBuilder();
            while ((readBytes = req.getInputStream().read(buffer)) > 0) {
                stringBuilder.append(new String(buffer, 0, readBytes));
            }
            InputStream inputStream = new ByteArrayInputStream(stringBuilder.toString().getBytes());
            Map<String, String> map = new HashMap<String, String>();
            Document document = reader.read(inputStream);
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();
            // 遍历所有子节点
            for (Element e : elementList)
                map.put(e.getName(), e.getText());
            inputStream.close();
            inputStream = null;
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    // 获取微信回调的内容
    public static String getWXXMLBody(HttpServletRequest req) {
        byte[] buffer = new byte[2048];
        int readBytes = 0;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            while ((readBytes = req.getInputStream().read(buffer)) > 0) {
                stringBuilder.append(new String(buffer, 0, readBytes));
            }
        } catch (Exception e) {
        }
        return stringBuilder.toString();
    }
 
    @SuppressWarnings("unchecked")
    public static Map<String, String> parseXML(String content, String msgSignature, String timeStamp, String nonce,
            String token, String encodingAesKey, String appId) {
        SAXReader reader = new SAXReader();
        try {
            String result = "";
            try {
                WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
                result = pc.DecryptMsg(msgSignature, timeStamp, nonce, content);
            } catch (AesException e1) {
                e1.printStackTrace();
            }
 
            InputStream inputStream = new ByteArrayInputStream(result.getBytes());
            Map<String, String> map = new HashMap<String, String>();
            Document document = reader.read(inputStream);
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();
            // 遍历所有子节点
            for (Element e : elementList)
                map.put(e.getName(), e.getText());
            inputStream.close();
            inputStream = null;
 
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    @SuppressWarnings("unchecked")
    public static Map<String, String> parseXML(HttpServletRequest req, String pwd) {
        SAXReader reader = new SAXReader();
        try {
            byte[] buffer = new byte[2048];
            int readBytes = 0;
            StringBuilder stringBuilder = new StringBuilder();
            while ((readBytes = req.getInputStream().read(buffer)) > 0) {
                stringBuilder.append(new String(buffer, 0, readBytes));
            }
            System.out.println(stringBuilder.toString());
            InputStream inputStream = new ByteArrayInputStream(stringBuilder.toString().getBytes());
            Map<String, String> map = new HashMap<String, String>();
            Document document = reader.read(inputStream);
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();
            // 遍历所有子节点
            for (Element e : elementList)
                map.put(e.getName(), e.getText());
            inputStream.close();
            inputStream = null;
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    @SuppressWarnings("unchecked")
    public static Map<String, String> parseXML(String data) {
        SAXReader reader = new SAXReader();
        try {
            InputStream inputStream = new ByteArrayInputStream(data.getBytes("UTF-8"));
            Map<String, String> map = new HashMap<String, String>();
            Document document = reader.read(inputStream);
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();
            // 遍历所有子节点
            for (Element e : elementList)
                map.put(e.getName(), e.getText());
            inputStream.close();
            inputStream = null;
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    public static String loadWXMessage(Map<String, String> map) {
        Set<String> set = map.keySet();
        Iterator<String> its = set.iterator();
        String xml = "<xml>";
        while (its.hasNext()) {
            String key = its.next();
            String value = map.get(key);
            xml += String.format("<%1$s><![CDATA[%2$s]]></%3$s>", key, value, key);
        }
        xml += "</xml>";
        return xml;
    }
 
    public static String getSignMD5(Map<String, String> map, String k) {
        Map<String, String> tm = new TreeMap<String, String>();
        tm.putAll(map);
        Set<String> set = tm.keySet();
        Iterator<String> its = set.iterator();
        String result = "";
        while (its.hasNext()) {
            String key = its.next();
            String val = tm.get(key);
            result += key + "=" + val + "&";
        }
        result += "key=" + k;
        return StringUtil.Md5(result).toUpperCase();
    }
 
    public static String getAcessToken(String appId, String appSecret) {
        String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId
                + "&secret=" + appSecret;
        String token = HttpUtil.get(tokenUrl);
        String access_token = JSONObject.fromObject(token).optString("access_token");
        return access_token;
    }
 
    // 获取详细的用户信息
    public static JSONObject getDetailGZUserInfo(String openId, String accessToken) {
        String url = String.format("https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN",
                accessToken, openId);
        String result = HttpUtil.get(url);
        return JSONObject.fromObject(result);
    }
 
    // 创建菜单
    public static void createMenu(String accessToken, JSONObject root) {
        HttpUtil.post("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken);
        // 创建菜单再
        String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
        String result = HttpUtil.post(url, root.toString());
    }
 
}