#1160 WxOpenComponentService接口类提供get和post相关方法,方便使用者扩展

This commit is contained in:
Binary Wang
2019-08-18 10:21:15 +08:00
parent ecc407a6e9
commit 5e8d4a2ec6
2 changed files with 86 additions and 75 deletions

View File

@ -12,6 +12,8 @@ import me.chanjar.weixin.open.bean.result.*;
import java.util.List; import java.util.List;
/** /**
* .
*
* @author <a href="https://github.com/007gzs">007</a> * @author <a href="https://github.com/007gzs">007</a>
*/ */
public interface WxOpenComponentService { public interface WxOpenComponentService {
@ -27,45 +29,45 @@ public interface WxOpenComponentService {
String COMPONENT_LOGIN_PAGE_URL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s&auth_type=xxx&biz_appid=xxx"; String COMPONENT_LOGIN_PAGE_URL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s&auth_type=xxx&biz_appid=xxx";
/** /**
* 手机端打开授权链接 * 手机端打开授权链接.
*/ */
String COMPONENT_MOBILE_LOGIN_PAGE_URL = "https://mp.weixin.qq.com/safe/bindcomponent?action=bindcomponent&no_scan=1&auth_type=3&component_appid=%s&pre_auth_code=%s&redirect_uri=%s&auth_type=xxx&biz_appid=xxx#wechat_redirect"; String COMPONENT_MOBILE_LOGIN_PAGE_URL = "https://mp.weixin.qq.com/safe/bindcomponent?action=bindcomponent&no_scan=1&auth_type=3&component_appid=%s&pre_auth_code=%s&redirect_uri=%s&auth_type=xxx&biz_appid=xxx#wechat_redirect";
String CONNECT_OAUTH2_AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect"; String CONNECT_OAUTH2_AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect";
/** /**
* 用code换取oauth2的access token * 用code换取oauth2的access token.
*/ */
String OAUTH2_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=%s&code=%s&grant_type=authorization_code&component_appid=%s"; String OAUTH2_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=%s&code=%s&grant_type=authorization_code&component_appid=%s";
/** /**
* 刷新oauth2的access token * 刷新oauth2的access token.
*/ */
String OAUTH2_REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/component/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s&component_appid=%s"; String OAUTH2_REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/component/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s&component_appid=%s";
String MINIAPP_JSCODE_2_SESSION = "https://api.weixin.qq.com/sns/component/jscode2session?appid=%s&js_code=%s&grant_type=authorization_code&component_appid=%s"; String MINIAPP_JSCODE_2_SESSION = "https://api.weixin.qq.com/sns/component/jscode2session?appid=%s&js_code=%s&grant_type=authorization_code&component_appid=%s";
String CREATE_OPEN_URL= "https://api.weixin.qq.com/cgi-bin/open/create"; String CREATE_OPEN_URL = "https://api.weixin.qq.com/cgi-bin/open/create";
/** /**
* 快速创建小程序接口 * 快速创建小程序接口.
*/ */
String FAST_REGISTER_WEAPP_URL = "https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=create"; String FAST_REGISTER_WEAPP_URL = "https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=create";
String FAST_REGISTER_WEAPP_SEARCH_URL = "https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=search"; String FAST_REGISTER_WEAPP_SEARCH_URL = "https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=search";
WxMpService getWxMpServiceByAppid(String appid); WxMpService getWxMpServiceByAppid(String appid);
/** /**
* 获取指定appid的开放平台小程序服务继承一般小程序服务能力 * 获取指定appid的开放平台小程序服务继承一般小程序服务能力.
* *
* @param appid * @param appid .
* @return * @return .
*/ */
WxOpenMaService getWxMaServiceByAppid(String appid); WxOpenMaService getWxMaServiceByAppid(String appid);
/** /**
* 获取指定appid的快速创建的小程序服务 * 获取指定appid的快速创建的小程序服务.
* @param appid *
* @return * @param appid .
* @return .
*/ */
WxOpenFastMaService getWxFastMaServiceByAppid(String appid); WxOpenFastMaService getWxFastMaServiceByAppid(String appid);
@ -75,62 +77,72 @@ public interface WxOpenComponentService {
String getComponentAccessToken(boolean forceRefresh) throws WxErrorException; String getComponentAccessToken(boolean forceRefresh) throws WxErrorException;
/** String post(String uri, String postData) throws WxErrorException;
* 获取用户授权页URL来路URL和成功跳转URL 的域名都需要为三方平台设置的 登录授权的发起页域名)
*/ String post(String uri, String postData, String accessTokenKey) throws WxErrorException;
String getPreAuthUrl(String redirectURI) throws WxErrorException;
String get(String uri) throws WxErrorException;
String get(String uri, String accessTokenKey) throws WxErrorException;
/** /**
* authType 要授权的帐号类型1则商户点击链接后手机端仅展示公众号、2表示仅展示小程序3表示公众号和小程序都展示。如果为未指定则默认小程序和公众号都展示。第三方平台开发者可以使用本字段来控制授权的帐号类型。 * 获取用户授权页URL来路URL和成功跳转URL 的域名都需要为三方平台设置的 登录授权的发起页域名).
* bizAppid 指定授权唯一的小程序或公众号
* 注auth_type、biz_appid两个字段互斥。
*/ */
String getPreAuthUrl(String redirectURI, String authType, String bizAppid) throws WxErrorException; String getPreAuthUrl(String redirectUri) throws WxErrorException;
/** /**
* 获取预授权链接(手机端预授权) * .
* *
* @param redirectURI * @param authType 要授权的帐号类型1则商户点击链接后手机端仅展示公众号、2表示仅展示小程序3表示公众号和小程序都展示。如果为未指定则默认小程序和公众号都展示。第三方平台开发者可以使用本字段来控制授权的帐号类型。
* @return * @param bizAppid 指定授权唯一的小程序或公众号
* @throws WxErrorException * authType、bizAppid 互斥。
*/ */
String getMobilePreAuthUrl(String redirectURI) throws WxErrorException; String getPreAuthUrl(String redirectUri, String authType, String bizAppid) throws WxErrorException;
/** /**
* 获取预授权链接(手机端预授权) * 获取预授权链接(手机端预授权).
* *
* @param redirectURI * @param redirectUri .
* @param authType * @return .
* @param bizAppid * @throws WxErrorException .
* @return
* @throws WxErrorException
*/ */
String getMobilePreAuthUrl(String redirectURI, String authType, String bizAppid) throws WxErrorException; String getMobilePreAuthUrl(String redirectUri) throws WxErrorException;
/**
* 获取预授权链接(手机端预授权).
*
* @param redirectUri .
* @param authType .
* @param bizAppid .
* @return .
* @throws WxErrorException .
*/
String getMobilePreAuthUrl(String redirectUri, String authType, String bizAppid) throws WxErrorException;
String route(WxOpenXmlMessage wxMessage) throws WxErrorException; String route(WxOpenXmlMessage wxMessage) throws WxErrorException;
/** /**
* 使用授权码换取公众号或小程序的接口调用凭据和授权信息 * 使用授权码换取公众号或小程序的接口调用凭据和授权信息.
*/ */
WxOpenQueryAuthResult getQueryAuth(String authorizationCode) throws WxErrorException; WxOpenQueryAuthResult getQueryAuth(String authorizationCode) throws WxErrorException;
/** /**
* 获取授权方的帐号基本信息 * 获取授权方的帐号基本信息.
*/ */
WxOpenAuthorizerInfoResult getAuthorizerInfo(String authorizerAppid) throws WxErrorException; WxOpenAuthorizerInfoResult getAuthorizerInfo(String authorizerAppid) throws WxErrorException;
/** /**
* 获取授权方的选项设置信息 * 获取授权方的选项设置信息.
*/ */
WxOpenAuthorizerOptionResult getAuthorizerOption(String authorizerAppid, String optionName) throws WxErrorException; WxOpenAuthorizerOptionResult getAuthorizerOption(String authorizerAppid, String optionName) throws WxErrorException;
/** /**
* 获取所有授权方列表 * 获取所有授权方列表.
*/ */
WxOpenAuthorizerListResult getAuthorizerList(int begin, int len) throws WxErrorException; WxOpenAuthorizerListResult getAuthorizerList(int begin, int len) throws WxErrorException;
/** /**
* 设置授权方的选项信息 * 设置授权方的选项信息.
*/ */
void setAuthorizerOption(String authorizerAppid, String optionName, String optionValue) throws WxErrorException; void setAuthorizerOption(String authorizerAppid, String optionName, String optionValue) throws WxErrorException;
@ -142,13 +154,12 @@ public interface WxOpenComponentService {
WxMpOAuth2AccessToken oauth2refreshAccessToken(String appid, String refreshToken) throws WxErrorException; WxMpOAuth2AccessToken oauth2refreshAccessToken(String appid, String refreshToken) throws WxErrorException;
String oauth2buildAuthorizationUrl(String appid, String redirectURI, String scope, String state); String oauth2buildAuthorizationUrl(String appid, String redirectUri, String scope, String state);
WxMaJscode2SessionResult miniappJscode2Session(String appId, String jsCode) throws WxErrorException; WxMaJscode2SessionResult miniappJscode2Session(String appId, String jsCode) throws WxErrorException;
/** /**
* 代小程序实现业务 * 代小程序实现业务.
* <p>
* 小程序代码模版库管理https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1506504150_nMMh6&token=&lang=zh_CN * 小程序代码模版库管理https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1506504150_nMMh6&token=&lang=zh_CN
* access_token 为 component_access_token * access_token 为 component_access_token
*/ */
@ -158,7 +169,7 @@ public interface WxOpenComponentService {
String DELETE_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/deletetemplate"; String DELETE_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/deletetemplate";
/** /**
* 获取草稿箱内的所有临时代码草稿 * 获取草稿箱内的所有临时代码草稿.
* *
* @return 草稿箱代码模板列表draftId * @return 草稿箱代码模板列表draftId
* @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档 * @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
@ -166,7 +177,7 @@ public interface WxOpenComponentService {
List<WxOpenMaCodeTemplate> getTemplateDraftList() throws WxErrorException; List<WxOpenMaCodeTemplate> getTemplateDraftList() throws WxErrorException;
/** /**
* 获取代码模版库中的所有小程序代码模版 * 获取代码模版库中的所有小程序代码模版.
* *
* @return 小程序代码模版列表templateId * @return 小程序代码模版列表templateId
* @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档 * @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
@ -174,7 +185,7 @@ public interface WxOpenComponentService {
List<WxOpenMaCodeTemplate> getTemplateList() throws WxErrorException; List<WxOpenMaCodeTemplate> getTemplateList() throws WxErrorException;
/** /**
* 将草稿箱的草稿选为小程序代码模版 * 将草稿箱的草稿选为小程序代码模版.
* *
* @param draftId 草稿ID本字段可通过“获取草稿箱内的所有临时代码草稿”接口获得 * @param draftId 草稿ID本字段可通过“获取草稿箱内的所有临时代码草稿”接口获得
* @throws WxErrorException 操作失败时抛出,具体错误码请看此接口的注释文档 * @throws WxErrorException 操作失败时抛出,具体错误码请看此接口的注释文档
@ -183,7 +194,7 @@ public interface WxOpenComponentService {
void addToTemplate(long draftId) throws WxErrorException; void addToTemplate(long draftId) throws WxErrorException;
/** /**
* 删除指定小程序代码模版 * 删除指定小程序代码模版.
* *
* @param templateId 要删除的模版ID * @param templateId 要删除的模版ID
* @throws WxErrorException 操作失败时抛出,具体错误码请看此接口的注释文档 * @throws WxErrorException 操作失败时抛出,具体错误码请看此接口的注释文档
@ -193,45 +204,41 @@ public interface WxOpenComponentService {
/** /**
* https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1498704199_1bcax&token=6df5e3650041eff2cd3ec3662425ad8d7beec8d9&lang=zh_CN * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1498704199_1bcax&token=6df5e3650041eff2cd3ec3662425ad8d7beec8d9&lang=zh_CN
* 创建 开放平台帐号并绑定公众号/小程序 * 创建 开放平台帐号并绑定公众号/小程序.
*
* https://api.weixin.qq.com/cgi-bin/open/create * https://api.weixin.qq.com/cgi-bin/open/create
* *
* @param appId 公众号/小程序的appId * @param appId 公众号/小程序的appId
* @return * @return .
*/ */
WxOpenCreateResult createOpenAccount(String appId) throws WxErrorException; WxOpenCreateResult createOpenAccount(String appId) throws WxErrorException;
/** /**
* https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=21538208049W8uwq&token=&lang=zh_CN * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=21538208049W8uwq&token=&lang=zh_CN
* 第三方平台快速创建小程序 * 第三方平台快速创建小程序.
* <pre>
* 注意:创建任务逻辑串行,单次任务结束后才可以使用相同信息下发第二次任务,请注意规避任务阻塞 * 注意:创建任务逻辑串行,单次任务结束后才可以使用相同信息下发第二次任务,请注意规避任务阻塞
* </pre> *
* @param name 企业名(需与工商部门登记信息一致) * @param name 企业名(需与工商部门登记信息一致)
* @param code 企业代码 * @param code 企业代码
* @param codeType 企业代码类型 1统一社会信用代码18位 2组织机构代码9位xxxxxxxx-x 3营业执照注册号(15位) * @param codeType 企业代码类型 1统一社会信用代码18位 2组织机构代码9位xxxxxxxx-x 3营业执照注册号(15位)
* @param legalPersonaWechat 法人微信号 * @param legalPersonaWechat 法人微信号
* @param legalPersonaName 法人姓名(绑定银行卡) * @param legalPersonaName 法人姓名(绑定银行卡)
* @param componentPhone 第三方联系电话(方便法人与第三方联系) * @param componentPhone 第三方联系电话(方便法人与第三方联系)
* @return * @return .
* @throws WxErrorException * @throws WxErrorException .
*/ */
WxOpenResult fastRegisterWeapp(String name, String code, String codeType, String legalPersonaWechat, String legalPersonaName, String componentPhone) throws WxErrorException; WxOpenResult fastRegisterWeapp(String name, String code, String codeType, String legalPersonaWechat, String legalPersonaName, String componentPhone) throws WxErrorException;
/** /**
* https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=21538208049W8uwq&token=&lang=zh_CN * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=21538208049W8uwq&token=&lang=zh_CN
* 查询第三方平台快速创建小程序的任务状态 * 查询第三方平台快速创建小程序的任务状态
* <pre>
* 注意:该接口只提供当下任务结果查询,不建议过分依赖该接口查询所创建小程序。 * 注意:该接口只提供当下任务结果查询,不建议过分依赖该接口查询所创建小程序。
* 小程序的成功状态可在第三方服务器中自行对账、查询。 * 小程序的成功状态可在第三方服务器中自行对账、查询。
* 不要频繁调用search接口消息接收需通过服务器查看。调用search接口会消耗接口整体调用quato * 不要频繁调用search接口消息接收需通过服务器查看。调用search接口会消耗接口整体调用quato
* </pre>
* *
* @param name 企业名(需与工商部门登记信息一致) * @param name 企业名(需与工商部门登记信息一致)
* @param legalPersonaWechat 法人微信号 * @param legalPersonaWechat 法人微信号
* @param legalPersonaName 法人姓名(绑定银行卡) * @param legalPersonaName 法人姓名(绑定银行卡)
* @throws WxErrorException * @throws WxErrorException .
*/ */
WxOpenResult fastRegisterWeappSearch(String name, String legalPersonaWechat, String legalPersonaName) throws WxErrorException; WxOpenResult fastRegisterWeappSearch(String name, String legalPersonaWechat, String legalPersonaName) throws WxErrorException;
} }

View File

@ -126,11 +126,13 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
return this.getWxOpenConfigStorage().getComponentAccessToken(); return this.getWxOpenConfigStorage().getComponentAccessToken();
} }
private String post(String uri, String postData) throws WxErrorException { @Override
public String post(String uri, String postData) throws WxErrorException {
return post(uri, postData, "component_access_token"); return post(uri, postData, "component_access_token");
} }
private String post(String uri, String postData, String accessTokenKey) throws WxErrorException { @Override
public String post(String uri, String postData, String accessTokenKey) throws WxErrorException {
String componentAccessToken = getComponentAccessToken(false); String componentAccessToken = getComponentAccessToken(false);
String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + accessTokenKey + "=" + componentAccessToken; String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + accessTokenKey + "=" + componentAccessToken;
try { try {
@ -157,11 +159,13 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
} }
} }
private String get(String uri) throws WxErrorException { @Override
public String get(String uri) throws WxErrorException {
return get(uri, "component_access_token"); return get(uri, "component_access_token");
} }
private String get(String uri, String accessTokenKey) throws WxErrorException { @Override
public String get(String uri, String accessTokenKey) throws WxErrorException {
String componentAccessToken = getComponentAccessToken(false); String componentAccessToken = getComponentAccessToken(false);
String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + accessTokenKey + "=" + componentAccessToken; String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + accessTokenKey + "=" + componentAccessToken;
try { try {
@ -189,8 +193,8 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
} }
@Override @Override
public String getPreAuthUrl(String redirectURI) throws WxErrorException { public String getPreAuthUrl(String redirectUri) throws WxErrorException {
return getPreAuthUrl(redirectURI, null, null); return getPreAuthUrl(redirectUri, null, null);
} }
@Override @Override