@@ -9,6 +9,12 @@ import java.util.Map; | |||
* @author Daniel Qian & binarywang | |||
*/ | |||
public class WxConsts { | |||
/** | |||
* 微信接口返回的参数errcode. | |||
*/ | |||
public static final String ERR_CODE = "errcode"; | |||
/** | |||
* 微信推送过来的消息的类型,和发送给微信xml格式消息的消息类型. | |||
*/ | |||
@@ -0,0 +1,36 @@ | |||
package cn.binarywang.wx.miniapp.api; | |||
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateNfcSchemeRequest; | |||
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest; | |||
import me.chanjar.weixin.common.error.WxErrorException; | |||
/** | |||
* <pre> | |||
* 小程序Scheme码相关操作接口. | |||
* | |||
* | |||
* </pre> | |||
* | |||
* @author : cofedream | |||
* created on : 2021-01-26 | |||
*/ | |||
public interface WxMaSchemeService { | |||
String GENERATE_SCHEME_URL = "https://api.weixin.qq.com/wxa/generatescheme"; | |||
String GENERATE_NFC_SCHEME_URL = "https://api.weixin.qq.com/wxa/generatenfcscheme"; | |||
/** | |||
* 获取小程序scheme码 | |||
*文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.generate.html | |||
* @param request 请求参数 | |||
* @throws WxErrorException 生成失败时抛出,具体错误码请看文档 | |||
*/ | |||
String generate(WxMaGenerateSchemeRequest request) throws WxErrorException; | |||
/** | |||
* 获取NFC 的小程序 scheme | |||
*文档地址:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/url-scheme/generateNFCScheme.html | |||
* @param request 请求参数 | |||
* @throws WxErrorException 生成失败时抛出,具体错误码请看文档 | |||
*/ | |||
String generateNFC(WxMaGenerateNfcSchemeRequest request) throws WxErrorException; | |||
} |
@@ -156,6 +156,13 @@ public interface WxMaService { | |||
*/ | |||
WxMaQrcodeService getQrcodeService(); | |||
/** | |||
* 返回获取小程序scheme码实现对象,以方便调用其各个接口. | |||
* | |||
* @return WxMaSchemeService wx ma scheme service | |||
*/ | |||
WxMaSchemeService getWxMaSchemeService(); | |||
/** | |||
* 返回模板配置相关接口方法的实现类对象, 以方便调用其各个接口. | |||
* | |||
@@ -0,0 +1,56 @@ | |||
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.WxMaGenerateNfcSchemeRequest; | |||
import cn.binarywang.wx.miniapp.bean.scheme.WxMaGenerateSchemeRequest; | |||
import com.google.gson.JsonObject; | |||
import lombok.AllArgsConstructor; | |||
import me.chanjar.weixin.common.WxType; | |||
import me.chanjar.weixin.common.api.WxConsts; | |||
import me.chanjar.weixin.common.error.WxError; | |||
import me.chanjar.weixin.common.error.WxErrorException; | |||
import me.chanjar.weixin.common.util.json.GsonParser; | |||
/** | |||
* @author : cofedream | |||
* created on : 2021-01-28 | |||
*/ | |||
@AllArgsConstructor | |||
public class WxMaSchemeServiceImpl implements WxMaSchemeService { | |||
private final WxMaService wxMaService; | |||
/** | |||
* 获取小程序scheme码 | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.generate.html | |||
* | |||
* @param request 请求参数 | |||
* @throws WxErrorException 生成失败时抛出,具体错误码请看文档 | |||
*/ | |||
@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(WxConsts.ERR_CODE).getAsInt() != 0) { | |||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); | |||
} | |||
return jsonObject.get("openlink").getAsString(); | |||
} | |||
/** | |||
* 获取NFC 的小程序 scheme | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/url-scheme/generateNFCScheme.html | |||
* | |||
* @param request 请求参数 | |||
* @throws WxErrorException 生成失败时抛出,具体错误码请看文档 | |||
*/ | |||
@Override | |||
public String generateNFC(WxMaGenerateNfcSchemeRequest request) throws WxErrorException { | |||
String responseContent = this.wxMaService.post(GENERATE_NFC_SCHEME_URL, request.toJson()); | |||
JsonObject jsonObject = GsonParser.parse(responseContent); | |||
if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) { | |||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); | |||
} | |||
return jsonObject.get("openlink").getAsString(); | |||
} | |||
} |
@@ -46,6 +46,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||
private WxMaMediaService materialService = new WxMaMediaServiceImpl(this); | |||
private WxMaUserService userService = new WxMaUserServiceImpl(this); | |||
private WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this); | |||
private WxMaSchemeService schemeService = new WxMaSchemeServiceImpl(this); | |||
private WxMaTemplateService templateService = new WxMaTemplateServiceImpl(this); | |||
private WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this); | |||
private WxMaCodeService codeService = new WxMaCodeServiceImpl(this); | |||
@@ -331,6 +332,11 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||
return this.qrCodeService; | |||
} | |||
@Override | |||
public WxMaSchemeService getWxMaSchemeService() { | |||
return schemeService; | |||
} | |||
@Override | |||
public WxMaTemplateService getTemplateService() { | |||
return this.templateService; | |||
@@ -0,0 +1,75 @@ | |||
package cn.binarywang.wx.miniapp.bean.scheme; | |||
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Builder; | |||
import lombok.Data; | |||
/** | |||
* @author : lyt | |||
* created on : 2023-07-31 | |||
*/ | |||
@Data | |||
@Builder(builderMethodName = "newBuilder") | |||
public class WxMaGenerateNfcSchemeRequest { | |||
/** | |||
* 跳转到的目标小程序信息。 | |||
* <pre> | |||
* 是否必填:否 | |||
* </pre> | |||
*/ | |||
@SerializedName("jump_wxa") | |||
private JumpWxa jumpWxa; | |||
/** | |||
* scheme对应的设备model_id | |||
* <pre> | |||
* 是否必填:是 | |||
* </pre> | |||
*/ | |||
@SerializedName("model_id") | |||
private String modelId; | |||
/** | |||
* scheme对应的设备sn,仅一机一码时填写 | |||
* <pre> | |||
* 是否必填:否 | |||
* </pre> | |||
*/ | |||
@SerializedName("sn") | |||
private String sn; | |||
@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; | |||
/** | |||
* 要打开的小程序版本。正式版为"release",体验版为"trial",开发版为"develop"默认值:release | |||
*/ | |||
@SerializedName("env_version") | |||
private String envVersion = "release"; | |||
} | |||
public String toJson() { | |||
return WxMaGsonBuilder.create().toJson(this); | |||
} | |||
} |
@@ -0,0 +1,94 @@ | |||
package cn.binarywang.wx.miniapp.bean.scheme; | |||
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Builder; | |||
import lombok.Data; | |||
/** | |||
* @author : cofedream | |||
* created on : 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; | |||
/** | |||
* 到期失效的 scheme 码失效类型,失效时间:0,失效间隔天数:1 | |||
* <pre> | |||
* 是否必填:否 | |||
* </pre> | |||
*/ | |||
@SerializedName("expire_type") | |||
private Integer expireType; | |||
/** | |||
* 到期失效的 scheme 码的失效间隔天数。 | |||
* <pre> | |||
* 生成的到期失效 scheme 码在该间隔时间到达前有效。最长间隔天数为365天。is_expire 为 true 且 expire_type 为 1 时必填 * <pre> | |||
* 是否必填:否 | |||
* </pre> | |||
*/ | |||
@SerializedName("expire_interval") | |||
private Integer expireInterval; | |||
@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; | |||
/** | |||
* 要打开的小程序版本。正式版为"release",体验版为"trial",开发版为"develop"默认值:release | |||
*/ | |||
@SerializedName("env_version") | |||
private String envVersion = "release"; | |||
} | |||
public String toJson() { | |||
return WxMaGsonBuilder.create().toJson(this); | |||
} | |||
} |