Skip to content

Commit 1dc58f0

Browse files
authored
🎨 #2506 【小程序】补充完善获取用户encryptKey接口的参数
1 parent 0601e8f commit 1dc58f0

File tree

4 files changed

+90
-10
lines changed

4 files changed

+90
-10
lines changed

Diff for: weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaInternetService.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,36 @@
88
* 【小程序-服务端-网络】网络相关接口.
99
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
1010
* </pre>
11+
*
1112
* @author <a href="https://github.com/chutian0124">chutian0124</a>
1213
*/
1314
public interface WxMaInternetService {
1415
/**
16+
* <pre>
17+
* 获取用户encryptKey。 会获取用户最近3次的key,每个key的存活时间为3600s。
18+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
19+
* 接口地址:POST https://api.weixin.qq.com/wxa/business/getuserencryptkey?access_token=ACCESS_TOKEN&openid=OPENID&signature=SIGNATURE&sig_method=hmac_sha256
20+
* @param openid 用户的openid
21+
* @param signature 用sessionkey对空字符串签名得到的结果
22+
* @param sigMethod 签名方法,只支持 hmac_sha256
23+
* </pre>
1524
*
16-
*
25+
* @return {@link WxMaInternetResponse}
26+
* @throws WxErrorException
27+
*/
28+
WxMaInternetResponse getUserEncryptKey(String openid, String signature, String sigMethod) throws WxErrorException;
29+
30+
/**
1731
* <pre>
1832
* 获取用户encryptKey。 会获取用户最近3次的key,每个key的存活时间为3600s。
1933
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
2034
* 接口地址:POST https://api.weixin.qq.com/wxa/business/getuserencryptkey?access_token=ACCESS_TOKEN&openid=OPENID&signature=SIGNATURE&sig_method=hmac_sha256
35+
* @param openid 用户的openid
36+
* @param sessionKey 用户的sessionKey
2137
* </pre>
2238
*
2339
* @return {@link WxMaInternetResponse}
2440
* @throws WxErrorException
2541
*/
26-
WxMaInternetResponse getUserEncryptKey() throws WxErrorException;
42+
WxMaInternetResponse getUserEncryptKey(String openid, String sessionKey) throws WxErrorException;
2743
}

Diff for: weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImpl.java

+36-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
import me.chanjar.weixin.common.enums.WxType;
1010
import me.chanjar.weixin.common.error.WxError;
1111
import me.chanjar.weixin.common.error.WxErrorException;
12+
import org.jetbrains.annotations.NotNull;
13+
14+
import javax.crypto.Mac;
15+
import javax.crypto.spec.SecretKeySpec;
1216

1317
/**
14-
*
1518
* 服务端网络相关接口
1619
*
1720
* @author <a href="https://github.com/chutian0124">chutian0124</a>
@@ -21,9 +24,39 @@
2124
public class WxMaInternetServiceImpl implements WxMaInternetService {
2225
private final WxMaService wxMaService;
2326

27+
private String sha256(String data, String sessionKey) throws Exception {
28+
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
29+
SecretKeySpec secret_key = new SecretKeySpec(sessionKey.getBytes("UTF-8"), "HmacSHA256");
30+
sha256_HMAC.init(secret_key);
31+
byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
32+
StringBuilder sb = new StringBuilder();
33+
for (byte item : array) {
34+
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
35+
}
36+
return sb.toString().toUpperCase();
37+
}
38+
2439
@Override
25-
public WxMaInternetResponse getUserEncryptKey() throws WxErrorException {
26-
String responseContent = this.wxMaService.post(WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY, "");
40+
public WxMaInternetResponse getUserEncryptKey(String openid, String signature, String sigMethod) throws WxErrorException {
41+
String url = WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY + "?openid=" + openid + "&signature=" + signature + "&sig_method=" + sigMethod;
42+
return getWxMaInternetResponse(url);
43+
}
44+
45+
@Override
46+
public WxMaInternetResponse getUserEncryptKey(String openid, String sessionKey) throws WxErrorException {
47+
String signature = null;
48+
try {
49+
signature = sha256("", sessionKey);
50+
} catch (Exception e) {
51+
throw new WxErrorException("签名错误");
52+
}
53+
String url = WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY + "?sig_method=hmac_sha256&openid=" + openid + "&signature=" + signature;
54+
return getWxMaInternetResponse(url);
55+
}
56+
57+
@NotNull
58+
private WxMaInternetResponse getWxMaInternetResponse(String url) throws WxErrorException {
59+
String responseContent = this.wxMaService.post(url, "");
2760
WxMaInternetResponse response = WxMaGsonBuilder.create().fromJson(responseContent, WxMaInternetResponse.class);
2861
if (response.getErrcode() == -1) {
2962
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));

Diff for: weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImplTest.java

+31-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import cn.binarywang.wx.miniapp.bean.internet.WxMaInternetResponse;
55
import cn.binarywang.wx.miniapp.test.ApiTestModule;
66
import com.google.inject.Inject;
7-
import me.chanjar.weixin.common.error.WxErrorException;
87
import org.testng.annotations.Guice;
98
import org.testng.annotations.Test;
109

10+
import javax.crypto.Mac;
11+
import javax.crypto.spec.SecretKeySpec;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
1115
/**
12-
*
1316
* 服务端网络相关接口测试
1417
*
1518
* @author <a href="https://github.com/chutian0124">chutian0124</a>
@@ -21,9 +24,32 @@ public class WxMaInternetServiceImplTest {
2124
@Inject
2225
private WxMaService wxService;
2326

27+
private static String HMACSHA256(String data, String key) throws Exception {
28+
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
29+
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
30+
sha256_HMAC.init(secret_key);
31+
byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
32+
StringBuilder sb = new StringBuilder();
33+
for (byte item : array) {
34+
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
35+
}
36+
return sb.toString().toUpperCase();
37+
}
38+
39+
@Test
40+
public void testGetUserEncryptKey() throws Exception {
41+
String openid = "ogu-84hVFTbTt-myGisQESoDJ6BM";
42+
String signature = HMACSHA256("", "9ny8n3t0KULoi0deF7T9pw==");
43+
String sigMethod = "hmac_sha256";
44+
WxMaInternetResponse response = this.wxService.getInternetService().getUserEncryptKey(openid, signature, sigMethod);
45+
assertThat(response).isNotNull();
46+
}
47+
2448
@Test
25-
public void testGetUserEncryptKey() throws WxErrorException {
26-
WxMaInternetResponse response = this.wxService.getInternetService().getUserEncryptKey();
27-
System.out.println(response);
49+
public void testGetUserEncryptKey2() throws Exception {
50+
String openid = "ogu-84hVFTbTt-myGisQESoDJ6BM";
51+
String sessionKey = "9ny8n3t0KULoi0deF7T9pw==";
52+
WxMaInternetResponse response = this.wxService.getInternetService().getUserEncryptKey(openid, sessionKey);
53+
assertThat(response).isNotNull();
2854
}
2955
}

Diff for: weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaUserServiceImplTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ public void testSetUserStorage() throws WxErrorException {
7676
public void testGetNewPhoneNoInfo() throws Exception{
7777
assertNotNull(wxService.getUserService().getNewPhoneNoInfo("test"));
7878
}
79+
80+
@Test
81+
public void testGetAccessToken() throws Exception{
82+
assertNotNull(wxService.getAccessToken(true));
83+
}
7984
}

0 commit comments

Comments
 (0)