1515import me .chanjar .weixin .mp .api .*;
1616import me .chanjar .weixin .mp .bean .*;
1717import me .chanjar .weixin .mp .bean .result .*;
18+ import org .apache .commons .lang3 .StringUtils ;
1819import org .slf4j .Logger ;
1920import org .slf4j .LoggerFactory ;
2021
@@ -70,8 +71,7 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
7071 }
7172
7273 if (this .getWxMpConfigStorage ().isJsapiTicketExpired ()) {
73- String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi" ;
74- String responseContent = execute (new SimpleGetRequestExecutor (), url , null );
74+ String responseContent = execute (new SimpleGetRequestExecutor (), WxMpApiUrls .GET_JSAPI_TICKET_URL , null );
7575 JsonElement tmpJsonElement = JSON_PARSER .parse (responseContent );
7676 JsonObject tmpJsonObject = tmpJsonElement .getAsJsonObject ();
7777 String jsapiTicket = tmpJsonObject .get ("ticket" ).getAsString ();
@@ -107,93 +107,66 @@ public String getAccessToken() throws WxErrorException {
107107
108108 @ Override
109109 public WxMpMassUploadResult massNewsUpload (WxMpMassNews news ) throws WxErrorException {
110- String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews" ;
111- String responseContent = this .post (url , news .toJson ());
110+ String responseContent = this .post (WxMpApiUrls .MEDIA_UPLOAD_NEWS_URL , news .toJson ());
112111 return WxMpMassUploadResult .fromJson (responseContent );
113112 }
114113
115114 @ Override
116115 public WxMpMassUploadResult massVideoUpload (WxMpMassVideo video ) throws WxErrorException {
117- String url = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo" ;
118- String responseContent = this .post (url , video .toJson ());
116+ String responseContent = this .post (WxMpApiUrls .MEDIA_UPLOAD_VIDEO_URL , video .toJson ());
119117 return WxMpMassUploadResult .fromJson (responseContent );
120118 }
121119
122120 @ Override
123121 public WxMpMassSendResult massGroupMessageSend (WxMpMassTagMessage message ) throws WxErrorException {
124- String url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall" ;
125- String responseContent = this .post (url , message .toJson ());
122+ String responseContent = this .post (WxMpApiUrls .MESSAGE_MASS_SENDALL_URL , message .toJson ());
126123 return WxMpMassSendResult .fromJson (responseContent );
127124 }
128125
129126 @ Override
130127 public WxMpMassSendResult massOpenIdsMessageSend (WxMpMassOpenIdsMessage message ) throws WxErrorException {
131- String url = "https://api.weixin.qq.com/cgi-bin/message/mass/send" ;
132- String responseContent = this .post (url , message .toJson ());
128+ String responseContent = this .post (WxMpApiUrls .MESSAGE_MASS_SEND_URL , message .toJson ());
133129 return WxMpMassSendResult .fromJson (responseContent );
134130 }
135131
136132 @ Override
137133 public WxMpMassSendResult massMessagePreview (WxMpMassPreviewMessage wxMpMassPreviewMessage ) throws Exception {
138- String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview" ;
139- String responseContent = this .post (url , wxMpMassPreviewMessage .toJson ());
134+ String responseContent = this .post (WxMpApiUrls .MESSAGE_MASS_PREVIEW_URL , wxMpMassPreviewMessage .toJson ());
140135 return WxMpMassSendResult .fromJson (responseContent );
141136 }
142137
143138 @ Override
144139 public String shortUrl (String long_url ) throws WxErrorException {
145- String url = "https://api.weixin.qq.com/cgi-bin/shorturl" ;
146140 JsonObject o = new JsonObject ();
147141 o .addProperty ("action" , "long2short" );
148142 o .addProperty ("long_url" , long_url );
149- String responseContent = this .post (url , o .toString ());
143+ String responseContent = this .post (WxMpApiUrls . SHORTURL_API_URL , o .toString ());
150144 JsonElement tmpJsonElement = JSON_PARSER .parse (responseContent );
151145 return tmpJsonElement .getAsJsonObject ().get ("short_url" ).getAsString ();
152146 }
153147
154148 @ Override
155149 public WxMpSemanticQueryResult semanticQuery (WxMpSemanticQuery semanticQuery ) throws WxErrorException {
156- String url = "https://api.weixin.qq.com/semantic/semproxy/search" ;
157- String responseContent = this .post (url , semanticQuery .toJson ());
150+ String responseContent = this .post (WxMpApiUrls .SEMANTIC_SEMPROXY_SEARCH_URL , semanticQuery .toJson ());
158151 return WxMpSemanticQueryResult .fromJson (responseContent );
159152 }
160153
161154 @ Override
162155 public String oauth2buildAuthorizationUrl (String redirectURI , String scope , String state ) {
163- StringBuilder url = new StringBuilder ();
164- url .append ("https://open.weixin.qq.com/connect/oauth2/authorize?" );
165- url .append ("appid=" ).append (this .getWxMpConfigStorage ().getAppId ());
166- url .append ("&redirect_uri=" ).append (URIUtil .encodeURIComponent (redirectURI ));
167- url .append ("&response_type=code" );
168- url .append ("&scope=" ).append (scope );
169- if (state != null ) {
170- url .append ("&state=" ).append (state );
171- }
172- url .append ("#wechat_redirect" );
173- return url .toString ();
156+ return String .format (WxMpApiUrls .CONNECT_OAUTH2_AUTHORIZE_URL ,
157+ this .getWxMpConfigStorage ().getAppId (), URIUtil .encodeURIComponent (redirectURI ), scope , StringUtils .trimToEmpty (state ));
174158 }
175159
176160 @ Override
177- public String buildQrConnectUrl (String redirectURI , String scope ,
178- String state ) {
179- StringBuilder url = new StringBuilder ();
180- url .append ("https://open.weixin.qq.com/connect/qrconnect?" );
181- url .append ("appid=" ).append (this .getWxMpConfigStorage ().getAppId ());
182- url .append ("&redirect_uri=" ).append (URIUtil .encodeURIComponent (redirectURI ));
183- url .append ("&response_type=code" );
184- url .append ("&scope=" ).append (scope );
185- if (state != null ) {
186- url .append ("&state=" ).append (state );
187- }
188-
189- url .append ("#wechat_redirect" );
190- return url .toString ();
161+ public String buildQrConnectUrl (String redirectURI , String scope , String state ) {
162+ return String .format (WxMpApiUrls .QRCONNECT_URL ,
163+ this .getWxMpConfigStorage ().getAppId (), URIUtil .encodeURIComponent (redirectURI ), scope , StringUtils .trimToEmpty (state ));
191164 }
192165
193- private WxMpOAuth2AccessToken getOAuth2AccessToken (StringBuilder url ) throws WxErrorException {
166+ private WxMpOAuth2AccessToken getOAuth2AccessToken (String url ) throws WxErrorException {
194167 try {
195168 RequestExecutor <String , String > executor = new SimpleGetRequestExecutor ();
196- String responseText = executor .execute (this , url . toString () , null );
169+ String responseText = executor .execute (this , url , null );
197170 return WxMpOAuth2AccessToken .fromJson (responseText );
198171 } catch (IOException e ) {
199172 throw new RuntimeException (e );
@@ -202,42 +175,27 @@ private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxE
202175
203176 @ Override
204177 public WxMpOAuth2AccessToken oauth2getAccessToken (String code ) throws WxErrorException {
205- StringBuilder url = new StringBuilder ();
206- url .append ("https://api.weixin.qq.com/sns/oauth2/access_token?" );
207- url .append ("appid=" ).append (this .getWxMpConfigStorage ().getAppId ());
208- url .append ("&secret=" ).append (this .getWxMpConfigStorage ().getSecret ());
209- url .append ("&code=" ).append (code );
210- url .append ("&grant_type=authorization_code" );
211-
178+ String url = String .format (WxMpApiUrls .OAUTH2_ACCESS_TOKEN_URL , this .getWxMpConfigStorage ().getAppId (), this .getWxMpConfigStorage ().getSecret (), code );
212179 return this .getOAuth2AccessToken (url );
213180 }
214181
215182 @ Override
216183 public WxMpOAuth2AccessToken oauth2refreshAccessToken (String refreshToken ) throws WxErrorException {
217- StringBuilder url = new StringBuilder ();
218- url .append ("https://api.weixin.qq.com/sns/oauth2/refresh_token?" );
219- url .append ("appid=" ).append (this .getWxMpConfigStorage ().getAppId ());
220- url .append ("&grant_type=refresh_token" );
221- url .append ("&refresh_token=" ).append (refreshToken );
222-
184+ String url = String .format (WxMpApiUrls .OAUTH2_REFRESH_TOKEN_URL , this .getWxMpConfigStorage ().getAppId (), refreshToken );
223185 return this .getOAuth2AccessToken (url );
224186 }
225187
226188 @ Override
227189 public WxMpUser oauth2getUserInfo (WxMpOAuth2AccessToken oAuth2AccessToken , String lang ) throws WxErrorException {
228- StringBuilder url = new StringBuilder ();
229- url .append ("https://api.weixin.qq.com/sns/userinfo?" );
230- url .append ("access_token=" ).append (oAuth2AccessToken .getAccessToken ());
231- url .append ("&openid=" ).append (oAuth2AccessToken .getOpenId ());
232190 if (lang == null ) {
233- url .append ("&lang=zh_CN" );
234- } else {
235- url .append ("&lang=" ).append (lang );
191+ lang = "zh_CN" ;
236192 }
237193
194+ String url = String .format (WxMpApiUrls .OAUTH2_USERINFO_URL , oAuth2AccessToken .getAccessToken (), oAuth2AccessToken .getOpenId (), lang );
195+
238196 try {
239197 RequestExecutor <String , String > executor = new SimpleGetRequestExecutor ();
240- String responseText = executor .execute (this , url . toString () , null );
198+ String responseText = executor .execute (this , url , null );
241199 return WxMpUser .fromJson (responseText );
242200 } catch (IOException e ) {
243201 throw new RuntimeException (e );
@@ -246,14 +204,11 @@ public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, Strin
246204
247205 @ Override
248206 public boolean oauth2validateAccessToken (WxMpOAuth2AccessToken oAuth2AccessToken ) {
249- StringBuilder url = new StringBuilder ();
250- url .append ("https://api.weixin.qq.com/sns/auth?" );
251- url .append ("access_token=" ).append (oAuth2AccessToken .getAccessToken ());
252- url .append ("&openid=" ).append (oAuth2AccessToken .getOpenId ());
207+ String url = String .format (WxMpApiUrls .OAUTH2_VALIDATE_TOKEN_URL , oAuth2AccessToken .getAccessToken (), oAuth2AccessToken .getOpenId ());
253208
254209 try {
255210 RequestExecutor <String , String > executor = new SimpleGetRequestExecutor ();
256- executor .execute (this , url . toString () , null );
211+ executor .execute (this , url , null );
257212 } catch (IOException e ) {
258213 throw new RuntimeException (e );
259214 } catch (WxErrorException e ) {
@@ -264,8 +219,7 @@ public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken
264219
265220 @ Override
266221 public String [] getCallbackIP () throws WxErrorException {
267- String url = "https://api.weixin.qq.com/cgi-bin/getcallbackip" ;
268- String responseContent = get (url , null );
222+ String responseContent = this .get (WxMpApiUrls .GET_CALLBACK_IP_URL , null );
269223 JsonElement tmpJsonElement = JSON_PARSER .parse (responseContent );
270224 JsonArray ipList = tmpJsonElement .getAsJsonObject ().get ("ip_list" ).getAsJsonArray ();
271225 String [] ipArray = new String [ipList .size ()];
0 commit comments