Skip to content

Commit

Permalink
封装SessionResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
dbstarll committed May 15, 2023
1 parent 0abbaa7 commit 0055b17
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ public String getAccessToken() {
return accessToken;
}

/**
* 设置获取到的凭证.
*
* @param accessToken 获取到的凭证
*/
public void setAccessToken(final String accessToken) {
void setAccessToken(final String accessToken) {
this.accessToken = accessToken;
}

Expand All @@ -34,12 +29,7 @@ public int getExpiresIn() {
return expiresIn;
}

/**
* 设置凭证有效时间,单位:秒.
*
* @param expiresIn 凭证有效时间
*/
public void setExpiresIn(final int expiresIn) {
void setExpiresIn(final int expiresIn) {
this.expiresIn = expiresIn;
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/io/github/dbstarll/weixin/sdk/SessionResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.github.dbstarll.weixin.sdk;

import java.io.Serializable;
import java.util.StringJoiner;

public final class SessionResponse implements Serializable {
private String sessionKey;
private String unionid;
private String openid;

/**
* 获得会话密钥.
*
* @return 会话密钥
*/
public String getSessionKey() {
return sessionKey;
}

void setSessionKey(final String sessionKey) {
this.sessionKey = sessionKey;
}

/**
* 获得用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回.
*
* @return 用户在开放平台的唯一标识符
*/
public String getUnionid() {
return unionid;
}

void setUnionid(final String unionid) {
this.unionid = unionid;
}

/**
* 获得用户唯一标识.
*
* @return 用户唯一标识
*/
public String getOpenid() {
return openid;
}

void setOpenid(final String openid) {
this.openid = openid;
}

@Override
public String toString() {
return new StringJoiner(", ", SessionResponse.class.getSimpleName() + "[", "]")
.add("openid='" + getOpenid() + "'")
.add("sessionKey='" + getSessionKey() + "'")
.add("unionid='" + getUnionid() + "'")
.toString();
}
}
6 changes: 3 additions & 3 deletions src/main/java/io/github/dbstarll/weixin/sdk/WeChatApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ protected <T> T postProcessing(final ClassicHttpRequest request, final T execute
*
* @param appId 小程序 appId
* @param code 登录时获取的 code
* @return 登录凭证
* @return SessionResponse
* @throws IOException in case of a problem or the connection was aborted
* @throws ApiException in case of an api error
*/
public ObjectNode session(final String appId, final String code) throws IOException, ApiException {
public SessionResponse session(final String appId, final String code) throws IOException, ApiException {
return execute(auth(post("/sns/jscode2session")
.addParameter("grant_type", "authorization_code")
.addParameter("js_code", notBlank(code, "code not set")), appId), ObjectNode.class);
.addParameter("js_code", notBlank(code, "code not set")), appId), SessionResponse.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class WeChatResponseException extends ApiProtocolException {
* @param errMsg 错误信息
*/
public WeChatResponseException(final int errCode, final String errMsg) {
super(null);
super(String.format("%d: %s\n", errCode, errMsg), null);
this.statusCode = errCode;
this.reasonPhrase = StringUtils.substringBefore(errMsg, RID_SPLIT_TOKEN);
this.rid = StringUtils.substringAfter(errMsg, RID_SPLIT_TOKEN);
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/io/github/dbstarll/weixin/sdk/WeChatApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,14 @@ void accessTokenInvalidAppId() throws Throwable {
assertNotNull(e.getRid());
}, appId -> "secret");
}

@Test
void sessionInvalidCode() throws Throwable {
useApi(api -> {
final WeChatResponseException e = assertThrowsExactly(WeChatResponseException.class, () -> api.session(testAppId, "code"));
assertEquals(40029, e.getStatusCode());
assertEquals("invalid code,", e.getReasonPhrase());
assertNotNull(e.getRid());
}, secretHolder);
}
}

0 comments on commit 0055b17

Please sign in to comment.