فهرست منبع

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

pg 3 سال پیش
والد
کامیت
3c267bbe61

+ 27 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaInternetService.java

@@ -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;
+}

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

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

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

@@ -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);
@@ -489,6 +490,11 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
   }
 
   @Override
+  public WxMaInternetService getInternetService() {
+    return this.internetService;
+  }
+
+  @Override
   public WxMaLiveService getLiveService() {
     return this.liveService;
   }

+ 33 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImpl.java

@@ -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;
+  }
+}

+ 61 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/internet/WxMaInternetResponse.java

@@ -0,0 +1,61 @@
+package cn.binarywang.wx.miniapp.bean.internet;
+
+import cn.binarywang.wx.miniapp.bean.WxMaBaseResponse;
+import com.google.gson.annotations.SerializedName;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ *
+ *
+ * <pre>
+ *  获取用户encryptKey。 用户最近三次的加密key,每个key的存活时间为3600s。
+ * 【小程序-服务端-网络】网络相关接口.
+ *  文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
+ *  微信返回报文:
+ *  {
+ *     "errcode":0,
+ *     "errmsg":"ok",
+ *     "key_info_list":
+ *     [
+ *         {
+ *             "encrypt_key":"VI6BpyrK9XH4i4AIGe86tg==",
+ *             "version":10,
+ *             "expire_in":3597,
+ *             "iv":"6003f73ec441c386",
+ *             "create_time":1616572301
+ *         },
+ *         {
+ *             "encrypt_key":"aoUGAHltcliiL9f23oTKHA==",
+ *             "version":9,
+ *             "expire_in":0,
+ *             "iv":"7996656384218dbb",
+ *             "create_time":1616504886
+ *         },
+ *         {
+ *             "encrypt_key":"MlZNQNnRQz3zXHHcr6A3mA==",
+ *             "version":8,
+ *             "expire_in":0,
+ *             "iv":"58a1814f88883024",
+ *             "create_time":1616488061
+ *         }
+ *     ]
+ * }
+ * </pre>
+ *
+ * @author <a href="https://github.com/chutian0124">chutian0124</a>
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+public class WxMaInternetResponse extends WxMaBaseResponse implements Serializable {
+
+  private static final long serialVersionUID = 6254922047193011785L;
+  /**
+   * 用户最近三次的加密key列表
+   */
+  @SerializedName("key_info_list")
+  List<WxMaInternetUserKeyInfo> keyInfoList;
+}

+ 56 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/internet/WxMaInternetUserKeyInfo.java

@@ -0,0 +1,56 @@
+package cn.binarywang.wx.miniapp.bean.internet;
+
+import com.google.gson.annotations.SerializedName;
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ *
+ *
+ * <pre>
+ * 【小程序-服务端-网络】网络相关接口.
+ *  文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
+ *  微信返回报文:
+ *  {
+ *  "encrypt_key":"VI6BpyrK9XH4i4AIGe86tg==",
+ *  "version":10,
+ *  "expire_in":3597,
+ *  "iv":"6003f73ec441c386",
+ *  "create_time":1616572301
+ *  }
+ * </pre>
+ *
+ * @author <a href="https://github.com/chutian0124">chutian0124</a>
+ */
+@Data
+public class WxMaInternetUserKeyInfo implements Serializable {
+  private static final long serialVersionUID = 117922490907396705L;
+  /**
+   * 加密key
+   */
+  @SerializedName("encrypt_key")
+  private String encryptKey;
+
+  /**
+   * key的版本号
+   */
+  private Integer version;
+
+  /**
+   * 剩余有效时间
+   */
+  @SerializedName("expire_in")
+  private Long expireIn;
+
+  /**
+   * 加密iv
+   */
+  private String iv;
+
+  /**
+   * 创建key的时间戳
+   */
+  @SerializedName("create_time")
+  private Long createTime;
+}

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

@@ -388,4 +388,8 @@ public class WxMaApiUrlConstants {
      */
     String UPDATE_STATUS_BATCH = "https://api.weixin.qq.com/card/invoice/reimburse/updatestatusbatch";
   }
+
+  public interface Internet{
+    String GET_USER_ENCRYPT_KEY = "https://api.weixin.qq.com/wxa/business/getuserencryptkey";
+  }
 }

+ 29 - 0
weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaInternetServiceImplTest.java

@@ -0,0 +1,29 @@
+package cn.binarywang.wx.miniapp.api.impl;
+
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.bean.internet.WxMaInternetResponse;
+import cn.binarywang.wx.miniapp.test.ApiTestModule;
+import com.google.inject.Inject;
+import me.chanjar.weixin.common.error.WxErrorException;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * 服务端网络相关接口测试
+ *
+ * @author <a href="https://github.com/chutian0124">chutian0124</a>
+ * @date 2021-09-06
+ */
+@Test
+@Guice(modules = ApiTestModule.class)
+public class WxMaInternetServiceImplTest {
+  @Inject
+  private WxMaService wxService;
+
+  @Test
+  public void testGetUserEncryptKey() throws WxErrorException {
+    WxMaInternetResponse response = this.wxService.getInternetService().getUserEncryptKey();
+    System.out.println(response);
+  }
+}