办学质量监测教学评价系统
ageer
2024-02-27 c84894c5a5393ee031a71f45e05a084096745ee5
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
package com.xmzs.common.wechat.api;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
import com.xmzs.common.wechat.core.Core;
import com.xmzs.common.wechat.utils.enums.StorageLoginInfoEnum;
import com.xmzs.common.wechat.utils.enums.URLEnum;
 
/**
 * 微信小工具,如获好友列表等
 *
 * @author https://github.com/yaphone
 * @date 创建时间:2017年5月4日 下午10:49:16
 * @version 1.0
 *
 */
public class WechatTools {
    private static Logger LOG = LoggerFactory.getLogger(WechatTools.class);
 
    private static Core core = Core.getInstance();
 
    /**
     * 根据用户名发送文本消息
     *
     * @author https://github.com/yaphone
     * @date 2017年5月4日 下午10:43:14
     * @param msg
     * @param toUserName
     */
    public static void sendMsgByUserName(String msg, String toUserName) {
        MessageTools.sendMsgById(msg, toUserName);
    }
 
    /**
     * <p>
     * 通过RealName获取本次UserName
     * </p>
     * <p>
     * 如NickName为"yaphone",则获取UserName=
     * "@1212d3356aea8285e5bbe7b91229936bc183780a8ffa469f2d638bf0d2e4fc63",
     * 可通过UserName发送消息
     * </p>
     *
     * @author https://github.com/yaphone
     * @date 2017年5月4日 下午10:56:31
     * @param name
     * @return
     */
    public static String getUserNameByNickName(String nickName) {
        for (JSONObject o : core.getContactList()) {
            if (o.getString("NickName").equals(nickName)) {
                return o.getString("UserName");
            }
        }
        return null;
    }
 
    /**
     * 返回好友昵称列表
     *
     * @author https://github.com/yaphone
     * @date 2017年5月4日 下午11:37:20
     * @return
     */
    public static List<String> getContactNickNameList() {
        List<String> contactNickNameList = new ArrayList<String>();
        for (JSONObject o : core.getContactList()) {
            contactNickNameList.add(o.getString("NickName"));
        }
        return contactNickNameList;
    }
 
    /**
     * 返回好友完整信息列表
     *
     * @date 2017年6月26日 下午9:45:39
     * @return
     */
    public static List<JSONObject> getContactList() {
        return core.getContactList();
    }
 
    /**
     * 返回群列表
     *
     * @author https://github.com/yaphone
     * @date 2017年5月5日 下午9:55:21
     * @return
     */
    public static List<JSONObject> getGroupList() {
        return core.getGroupList();
    }
 
    /**
     * 获取群ID列表
     *
     * @date 2017年6月21日 下午11:42:56
     * @return
     */
    public static List<String> getGroupIdList() {
        return core.getGroupIdList();
    }
 
    /**
     * 获取群NickName列表
     *
     * @date 2017年6月21日 下午11:43:38
     * @return
     */
    public static List<String> getGroupNickNameList() {
        return core.getGroupNickNameList();
    }
 
    /**
     * 根据groupIdList返回群成员列表
     *
     * @date 2017年6月13日 下午11:12:31
     * @param groupId
     * @return
     */
    public static JSONArray getMemberListByGroupId(String groupId) {
        return core.getGroupMemeberMap().get(groupId);
    }
 
    /**
     * 退出微信
     *
     * @author https://github.com/yaphone
     * @date 2017年5月18日 下午11:56:54
     */
    public static void logout() {
        webWxLogout();
    }
 
    private static boolean webWxLogout() {
        String url = String.format(URLEnum.WEB_WX_LOGOUT.getUrl(),
                core.getLoginInfo().get(StorageLoginInfoEnum.url.getKey()));
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("redirect", "1"));
        params.add(new BasicNameValuePair("type", "1"));
        params.add(
                new BasicNameValuePair("skey", (String) core.getLoginInfo().get(StorageLoginInfoEnum.skey.getKey())));
        try {
            HttpEntity entity = core.getMyHttpClient().doGet(url, params, false, null);
            String text = EntityUtils.toString(entity, Consts.UTF_8); // 无消息
            return true;
        } catch (Exception e) {
            LOG.debug(e.getMessage());
        }
        return false;
    }
 
    public static void setUserInfo() {
        for (JSONObject o : core.getContactList()) {
            core.getUserInfoMap().put(o.getString("NickName"), o);
            core.getUserInfoMap().put(o.getString("UserName"), o);
        }
    }
 
    /**
     *
     * 根据用户昵称设置备注名称
     *
     * @date 2017年5月27日 上午12:21:40
     * @param userName
     * @param remName
     */
    public static void remarkNameByNickName(String nickName, String remName) {
        String url = String.format(URLEnum.WEB_WX_REMARKNAME.getUrl(), core.getLoginInfo().get("url"),
                core.getLoginInfo().get(StorageLoginInfoEnum.pass_ticket.getKey()));
        Map<String, Object> msgMap = new HashMap<String, Object>();
        Map<String, Object> msgMap_BaseRequest = new HashMap<String, Object>();
        msgMap.put("CmdId", 2);
        msgMap.put("RemarkName", remName);
        msgMap.put("UserName", core.getUserInfoMap().get(nickName).get("UserName"));
        msgMap_BaseRequest.put("Uin", core.getLoginInfo().get(StorageLoginInfoEnum.wxuin.getKey()));
        msgMap_BaseRequest.put("Sid", core.getLoginInfo().get(StorageLoginInfoEnum.wxsid.getKey()));
        msgMap_BaseRequest.put("Skey", core.getLoginInfo().get(StorageLoginInfoEnum.skey.getKey()));
        msgMap_BaseRequest.put("DeviceID", core.getLoginInfo().get(StorageLoginInfoEnum.deviceid.getKey()));
        msgMap.put("BaseRequest", msgMap_BaseRequest);
        try {
            String paramStr = JSON.toJSONString(msgMap);
            HttpEntity entity = core.getMyHttpClient().doPost(url, paramStr);
            // String result = EntityUtils.toString(entity, Consts.UTF_8);
            LOG.info("修改备注" + remName);
        } catch (Exception e) {
            LOG.error("remarkNameByUserName", e);
        }
    }
 
    /**
     * 获取微信在线状态
     *
     * @date 2017年6月16日 上午12:47:46
     * @return
     */
    public static boolean getWechatStatus() {
        return core.isAlive();
    }
 
}