Skip to content

Commit ea31b7b

Browse files
authored
🆕 #2306【微信小程序】增加小程序Short Link生成的接口
1 parent 3c267bb commit ea31b7b

File tree

7 files changed

+134
-0
lines changed

7 files changed

+134
-0
lines changed

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
@@ -456,6 +456,13 @@ public interface WxMaService extends WxService {
456456
*/
457457
WxMaLinkService getLinkService();
458458

459+
/**
460+
* 获取小程序 Short Link服务接口
461+
*
462+
* @return
463+
*/
464+
WxMaShortLinkService getShortLinkService();
465+
459466
/**
460467
* 获取电子发票报销方服务接口
461468
*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
4+
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
5+
import me.chanjar.weixin.common.error.WxErrorException;
6+
7+
/**
8+
* 获取小程序 Short Link接口
9+
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/short-link/shortlink.generate.html
10+
*/
11+
public interface WxMaShortLinkService {
12+
13+
String generate(GenerateShortLinkRequest request) throws WxErrorException;
14+
}

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
@@ -74,6 +74,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
7474
private final WxMaShopAfterSaleService shopAfterSaleService = new WxMaShopAfterSaleServiceImpl(this);
7575
private final WxMaShopDeliveryService shopDeliveryService = new WxMaShopDeliveryServiceImpl(this);
7676
private final WxMaLinkService linkService = new WxMaLinkServiceImpl(this);
77+
private final WxMaShortLinkService shortlinkService = new WxMaShortLinkServiceImpl(this);
7778
private final WxMaReimburseInvoiceService reimburseInvoiceService = new WxMaReimburseInvoiceServiceImpl(this);
7879
private Map<String, WxMaConfig> configMap;
7980
private int retrySleepMillis = 1000;
@@ -569,6 +570,11 @@ public WxMaLinkService getLinkService() {
569570
return this.linkService;
570571
}
571572

573+
@Override
574+
public WxMaShortLinkService getShortLinkService() {
575+
return this.shortlinkService;
576+
}
577+
572578
@Override
573579
public WxMaReimburseInvoiceService getReimburseInvoiceService() {
574580
return this.reimburseInvoiceService;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import cn.binarywang.wx.miniapp.api.WxMaShortLinkService;
5+
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
6+
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
7+
import com.google.gson.JsonObject;
8+
import lombok.AllArgsConstructor;
9+
import me.chanjar.weixin.common.error.WxErrorException;
10+
import me.chanjar.weixin.common.util.json.GsonParser;
11+
12+
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Link.GENERATE_URLLINK_URL;
13+
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.ShortLink.GENERATE_SHORT_LINK_URL;
14+
15+
/**
16+
* 获取小程序 Short Link接口
17+
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/short-link/shortlink.generate.html
18+
*/
19+
@AllArgsConstructor
20+
public class WxMaShortLinkServiceImpl implements WxMaShortLinkService {
21+
22+
private final WxMaService wxMaService;
23+
24+
@Override
25+
public String generate(GenerateShortLinkRequest request) throws WxErrorException {
26+
String result = this.wxMaService.post(GENERATE_SHORT_LINK_URL,request);
27+
String linkField = "link";
28+
JsonObject jsonObject = GsonParser.parse(result);
29+
if(jsonObject.has(linkField)){
30+
return jsonObject.get(linkField).toString();
31+
}
32+
throw new WxErrorException("无link");
33+
}
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.binarywang.wx.miniapp.bean.shortlink;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
* <pre>
11+
* 获取小程序 Short Link参数对象
12+
* </pre>
13+
*/
14+
@Data
15+
@Builder
16+
public class GenerateShortLinkRequest implements Serializable {
17+
private static final long serialVersionUID = -7517804620683442832L;
18+
19+
/**
20+
* 通过 Short Link 进入的小程序页面路径,必须是已经发布的小程序存在的页面,可携带 query,最大1024个字符
21+
* <pre>
22+
* 是否必填: 是
23+
* </pre>
24+
*/
25+
@SerializedName("page_url")
26+
private String pageUrl;
27+
28+
/**
29+
* 页面标题,不能包含违法信息,超过20字符会用... 截断代替
30+
* <pre>
31+
* 是否必填: 是
32+
* </pre>
33+
*/
34+
@SerializedName("page_title")
35+
private String pageTitle;
36+
37+
/**
38+
* 生成的 Short Link 类型,短期有效:false,永久有效:true
39+
* <pre>
40+
* 是否必填: 否
41+
* </pre>
42+
*/
43+
@SerializedName("is_permanent")
44+
private Boolean isPermanent;
45+
}

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
@@ -226,6 +226,10 @@ public interface Link {
226226
String GENERATE_URLLINK_URL = "https://api.weixin.qq.com/wxa/generate_urllink";
227227
}
228228

229+
public interface ShortLink {
230+
String GENERATE_SHORT_LINK_URL = "https://api.weixin.qq.com/wxa/genwxashortlink";
231+
}
232+
229233
public interface SecCheck {
230234
String IMG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/img_sec_check";
231235
String MSG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/msg_sec_check";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
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+
@Test
12+
@Guice(modules = ApiTestModule.class)
13+
public class WxMaShortLinkServiceImplTest {
14+
@Inject
15+
private WxMaService wxService;
16+
17+
@Test
18+
public void testGenerate() throws WxErrorException {
19+
final String generate = this.wxService.getShortLinkService().generate(GenerateShortLinkRequest.builder().
20+
pageUrl("pages/productView/editPhone/editPhone?id=31832").pageTitle("productView").isPermanent(false).build());
21+
System.out.println("generate:");
22+
System.out.println(generate);
23+
}
24+
}

0 commit comments

Comments
 (0)