1
0
mirror of synced 2025-12-22 08:59:23 +08:00

🎨 优化部分代码,重构OAuth2网页授权、网页登录等相关接口,方便接入open模块

This commit is contained in:
Binary Wang
2020-10-11 00:10:08 +08:00
parent 6cc5ebdcce
commit c7b1abc796
26 changed files with 352 additions and 367 deletions

View File

@@ -10,7 +10,8 @@ import me.chanjar.weixin.mp.bean.card.*;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import static org.testng.AssertJUnit.*;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertTrue;
/**
* 测试代码仅供参考,未做严格测试,因原接口作者并未提供单元测试代码
@@ -234,7 +235,7 @@ public class WxMpCardServiceImplTest {
String openId = "ou7Gr5sJZgFGgj38sRCNQg5pc3Fc";
String cardId = "pu7Gr5secJXPkxBeuYUhmp8TYsuY";
WxUserCardListResult result = this.wxService.getCardService().getUserCardList(openId, cardId);
assertTrue(result.isSuccess());
assertNotNull(result);
System.out.println(result);
}
}

View File

@@ -0,0 +1,59 @@
package me.chanjar.weixin.mp.api.impl;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.test.ApiTestModule;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import javax.inject.Inject;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 测试类.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-08-09
*/
@Test
@Guice(modules = ApiTestModule.class)
public class WxMpOAuth2ServiceImplTest {
@Inject
private WxMpService mpService;
@Test
public void testBuildAuthorizationUrl() {
final String url = this.mpService.getOAuth2Service().buildAuthorizationUrl("http://www.baidu.com", "test", "GOD");
assertThat(url).isEqualTo("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
this.mpService.getWxMpConfigStorage().getAppId() +
"&redirect_uri=http%3A%2F%2Fwww.baidu.com&response_type=code&scope=test&state=GOD&connect_redirect=1#wechat_redirect");
}
@Test
public void testGetAccessToken() throws WxErrorException {
final WxOAuth2AccessToken accessToken = this.mpService.getOAuth2Service().getAccessToken("11");
assertThat(accessToken).isNotNull();
}
@Test
public void testRefreshAccessToken() throws WxErrorException {
final WxOAuth2AccessToken accessToken = this.mpService.getOAuth2Service().refreshAccessToken("11");
assertThat(accessToken).isNotNull();
}
@Test
public void testGetUserInfo() throws WxErrorException {
final WxOAuth2AccessToken accessToken = this.mpService.getOAuth2Service().getAccessToken("11");
final WxOAuth2UserInfo userInfo = this.mpService.getOAuth2Service().getUserInfo(accessToken, null);
assertThat(userInfo).isNotNull();
}
@Test
public void testValidateAccessToken() {
final boolean result = this.mpService.getOAuth2Service().validateAccessToken(new WxOAuth2AccessToken());
assertThat(result).isTrue();
}
}

View File

@@ -1,47 +0,0 @@
package me.chanjar.weixin.mp.api.impl;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.test.ApiTestModule;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import javax.inject.Inject;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 测试类.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-08-09
*/
@Test
@Guice(modules = ApiTestModule.class)
public class WxOAuth2ServiceImplTest {
@Inject
private WxMpService mpService;
@Test
public void testBuildAuthorizationUrl() {
final String url = this.mpService.getOAuth2Service().buildAuthorizationUrl("http://www.baidu.com", "test", "GOD");
assertThat(url).isEqualTo("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
this.mpService.getWxMpConfigStorage().getAppId() +
"&redirect_uri=http%3A%2F%2Fwww.baidu.com&response_type=code&scope=test&state=GOD&connect_redirect=1#wechat_redirect");
}
@Test
public void testGetAccessToken() {
}
@Test
public void testRefreshAccessToken() {
}
@Test
public void testGetUserInfo() {
}
@Test
public void testValidateAccessToken() {
}
}

View File

@@ -1,8 +1,9 @@
package me.chanjar.weixin.mp.demo;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import javax.servlet.http.HttpServlet;
@@ -31,17 +32,17 @@ public class WxMpOAuth2Servlet extends HttpServlet {
response.getWriter().println("<h1>code</h1>");
response.getWriter().println(code);
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = this.wxMpService.getOAuth2Service().getAccessToken(code);
WxOAuth2AccessToken oAuth2AccessToken = this.wxMpService.getOAuth2Service().getAccessToken(code);
response.getWriter().println("<h1>access token</h1>");
response.getWriter().println(wxMpOAuth2AccessToken.toString());
response.getWriter().println(oAuth2AccessToken.toString());
WxMpUser wxMpUser = this.wxMpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
WxOAuth2UserInfo wxMpUser = this.wxMpService.getOAuth2Service().getUserInfo(oAuth2AccessToken, null);
response.getWriter().println("<h1>user info</h1>");
response.getWriter().println(wxMpUser.toString());
wxMpOAuth2AccessToken = this.wxMpService.getOAuth2Service().refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
oAuth2AccessToken = this.wxMpService.getOAuth2Service().refreshAccessToken(oAuth2AccessToken.getRefreshToken());
response.getWriter().println("<h1>after refresh</h1>");
response.getWriter().println(wxMpOAuth2AccessToken.toString());
response.getWriter().println(oAuth2AccessToken.toString());
} catch (WxErrorException e) {
e.printStackTrace();