Skip to content

feat: 新增小程序插件管理相关接口 #1129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ public interface WxMaService {
*/
WxMaSecCheckService getSecCheckService();

/**
* 返回插件相关接口服务对象.
*
* @return WxMaPluginService
*/
WxMaPluginService getPluginService();

/**
* 初始化http请求对象.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -124,7 +125,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
}
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());

return this.getWxMaConfig().getAccessToken();
} finally {
httpGet.releaseConnection();
Expand Down Expand Up @@ -359,4 +360,9 @@ public WxMaRunService getRunService() {
public WxMaSecCheckService getSecCheckService() {
return this.secCheckService;
}

@Override
public WxMaPluginService getPluginService() {
return this.pluginService;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}