Skip to content

Commit 04fb35d

Browse files
authored
🐛 #1604 修复微信开放平台帐号管理相关接口,使用指定appId的access_token
* fix:修改微信开放平台帐号管理相关接口,使用指定appId的access_token,非开放平台自身的component_access_token
1 parent 4d14409 commit 04fb35d

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,5 +394,17 @@ public static class NetCheckArgs {
394394
public static final String OPERATORDEFAULT = "DEFAULT";
395395
}
396396

397-
397+
/**
398+
* appId 类型
399+
*/
400+
public static class AppIdType {
401+
/**
402+
* 公众号appId类型
403+
*/
404+
public static final String MP_TYPE = "mp";
405+
/**
406+
* 小程序appId类型
407+
*/
408+
public static final String MINI_TYPE = "mini";
409+
}
398410
}

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,43 +415,47 @@ public interface WxOpenComponentService {
415415
* 创建 开放平台帐号并绑定公众号/小程序.
416416
* https://api.weixin.qq.com/cgi-bin/open/create
417417
*
418-
* @param appId 公众号/小程序的appId
418+
* @param appId 公众号/小程序的appId
419+
* @param appIdType appId类型 me.chanjar.weixin.common.api.WxConsts.AppIdType mp-公众号 mini-小程序
419420
* @return . wx open create result
420421
* @throws WxErrorException .
421422
*/
422-
WxOpenCreateResult createOpenAccount(String appId) throws WxErrorException;
423+
WxOpenCreateResult createOpenAccount(String appId, String appIdType) throws WxErrorException;
423424

424425
/**
425426
* https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/account/bind.html
426427
* 将公众号/小程序绑定到开放平台帐号下
427428
*
428429
* @param appId 公众号/小程序的appId
430+
* @param appIdType appId类型 me.chanjar.weixin.common.api.WxConsts.AppIdType mp-公众号 mini-小程序
429431
* @param openAppid 开放平台帐号 appid,由创建开发平台帐号接口返回
430432
* @return the boolean
431433
* @throws WxErrorException the wx error exception
432434
*/
433-
Boolean bindOpenAccount(String appId, String openAppid) throws WxErrorException;
435+
Boolean bindOpenAccount(String appId, String appIdType, String openAppid) throws WxErrorException;
434436

435437
/**
436438
* https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/account/unbind.html
437439
* 将公众号/小程序从开放平台帐号下解绑
438440
*
439441
* @param appId 公众号/小程序的appId
442+
* @param appIdType appId类型 me.chanjar.weixin.common.api.WxConsts.AppIdType mp-公众号 mini-小程序
440443
* @param openAppid 开放平台帐号 appid,由创建开发平台帐号接口返回
441444
* @return the boolean
442445
* @throws WxErrorException the wx error exception
443446
*/
444-
Boolean unbindOpenAccount(String appId, String openAppid) throws WxErrorException;
447+
Boolean unbindOpenAccount(String appId, String appIdType, String openAppid) throws WxErrorException;
445448

446449
/**
447450
* https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/account/get.html
448451
* 获取公众号/小程序所绑定的开放平台帐号
449452
*
450-
* @param appId 公众号/小程序的appId
453+
* @param appId 公众号/小程序的appId
454+
* @param appIdType appId类型 me.chanjar.weixin.common.api.WxConsts.AppIdType mp-公众号 mini-小程序
451455
* @return 开放平台帐号 appid,由创建开发平台帐号接口返回
452456
* @throws WxErrorException the wx error exception
453457
*/
454-
WxOpenGetResult getOpenAccount(String appId) throws WxErrorException;
458+
WxOpenGetResult getOpenAccount(String appId, String appIdType) throws WxErrorException;
455459

456460
/**
457461
* https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=21538208049W8uwq&token=&lang=zh_CN

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.reflect.TypeToken;
66
import lombok.AllArgsConstructor;
77
import lombok.extern.slf4j.Slf4j;
8+
import me.chanjar.weixin.common.api.WxConsts;
89
import me.chanjar.weixin.common.error.WxError;
910
import me.chanjar.weixin.common.error.WxErrorException;
1011
import me.chanjar.weixin.common.util.crypto.SHA1;
@@ -474,47 +475,71 @@ public void deleteTemplate(long templateId) throws WxErrorException {
474475
post(DELETE_TEMPLATE_URL, param.toString(), "access_token");
475476
}
476477

478+
/**
479+
* 微信开放平台帐号管理统一请求入口
480+
*
481+
* @param appId 操作appId 小程序/公众号
482+
* @param appIdType 操作类型 小程序/公众号
483+
* @param requestUrl 请求地址
484+
* @param param 请求参数
485+
* @return 请求结果
486+
* @throws WxErrorException
487+
*/
488+
private String openAccountServicePost(String appId, String appIdType, String requestUrl, JsonObject param) throws WxErrorException {
489+
String result = "";
490+
switch (appIdType) {
491+
case WxConsts.AppIdType.MP_TYPE:
492+
WxMpService wxMpService = this.getWxMpServiceByAppid(appId);
493+
result = wxMpService.post(requestUrl, param.toString());
494+
return result;
495+
case WxConsts.AppIdType.MINI_TYPE:
496+
WxOpenMaService maService = this.getWxMaServiceByAppid(appId);
497+
result = maService.post(requestUrl, param.toString());
498+
return result;
499+
default:
500+
throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("appIdType类型异常").build());
501+
}
502+
}
503+
477504
@Override
478-
public WxOpenCreateResult createOpenAccount(String appId) throws WxErrorException {
505+
public WxOpenCreateResult createOpenAccount(String appId, String appIdType) throws WxErrorException {
479506
JsonObject param = new JsonObject();
480507
param.addProperty("appid", appId);
481508

482-
String json = post(CREATE_OPEN_URL, param.toString(), "access_token");
509+
String json = openAccountServicePost(appId, appIdType, CREATE_OPEN_URL, param);
483510

484511
return WxOpenCreateResult.fromJson(json);
485512
}
486513

487514

488515
@Override
489-
public Boolean bindOpenAccount(String appId, String openAppid) throws WxErrorException {
516+
public Boolean bindOpenAccount(String appId, String appIdType, String openAppid) throws WxErrorException {
490517
JsonObject param = new JsonObject();
491518
param.addProperty("appid", appId);
492519
param.addProperty("open_appid", openAppid);
493520

494-
String json = post(BIND_OPEN_URL, param.toString(), "access_token");
495-
521+
String json = openAccountServicePost(appId, appIdType, BIND_OPEN_URL, param);
496522
return WxOpenResult.fromJson(json).isSuccess();
497523
}
498524

499525

500526
@Override
501-
public Boolean unbindOpenAccount(String appId, String openAppid) throws WxErrorException {
527+
public Boolean unbindOpenAccount(String appId, String appIdType, String openAppid) throws WxErrorException {
502528
JsonObject param = new JsonObject();
503529
param.addProperty("appid", appId);
504530
param.addProperty("open_appid", openAppid);
505531

506-
String json = post(UNBIND_OPEN_URL, param.toString(), "access_token");
507-
532+
String json = openAccountServicePost(appId, appIdType, UNBIND_OPEN_URL, param);
508533
return WxOpenResult.fromJson(json).isSuccess();
509534
}
510535

511536

512537
@Override
513-
public WxOpenGetResult getOpenAccount(String appId) throws WxErrorException {
538+
public WxOpenGetResult getOpenAccount(String appId, String appIdType) throws WxErrorException {
514539
JsonObject param = new JsonObject();
515540
param.addProperty("appid", appId);
516541

517-
String json = post(GET_OPEN_URL, param.toString(), "access_token");
542+
String json = openAccountServicePost(appId, appIdType, GET_OPEN_URL, param);
518543
return WxOpenGetResult.fromJson(json);
519544
}
520545

0 commit comments

Comments
 (0)