8000 【微信小程序】支持小程序Short Link by linlinjava · Pull Request #2306 · binarywang/WxJava · GitHub
[go: up one dir, main page]

Skip to content

【微信小程序】支持小程序Short Link #2306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,13 @@ public interface WxMaService extends WxService {
*/
WxMaLinkService getLinkService();

/**
* 获取小程序 Short Link服务接口
*
* @return
*/
WxMaShortLinkService getShortLinkService();

/**
* 获取电子发票报销方服务接口
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cn.binarywang.wx.miniapp.api;


import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
import me.chanjar.weixin.common.error.WxErrorException;

/**
* 获取小程序 Short Link接口
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/short-link/shortlink.generate.html
*/
public interface WxMaShortLinkService {

String generate(GenerateShortLinkRequest request) throws WxErrorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
private final WxMaShopAfterSaleService shopAfterSaleService = new WxMaShopAfterSaleServiceImpl(this);
private final WxMaShopDeliveryService shopDeliveryService = new WxMaShopDeliveryServiceImpl(this);
private final WxMaLinkService linkService = new WxMaLinkServiceImpl(this);
private final WxMaShortLinkService shortlinkService = new WxMaShortLinkServiceImpl(this);
private final WxMaReimburseInvoiceService reimburseInvoiceService = new WxMaReimburseInvoiceServiceImpl(this);
private Map<String, WxMaConfig> configMap;
private int retrySleepMillis = 1000;
Expand Down Expand Up @@ -563,6 +564,11 @@ public WxMaLinkService getLinkService() {
return this.linkService;
}

@Override
public WxMaShortLinkService getShortLinkService() {
return this.shortlinkService;
}

@Override
public WxMaReimburseInvoiceService getReimburseInvoiceService() {
return this.reimburseInvoiceService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cn.binarywang.wx.miniapp.api.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaShortLinkService;
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;

import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Link.GENERATE_URLLINK_URL;
import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.ShortLink.GENERATE_SHORT_LINK_URL;

/**
* 获取小程序 Short Link接口
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/short-link/shortlink.generate.html
*/
@AllArgsConstructor
public class WxMaShortLinkServiceImpl implements WxMaShortLinkService {

private final WxMaService wxMaService;

@Override
public String generate(GenerateShortLinkRequest request) throws WxErrorException {
String result = this.wxMaService.post(GENERATE_SHORT_LINK_URL,request);
String linkField = "link";
JsonObject jsonObject = GsonParser.parse(result);
if(jsonObject.has(linkField)){
return jsonObject.get(linkField).toString();
}
throw new WxErrorException("无link");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cn.binarywang.wx.miniapp.bean.shortlink;

import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
* <pre>
* 获取小程序 Short Link参数对象
* </pre>
*/
@Data
@Builder
public class GenerateShortLinkRequest implements Serializable {
private static final long serialVersionUID = -7517804620683442832L;

/**
* 通过 Short Link 进入的小程序页面路径,必须是已经发布的小程序存在的页面,可携带 query,最大1024个字符
* <pre>
* 是否必填: 是
* </pre>
*/
@SerializedName("page_url")
private String pageUrl;

/**
* 页面标题,不能包含违法信息,超过20字符会用... 截断代替
* <pre>
* 是否必填: 是
* </pre>
*/
@SerializedName("page_title")
private String pageTitle;

/**
* 生成的 Short Link 类型,短期有效:false,永久有效:true
* <pre>
* 是否必填: 否
* </pre>
*/
@SerializedName("is_permanent")
private Boolean isPermanent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ public interface Link {
String GENERATE_URLLINK_URL = "https://api.weixin.qq.com/wxa/generate_urllink";
}

public interface ShortLink {
String GENERATE_SHORT_LINK_URL = "https://api.weixin.qq.com/wxa/genwxashortlink";
}

public interface SecCheck {
String IMG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/img_sec_check";
String MSG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/msg_sec_check";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.test.ApiTestModule;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

@Test
@Guice(modules = ApiTestModule.class)
public class WxMaShortLinkServiceImplTest {
@Inject
private WxMaService wxService;

@Test
public void testGenerate() throws WxErrorException {
final String generate = this.wxService.getShortLinkService().generate(GenerateShortLinkRequest.builder().
pageUrl("pages/productView/editPhone/editPhone?id=31832").pageTitle("productView").isPermanent(false).build());
System.out.println("generate:");
System.out.println(generate);
}
}
0