1
0
mirror of synced 2025-12-17 04:43:15 +08:00

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

This commit is contained in:
linlinjava
2021-09-10 11:38:35 +08:00
committed by GitHub
parent 3c267bbe61
commit ea31b7bfbe
7 changed files with 134 additions and 0 deletions

View File

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

View File

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

View File

@@ -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;
@@ -569,6 +570,11 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
return this.linkService;
}
@Override
public WxMaShortLinkService getShortLinkService() {
return this.shortlinkService;
}
@Override
public WxMaReimburseInvoiceService getReimburseInvoiceService() {
return this.reimburseInvoiceService;

View File

@@ -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");
}
}

View File

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

View File

@@ -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";

View File

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