mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	🆕 #1976 【小程序】增加获取小程序scheme码的接口
This commit is contained in:
		| @ -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; | ||||||
|  | } | ||||||
| @ -229,6 +229,12 @@ public interface WxMaService extends WxService { | |||||||
|    */ |    */ | ||||||
|   WxMaQrcodeService getQrcodeService(); |   WxMaQrcodeService getQrcodeService(); | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 返回获取小程序scheme码实现对象,以方便调用其各个接口. | ||||||
|  |    * @return WxMaSchemeService | ||||||
|  |    */ | ||||||
|  |   WxMaSchemeService getWxMaSchemeService(); | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * 返回订阅消息配置相关接口方法的实现类对象, 以方便调用其各个接口. |    * 返回订阅消息配置相关接口方法的实现类对象, 以方便调用其各个接口. | ||||||
|    * |    * | ||||||
|  | |||||||
| @ -47,6 +47,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH | |||||||
|   private final WxMaMediaService materialService = new WxMaMediaServiceImpl(this); |   private final WxMaMediaService materialService = new WxMaMediaServiceImpl(this); | ||||||
|   private final WxMaUserService userService = new WxMaUserServiceImpl(this); |   private final WxMaUserService userService = new WxMaUserServiceImpl(this); | ||||||
|   private final WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this); |   private final WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this); | ||||||
|  |   private final WxMaSchemeService schemeService = new WxMaSchemeServiceImpl(this); | ||||||
|   private final WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this); |   private final WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this); | ||||||
|   private final WxMaCodeService codeService = new WxMaCodeServiceImpl(this); |   private final WxMaCodeService codeService = new WxMaCodeServiceImpl(this); | ||||||
|   private final WxMaSettingService settingService = new WxMaSettingServiceImpl(this); |   private final WxMaSettingService settingService = new WxMaSettingServiceImpl(this); | ||||||
| @ -413,6 +414,11 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH | |||||||
|     return this.qrCodeService; |     return this.qrCodeService; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public WxMaSchemeService getWxMaSchemeService() { | ||||||
|  |     return schemeService; | ||||||
|  |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMaSubscribeService getSubscribeService() { |   public WxMaSubscribeService getSubscribeService() { | ||||||
|     return this.subscribeService; |     return this.subscribeService; | ||||||
|  | |||||||
| @ -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(); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -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码进入的小程序页面路径,必须是已经发布的小程序存在的页面,不可携带query。path为空时会跳转小程序主页。 | ||||||
|  |      * <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); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
| @ -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); | ||||||
|  |   } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 cofe
					cofe