Browse Source

:new: #2306【微信小程序】增加小程序Short Link生成的接口

linlinjava 3 years ago
parent
commit
ea31b7bfbe

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

@@ -457,6 +457,13 @@ public interface WxMaService extends WxService {
   WxMaLinkService getLinkService();
 
   /**
+   * 获取小程序 Short Link服务接口
+   *
+   * @return
+   */
+  WxMaShortLinkService getShortLinkService();
+
+  /**
    * 获取电子发票报销方服务接口
    *
    * @return

+ 14 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaShortLinkService.java

@@ -0,0 +1,14 @@
+package cn.binarywang.wx.miniapp.api;
+
+
+import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
+import me.chanjar.weixin.common.error.WxErrorException;
+
+/**
+ * 获取小程序 Short Link接口
+ * 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/short-link/shortlink.generate.html
+ */
+public interface WxMaShortLinkService {
+
+  String generate(GenerateShortLinkRequest request) throws WxErrorException;
+}

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

@@ -74,6 +74,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
   private final WxMaShopAfterSaleService shopAfterSaleService = new WxMaShopAfterSaleServiceImpl(this);
   private final WxMaShopDeliveryService shopDeliveryService = new WxMaShopDeliveryServiceImpl(this);
   private final WxMaLinkService linkService = new WxMaLinkServiceImpl(this);
+  private final WxMaShortLinkService shortlinkService = new WxMaShortLinkServiceImpl(this);
   private final WxMaReimburseInvoiceService reimburseInvoiceService = new WxMaReimburseInvoiceServiceImpl(this);
   private Map<String, WxMaConfig> configMap;
   private int retrySleepMillis = 1000;
@@ -570,6 +571,11 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
   }
 
   @Override
+  public WxMaShortLinkService getShortLinkService() {
+    return this.shortlinkService;
+  }
+
+  @Override
   public WxMaReimburseInvoiceService getReimburseInvoiceService() {
     return this.reimburseInvoiceService;
   }

+ 34 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaShortLinkServiceImpl.java

@@ -0,0 +1,34 @@
+package cn.binarywang.wx.miniapp.api.impl;
+
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.api.WxMaShortLinkService;
+import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
+import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
+import com.google.gson.JsonObject;
+import lombok.AllArgsConstructor;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.common.util.json.GsonParser;
+
+import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Link.GENERATE_URLLINK_URL;
+import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.ShortLink.GENERATE_SHORT_LINK_URL;
+
+/**
+ * 获取小程序 Short Link接口
+ * 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/short-link/shortlink.generate.html
+ */
+@AllArgsConstructor
+public class WxMaShortLinkServiceImpl implements WxMaShortLinkService {
+
+  private final WxMaService wxMaService;
+
+  @Override
+  public String generate(GenerateShortLinkRequest request) throws WxErrorException {
+    String result = this.wxMaService.post(GENERATE_SHORT_LINK_URL,request);
+    String linkField = "link";
+    JsonObject jsonObject = GsonParser.parse(result);
+    if(jsonObject.has(linkField)){
+      return jsonObject.get(linkField).toString();
+    }
+    throw new WxErrorException("无link");
+  }
+  }

+ 45 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/shortlink/GenerateShortLinkRequest.java

@@ -0,0 +1,45 @@
+package cn.binarywang.wx.miniapp.bean.shortlink;
+
+import com.google.gson.annotations.SerializedName;
+import lombok.Builder;
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * <pre>
+ * 获取小程序 Short Link参数对象
+ * </pre>
+ */
+@Data
+@Builder
+public class GenerateShortLinkRequest implements Serializable {
+  private static final long serialVersionUID = -7517804620683442832L;
+
+  /**
+   * 通过 Short Link 进入的小程序页面路径,必须是已经发布的小程序存在的页面,可携带 query,最大1024个字符
+   * <pre>
+   * 是否必填: 是
+   * </pre>
+   */
+  @SerializedName("page_url")
+  private String pageUrl;
+
+  /**
+   * 页面标题,不能包含违法信息,超过20字符会用... 截断代替
+   * <pre>
+   * 是否必填: 是
+   * </pre>
+   */
+  @SerializedName("page_title")
+  private String pageTitle;
+
+  /**
+   * 生成的 Short Link 类型,短期有效:false,永久有效:true
+   * <pre>
+   * 是否必填: 否
+   * </pre>
+   */
+  @SerializedName("is_permanent")
+  private Boolean isPermanent;
+}

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

@@ -226,6 +226,10 @@ public class WxMaApiUrlConstants {
     String GENERATE_URLLINK_URL = "https://api.weixin.qq.com/wxa/generate_urllink";
   }
 
+  public interface ShortLink {
+    String GENERATE_SHORT_LINK_URL = "https://api.weixin.qq.com/wxa/genwxashortlink";
+  }
+
   public interface SecCheck {
     String IMG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/img_sec_check";
     String MSG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/msg_sec_check";

+ 24 - 0
weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaShortLinkServiceImplTest.java

@@ -0,0 +1,24 @@
+package cn.binarywang.wx.miniapp.api.impl;
+
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
+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;
+
+@Test
+@Guice(modules = ApiTestModule.class)
+public class WxMaShortLinkServiceImplTest {
+  @Inject
+  private WxMaService wxService;
+
+  @Test
+  public void testGenerate() throws WxErrorException {
+    final String generate = this.wxService.getShortLinkService().generate(GenerateShortLinkRequest.builder().
+                    pageUrl("pages/productView/editPhone/editPhone?id=31832").pageTitle("productView").isPermanent(false).build());
+    System.out.println("generate:");
+    System.out.println(generate);
+  }
+}