mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-25 10:08:16 +08:00
抽取公众号部分微信请求URL到接口类中 #195
This commit is contained in:
parent
bb83ead12c
commit
879c1f5de1
@ -1,90 +0,0 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 公众号相关接口URL常量类
|
||||
* Created by Binary Wang on 2017-4-28.
|
||||
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
|
||||
* </pre>
|
||||
*/
|
||||
public class WxMpApiUrls {
|
||||
/**
|
||||
* 获取access_token
|
||||
*/
|
||||
public static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
|
||||
|
||||
/**
|
||||
* 获得jsapi_ticket
|
||||
*/
|
||||
public static final String GET_JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
|
||||
|
||||
/**
|
||||
* 上传群发用的图文消息
|
||||
*/
|
||||
public static final String MEDIA_UPLOAD_NEWS_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
|
||||
|
||||
/**
|
||||
* 上传群发用的视频
|
||||
*/
|
||||
public static final String MEDIA_UPLOAD_VIDEO_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo";
|
||||
|
||||
/**
|
||||
* 分组群发消息
|
||||
*/
|
||||
public static final String MESSAGE_MASS_SENDALL_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
|
||||
|
||||
/**
|
||||
* 按openId列表群发消息
|
||||
*/
|
||||
public static final String MESSAGE_MASS_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/send";
|
||||
|
||||
/**
|
||||
* 群发消息预览接口
|
||||
*/
|
||||
public static final String MESSAGE_MASS_PREVIEW_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
|
||||
|
||||
/**
|
||||
* 长链接转短链接接口
|
||||
*/
|
||||
public static final String SHORTURL_API_URL = "https://api.weixin.qq.com/cgi-bin/shorturl";
|
||||
|
||||
/**
|
||||
* 语义查询接口
|
||||
*/
|
||||
public static final String SEMANTIC_SEMPROXY_SEARCH_URL = "https://api.weixin.qq.com/semantic/semproxy/search";
|
||||
|
||||
/**
|
||||
* 用code换取oauth2的access token
|
||||
*/
|
||||
public static final String OAUTH2_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
|
||||
|
||||
/**
|
||||
* 刷新oauth2的access token
|
||||
*/
|
||||
public static final String OAUTH2_REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s";
|
||||
|
||||
/**
|
||||
* 用oauth2获取用户信息
|
||||
*/
|
||||
public static final String OAUTH2_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=%s";
|
||||
|
||||
/**
|
||||
* 验证oauth2的access token是否有效
|
||||
*/
|
||||
public static final String OAUTH2_VALIDATE_TOKEN_URL = "https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s";
|
||||
|
||||
/**
|
||||
* 获取微信服务器IP地址
|
||||
*/
|
||||
public static final String GET_CALLBACK_IP_URL = "https://api.weixin.qq.com/cgi-bin/getcallbackip";
|
||||
|
||||
/**
|
||||
* 第三方使用网站应用授权登录的url
|
||||
*/
|
||||
public static final String QRCONNECT_URL = "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
|
||||
|
||||
/**
|
||||
* oauth2授权的url连接
|
||||
*/
|
||||
public static final 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#wechat_redirect";
|
||||
}
|
@ -11,6 +11,70 @@ import me.chanjar.weixin.mp.bean.result.*;
|
||||
* 微信API的Service
|
||||
*/
|
||||
public interface WxMpService {
|
||||
/**
|
||||
* 获取access_token
|
||||
*/
|
||||
String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
|
||||
/**
|
||||
* 获得jsapi_ticket
|
||||
*/
|
||||
String GET_JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
|
||||
/**
|
||||
* 上传群发用的图文消息
|
||||
*/
|
||||
String MEDIA_UPLOAD_NEWS_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
|
||||
/**
|
||||
* 上传群发用的视频
|
||||
*/
|
||||
String MEDIA_UPLOAD_VIDEO_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo";
|
||||
/**
|
||||
* 分组群发消息
|
||||
*/
|
||||
String MESSAGE_MASS_SENDALL_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
|
||||
/**
|
||||
* 按openId列表群发消息
|
||||
*/
|
||||
String MESSAGE_MASS_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/send";
|
||||
/**
|
||||
* 群发消息预览接口
|
||||
*/
|
||||
String MESSAGE_MASS_PREVIEW_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
|
||||
/**
|
||||
* 长链接转短链接接口
|
||||
*/
|
||||
String SHORTURL_API_URL = "https://api.weixin.qq.com/cgi-bin/shorturl";
|
||||
/**
|
||||
* 语义查询接口
|
||||
*/
|
||||
String SEMANTIC_SEMPROXY_SEARCH_URL = "https://api.weixin.qq.com/semantic/semproxy/search";
|
||||
/**
|
||||
* 用code换取oauth2的access token
|
||||
*/
|
||||
String OAUTH2_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
|
||||
/**
|
||||
* 刷新oauth2的access token
|
||||
*/
|
||||
String OAUTH2_REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s";
|
||||
/**
|
||||
* 用oauth2获取用户信息
|
||||
*/
|
||||
String OAUTH2_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=%s";
|
||||
/**
|
||||
* 验证oauth2的access token是否有效
|
||||
*/
|
||||
String OAUTH2_VALIDATE_TOKEN_URL = "https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s";
|
||||
/**
|
||||
* 获取微信服务器IP地址
|
||||
*/
|
||||
String GET_CALLBACK_IP_URL = "https://api.weixin.qq.com/cgi-bin/getcallbackip";
|
||||
/**
|
||||
* 第三方使用网站应用授权登录的url
|
||||
*/
|
||||
String QRCONNECT_URL = "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
|
||||
/**
|
||||
* oauth2授权的url连接
|
||||
*/
|
||||
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#wechat_redirect";
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
@ -71,7 +71,7 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ
|
||||
}
|
||||
|
||||
if (this.getWxMpConfigStorage().isJsapiTicketExpired()) {
|
||||
String responseContent = execute(new SimpleGetRequestExecutor(), WxMpApiUrls.GET_JSAPI_TICKET_URL, null);
|
||||
String responseContent = execute(new SimpleGetRequestExecutor(), WxMpService.GET_JSAPI_TICKET_URL, null);
|
||||
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
|
||||
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
|
||||
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
|
||||
@ -107,31 +107,31 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ
|
||||
|
||||
@Override
|
||||
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
|
||||
String responseContent = this.post(WxMpApiUrls.MEDIA_UPLOAD_NEWS_URL, news.toJson());
|
||||
String responseContent = this.post(WxMpService.MEDIA_UPLOAD_NEWS_URL, news.toJson());
|
||||
return WxMpMassUploadResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException {
|
||||
String responseContent = this.post(WxMpApiUrls.MEDIA_UPLOAD_VIDEO_URL, video.toJson());
|
||||
String responseContent = this.post(WxMpService.MEDIA_UPLOAD_VIDEO_URL, video.toJson());
|
||||
return WxMpMassUploadResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMassSendResult massGroupMessageSend(WxMpMassTagMessage message) throws WxErrorException {
|
||||
String responseContent = this.post(WxMpApiUrls.MESSAGE_MASS_SENDALL_URL, message.toJson());
|
||||
String responseContent = this.post(WxMpService.MESSAGE_MASS_SENDALL_URL, message.toJson());
|
||||
return WxMpMassSendResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException {
|
||||
String responseContent = this.post(WxMpApiUrls.MESSAGE_MASS_SEND_URL, message.toJson());
|
||||
String responseContent = this.post(WxMpService.MESSAGE_MASS_SEND_URL, message.toJson());
|
||||
return WxMpMassSendResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception {
|
||||
String responseContent = this.post(WxMpApiUrls.MESSAGE_MASS_PREVIEW_URL, wxMpMassPreviewMessage.toJson());
|
||||
String responseContent = this.post(WxMpService.MESSAGE_MASS_PREVIEW_URL, wxMpMassPreviewMessage.toJson());
|
||||
return WxMpMassSendResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@ -140,26 +140,26 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ
|
||||
JsonObject o = new JsonObject();
|
||||
o.addProperty("action", "long2short");
|
||||
o.addProperty("long_url", long_url);
|
||||
String responseContent = this.post(WxMpApiUrls.SHORTURL_API_URL, o.toString());
|
||||
String responseContent = this.post(WxMpService.SHORTURL_API_URL, o.toString());
|
||||
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
|
||||
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException {
|
||||
String responseContent = this.post(WxMpApiUrls.SEMANTIC_SEMPROXY_SEARCH_URL, semanticQuery.toJson());
|
||||
String responseContent = this.post(WxMpService.SEMANTIC_SEMPROXY_SEARCH_URL, semanticQuery.toJson());
|
||||
return WxMpSemanticQueryResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
|
||||
return String.format(WxMpApiUrls.CONNECT_OAUTH2_AUTHORIZE_URL,
|
||||
return String.format(WxMpService.CONNECT_OAUTH2_AUTHORIZE_URL,
|
||||
this.getWxMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String buildQrConnectUrl(String redirectURI, String scope, String state) {
|
||||
return String.format(WxMpApiUrls.QRCONNECT_URL,
|
||||
return String.format(WxMpService.QRCONNECT_URL,
|
||||
this.getWxMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
|
||||
}
|
||||
|
||||
@ -175,13 +175,13 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ
|
||||
|
||||
@Override
|
||||
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
|
||||
String url = String.format(WxMpApiUrls.OAUTH2_ACCESS_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret(), code);
|
||||
String url = String.format(WxMpService.OAUTH2_ACCESS_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret(), code);
|
||||
return this.getOAuth2AccessToken(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
|
||||
String url = String.format(WxMpApiUrls.OAUTH2_REFRESH_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), refreshToken);
|
||||
String url = String.format(WxMpService.OAUTH2_REFRESH_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), refreshToken);
|
||||
return this.getOAuth2AccessToken(url);
|
||||
}
|
||||
|
||||
@ -191,7 +191,7 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ
|
||||
lang = "zh_CN";
|
||||
}
|
||||
|
||||
String url = String.format(WxMpApiUrls.OAUTH2_USERINFO_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId(), lang);
|
||||
String url = String.format(WxMpService.OAUTH2_USERINFO_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId(), lang);
|
||||
|
||||
try {
|
||||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
|
||||
@ -204,7 +204,7 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ
|
||||
|
||||
@Override
|
||||
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) {
|
||||
String url = String.format(WxMpApiUrls.OAUTH2_VALIDATE_TOKEN_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId());
|
||||
String url = String.format(WxMpService.OAUTH2_VALIDATE_TOKEN_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId());
|
||||
|
||||
try {
|
||||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
|
||||
@ -219,7 +219,7 @@ public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, Requ
|
||||
|
||||
@Override
|
||||
public String[] getCallbackIP() throws WxErrorException {
|
||||
String responseContent = this.get(WxMpApiUrls.GET_CALLBACK_IP_URL, null);
|
||||
String responseContent = this.get(WxMpService.GET_CALLBACK_IP_URL, null);
|
||||
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
|
||||
JsonArray ipList = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
|
||||
String[] ipArray = new String[ipList.size()];
|
||||
|
@ -5,8 +5,8 @@ import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.mp.api.WxMpApiUrls;
|
||||
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.api.impl.AbstractWxMpServiceImpl;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
@ -66,7 +66,7 @@ public class WxMpServiceImpl extends AbstractWxMpServiceImpl<CloseableHttpClient
|
||||
}
|
||||
|
||||
if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
|
||||
String url = String.format(WxMpApiUrls.GET_ACCESS_TOKEN_URL,
|
||||
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
||||
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
||||
try {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
|
@ -51,7 +51,7 @@ public class WxMpServiceImpl extends AbstractWxMpServiceImpl<HttpConnectionProvi
|
||||
}
|
||||
|
||||
if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
|
||||
String url = String.format(WxMpApiUrls.GET_ACCESS_TOKEN_URL,
|
||||
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
||||
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
||||
|
||||
HttpRequest request = HttpRequest.get(url);
|
||||
|
@ -4,8 +4,8 @@ import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
|
||||
import me.chanjar.weixin.mp.api.WxMpApiUrls;
|
||||
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.api.impl.AbstractWxMpServiceImpl;
|
||||
import okhttp3.*;
|
||||
|
||||
@ -37,7 +37,7 @@ public class WxMpServiceImpl extends AbstractWxMpServiceImpl<ConnectionPool, Okh
|
||||
}
|
||||
|
||||
if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
|
||||
String url = String.format(WxMpApiUrls.GET_ACCESS_TOKEN_URL,
|
||||
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
||||
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
||||
|
||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(httpClient);
|
||||
|
Loading…
Reference in New Issue
Block a user