Skip to content

Commit 3c267bb

Browse files
authored
🆕 #2281【小程序】增加小程序加密网络通道支持
1 parent f7f2121 commit 3c267bb

File tree

8 files changed

+223
-0
lines changed

8 files changed

+223
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import cn.binarywang.wx.miniapp.bean.internet.WxMaInternetResponse;
4+
import me.chanjar.weixin.common.error.WxErrorException;
5+
6+
/**
7+
* <pre>
8+
* 【小程序-服务端-网络】网络相关接口.
9+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
10+
* </pre>
11+
* @author <a href="https://github.com/chutian0124">chutian0124</a>
12+
*/
13+
public interface WxMaInternetService {
14+
/**
15+
*
16+
*
17+
* <pre>
18+
* 获取用户encryptKey。 会获取用户最近3次的key,每个key的存活时间为3600s。
19+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
20+
* 接口地址:POST https://api.weixin.qq.com/wxa/business/getuserencryptkey?access_token=ACCESS_TOKEN&openid=OPENID&signature=SIGNATURE&sig_method=hmac_sha256
21+
* </pre>
22+
*
23+
* @return {@link WxMaInternetResponse}
24+
* @throws WxErrorException
25+
*/
26+
WxMaInternetResponse getUserEncryptKey() throws WxErrorException;
27+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,13 @@ public interface WxMaService extends WxService {
342342
*/
343343
WxMaCloudService getCloudService();
344344

345+
/**
346+
* 获取服务端网络接口服务对象
347+
*
348+
* @return 。internet service
349+
*/
350+
WxMaInternetService getInternetService();
351+
345352
/**
346353
* 获取直播接口服务对象
347354
*

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
4949
private final WxMaSchemeService schemeService = new WxMaSchemeServiceImpl(this);
5050
private final WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this);
5151
private final WxMaCodeService codeService = new WxMaCodeServiceImpl(this);
52+
private final WxMaInternetService internetService = new WxMaInternetServiceImpl(this);
5253
private final WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
5354
private final WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl(this);
5455
private final WxMaShareService shareService = new WxMaShareServiceImpl(this);
@@ -488,6 +489,11 @@ public WxMaCloudService getCloudService() {
488489
return this.cloudService;
489490
}
490491

492+
@Override
493+
public WxMaInternetService getInternetService() {
494+
return this.internetService;
495+
}
496+
491497
@Override
492498
public WxMaLiveService getLiveService() {
493499
return this.liveService;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaInternetService;
4+
import cn.binarywang.wx.miniapp.api.WxMaService;
5+
import cn.binarywang.wx.miniapp.bean.internet.WxMaInternetResponse;
6+
import cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants;
7+
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
8+
import lombok.RequiredArgsConstructor;
9+
import me.chanjar.weixin.common.enums.WxType;
10+
import me.chanjar.weixin.common.error.WxError;
11+
import me.chanjar.weixin.common.error.WxErrorException;
12+
13+
/**
14+
*
15+
* 服务端网络相关接口
16+
*
17+
* @author <a href="https://github.com/chutian0124">chutian0124</a>
18+
* @Date 2021-09-06
19+
*/
20+
@RequiredArgsConstructor
21+
public class WxMaInternetServiceImpl implements WxMaInternetService {
22+
private final WxMaService wxMaService;
23+
24+
@Override
25+
public WxMaInternetResponse getUserEncryptKey() throws WxErrorException {
26+
String responseContent = this.wxMaService.post(WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY, "");
27+
WxMaInternetResponse response = WxMaGsonBuilder.create().fromJson(responseContent, WxMaInternetResponse.class);
28+
if (response.getErrcode() == -1) {
29+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
30+
}
31+
return response;
32+
}
33+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cn.binarywang.wx.miniapp.bean.internet;
2+
3+
import cn.binarywang.wx.miniapp.bean.WxMaBaseResponse;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.Data;
6+
import lombok.EqualsAndHashCode;
7+
8+
import java.io.Serializable;
9+
import java.util.List;
10+
11+
/**
12+
*
13+
*
14+
* <pre>
15+
* 获取用户encryptKey。 用户最近三次的加密key,每个key的存活时间为3600s。
16+
* 【小程序-服务端-网络】网络相关接口.
17+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
18+
* 微信返回报文:
19+
* {
20+
* "errcode":0,
21+
* "errmsg":"ok",
22+
* "key_info_list":
23+
* [
24+
* {
25+
* "encrypt_key":"VI6BpyrK9XH4i4AIGe86tg==",
26+
* "version":10,
27+
* "expire_in":3597,
28+
* "iv":"6003f73ec441c386",
29+
* "create_time":1616572301
30+
* },
31+
* {
32+
* "encrypt_key":"aoUGAHltcliiL9f23oTKHA==",
33+
* "version":9,
34+
* "expire_in":0,
35+
* "iv":"7996656384218dbb",
36+
* "create_time":1616504886
37+
* },
38+
* {
39+
* "encrypt_key":"MlZNQNnRQz3zXHHcr6A3mA==",
40+
* "version":8,
41+
* "expire_in":0,
42+
* "iv":"58a1814f88883024",
43+
* "create_time":1616488061
44+
* }
45+
* ]
46+
* }
47+
* </pre>
48+
*
49+
* @author <a href="https://github.com/chutian0124">chutian0124</a>
50+
*/
51+
@Data
52+
@EqualsAndHashCode(callSuper = false)
53+
public class WxMaInternetResponse extends WxMaBaseResponse implements Serializable {
54+
55+
private static final long serialVersionUID = 6254922047193011785L;
56+
/**
57+
* 用户最近三次的加密key列表
58+
*/
59+
@SerializedName("key_info_list")
60+
List<WxMaInternetUserKeyInfo> keyInfoList;
61+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cn.binarywang.wx.miniapp.bean.internet;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
6+
import java.io.Serializable;
7+
8+
/**
9+
*
10+
*
11+
* <pre>
12+
* 【小程序-服务端-网络】网络相关接口.
13+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
14+
* 微信返回报文:
15+
* {
16+
* "encrypt_key":"VI6BpyrK9XH4i4AIGe86tg==",
17+
* "version":10,
18+
* "expire_in":3597,
19+
* "iv":"6003f73ec441c386",
20+
* "create_time":1616572301
21+
* }
22+
* </pre>
23+
*
24+
* @author <a href="https://github.com/chutian0124">chutian0124</a>
25+
*/
26+
@Data
27+
public class WxMaInternetUserKeyInfo implements Serializable {
28+
private static final long serialVersionUID = 117922490907396705L;
29+
/**
30+
* 加密key
31+
*/
32+
@SerializedName("encrypt_key")
33+
private String encryptKey;
34+
35+
/**
36+
* key的版本号
37+
*/
38+
private Integer version;
39+
40+
/**
41+
* 剩余有效时间
42+
*/
43+
@SerializedName("expire_in")
44+
private Long expireIn;
45+
46+
/**
47+
* 加密iv
48+
*/
49+
private String iv;
50+
51+
/**
52+
* 创建key的时间戳
53+
*/
54+
@SerializedName("create_time")
55+
private Long createTime;
56+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaApiUrlConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,8 @@ public interface Invoice{
388388
*/
389389
String UPDATE_STATUS_BATCH = "https://api.weixin.qq.com/card/invoice/reimburse/updatestatusbatch";
390390
}
391+
392+
public interface Internet{
393+
String GET_USER_ENCRYPT_KEY = "https://api.weixin.qq.com/wxa/business/getuserencryptkey";
394+
}
391395
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import cn.binarywang.wx.miniapp.bean.internet.WxMaInternetResponse;
5+
import cn.binarywang.wx.miniapp.test.ApiTestModule;
6+
import com.google.inject.Inject;
7+
import me.chanjar.weixin.common.error.WxErrorException;
8+
import org.testng.annotations.Guice;
9+
import org.testng.annotations.Test;
10+
11+
/**
12+
*
13+
* 服务端网络相关接口测试
14+
*
15+
* @author <a href="https://github.com/chutian0124">chutian0124</a>
16+
* @date 2021-09-06
17+
*/
18+
@Test
19+
@Guice(modules = ApiTestModule.class)
20+
public class WxMaInternetServiceImplTest {
21+
@Inject
22+
private WxMaService wxService;
23+
24+
@Test
25+
public void testGetUserEncryptKey() throws WxErrorException {
26+
WxMaInternetResponse response = this.wxService.getInternetService().getUserEncryptKey();
27+
System.out.println(response);
28+
}
29+
}

0 commit comments

Comments
 (0)