🆕 #1976 【小程序】增加获取小程序scheme码的接口

This commit is contained in:
cofe 2021-01-28 21:39:31 +08:00 committed by GitHub
parent 566d58e899
commit 73c135fb10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 178 additions and 0 deletions

View File

@ -0,0 +1,26 @@
package cn.binarywang.wx.miniapp.api;
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest;
import me.chanjar.weixin.common.error.WxErrorException;
/**
* <pre>
* 小程序Scheme码相关操作接口.
*
* 文档地址https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.generate.html
* </pre>
*
* @author : cofedream
* @date : 2021-01-26
*/
public interface WxMaSchemeService {
String GENERATE_SCHEME_URL = "https://api.weixin.qq.com/wxa/generatescheme";
/**
* 获取小程序scheme码
*
* @param request 请求参数
* @throws WxErrorException 生成失败时抛出具体错误码请看文档
*/
String generate(WxMaGenerateSchemeRequest request) throws WxErrorException;
}

View File

@ -229,6 +229,12 @@ public interface WxMaService extends WxService {
*/
WxMaQrcodeService getQrcodeService();
/**
* 返回获取小程序scheme码实现对象以方便调用其各个接口.
* @return WxMaSchemeService
*/
WxMaSchemeService getWxMaSchemeService();
/**
* 返回订阅消息配置相关接口方法的实现类对象, 以方便调用其各个接口.
*

View File

@ -47,6 +47,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
private final WxMaMediaService materialService = new WxMaMediaServiceImpl(this);
private final WxMaUserService userService = new WxMaUserServiceImpl(this);
private final WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this);
private final WxMaSchemeService schemeService = new WxMaSchemeServiceImpl(this);
private final WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this);
private final WxMaCodeService codeService = new WxMaCodeServiceImpl(this);
private final WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
@ -413,6 +414,11 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
return this.qrCodeService;
}
@Override
public WxMaSchemeService getWxMaSchemeService() {
return schemeService;
}
@Override
public WxMaSubscribeService getSubscribeService() {
return this.subscribeService;

View File

@ -0,0 +1,31 @@
package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaSchemeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest;
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
/**
* @author : cofedream
* @date : 2021-01-28
*/
@AllArgsConstructor
public class WxMaSchemeServiceImpl implements WxMaSchemeService {
private static final String ERR_CODE = "errcode";
private final WxMaService wxMaService;
@Override
public String generate(WxMaGenerateSchemeRequest request) throws WxErrorException {
String responseContent = this.wxMaService.post(GENERATE_SCHEME_URL, request.toJson());
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
}
return jsonObject.get("openlink").getAsString();
}
}

View File

@ -0,0 +1,69 @@
package cn.binarywang.wx.miniapp.bean.scheme;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.Data;
/**
* @author : cofedream
* @date : 2021-01-26
*/
@Data
@Builder(builderMethodName = "newBuilder")
public class WxMaGenerateSchemeRequest {
/**
* 跳转到的目标小程序信息
* <pre>
* 是否必填
* </pre>
*/
@SerializedName("jump_wxa")
private JumpWxa jumpWxa;
/**
* 生成的scheme码类型到期失效true永久有效false
* <pre>
* 是否必填
* </pre>
*/
@SerializedName("is_expire")
private Boolean isExpire;
/**
* 到期失效的scheme码的失效时间为Unix时间戳生成的到期失效scheme码在该时间前有效最长有效期为1年生成到期失效的scheme时必填
* <pre>
* 是否必填
* </pre>
*/
@SerializedName("expire_time")
private Long expireTime;
@Data
@Builder(builderMethodName = "newBuilder")
public static class JumpWxa {
/**
* 通过scheme码进入的小程序页面路径必须是已经发布的小程序存在的页面不可携带querypath为空时会跳转小程序主页
* <pre>
* 是否必填
* </pre>
*/
@SerializedName("path")
private String path;
/**
* 通过scheme码进入小程序时的query最大128个字符只支持数字大小写英文以及部分特殊字符!#$&'()*+,/:;=?@-._~
* 返回值
* <pre>
* 是否必填
* </pre>
*/
@SerializedName("query")
private String query;
}
public String toJson() {
return WxMaGsonBuilder.create().toJson(this);
}
}

View File

@ -0,0 +1,40 @@
package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest;
import cn.binarywang.wx.miniapp.test.ApiTestModule;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.time.DateUtils;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import java.util.Date;
/**
* @author : cofedream
* @date : 2021-01-28
*/
@Test
@Guice(modules = ApiTestModule.class)
public class WxMaSchemeServiceImplTest {
@Inject
private WxMaService wxService;
@Test
public void testGenerate() throws WxErrorException {
final Date date = DateUtils.addMinutes(new Date(), 20); // 20分钟后失效
final long expireTime = date.getTime() / 1000;
final String generate = this.wxService.getWxMaSchemeService().generate(WxMaGenerateSchemeRequest.newBuilder()
.jumpWxa(WxMaGenerateSchemeRequest.JumpWxa.newBuilder()
// .path("/pages/productView/editPhone/editPhone") // 都可以
.path("pages/productView/editPhone/editPhone") //
.query("")
.build())
.isExpire(true) // 到期失效
.expireTime(expireTime) // 失效时间
.build());
System.out.println("generate:");
System.out.println(generate);
}
}