2
0
mirror of https://gitee.com/hotlcc/wechat4j.git synced 2025-08-18 13:59:33 +08:00

[update] WebWeixinApi改为WebWeixinApiUtil工具类方式

This commit is contained in:
Allen 2019-04-16 16:46:03 +08:00 committed by lichangchun
parent 31f84b62bf
commit 9302b2e68c
4 changed files with 83 additions and 95 deletions

View File

@ -17,10 +17,8 @@
## 简单使用 ## 简单使用
```java ```java
WebWeixinApi api = new WebWeixinApi();
// 实例化微信客户端 // 实例化微信客户端
Wechat wechat = new Wechat(); Wechat wechat = new Wechat();
wechat.setWebWeixinApi(api);
// 自动登录 // 自动登录
wechat.autoLogin(); wechat.autoLogin();
``` ```

View File

@ -2,7 +2,6 @@ package com.hotlcc.wechat4j;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.hotlcc.wechat4j.api.WebWeixinApi;
import com.hotlcc.wechat4j.enums.*; import com.hotlcc.wechat4j.enums.*;
import com.hotlcc.wechat4j.handler.ExitEventHandler; import com.hotlcc.wechat4j.handler.ExitEventHandler;
import com.hotlcc.wechat4j.handler.ReceivedMsgHandler; import com.hotlcc.wechat4j.handler.ReceivedMsgHandler;
@ -39,8 +38,6 @@ import java.util.concurrent.locks.ReentrantLock;
public class Wechat { public class Wechat {
private static Logger logger = LoggerFactory.getLogger(Wechat.class); private static Logger logger = LoggerFactory.getLogger(Wechat.class);
private WebWeixinApi webWeixinApi;
private CookieStore cookieStore; private CookieStore cookieStore;
private HttpClient httpClient; private HttpClient httpClient;
@ -78,10 +75,6 @@ public class Wechat {
this(new BasicCookieStore()); this(new BasicCookieStore());
} }
public void setWebWeixinApi(WebWeixinApi webWeixinApi) {
this.webWeixinApi = webWeixinApi;
}
/** /**
* 获取Cookie * 获取Cookie
* *
@ -216,7 +209,7 @@ public class Wechat {
pw.print("\t第" + i + "次尝试..."); pw.print("\t第" + i + "次尝试...");
pw.flush(); pw.flush();
} }
JSONObject result = webWeixinApi.getWxUuid(httpClient); JSONObject result = WebWeixinApiUtil.getWxUuid(httpClient);
if (result == null) { if (result == null) {
pw.println("\t失败出现异常"); pw.println("\t失败出现异常");
@ -265,7 +258,7 @@ public class Wechat {
pw.flush(); pw.flush();
} }
byte[] data = webWeixinApi.getQR(httpClient, uuid); byte[] data = WebWeixinApiUtil.getQR(httpClient, uuid);
if (data == null || data.length <= 0) { if (data == null || data.length <= 0) {
pw.print("\t失败"); pw.print("\t失败");
@ -302,7 +295,7 @@ public class Wechat {
boolean flag = false; boolean flag = false;
while (true) { while (true) {
JSONObject result = webWeixinApi.getRedirectUri(httpClient, LoginTipEnum.TIP_0, uuid); JSONObject result = WebWeixinApiUtil.getRedirectUri(httpClient, LoginTipEnum.TIP_0, uuid);
if (result == null) { if (result == null) {
pw.println("\t失败出现异常"); pw.println("\t失败出现异常");
pw.flush(); pw.flush();
@ -343,7 +336,7 @@ public class Wechat {
pw.print("获取登录认证码..."); pw.print("获取登录认证码...");
pw.flush(); pw.flush();
JSONObject result = webWeixinApi.getLoginCode(httpClient, redirectUri); JSONObject result = WebWeixinApiUtil.getLoginCode(httpClient, redirectUri);
if (result == null) { if (result == null) {
pw.println("\t失败出现异常"); pw.println("\t失败出现异常");
pw.flush(); pw.flush();
@ -379,7 +372,7 @@ public class Wechat {
pw.print("尝试push方式获取uuid..."); pw.print("尝试push方式获取uuid...");
pw.flush(); pw.flush();
JSONObject result = webWeixinApi.pushLogin(httpClient, urlVersion, wxuin); JSONObject result = WebWeixinApiUtil.pushLogin(httpClient, urlVersion, wxuin);
if (result == null) { if (result == null) {
pw.println("\t失败出现异常"); pw.println("\t失败出现异常");
pw.flush(); pw.flush();
@ -412,7 +405,7 @@ public class Wechat {
* @return * @return
*/ */
private boolean wxInit() { 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) { if (result == null) {
return false; return false;
} }
@ -476,7 +469,7 @@ public class Wechat {
*/ */
private boolean statusNotify(int time) { private boolean statusNotify(int time) {
for (int i = 0; i < time; i++) { 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) { if (result == null) {
continue; continue;
} }
@ -593,7 +586,7 @@ public class Wechat {
isOnlineLock.lock(); isOnlineLock.lock();
if (isOnline) { if (isOnline) {
webWeixinApi.logout(httpClient, urlVersion, new BaseRequest(wxsid, skey, wxuin)); WebWeixinApiUtil.logout(httpClient, urlVersion, new BaseRequest(wxsid, skey, wxuin));
isOnline = false; isOnline = false;
if (clearAllLoginInfo) { if (clearAllLoginInfo) {
@ -663,7 +656,7 @@ public class Wechat {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
try { 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); logger.info("微信同步监听心跳返回数据:{}", result);
if (result == null) { if (result == null) {
throw new RuntimeException("微信API调用异常"); throw new RuntimeException("微信API调用异常");
@ -797,7 +790,7 @@ public class Wechat {
*/ */
private void webWxSync() { private void webWxSync() {
try { 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) { if (result == null) {
logger.error("从服务端同步新数据异常"); logger.error("从服务端同步新数据异常");
return; return;
@ -879,7 +872,7 @@ public class Wechat {
*/ */
public UserInfo getLoginUser(boolean update) { public UserInfo getLoginUser(boolean update) {
if (loginUser == null || 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) { if (result == null) {
return loginUser; return loginUser;
} }
@ -954,7 +947,7 @@ public class Wechat {
*/ */
private JSONObject getSyncKey(boolean update) { private JSONObject getSyncKey(boolean update) {
if (syncKey == null || 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) { if (result == null) {
return syncKey; return syncKey;
} }
@ -1005,7 +998,7 @@ public class Wechat {
*/ */
public List<UserInfo> getContactList(boolean update) { public List<UserInfo> getContactList(boolean update) {
if (contactList == null || update) { if (contactList == null || update) {
JSONObject result = webWeixinApi.getContact(httpClient, urlVersion, passTicket, skey); JSONObject result = WebWeixinApiUtil.getContact(httpClient, urlVersion, passTicket, skey);
if (result == null) { if (result == null) {
return contactList; return contactList;
} }
@ -1164,7 +1157,7 @@ public class Wechat {
} }
message.setType(MsgTypeEnum.TEXT_MSG.getCode()); 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"); 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) { if (result == null) {
return null; return null;
} }
@ -1294,7 +1287,7 @@ public class Wechat {
message.setMediaId(mediaId); message.setMediaId(mediaId);
message.setToUserName(toUserName); message.setToUserName(toUserName);
message.setType(MsgTypeEnum.IMAGE_MSG.getCode()); message.setType(MsgTypeEnum.IMAGE_MSG.getCode());
result = webWeixinApi.sendImageMsg(httpClient, urlVersion, passTicket, baseRequest, message); result = WebWeixinApiUtil.sendImageMsg(httpClient, urlVersion, passTicket, baseRequest, message);
return result; return result;
} }

View File

@ -1,4 +1,4 @@
package com.hotlcc.wechat4j.api; package com.hotlcc.wechat4j.util;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; 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.BaseRequest;
import com.hotlcc.wechat4j.model.MediaMessage; import com.hotlcc.wechat4j.model.MediaMessage;
import com.hotlcc.wechat4j.model.WxMessage; 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.commons.codec.digest.DigestUtils;
import org.apache.http.*; import org.apache.http.*;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
@ -40,8 +37,11 @@ import java.util.regex.Pattern;
* @author Allen * @author Allen
*/ */
@SuppressWarnings({"Duplicates", "unused"}) @SuppressWarnings({"Duplicates", "unused"})
public class WebWeixinApi { public final class WebWeixinApiUtil {
private static Logger logger = LoggerFactory.getLogger(WebWeixinApi.class); private WebWeixinApiUtil() {
}
private static Logger logger = LoggerFactory.getLogger(WebWeixinApiUtil.class);
/** /**
* 预编译正则匹配 * 预编译正则匹配
@ -63,7 +63,7 @@ public class WebWeixinApi {
* @param httpClient http客户端 * @param httpClient http客户端
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject getWxUuid(HttpClient httpClient) { public static JSONObject getWxUuid(HttpClient httpClient) {
try { try {
String url = new ST(PropertiesUtil.getProperty("webwx-url.uuid_url")) String url = new ST(PropertiesUtil.getProperty("webwx-url.uuid_url"))
.add("appid", PropertiesUtil.getProperty("webwx.appid")) .add("appid", PropertiesUtil.getProperty("webwx.appid"))
@ -120,7 +120,7 @@ public class WebWeixinApi {
* @param uuid uuid * @param uuid uuid
* @return 二维码图片字节数据 * @return 二维码图片字节数据
*/ */
public byte[] getQR(HttpClient httpClient, public static byte[] getQR(HttpClient httpClient,
String uuid) { String uuid) {
try { try {
String url = new ST(PropertiesUtil.getProperty("webwx-url.qrcode_url")) String url = new ST(PropertiesUtil.getProperty("webwx-url.qrcode_url"))
@ -157,7 +157,7 @@ public class WebWeixinApi {
* @param uuid uuid * @param uuid uuid
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject getRedirectUri(HttpClient httpClient, public static JSONObject getRedirectUri(HttpClient httpClient,
LoginTipEnum tip, LoginTipEnum tip,
String uuid) { String uuid) {
try { try {
@ -229,7 +229,7 @@ public class WebWeixinApi {
* @param redirectUri 调整uri * @param redirectUri 调整uri
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject getLoginCode(HttpClient httpClient, public static JSONObject getLoginCode(HttpClient httpClient,
String redirectUri) { String redirectUri) {
try { try {
String url = new ST(PropertiesUtil.getProperty("webwx-url.newlogin_url")) String url = new ST(PropertiesUtil.getProperty("webwx-url.newlogin_url"))
@ -262,7 +262,7 @@ public class WebWeixinApi {
* @param urlVersion url版本号 * @param urlVersion url版本号
* @param baseRequest BaseRequest * @param baseRequest BaseRequest
*/ */
public void logout(HttpClient httpClient, public static void logout(HttpClient httpClient,
String urlVersion, String urlVersion,
BaseRequest baseRequest) { BaseRequest baseRequest) {
try { try {
@ -299,7 +299,7 @@ public class WebWeixinApi {
* @param wxuin uin * @param wxuin uin
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject pushLogin(HttpClient httpClient, public static JSONObject pushLogin(HttpClient httpClient,
String urlVersion, String urlVersion,
String wxuin) { String wxuin) {
try { try {
@ -336,7 +336,7 @@ public class WebWeixinApi {
* @param baseRequest BaseRequest * @param baseRequest BaseRequest
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject webWeixinInit(HttpClient httpClient, public static JSONObject webWeixinInit(HttpClient httpClient,
String urlVersion, String urlVersion,
String passticket, String passticket,
BaseRequest baseRequest) { BaseRequest baseRequest) {
@ -381,7 +381,7 @@ public class WebWeixinApi {
* @param loginUserName 当前登录账号用户名 * @param loginUserName 当前登录账号用户名
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject statusNotify(HttpClient httpClient, public static JSONObject statusNotify(HttpClient httpClient,
String urlVersion, String urlVersion,
String passticket, String passticket,
BaseRequest baseRequest, BaseRequest baseRequest,
@ -429,7 +429,7 @@ public class WebWeixinApi {
* @param syncKeyList SyncKeyList * @param syncKeyList SyncKeyList
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject syncCheck(HttpClient httpClient, public static JSONObject syncCheck(HttpClient httpClient,
String urlVersion, String urlVersion,
BaseRequest baseRequest, BaseRequest baseRequest,
JSONArray syncKeyList) { JSONArray syncKeyList) {
@ -488,7 +488,7 @@ public class WebWeixinApi {
* @param skey skey * @param skey skey
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject getContact(HttpClient httpClient, public static JSONObject getContact(HttpClient httpClient,
String urlVersion, String urlVersion,
String passticket, String passticket,
String skey) { String skey) {
@ -528,7 +528,7 @@ public class WebWeixinApi {
* @param batchContactList 联系人列表 * @param batchContactList 联系人列表
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject batchGetContact(HttpClient httpClient, public static JSONObject batchGetContact(HttpClient httpClient,
String urlVersion, String urlVersion,
String passticket, String passticket,
BaseRequest baseRequest, BaseRequest baseRequest,
@ -576,7 +576,7 @@ public class WebWeixinApi {
* @param syncKey syncKey * @param syncKey syncKey
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject webWxSync(HttpClient httpClient, public static JSONObject webWxSync(HttpClient httpClient,
String urlVersion, String urlVersion,
String passticket, String passticket,
BaseRequest baseRequest, BaseRequest baseRequest,
@ -624,7 +624,7 @@ public class WebWeixinApi {
* @param message 消息 * @param message 消息
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject sendMsg(HttpClient httpClient, public static JSONObject sendMsg(HttpClient httpClient,
String urlVersion, String urlVersion,
String passticket, String passticket,
BaseRequest baseRequest, BaseRequest baseRequest,
@ -676,7 +676,7 @@ public class WebWeixinApi {
* @param contentType 媒体文件类型 * @param contentType 媒体文件类型
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject uploadMedia(HttpClient httpClient, public static JSONObject uploadMedia(HttpClient httpClient,
String urlVersion, String urlVersion,
String passticket, String passticket,
BaseRequest baseRequest, BaseRequest baseRequest,
@ -778,7 +778,7 @@ public class WebWeixinApi {
* @param message 消息 * @param message 消息
* @return 返回数据 * @return 返回数据
*/ */
public JSONObject sendImageMsg(HttpClient httpClient, public static JSONObject sendImageMsg(HttpClient httpClient,
String urlVersion, String urlVersion,
String passticket, String passticket,
BaseRequest baseRequest, BaseRequest baseRequest,

View File

@ -1,6 +1,5 @@
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.hotlcc.wechat4j.Wechat; import com.hotlcc.wechat4j.Wechat;
import com.hotlcc.wechat4j.api.WebWeixinApi;
import com.hotlcc.wechat4j.handler.ReceivedMsgHandler; import com.hotlcc.wechat4j.handler.ReceivedMsgHandler;
import com.hotlcc.wechat4j.model.ReceivedMsg; import com.hotlcc.wechat4j.model.ReceivedMsg;
import com.hotlcc.wechat4j.model.UserInfo; import com.hotlcc.wechat4j.model.UserInfo;
@ -16,8 +15,6 @@ public class TestClass {
@Before @Before
public void initAndLogin() { public void initAndLogin() {
wechat = new Wechat(); wechat = new Wechat();
WebWeixinApi api = new WebWeixinApi();
wechat.setWebWeixinApi(api);
wechat.addReceivedMsgHandler(new ReceivedMsgHandler() { wechat.addReceivedMsgHandler(new ReceivedMsgHandler() {
@Override @Override
public void handleAllType(Wechat wechat, ReceivedMsg msg) { public void handleAllType(Wechat wechat, ReceivedMsg msg) {