mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
🆕 #3124 【小程序】实现查询 URL Link的接口
This commit is contained in:
parent
473d2c14c3
commit
b9bd619e6f
@ -3,6 +3,8 @@ package cn.binarywang.wx.miniapp.api;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.urllink.request.QueryUrlLinkRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.urllink.response.QueryUrlLinkResponse;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
|
||||
/**
|
||||
@ -29,4 +31,14 @@ public interface WxMaLinkService {
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
String generateShortLink(GenerateShortLinkRequest request) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 查询 URL Link
|
||||
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/url-link/queryUrlLink.html
|
||||
*
|
||||
* @param request 请求
|
||||
* @return link地址
|
||||
* @throws WxErrorException .
|
||||
*/
|
||||
QueryUrlLinkResponse queryUrlLink(QueryUrlLinkRequest request) throws WxErrorException;
|
||||
}
|
||||
|
@ -4,12 +4,16 @@ import cn.binarywang.wx.miniapp.api.WxMaLinkService;
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.urllink.request.QueryUrlLinkRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.urllink.response.QueryUrlLinkResponse;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Link.GENERATE_URLLINK_URL;
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Link.QUERY_URLLINK_URL;
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.ShortLink.GENERATE_SHORT_LINK_URL;
|
||||
|
||||
/**
|
||||
@ -44,4 +48,10 @@ public class WxMaLinkServiceImpl implements WxMaLinkService {
|
||||
}
|
||||
throw new WxErrorException("无link");
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryUrlLinkResponse queryUrlLink(QueryUrlLinkRequest request) throws WxErrorException {
|
||||
String result = this.wxMaService.post(QUERY_URLLINK_URL, request);
|
||||
return WxGsonBuilder.create().fromJson(result, QueryUrlLinkResponse.class);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
package cn.binarywang.wx.miniapp.bean.urllink.request;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 查询小程序 URL Link参数对象
|
||||
* </pre>
|
||||
* @author <a href="https://github.com/imyzt">imyzt</a>
|
||||
* @since 2023-11-13
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@Accessors(chain = true)
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class QueryUrlLinkRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 小程序 url_link
|
||||
* <pre>
|
||||
* 是否必填: 是
|
||||
* </pre>
|
||||
*/
|
||||
@SerializedName("url_link")
|
||||
private String urlLink;
|
||||
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package cn.binarywang.wx.miniapp.bean.urllink.response;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaBaseResponse;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 查询小程序 URL Link响应对象
|
||||
* </pre>
|
||||
* @author <a href="https://github.com/imyzt">imyzt</a>
|
||||
* @since 2023-11-13
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Builder
|
||||
@Accessors(chain = true)
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class QueryUrlLinkResponse extends WxMaBaseResponse implements Serializable {
|
||||
|
||||
/**
|
||||
* 访问Link的用户openid,为空表示未被访问过
|
||||
*/
|
||||
@SerializedName("visit_openid")
|
||||
private String visitOpenid;
|
||||
|
||||
/**
|
||||
* url_link 配置
|
||||
*/
|
||||
@SerializedName("url_link_info")
|
||||
private UrlLinkInfo urlLinkInfo;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public static class UrlLinkInfo {
|
||||
|
||||
/**
|
||||
* 小程序 appid
|
||||
*/
|
||||
private String appid;
|
||||
|
||||
/**
|
||||
* 小程序页面路径
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* 小程序页面query
|
||||
*/
|
||||
private String query;
|
||||
|
||||
/**
|
||||
* 创建时间,为 Unix 时间戳
|
||||
*/
|
||||
@SerializedName("create_time")
|
||||
private Long createTime;
|
||||
|
||||
/**
|
||||
* 到期失效时间,为 Unix 时间戳,0 表示永久生效
|
||||
*/
|
||||
@SerializedName("expire_time")
|
||||
private Long expireTime;
|
||||
|
||||
/**
|
||||
* 要打开的小程序版本。正式版为"release",体验版为"trial",开发版为"develop"
|
||||
*/
|
||||
@SerializedName("env_version")
|
||||
private String envVersion;
|
||||
}
|
||||
|
||||
}
|
@ -344,6 +344,7 @@ public class WxMaApiUrlConstants {
|
||||
|
||||
public interface Link {
|
||||
String GENERATE_URLLINK_URL = "https://api.weixin.qq.com/wxa/generate_urllink";
|
||||
String QUERY_URLLINK_URL = "https://api.weixin.qq.com/wxa/query_urllink";
|
||||
}
|
||||
|
||||
public interface ShortLink {
|
||||
|
@ -3,10 +3,13 @@ package cn.binarywang.wx.miniapp.api.impl;
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.urllink.request.QueryUrlLinkRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.urllink.response.QueryUrlLinkResponse;
|
||||
import cn.binarywang.wx.miniapp.test.ApiTestModule;
|
||||
import com.google.inject.Inject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@ -53,4 +56,21 @@ public class WxMaLinkServiceImplTest {
|
||||
.build());
|
||||
log.info("generate url link = {}", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryUrlLink() throws WxErrorException {
|
||||
|
||||
String path = "pages/index";
|
||||
String urlLink = this.wxMaService.getLinkService().generateUrlLink(GenerateUrlLinkRequest.builder()
|
||||
.path(path)
|
||||
.expireTime(LocalDateTime.now().plusDays(5).atZone(ZoneId.systemDefault()).toEpochSecond()) //增加有效期,此行可注释
|
||||
.build());
|
||||
System.out.println("urlLink: " + urlLink);
|
||||
|
||||
QueryUrlLinkResponse queryUrlLinkResponse = this.wxMaService.getLinkService()
|
||||
.queryUrlLink(QueryUrlLinkRequest.builder().urlLink(urlLink).build());
|
||||
System.out.println("linkInfo: " + queryUrlLinkResponse.toString());
|
||||
|
||||
Assert.assertEquals(path, queryUrlLinkResponse.getUrlLinkInfo().getPath());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user