mirror of
https://gitee.com/hotlcc/wechat4j.git
synced 2025-06-13 05:54:07 +08:00
[update] WebWeixinApi改为WebWeixinApiUtil工具类方式
This commit is contained in:
parent
31f84b62bf
commit
9302b2e68c
@ -17,10 +17,8 @@
|
||||
## 简单使用
|
||||
|
||||
```java
|
||||
WebWeixinApi api = new WebWeixinApi();
|
||||
// 实例化微信客户端
|
||||
Wechat wechat = new Wechat();
|
||||
wechat.setWebWeixinApi(api);
|
||||
// 自动登录
|
||||
wechat.autoLogin();
|
||||
```
|
||||
|
@ -2,7 +2,6 @@ package com.hotlcc.wechat4j;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hotlcc.wechat4j.api.WebWeixinApi;
|
||||
import com.hotlcc.wechat4j.enums.*;
|
||||
import com.hotlcc.wechat4j.handler.ExitEventHandler;
|
||||
import com.hotlcc.wechat4j.handler.ReceivedMsgHandler;
|
||||
@ -39,8 +38,6 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
public class Wechat {
|
||||
private static Logger logger = LoggerFactory.getLogger(Wechat.class);
|
||||
|
||||
private WebWeixinApi webWeixinApi;
|
||||
|
||||
private CookieStore cookieStore;
|
||||
private HttpClient httpClient;
|
||||
|
||||
@ -78,10 +75,6 @@ public class Wechat {
|
||||
this(new BasicCookieStore());
|
||||
}
|
||||
|
||||
public void setWebWeixinApi(WebWeixinApi webWeixinApi) {
|
||||
this.webWeixinApi = webWeixinApi;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Cookie
|
||||
*
|
||||
@ -216,7 +209,7 @@ public class Wechat {
|
||||
pw.print("\t第" + i + "次尝试...");
|
||||
pw.flush();
|
||||
}
|
||||
JSONObject result = webWeixinApi.getWxUuid(httpClient);
|
||||
JSONObject result = WebWeixinApiUtil.getWxUuid(httpClient);
|
||||
|
||||
if (result == null) {
|
||||
pw.println("\t失败:出现异常");
|
||||
@ -265,7 +258,7 @@ public class Wechat {
|
||||
pw.flush();
|
||||
}
|
||||
|
||||
byte[] data = webWeixinApi.getQR(httpClient, uuid);
|
||||
byte[] data = WebWeixinApiUtil.getQR(httpClient, uuid);
|
||||
|
||||
if (data == null || data.length <= 0) {
|
||||
pw.print("\t失败");
|
||||
@ -302,7 +295,7 @@ public class Wechat {
|
||||
|
||||
boolean flag = false;
|
||||
while (true) {
|
||||
JSONObject result = webWeixinApi.getRedirectUri(httpClient, LoginTipEnum.TIP_0, uuid);
|
||||
JSONObject result = WebWeixinApiUtil.getRedirectUri(httpClient, LoginTipEnum.TIP_0, uuid);
|
||||
if (result == null) {
|
||||
pw.println("\t失败:出现异常");
|
||||
pw.flush();
|
||||
@ -343,7 +336,7 @@ public class Wechat {
|
||||
pw.print("获取登录认证码...");
|
||||
pw.flush();
|
||||
|
||||
JSONObject result = webWeixinApi.getLoginCode(httpClient, redirectUri);
|
||||
JSONObject result = WebWeixinApiUtil.getLoginCode(httpClient, redirectUri);
|
||||
if (result == null) {
|
||||
pw.println("\t失败:出现异常");
|
||||
pw.flush();
|
||||
@ -379,7 +372,7 @@ public class Wechat {
|
||||
pw.print("尝试push方式获取uuid...");
|
||||
pw.flush();
|
||||
|
||||
JSONObject result = webWeixinApi.pushLogin(httpClient, urlVersion, wxuin);
|
||||
JSONObject result = WebWeixinApiUtil.pushLogin(httpClient, urlVersion, wxuin);
|
||||
if (result == null) {
|
||||
pw.println("\t失败:出现异常");
|
||||
pw.flush();
|
||||
@ -412,7 +405,7 @@ public class Wechat {
|
||||
* @return
|
||||
*/
|
||||
private boolean wxInit() {
|
||||
JSONObject result = webWeixinApi.webWeixinInit(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin));
|
||||
JSONObject result = WebWeixinApiUtil.webWeixinInit(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin));
|
||||
if (result == null) {
|
||||
return false;
|
||||
}
|
||||
@ -476,7 +469,7 @@ public class Wechat {
|
||||
*/
|
||||
private boolean statusNotify(int time) {
|
||||
for (int i = 0; i < time; i++) {
|
||||
JSONObject result = webWeixinApi.statusNotify(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin), getLoginUserName(false));
|
||||
JSONObject result = WebWeixinApiUtil.statusNotify(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin), getLoginUserName(false));
|
||||
if (result == null) {
|
||||
continue;
|
||||
}
|
||||
@ -593,7 +586,7 @@ public class Wechat {
|
||||
isOnlineLock.lock();
|
||||
|
||||
if (isOnline) {
|
||||
webWeixinApi.logout(httpClient, urlVersion, new BaseRequest(wxsid, skey, wxuin));
|
||||
WebWeixinApiUtil.logout(httpClient, urlVersion, new BaseRequest(wxsid, skey, wxuin));
|
||||
isOnline = false;
|
||||
|
||||
if (clearAllLoginInfo) {
|
||||
@ -663,7 +656,7 @@ public class Wechat {
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
try {
|
||||
JSONObject result = webWeixinApi.syncCheck(httpClient, urlVersion, new BaseRequest(wxsid, skey, wxuin), getSyncKeyList(false));
|
||||
JSONObject result = WebWeixinApiUtil.syncCheck(httpClient, urlVersion, new BaseRequest(wxsid, skey, wxuin), getSyncKeyList(false));
|
||||
logger.info("微信同步监听心跳返回数据:{}", result);
|
||||
if (result == null) {
|
||||
throw new RuntimeException("微信API调用异常");
|
||||
@ -797,7 +790,7 @@ public class Wechat {
|
||||
*/
|
||||
private void webWxSync() {
|
||||
try {
|
||||
JSONObject result = webWeixinApi.webWxSync(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin), syncKey);
|
||||
JSONObject result = WebWeixinApiUtil.webWxSync(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin), syncKey);
|
||||
if (result == null) {
|
||||
logger.error("从服务端同步新数据异常");
|
||||
return;
|
||||
@ -879,7 +872,7 @@ public class Wechat {
|
||||
*/
|
||||
public UserInfo getLoginUser(boolean update) {
|
||||
if (loginUser == null || update) {
|
||||
JSONObject result = webWeixinApi.webWeixinInit(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin));
|
||||
JSONObject result = WebWeixinApiUtil.webWeixinInit(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin));
|
||||
if (result == null) {
|
||||
return loginUser;
|
||||
}
|
||||
@ -954,7 +947,7 @@ public class Wechat {
|
||||
*/
|
||||
private JSONObject getSyncKey(boolean update) {
|
||||
if (syncKey == null || update) {
|
||||
JSONObject result = webWeixinApi.webWeixinInit(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin));
|
||||
JSONObject result = WebWeixinApiUtil.webWeixinInit(httpClient, urlVersion, passTicket, new BaseRequest(wxsid, skey, wxuin));
|
||||
if (result == null) {
|
||||
return syncKey;
|
||||
}
|
||||
@ -1005,7 +998,7 @@ public class Wechat {
|
||||
*/
|
||||
public List<UserInfo> getContactList(boolean update) {
|
||||
if (contactList == null || update) {
|
||||
JSONObject result = webWeixinApi.getContact(httpClient, urlVersion, passTicket, skey);
|
||||
JSONObject result = WebWeixinApiUtil.getContact(httpClient, urlVersion, passTicket, skey);
|
||||
if (result == null) {
|
||||
return contactList;
|
||||
}
|
||||
@ -1164,7 +1157,7 @@ public class Wechat {
|
||||
}
|
||||
message.setType(MsgTypeEnum.TEXT_MSG.getCode());
|
||||
|
||||
return webWeixinApi.sendMsg(httpClient, urlVersion, passTicket, baseRequest, message);
|
||||
return WebWeixinApiUtil.sendMsg(httpClient, urlVersion, passTicket, baseRequest, message);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1266,7 +1259,7 @@ public class Wechat {
|
||||
|
||||
// 上传媒体文件
|
||||
String dataTicket = getCookieValue("webwx_data_ticket");
|
||||
JSONObject result = webWeixinApi.uploadMedia(httpClient, urlVersion, passTicket, baseRequest, loginUserName, toUserName, dataTicket, mediaData, mediaName, contentType);
|
||||
JSONObject result = WebWeixinApiUtil.uploadMedia(httpClient, urlVersion, passTicket, baseRequest, loginUserName, toUserName, dataTicket, mediaData, mediaName, contentType);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
@ -1294,7 +1287,7 @@ public class Wechat {
|
||||
message.setMediaId(mediaId);
|
||||
message.setToUserName(toUserName);
|
||||
message.setType(MsgTypeEnum.IMAGE_MSG.getCode());
|
||||
result = webWeixinApi.sendImageMsg(httpClient, urlVersion, passTicket, baseRequest, message);
|
||||
result = WebWeixinApiUtil.sendImageMsg(httpClient, urlVersion, passTicket, baseRequest, message);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.hotlcc.wechat4j.api;
|
||||
package com.hotlcc.wechat4j.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
@ -6,9 +6,6 @@ import com.hotlcc.wechat4j.enums.LoginTipEnum;
|
||||
import com.hotlcc.wechat4j.model.BaseRequest;
|
||||
import com.hotlcc.wechat4j.model.MediaMessage;
|
||||
import com.hotlcc.wechat4j.model.WxMessage;
|
||||
import com.hotlcc.wechat4j.util.PropertiesUtil;
|
||||
import com.hotlcc.wechat4j.util.StringUtil;
|
||||
import com.hotlcc.wechat4j.util.WechatUtil;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.http.*;
|
||||
import org.apache.http.client.HttpClient;
|
||||
@ -40,8 +37,11 @@ import java.util.regex.Pattern;
|
||||
* @author Allen
|
||||
*/
|
||||
@SuppressWarnings({"Duplicates", "unused"})
|
||||
public class WebWeixinApi {
|
||||
private static Logger logger = LoggerFactory.getLogger(WebWeixinApi.class);
|
||||
public final class WebWeixinApiUtil {
|
||||
private WebWeixinApiUtil() {
|
||||
}
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(WebWeixinApiUtil.class);
|
||||
|
||||
/**
|
||||
* 预编译正则匹配
|
||||
@ -63,7 +63,7 @@ public class WebWeixinApi {
|
||||
* @param httpClient http客户端
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject getWxUuid(HttpClient httpClient) {
|
||||
public static JSONObject getWxUuid(HttpClient httpClient) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.uuid_url"))
|
||||
.add("appid", PropertiesUtil.getProperty("webwx.appid"))
|
||||
@ -120,8 +120,8 @@ public class WebWeixinApi {
|
||||
* @param uuid uuid
|
||||
* @return 二维码图片字节数据
|
||||
*/
|
||||
public byte[] getQR(HttpClient httpClient,
|
||||
String uuid) {
|
||||
public static byte[] getQR(HttpClient httpClient,
|
||||
String uuid) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.qrcode_url"))
|
||||
.add("uuid", uuid)
|
||||
@ -157,9 +157,9 @@ public class WebWeixinApi {
|
||||
* @param uuid uuid
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject getRedirectUri(HttpClient httpClient,
|
||||
LoginTipEnum tip,
|
||||
String uuid) {
|
||||
public static JSONObject getRedirectUri(HttpClient httpClient,
|
||||
LoginTipEnum tip,
|
||||
String uuid) {
|
||||
try {
|
||||
long millis = System.currentTimeMillis();
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.redirect_uri"))
|
||||
@ -229,8 +229,8 @@ public class WebWeixinApi {
|
||||
* @param redirectUri 调整uri
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject getLoginCode(HttpClient httpClient,
|
||||
String redirectUri) {
|
||||
public static JSONObject getLoginCode(HttpClient httpClient,
|
||||
String redirectUri) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.newlogin_url"))
|
||||
.add("redirectUri", redirectUri)
|
||||
@ -262,9 +262,9 @@ public class WebWeixinApi {
|
||||
* @param urlVersion url版本号
|
||||
* @param baseRequest BaseRequest
|
||||
*/
|
||||
public void logout(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
BaseRequest baseRequest) {
|
||||
public static void logout(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
BaseRequest baseRequest) {
|
||||
try {
|
||||
List<NameValuePair> pairList = new ArrayList<>();
|
||||
pairList.add(new BasicNameValuePair("sid", baseRequest.getSid()));
|
||||
@ -299,9 +299,9 @@ public class WebWeixinApi {
|
||||
* @param wxuin uin
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject pushLogin(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String wxuin) {
|
||||
public static JSONObject pushLogin(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String wxuin) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.pushlogin_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
@ -336,10 +336,10 @@ public class WebWeixinApi {
|
||||
* @param baseRequest BaseRequest
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject webWeixinInit(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest) {
|
||||
public static JSONObject webWeixinInit(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.webwxinit_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
@ -381,11 +381,11 @@ public class WebWeixinApi {
|
||||
* @param loginUserName 当前登录账号用户名
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject statusNotify(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
String loginUserName) {
|
||||
public static JSONObject statusNotify(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
String loginUserName) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.statusnotify_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
@ -429,10 +429,10 @@ public class WebWeixinApi {
|
||||
* @param syncKeyList SyncKeyList
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject syncCheck(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
BaseRequest baseRequest,
|
||||
JSONArray syncKeyList) {
|
||||
public static JSONObject syncCheck(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
BaseRequest baseRequest,
|
||||
JSONArray syncKeyList) {
|
||||
try {
|
||||
long millis = System.currentTimeMillis();
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.synccheck_url"))
|
||||
@ -488,10 +488,10 @@ public class WebWeixinApi {
|
||||
* @param skey skey
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject getContact(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
String skey) {
|
||||
public static JSONObject getContact(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
String skey) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.getcontact_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
@ -528,11 +528,11 @@ public class WebWeixinApi {
|
||||
* @param batchContactList 联系人列表
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject batchGetContact(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
JSONArray batchContactList) {
|
||||
public static JSONObject batchGetContact(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
JSONArray batchContactList) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.batchgetcontact_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
@ -576,11 +576,11 @@ public class WebWeixinApi {
|
||||
* @param syncKey syncKey
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject webWxSync(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
JSONObject syncKey) {
|
||||
public static JSONObject webWxSync(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
JSONObject syncKey) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.webwxsync_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
@ -624,11 +624,11 @@ public class WebWeixinApi {
|
||||
* @param message 消息
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendMsg(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
WxMessage message) {
|
||||
public static JSONObject sendMsg(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
WxMessage message) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.webwxsendmsg_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
@ -676,16 +676,16 @@ public class WebWeixinApi {
|
||||
* @param contentType 媒体文件类型
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject uploadMedia(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
String fromUserName,
|
||||
String toUserName,
|
||||
String dataTicket,
|
||||
byte[] mediaData,
|
||||
String mediaName,
|
||||
ContentType contentType) {
|
||||
public static JSONObject uploadMedia(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
String fromUserName,
|
||||
String toUserName,
|
||||
String dataTicket,
|
||||
byte[] mediaData,
|
||||
String mediaName,
|
||||
ContentType contentType) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.uploadmedia_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
@ -778,11 +778,11 @@ public class WebWeixinApi {
|
||||
* @param message 消息
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendImageMsg(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
MediaMessage message) {
|
||||
public static JSONObject sendImageMsg(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String passticket,
|
||||
BaseRequest baseRequest,
|
||||
MediaMessage message) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.webwxsendmsgimg_url"))
|
||||
.add("urlVersion", urlVersion)
|
@ -1,6 +1,5 @@
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hotlcc.wechat4j.Wechat;
|
||||
import com.hotlcc.wechat4j.api.WebWeixinApi;
|
||||
import com.hotlcc.wechat4j.handler.ReceivedMsgHandler;
|
||||
import com.hotlcc.wechat4j.model.ReceivedMsg;
|
||||
import com.hotlcc.wechat4j.model.UserInfo;
|
||||
@ -16,8 +15,6 @@ public class TestClass {
|
||||
@Before
|
||||
public void initAndLogin() {
|
||||
wechat = new Wechat();
|
||||
WebWeixinApi api = new WebWeixinApi();
|
||||
wechat.setWebWeixinApi(api);
|
||||
wechat.addReceivedMsgHandler(new ReceivedMsgHandler() {
|
||||
@Override
|
||||
public void handleAllType(Wechat wechat, ReceivedMsg msg) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user