Skip to content

Commit 73c135f

Browse files
authored
🆕 binarywang#1976 【小程序】增加获取小程序scheme码的接口
1 parent 566d58e commit 73c135f

6 files changed

Lines changed: 178 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest;
4+
import me.chanjar.weixin.common.error.WxErrorException;
5+
6+
/**
7+
* <pre>
8+
* 小程序Scheme码相关操作接口.
9+
*
10+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.generate.html
11+
* </pre>
12+
*
13+
* @author : cofedream
14+
* @date : 2021-01-26
15+
*/
16+
public interface WxMaSchemeService {
17+
String GENERATE_SCHEME_URL = "https://api.weixin.qq.com/wxa/generatescheme";
18+
19+
/**
20+
* 获取小程序scheme码
21+
*
22+
* @param request 请求参数
23+
* @throws WxErrorException 生成失败时抛出,具体错误码请看文档
24+
*/
25+
String generate(WxMaGenerateSchemeRequest request) throws WxErrorException;
26+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ public interface WxMaService extends WxService {
229229
*/
230230
WxMaQrcodeService getQrcodeService();
231231

232+
/**
233+
* 返回获取小程序scheme码实现对象,以方便调用其各个接口.
234+
* @return WxMaSchemeService
235+
*/
236+
WxMaSchemeService getWxMaSchemeService();
237+
232238
/**
233239
* 返回订阅消息配置相关接口方法的实现类对象, 以方便调用其各个接口.
234240
*

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
@@ -47,6 +47,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
4747
private final WxMaMediaService materialService = new WxMaMediaServiceImpl(this);
4848
private final WxMaUserService userService = new WxMaUserServiceImpl(this);
4949
private final WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this);
50+
private final WxMaSchemeService schemeService = new WxMaSchemeServiceImpl(this);
5051
private final WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this);
5152
private final WxMaCodeService codeService = new WxMaCodeServiceImpl(this);
5253
private final WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
@@ -413,6 +414,11 @@ public WxMaQrcodeService getQrcodeService() {
413414
return this.qrCodeService;
414415
}
415416

417+
@Override
418+
public WxMaSchemeService getWxMaSchemeService() {
419+
return schemeService;
420+
}
421+
416422
@Override
417423
public WxMaSubscribeService getSubscribeService() {
418424
return this.subscribeService;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaSchemeService;
4+
import cn.binarywang.wx.miniapp.api.WxMaService;
5+
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest;
6+
import com.google.gson.JsonObject;
7+
import lombok.AllArgsConstructor;
8+
import me.chanjar.weixin.common.enums.WxType;
9+
import me.chanjar.weixin.common.error.WxError;
10+
import me.chanjar.weixin.common.error.WxErrorException;
11+
import me.chanjar.weixin.common.util.json.GsonParser;
12+
13+
/**
14+
* @author : cofedream
15+
* @date : 2021-01-28
16+
*/
17+
@AllArgsConstructor
18+
public class WxMaSchemeServiceImpl implements WxMaSchemeService {
19+
private static final String ERR_CODE = "errcode";
20+
private final WxMaService wxMaService;
21+
22+
@Override
23+
public String generate(WxMaGenerateSchemeRequest request) throws WxErrorException {
24+
String responseContent = this.wxMaService.post(GENERATE_SCHEME_URL, request.toJson());
25+
JsonObject jsonObject = GsonParser.parse(responseContent);
26+
if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
27+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
28+
}
29+
return jsonObject.get("openlink").getAsString();
30+
}
31+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cn.binarywang.wx.miniapp.bean.scheme;
2+
3+
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
8+
/**
9+
* @author : cofedream
10+
* @date : 2021-01-26
11+
*/
12+
@Data
13+
@Builder(builderMethodName = "newBuilder")
14+
public class WxMaGenerateSchemeRequest {
15+
/**
16+
* 跳转到的目标小程序信息。
17+
* <pre>
18+
* 是否必填:否
19+
* </pre>
20+
*/
21+
@SerializedName("jump_wxa")
22+
private JumpWxa jumpWxa;
23+
24+
/**
25+
* 生成的scheme码类型,到期失效:true,永久有效:false。
26+
* <pre>
27+
* 是否必填:否
28+
* </pre>
29+
*/
30+
@SerializedName("is_expire")
31+
private Boolean isExpire;
32+
33+
/**
34+
* 到期失效的scheme码的失效时间,为Unix时间戳。生成的到期失效scheme码在该时间前有效。最长有效期为1年。生成到期失效的scheme时必填。
35+
* <pre>
36+
* 是否必填:否
37+
* </pre>
38+
*/
39+
@SerializedName("expire_time")
40+
private Long expireTime;
41+
42+
@Data
43+
@Builder(builderMethodName = "newBuilder")
44+
public static class JumpWxa {
45+
/**
46+
* 通过scheme码进入的小程序页面路径,必须是已经发布的小程序存在的页面,不可携带query。path为空时会跳转小程序主页。
47+
* <pre>
48+
* 是否必填:是
49+
* </pre>
50+
*/
51+
@SerializedName("path")
52+
private String path;
53+
54+
/**
55+
* 通过scheme码进入小程序时的query,最大128个字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~
56+
* 返回值
57+
* <pre>
58+
* 是否必填:是
59+
* </pre>
60+
*/
61+
@SerializedName("query")
62+
private String query;
63+
}
64+
65+
public String toJson() {
66+
return WxMaGsonBuilder.create().toJson(this);
67+
}
68+
69+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest;
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.apache.commons.lang3.time.DateUtils;
9+
import org.testng.annotations.Guice;
10+
import org.testng.annotations.Test;
11+
12+
import java.util.Date;
13+
14+
/**
15+
* @author : cofedream
16+
* @date : 2021-01-28
17+
*/
18+
@Test
19+
@Guice(modules = ApiTestModule.class)
20+
public class WxMaSchemeServiceImplTest {
21+
@Inject
22+
private WxMaService wxService;
23+
24+
@Test
25+
public void testGenerate() throws WxErrorException {
26+
final Date date = DateUtils.addMinutes(new Date(), 20); // 20分钟后失效
27+
final long expireTime = date.getTime() / 1000;
28+
final String generate = this.wxService.getWxMaSchemeService().generate(WxMaGenerateSchemeRequest.newBuilder()
29+
.jumpWxa(WxMaGenerateSchemeRequest.JumpWxa.newBuilder()
30+
// .path("/pages/productView/editPhone/editPhone") // 都可以
31+
.path("pages/productView/editPhone/editPhone") //
32+
.query("")
33+
.build())
34+
.isExpire(true) // 到期失效
35+
.expireTime(expireTime) // 失效时间
36+
.build());
37+
System.out.println("generate:");
38+
System.out.println(generate);
39+
}
40+
}

0 commit comments

Comments
 (0)