🎨 重构优化小程序Link相关接口

This commit is contained in:
Binary Wang
2021-09-13 23:08:32 +08:00
parent ba23afd49f
commit 123bef0ea9
6 changed files with 51 additions and 89 deletions

View File

@@ -1,16 +1,32 @@
package cn.binarywang.wx.miniapp.api;
import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
import me.chanjar.weixin.common.error.WxErrorException;
/**
* 获取小程序 URL Link接口
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-link/urllink.generate.html
* @author <a href="https://github.com/mr-xiaoyu">xiaoyu</a>
* @since 2021-06-10
*/
public interface WxMaLinkService {
/**
* 获取小程序 URL Link接口
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-link/urllink.generate.html
*
* @param request 请求
* @return link地址
* @throws WxErrorException .
*/
String generateUrlLink(GenerateUrlLinkRequest request) throws WxErrorException;
String generate(GenerateUrlLinkRequest request) throws WxErrorException;
/**
* 获取小程序 Short Link接口
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/short-link/shortlink.generate.html
*
* @param request 请求
* @return link地址
* @throws WxErrorException .
*/
String generateShortLink(GenerateShortLinkRequest request) throws WxErrorException;
}

View File

@@ -1,14 +0,0 @@
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;
}

View File

@@ -2,34 +2,46 @@ package cn.binarywang.wx.miniapp.api.impl;
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 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.ShortLink.GENERATE_SHORT_LINK_URL;
/**
* 获取小程序 URL Link接口实现
* 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-link/urllink.generate.html
*
* @author <a href="https://github.com/mr-xiaoyu">xiaoyu</a>
* @since 2021-06-10
*/
@AllArgsConstructor
public class WxMaLinkServiceImpl implements WxMaLinkService {
private final WxMaService wxMaService;
@Override
public String generate(GenerateUrlLinkRequest request) throws WxErrorException {
String result = this.wxMaService.post(GENERATE_URLLINK_URL,request);
public String generateUrlLink(GenerateUrlLinkRequest request) throws WxErrorException {
String result = this.wxMaService.post(GENERATE_URLLINK_URL, request);
String linkField = "url_link";
JsonObject jsonObject = GsonParser.parse(result);
if(jsonObject.has(linkField)){
if (jsonObject.has(linkField)) {
return jsonObject.get(linkField).toString();
}
throw new WxErrorException("无url_link");
}
@Override
public String generateShortLink(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");
}
}

View File

@@ -1,34 +0,0 @@
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");
}
}