mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-09-24 21:13:59 +08:00
polish code
This commit is contained in:
@@ -6,7 +6,7 @@ import chanjarster.weixin.bean.WxXmlMessage;
|
||||
|
||||
|
||||
/**
|
||||
* 微信消息拦截器,可以用来做一些验证
|
||||
* 微信消息拦截器,可以用来做验证
|
||||
* @author qianjia
|
||||
*
|
||||
*/
|
||||
|
@@ -75,5 +75,5 @@ public interface WxService {
|
||||
*/
|
||||
public WxMenu getMenu() throws WxErrorException;
|
||||
|
||||
public void setWxConfigProvider(WxConfigStorage wxConfigProvider);
|
||||
public void setWxConfigStorage(WxConfigStorage wxConfigProvider);
|
||||
}
|
||||
|
@@ -35,11 +35,11 @@ public class WxServiceImpl implements WxService {
|
||||
|
||||
protected static final CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
|
||||
protected WxConfigStorage wxConfigProvider;
|
||||
protected WxConfigStorage wxConfigStorage;
|
||||
|
||||
public boolean checkSignature(String timestamp, String nonce, String signature) {
|
||||
try {
|
||||
String token = wxConfigProvider.getToken();
|
||||
String token = wxConfigStorage.getToken();
|
||||
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
|
||||
String[] arr = new String[] { token, timestamp, nonce };
|
||||
Arrays.sort(arr);
|
||||
@@ -70,8 +70,8 @@ public class WxServiceImpl implements WxService {
|
||||
if (!GLOBAL_ACCESS_TOKEN_REFRESH_FLAG.getAndSet(true)) {
|
||||
try {
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
|
||||
+ "&appid=" + wxConfigProvider.getAppId()
|
||||
+ "&secret=" + wxConfigProvider.getSecret()
|
||||
+ "&appid=" + wxConfigStorage.getAppId()
|
||||
+ "&secret=" + wxConfigStorage.getSecret()
|
||||
;
|
||||
try {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
@@ -82,7 +82,7 @@ public class WxServiceImpl implements WxService {
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||
wxConfigProvider.updateAccessToken(accessToken.getAccess_token(), accessToken.getExpires_in());
|
||||
wxConfigStorage.updateAccessToken(accessToken.getAccess_token(), accessToken.getExpires_in());
|
||||
} catch (ClientProtocolException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
@@ -147,10 +147,10 @@ public class WxServiceImpl implements WxService {
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
protected String execute(String method, String uri, String data) throws WxErrorException {
|
||||
if (StringUtils.isBlank(wxConfigProvider.getAccessToken())) {
|
||||
if (StringUtils.isBlank(wxConfigStorage.getAccessToken())) {
|
||||
refreshAccessToken();
|
||||
}
|
||||
String accessToken = wxConfigProvider.getAccessToken();
|
||||
String accessToken = wxConfigStorage.getAccessToken();
|
||||
|
||||
String uriWithAccessToken = uri;
|
||||
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
|
||||
@@ -196,8 +196,8 @@ public class WxServiceImpl implements WxService {
|
||||
}
|
||||
}
|
||||
|
||||
public void setWxConfigProvider(WxConfigStorage wxConfigProvider) {
|
||||
this.wxConfigProvider = wxConfigProvider;
|
||||
public void setWxConfigStorage(WxConfigStorage wxConfigProvider) {
|
||||
this.wxConfigStorage = wxConfigProvider;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -2,7 +2,6 @@ package chanjarster.weixin.bean;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.management.RuntimeErrorException;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
|
@@ -28,16 +28,16 @@ public class WxServiceTest {
|
||||
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
|
||||
WxXmlConfigStorage config1 = XmlTransformer.fromXml(WxXmlConfigStorage.class, is1);
|
||||
this.wxService = new WxServiceImpl();
|
||||
this.wxService.setWxConfigProvider(config1);
|
||||
this.wxService.setWxConfigStorage(config1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefreshAccessToken() throws WxErrorException {
|
||||
WxConfigStorage configProvider = wxService.wxConfigProvider;
|
||||
String before = configProvider.getAccessToken();
|
||||
WxConfigStorage configStorage = wxService.wxConfigStorage;
|
||||
String before = configStorage.getAccessToken();
|
||||
wxService.refreshAccessToken();
|
||||
|
||||
String after = configProvider.getAccessToken();
|
||||
String after = configStorage.getAccessToken();
|
||||
|
||||
Assert.assertNotEquals(before, after);
|
||||
Assert.assertTrue(StringUtils.isNotBlank(after));
|
||||
@@ -45,7 +45,7 @@ public class WxServiceTest {
|
||||
|
||||
@Test(dependsOnMethods = "testRefreshAccessToken")
|
||||
public void sendCustomMessage() throws WxErrorException {
|
||||
WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigProvider;
|
||||
WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
WxCustomMessage message = new WxCustomMessage();
|
||||
message.setMsgtype(WxConsts.MSG_TEXT);
|
||||
message.setTouser(configProvider.getOpenId());
|
||||
|
@@ -4,7 +4,6 @@ import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.api.WxConsts;
|
||||
import chanjarster.weixin.bean.WxCustomMessage;
|
||||
import chanjarster.weixin.bean.WxCustomMessage.WxArticle;
|
||||
|
||||
@Test
|
||||
|
@@ -3,8 +3,6 @@ package chanjarster.weixin.bean;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.bean.WxError;
|
||||
|
||||
@Test
|
||||
public class WxErrorTest {
|
||||
|
||||
|
@@ -4,7 +4,6 @@ import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.api.WxConsts;
|
||||
import chanjarster.weixin.bean.WxXmlMessage;
|
||||
|
||||
@Test
|
||||
public class WxXmlMessageTest {
|
||||
|
Reference in New Issue
Block a user