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
package org.yeshi.utils.wx;
 
import java.io.File;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.yeshi.utils.HttpUtil;
 
import net.sf.json.JSONObject;
 
//小程序客服消息处理
public class XCXKeFuUtil {
    public final static String TYPE_IMAGE = "image";
 
    public static JSONObject parseHuiHuaMsg(HttpServletRequest request, String xmlContent, String token,
            String encodingASEKey, String appId) {
        String msgSignature = request.getParameter("msg_signature");
        String timeStamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        JSONObject root = new JSONObject();
        Map<String, String> map = WXUtil.parseXML(xmlContent, msgSignature, timeStamp, nonce, token, encodingASEKey,
                appId);
 
        JSONObject data = new JSONObject();
        data.put("openid", map.get("FromUserName"));
        data.put("ghid", map.get("ToUserName"));
        data.put("sessionFrom", map.get("SessionFrom"));
        root.put("data", data);
        if ("user_enter_tempsession".equalsIgnoreCase(map.get("Event"))) {
            root.put("code", 0);// 进入会话事件
        } else
            root.put("code", 1);// 其他事件
        return root;
    }
 
    private static boolean sendMsg(String acessToken, JSONObject data) {
        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + acessToken;
        String result = HttpUtil.post(url, data.toString());
        return true;
    }
 
    public static String uploadFile(String accessToken, String type, File f) {
        String url = String.format("https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s",
                accessToken, type);
        String result = HttpUtil.post(url, "media", f);
        JSONObject resultObject = JSONObject.fromObject(result);
        return resultObject.optString("media_id");
    }
 
    // 发送文字消息
    public static boolean sendTextMsg(String acessToken, String openId, String text) {
 
        JSONObject data = new JSONObject();
        data.put("touser", openId);
        data.put("msgtype", "text");
        JSONObject textObject = new JSONObject();
        textObject.put("content", text);
        data.put("text", textObject);
        return sendMsg(acessToken, data);
    }
 
    // 发送图片消息
    public static boolean sendImageMsg(String acessToken, String openId, String mediaId) {
        JSONObject data = new JSONObject();
        data.put("touser", openId);
        data.put("msgtype", "image");
        JSONObject imageObject = new JSONObject();
        imageObject.put("media_id", mediaId);
        data.put("image", imageObject);
        return sendMsg(acessToken, data);
    }
 
    // 发送图文消息
    public static boolean sendTextImageMsg(String acessToken, String openId, String title, String desc, String url,
            String thumbUrl) {
        JSONObject data = new JSONObject();
        data.put("touser", openId);
        data.put("msgtype", "link");
        JSONObject linkObject = new JSONObject();
        linkObject.put("title", title);
        linkObject.put("description", desc);
        linkObject.put("url", url);
        linkObject.put("thumb_url", thumbUrl);
        data.put("link", linkObject);
        return sendMsg(acessToken, data);
    }
    
    
 
}