Skip to content

Commit 123bef0

Browse files
committed
🎨 重构优化小程序Link相关接口
1 parent ba23afd commit 123bef0

File tree

6 files changed

+51
-89
lines changed

6 files changed

+51
-89
lines changed
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
package cn.binarywang.wx.miniapp.api;
22

33

4+
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
45
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
56
import me.chanjar.weixin.common.error.WxErrorException;
67

78
/**
8-
* 获取小程序 URL Link接口
9-
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-link/urllink.generate.html
109
* @author <a href="https://github.com/mr-xiaoyu">xiaoyu</a>
1110
* @since 2021-06-10
1211
*/
1312
public interface WxMaLinkService {
13+
/**
14+
* 获取小程序 URL Link接口
15+
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-link/urllink.generate.html
16+
*
17+
* @param request 请求
18+
* @return link地址
19+
* @throws WxErrorException .
20+
*/
21+
String generateUrlLink(GenerateUrlLinkRequest request) throws WxErrorException;
1422

15-
String generate(GenerateUrlLinkRequest request) throws WxErrorException;
23+
/**
24+
* 获取小程序 Short Link接口
25+
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/short-link/shortlink.generate.html
26+
*
27+
* @param request 请求
28+
* @return link地址
29+
* @throws WxErrorException .
30+
*/
31+
String generateShortLink(GenerateShortLinkRequest request) throws WxErrorException;
1632
}

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

Lines changed: 0 additions & 14 deletions
This file was deleted.

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLinkServiceImpl.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,46 @@
22

33
import cn.binarywang.wx.miniapp.api.WxMaLinkService;
44
import cn.binarywang.wx.miniapp.api.WxMaService;
5+
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
56
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
67
import com.google.gson.JsonObject;
78
import lombok.AllArgsConstructor;
89
import me.chanjar.weixin.common.error.WxErrorException;
910
import me.chanjar.weixin.common.util.json.GsonParser;
10-
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
1111

1212
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;
1314

1415
/**
1516
* 获取小程序 URL Link接口实现
1617
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-link/urllink.generate.html
18+
*
1719
* @author <a href="https://github.com/mr-xiaoyu">xiaoyu</a>
1820
* @since 2021-06-10
1921
*/
2022
@AllArgsConstructor
2123
public class WxMaLinkServiceImpl implements WxMaLinkService {
22-
2324
private final WxMaService wxMaService;
2425

2526
@Override
26-
public String generate(GenerateUrlLinkRequest request) throws WxErrorException {
27-
String result = this.wxMaService.post(GENERATE_URLLINK_URL,request);
27+
public String generateUrlLink(GenerateUrlLinkRequest request) throws WxErrorException {
28+
String result = this.wxMaService.post(GENERATE_URLLINK_URL, request);
2829
String linkField = "url_link";
2930
JsonObject jsonObject = GsonParser.parse(result);
30-
if(jsonObject.has(linkField)){
31+
if (jsonObject.has(linkField)) {
3132
return jsonObject.get(linkField).toString();
3233
}
3334
throw new WxErrorException("无url_link");
3435
}
36+
37+
@Override
38+
public String generateShortLink(GenerateShortLinkRequest request) throws WxErrorException {
39+
String result = this.wxMaService.post(GENERATE_SHORT_LINK_URL, request);
40+
String linkField = "link";
41+
JsonObject jsonObject = GsonParser.parse(result);
42+
if (jsonObject.has(linkField)) {
43+
return jsonObject.get(linkField).toString();
44+
}
45+
throw new WxErrorException("无link");
3546
}
47+
}

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

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
package cn.binarywang.wx.miniapp.api.impl;
22

33
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
45
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
56
import cn.binarywang.wx.miniapp.test.ApiTestModule;
67
import com.google.inject.Inject;
78
import me.chanjar.weixin.common.error.WxErrorException;
89
import org.testng.annotations.Guice;
910
import org.testng.annotations.Test;
1011

11-
import static org.testng.Assert.*;
12-
1312
@Test
1413
@Guice(modules = ApiTestModule.class)
1514
public class WxMaLinkServiceImplTest {
16-
1715
@Inject
1816
private WxMaService wxMaService;
1917

2018
@Test
21-
public void testGenerate() throws WxErrorException {
22-
23-
GenerateUrlLinkRequest request = GenerateUrlLinkRequest.builder()
19+
public void testGenerateUrlLink() throws WxErrorException {
20+
String url = this.wxMaService.getLinkService().generateUrlLink(GenerateUrlLinkRequest.builder()
2421
.path("pages/tabBar/home/home")
25-
.build();
26-
27-
String url = this.wxMaService.getLinkService().generate(request);
22+
.build());
2823

2924
System.out.println(url);
3025
}
26+
27+
@Test
28+
public void testGenerateShortLink() throws WxErrorException {
29+
final String generate = this.wxMaService.getLinkService()
30+
.generateShortLink(GenerateShortLinkRequest.builder().
31+
pageUrl("pages/productView/editPhone/editPhone?id=31832")
32+
.pageTitle("productView")
33+
.isPermanent(false).build());
34+
System.out.println("generate:");
35+
System.out.println(generate);
36+
}
3137
}

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

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)