yujian
2020-01-03 c9cbdfa41d645d42eeaa7e06d550d4ef8ac328e7
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
package com.yeshi.fanli.util;
 
import java.net.URLEncoder;
import java.util.List;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class OpenfireUtil {
    public final static String HOST = "http://193.112.35.168:9090";
 
    public final static String KEY = "linestoken2016bigbig";
 
    public final static int PRESENSE_UNKOWN = -1;
    public final static int PRESENSE_UNREGISTER = 0;
    public final static int PRESENSE_ONLINE = 1;
    public final static int PRESENSE_OFFLINE = 2;
 
    public static String pushMessage(String to, String msg) {
        try {
            String url = String.format(HOST + "/plugins/systemManager?type=pushMessage&to=%s&body=%s&sign=%s", to,
                    URLEncoder.encode(StringUtil.getBase64String(msg), "UTF-8"), StringUtil.Md5(to + KEY));
            String result = get(url);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static int getPresenseState(String node) {
        int state = -1;
        String url = String.format(HOST + "/plugins/systemManager?type=getPresence&from=%s&sign=%s", node,
                StringUtil.Md5(node + KEY));
        String result = get(url);
        try {
            JSONObject object = JSONObject.fromObject(result);
            if (object.optInt("state") == 0) {
                state = object.optJSONObject("data").optInt("state");
            }
        } catch (Exception e) {
 
        }
        return state;
    }
 
    public static boolean addWXFriendByPhone(String from, String phone, String welcometext) {
        JSONObject root = new JSONObject();
        root.put("job", "add_friend_by_search_mobile_job");
        JSONObject data = new JSONObject();
        data.put("mobile", phone);
        data.put("validation_message", welcometext);
        root.put("data", data);
        String result = pushMessage(from, root.toString());
        if (JSONObject.fromObject(result).optInt("state") == 0) {// �ɹ�
            return true;
        } else
            return false;
    }
 
    // ���΢���?
    public static boolean addWXGroup(String from, String ownerName, String adName, int numberCount, String groupName) {
        JSONObject root = new JSONObject();
        root.put("job", "create_group_chat_job");
        JSONObject data = new JSONObject();
        data.put("group_chat_ad_member", adName);
        data.put("group_chat_room_owner", ownerName);
        data.put("group_chat_member_count", numberCount);
        data.put("group_chat_room_name", groupName);
        root.put("data", data);
        String result = pushMessage(from, root.toString());
        if (JSONObject.fromObject(result).optInt("state") == 0) {// �ɹ�
            return true;
        } else
            return false;
    }
 
    // ת��С��Ƶ
    public static boolean forwardSightJob(String from, String fromRoom, String toRoom) {
        JSONObject root = new JSONObject();
        root.put("job", "forward_sight_job");
        JSONObject data = new JSONObject();
        data.put("room_name_for_forward_sight_from", fromRoom);
        data.put("room_name_for_forward_sight_to", toRoom);
        root.put("data", data);
        String result = pushMessage(from, root.toString());
        if (JSONObject.fromObject(result).optInt("state") == 0) {// �ɹ�
            return true;
        } else
            return false;
    }
 
    // ��ȡȺ��ά��
    public static boolean getRoomQrCode(String from, String to) {
        JSONObject root = new JSONObject();
        root.put("job", "get_room_qr_code_job");
        JSONObject data = new JSONObject();
        data.put("room_name_for_forward_sight_from", from);
        data.put("room_name_for_forward_sight_to", to);
        root.put("data", data);
        String result = pushMessage(from, root.toString());
        if (JSONObject.fromObject(result).optInt("state") == 0) {// �ɹ�
            return true;
        } else
            return false;
    }
 
    // ������Ȧ
    public static boolean sendWXCircle(String from, String text) {
        JSONObject root = new JSONObject();
        root.put("job", "create_moment_job");
        JSONObject data = new JSONObject();
        data.put("moment_text", text);
        root.put("data", data);
        String result = pushMessage(from, root.toString());
        if (JSONObject.fromObject(result).optInt("state") == 0) {// �ɹ�
            return true;
        } else
            return false;
    }
 
    // ������Ȧ
    public static boolean sendWXCircle(String from, String text, List<String> imgUrlList) {
        JSONObject root = new JSONObject();
        root.put("job", "create_moment_job");
        JSONObject data = new JSONObject();
        data.put("moment_text", text);
        JSONArray array = new JSONArray();
        for (String url : imgUrlList)
            array.add(url);
        data.put("moment_photos", array);
        root.put("data", data);
        String result = pushMessage(from, root.toString());
        if (JSONObject.fromObject(result).optInt("state") == 0) {// �ɹ�
            return true;
        } else
            return false;
    }
 
    /**
     * �޸ĸ�����Ϣ
     * 
     * @param from
     * @param portrait
     *            ͷ��
     * @param name
     *            �dz�
     * @param sex
     *            �Ա�
     * @param sign
     *            ǩ��
     * @return
     */
    public static boolean uploadPerinfo(String from, String portrait, String name, String sex, String sign) {
        JSONObject root = new JSONObject();
        root.put("job", "modify_account_info_job");
        JSONObject data = new JSONObject();
        data.put("account_avatar", portrait);
        data.put("account_name", name);
        data.put("account_gender", sex);
        data.put("account_sign", sign);
        root.put("data", data);
        String result = pushMessage(from, root.toString());
        if (JSONObject.fromObject(result).optInt("state") == 0) {// �ɹ�
            return true;
        } else
            return false;
    }
 
    private static String get(String url) {
        HttpClient client = new HttpClient();
        GetMethod gm = new GetMethod(url);
        try {
            client.executeMethod(gm);
            return gm.getResponseBodyAsString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}