@@ -0,0 +1,50 @@ | |||
package cn.binarywang.wx.miniapp.api; | |||
import cn.binarywang.wx.miniapp.bean.WxMaPluginListResult; | |||
import me.chanjar.weixin.common.error.WxErrorException; | |||
/** | |||
* 小程序插件管理 API | |||
* <p> | |||
* 详情请见:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/plugin-management/pluginManager.applyPlugin.html | |||
* 或者:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Mini_Programs/Plug-ins_Management.html | |||
*/ | |||
public interface WxMaPluginService { | |||
String PLUGIN_URL = "https://api.weixin.qq.com/wxa/plugin"; | |||
/** | |||
* 向插件开发者发起使用插件的申请 | |||
* | |||
* @param pluginAppId 插件 appId | |||
* @param reason 申请使用理由 | |||
* @throws WxErrorException 异常 | |||
*/ | |||
void applyPlugin(String pluginAppId, String reason) throws WxErrorException; | |||
/** | |||
* 查询已添加的插件 | |||
* | |||
* @return | |||
* @throws WxErrorException | |||
*/ | |||
WxMaPluginListResult getPluginList() throws WxErrorException; | |||
/** | |||
* 删除已添加的插件 | |||
* | |||
* @param pluginAppId 插件 appId | |||
* @throws WxErrorException | |||
*/ | |||
void unbindPlugin(String pluginAppId) throws WxErrorException; | |||
/** | |||
* 快速更新插件版本号(第三方平台代小程序管理插件) | |||
* | |||
* @param pluginAppId 插件 appid | |||
* @param userVersion 升级至版本号,要求此插件版本支持快速更新 | |||
* @throws WxErrorException | |||
*/ | |||
void updatePlugin(String pluginAppId, String userVersion) throws WxErrorException; | |||
} |
@@ -207,6 +207,13 @@ public interface WxMaService { | |||
*/ | |||
WxMaSecCheckService getSecCheckService(); | |||
/** | |||
* 返回插件相关接口服务对象. | |||
* | |||
* @return WxMaPluginService | |||
*/ | |||
WxMaPluginService getPluginService(); | |||
/** | |||
* 初始化http请求对象. | |||
*/ | |||
@@ -0,0 +1,57 @@ | |||
package cn.binarywang.wx.miniapp.api.impl; | |||
import cn.binarywang.wx.miniapp.api.WxMaPluginService; | |||
import cn.binarywang.wx.miniapp.api.WxMaService; | |||
import cn.binarywang.wx.miniapp.bean.WxMaPluginListResult; | |||
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | |||
import me.chanjar.weixin.common.error.WxErrorException; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
public class WxMaPluginServiceImpl implements WxMaPluginService { | |||
private WxMaService wxMaService; | |||
public WxMaPluginServiceImpl(WxMaService wxMaService) { | |||
this.wxMaService = wxMaService; | |||
} | |||
@Override | |||
public void applyPlugin(String pluginAppId, String reason) throws WxErrorException { | |||
Map<String, String> params = new HashMap<>(); | |||
params.put("action", "apply"); | |||
params.put("plugin_appid", pluginAppId); | |||
params.put("reason", reason); | |||
this.wxMaService.post(PLUGIN_URL, WxMaGsonBuilder.create().toJson(params)); | |||
} | |||
@Override | |||
public WxMaPluginListResult getPluginList() throws WxErrorException { | |||
Map<String, String> params = new HashMap<>(); | |||
params.put("action", "list"); | |||
String responseContent = this.wxMaService.post(PLUGIN_URL, WxMaGsonBuilder.create().toJson(params)); | |||
return WxMaPluginListResult.fromJson(responseContent); | |||
} | |||
@Override | |||
public void unbindPlugin(String pluginAppId) throws WxErrorException { | |||
Map<String, String> params = new HashMap<>(); | |||
params.put("action", "unbind"); | |||
params.put("plugin_appid", pluginAppId); | |||
this.wxMaService.post(PLUGIN_URL, WxMaGsonBuilder.create().toJson(params)); | |||
} | |||
@Override | |||
public void updatePlugin(String pluginAppId, String userVersion) throws WxErrorException { | |||
Map<String, String> params = new HashMap<>(); | |||
params.put("action", "update"); | |||
params.put("plugin_appid", pluginAppId); | |||
params.put("user_version", userVersion); | |||
this.wxMaService.post(PLUGIN_URL, WxMaGsonBuilder.create().toJson(params)); | |||
} | |||
} |
@@ -53,6 +53,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||
private WxMaShareService shareService = new WxMaShareServiceImpl(this); | |||
private WxMaRunService runService = new WxMaRunServiceImpl(this); | |||
private WxMaSecCheckService secCheckService = new WxMaSecCheckServiceImpl(this); | |||
private WxMaPluginService pluginService = new WxMaPluginServiceImpl(this); | |||
private int retrySleepMillis = 1000; | |||
private int maxRetryTimes = 5; | |||
@@ -124,7 +125,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||
} | |||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||
this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||
return this.getWxMaConfig().getAccessToken(); | |||
} finally { | |||
httpGet.releaseConnection(); | |||
@@ -359,4 +360,9 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||
public WxMaSecCheckService getSecCheckService() { | |||
return this.secCheckService; | |||
} | |||
@Override | |||
public WxMaPluginService getPluginService() { | |||
return this.pluginService; | |||
} | |||
} |
@@ -0,0 +1,36 @@ | |||
package cn.binarywang.wx.miniapp.bean; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Data; | |||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
import java.io.Serializable; | |||
import java.util.List; | |||
@Data | |||
public class WxMaPluginListResult implements Serializable { | |||
private static final long serialVersionUID = -5898572369543593656L; | |||
@SerializedName("plugin_list") | |||
private List<PluginInfo> pluginList; | |||
public static WxMaPluginListResult fromJson(String json) { | |||
return WxGsonBuilder.create().fromJson(json, WxMaPluginListResult.class); | |||
} | |||
@Data | |||
public static class PluginInfo { | |||
@SerializedName("appid") | |||
private String appId; | |||
private String status; | |||
@SerializedName("nickname") | |||
private String nickName; | |||
@SerializedName("headimgurl") | |||
private String headImgUrl; | |||
} | |||
} |
@@ -0,0 +1,39 @@ | |||
package cn.binarywang.wx.miniapp.api.impl; | |||
import cn.binarywang.wx.miniapp.api.WxMaService; | |||
import cn.binarywang.wx.miniapp.bean.WxMaPluginListResult; | |||
import cn.binarywang.wx.miniapp.test.ApiTestModule; | |||
import com.google.inject.Inject; | |||
import org.testng.annotations.Guice; | |||
import org.testng.annotations.Test; | |||
import static org.testng.Assert.assertNotNull; | |||
@Test | |||
@Guice(modules = ApiTestModule.class) | |||
public class WxMaPluginServiceImplTest { | |||
@Inject | |||
private WxMaService wxService; | |||
@Test | |||
public void testApplyPlugin() throws Exception { | |||
this.wxService.getPluginService().applyPlugin("wx4418e3e031e551be", null); | |||
} | |||
@Test | |||
public void testGetPluginList() throws Exception { | |||
WxMaPluginListResult result = this.wxService.getPluginService().getPluginList(); | |||
assertNotNull(result); | |||
System.out.println(result.toString()); | |||
} | |||
@Test | |||
public void testUnbindPlugin() throws Exception { | |||
this.wxService.getPluginService().unbindPlugin("wx4418e3e031e551be"); | |||
} | |||
@Test | |||
public void testUpdatePlugin() throws Exception { | |||
this.wxService.getPluginService().updatePlugin("wx4418e3e031e551be", "2.0.2"); | |||
} | |||
} |