🆕 #2281【小程序】增加小程序加密网络通道支持

This commit is contained in:
pg
2021-09-07 22:44:03 +08:00
committed by GitHub
parent f7f2121fca
commit 3c267bbe61
8 changed files with 223 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
package cn.binarywang.wx.miniapp.api;
import cn.binarywang.wx.miniapp.bean.internet.WxMaInternetResponse;
import me.chanjar.weixin.common.error.WxErrorException;
/**
* <pre>
* 【小程序-服务端-网络】网络相关接口.
* 文档地址https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
* </pre>
* @author <a href="https://github.com/chutian0124">chutian0124</a>
*/
public interface WxMaInternetService {
/**
*
*
* <pre>
* 获取用户encryptKey。 会获取用户最近3次的key每个key的存活时间为3600s。
* 文档地址https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
* 接口地址POST https://api.weixin.qq.com/wxa/business/getuserencryptkey?access_token=ACCESS_TOKEN&openid=OPENID&signature=SIGNATURE&sig_method=hmac_sha256
* </pre>
*
* @return {@link WxMaInternetResponse}
* @throws WxErrorException
*/
WxMaInternetResponse getUserEncryptKey() throws WxErrorException;
}

View File

@@ -342,6 +342,13 @@ public interface WxMaService extends WxService {
*/
WxMaCloudService getCloudService();
/**
* 获取服务端网络接口服务对象
*
* @return 。internet service
*/
WxMaInternetService getInternetService();
/**
* 获取直播接口服务对象
*

View File

@@ -49,6 +49,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
private final WxMaSchemeService schemeService = new WxMaSchemeServiceImpl(this);
private final WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this);
private final WxMaCodeService codeService = new WxMaCodeServiceImpl(this);
private final WxMaInternetService internetService = new WxMaInternetServiceImpl(this);
private final WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
private final WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl(this);
private final WxMaShareService shareService = new WxMaShareServiceImpl(this);
@@ -488,6 +489,11 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
return this.cloudService;
}
@Override
public WxMaInternetService getInternetService() {
return this.internetService;
}
@Override
public WxMaLiveService getLiveService() {
return this.liveService;

View File

@@ -0,0 +1,33 @@
package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaInternetService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.internet.WxMaInternetResponse;
import cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
/**
*
* 服务端网络相关接口
*
* @author <a href="https://github.com/chutian0124">chutian0124</a>
* @Date 2021-09-06
*/
@RequiredArgsConstructor
public class WxMaInternetServiceImpl implements WxMaInternetService {
private final WxMaService wxMaService;
@Override
public WxMaInternetResponse getUserEncryptKey() throws WxErrorException {
String responseContent = this.wxMaService.post(WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY, "");
WxMaInternetResponse response = WxMaGsonBuilder.create().fromJson(responseContent, WxMaInternetResponse.class);
if (response.getErrcode() == -1) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
}
return response;
}
}