Skip to content

小程序:增加代码管理相关 API #562

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 4 commits into from
Apr 26, 2018
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,140 @@
package cn.binarywang.wx.miniapp.api;

import cn.binarywang.wx.miniapp.bean.code.WxMaCategory;
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeAuditStatus;
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeCommitRequest;
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeSubmitAuditRequest;
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeVersionDistribution;
import me.chanjar.weixin.common.exception.WxErrorException;

import java.util.List;

/**
* 小程序代码管理相关 API(大部分只能是第三方平台调用)
* 文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1489140610_Uavc4&token=&lang=zh_CN
*
* @author <a href="https://github.com/charmingoh">Charming</a>
* @since 2018-04-26 19:43
*/
public interface WxMaCodeService {
/**
* 为授权的小程序帐号上传小程序代码
*/
String COMMIT_URL = "https://api.weixin.qq.com/wxa/commit";
String GET_QRCODE_URL = "https://api.weixin.qq.com/wxa/get_qrcode";
String GET_CATEGORY_URL = "https://api.weixin.qq.com/wxa/get_category";
String GET_PAGE_URL = "https://api.weixin.qq.com/wxa/get_page";
String SUBMIT_AUDIT_URL = "https://api.weixin.qq.com/wxa/submit_audit";
String GET_AUDIT_STATUS_URL = "https://api.weixin.qq.com/wxa/get_auditstatus";
String GET_LATEST_AUDIT_STATUS_URL = "https://api.weixin.qq.com/wxa/get_latest_auditstatus";
String RELEASE_URL = "https://api.weixin.qq.com/wxa/release";
String CHANGE_VISIT_STATUS_URL = "https://api.weixin.qq.com/wxa/change_visitstatus";
String REVERT_CODE_RELEASE_URL = "https://api.weixin.qq.com/wxa/revertcoderelease";
String GET_SUPPORT_VERSION_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/getweappsupportversion";
String SET_SUPPORT_VERSION_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/setweappsupportversion";
String UNDO_CODE_AUDIT_URL = "https://api.weixin.qq.com/wxa/undocodeaudit";

/**
* 为授权的小程序帐号上传小程序代码(仅仅支持第三方开放平台)
*
* @param commitRequest 参数
* @throws WxErrorException 上传失败时抛出,具体错误码请看类注释文档
*/
void commit(WxMaCodeCommitRequest commitRequest) throws WxErrorException;

/**
* 获取体验小程序的体验二维码
*
* @return 二维码 bytes
* @throws WxErrorException 上传失败时抛出,具体错误码请看类注释文档
*/
byte[] getQrCode() throws WxErrorException;

/**
* 获取授权小程序帐号的可选类目
*
* @return List<WxMaCategory>
* @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
*/
List<WxMaCategory> getCategory() throws WxErrorException;

/**
* 获取小程序的第三方提交代码的页面配置(仅供第三方开发者代小程序调用)
*
* @return page_list 页面配置列表
* @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
*/
List<String> getPage() throws WxErrorException;

/**
* 将第三方提交的代码包提交审核(仅供第三方开发者代小程序调用)
*
* @param auditRequest 提交审核参数
* @return 审核编号
* @throws WxErrorException 提交失败时返回,具体错误码请看此接口的注释文档
*/
long submitAudit(WxMaCodeSubmitAuditRequest auditRequest) throws WxErrorException;

/**
* 查询某个指定版本的审核状态(仅供第三方代小程序调用)
*
* @param auditId 提交审核时获得的审核id
* @return 审核状态
* @throws WxErrorException 查询失败时返回,具体错误码请看此接口的注释文档
*/
WxMaCodeAuditStatus getAuditStatus(long auditId) throws WxErrorException;

/**
* 查询最新一次提交的审核状态(仅供第三方代小程序调用)
*
* @return 审核状态
* @throws WxErrorException 查询失败时返回,具体错误码请看此接口的注释文档
*/
WxMaCodeAuditStatus getLatestAuditStatus() throws WxErrorException;

/**
* 发布已通过审核的小程序(仅供第三方代小程序调用)
*
* @throws WxErrorException 发布失败时抛出,具体错误码请看此接口的注释文档
*/
void release() throws WxErrorException;

/**
* 修改小程序线上代码的可见状态(仅供第三方代小程序调用)
*
* @param action 设置可访问状态,发布后默认可访问,close为不可见,open为可见
* @throws WxErrorException 发布失败时抛出,具体错误码请看此接口的注释文档
*/
void changeVisitStatus(String action) throws WxErrorException;

/**
* 小程序版本回退(仅供第三方代小程序调用)
*
* @throws WxErrorException 失败时抛出,具体错误码请看此接口的注释文档
*/
void revertCodeRelease() throws WxErrorException;

/**
* 查询当前设置的最低基础库版本及各版本用户占比 (仅供第三方代小程序调用)
*
* @return 小程序版本分布信息
* @throws WxErrorException 失败时抛出,具体错误码请看此接口的注释文档
*/
WxMaCodeVersionDistribution getSupportVersion() throws WxErrorException;

/**
* 设置最低基础库版本(仅供第三方代小程序调用)
*
* @param version 版本
* @throws WxErrorException 失败时抛出,具体错误码请看此接口的注释文档
*/
void setSupportVersion(String version) throws WxErrorException;

/**
* 小程序审核撤回
* 单个帐号每天审核撤回次数最多不超过1次,一个月不超过10次
*
* @throws WxErrorException 失败时抛出,具体错误码请看此接口的注释文档
*/
void undoCodeAudit() throws WxErrorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ public interface WxMaService {
*/
WxMaTemplateService getTemplateService();

/**
* 返回代码操作相关的 API
*
* @return WxMaCodeService
*/
WxMaCodeService getCodeService();

/**
* 初始化http请求对象.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package cn.binarywang.wx.miniapp.api.impl;

import cn.binarywang.wx.miniapp.api.WxMaCodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.code.WxMaCategory;
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeAuditStatus;
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeCommitRequest;
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeSubmitAuditRequest;
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeVersionDistribution;
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.json.GsonHelper;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
* @author <a href="https://github.com/charmingoh">Charming</a>
* @since 2018-04-26 20:00
*/
public class WxMaCodeServiceImpl implements WxMaCodeService {
private static final JsonParser JSON_PARSER = new JsonParser();
private WxMaService wxMaService;

public WxMaCodeServiceImpl(WxMaService wxMaService) {
this.wxMaService = wxMaService;
}

@Override
public void commit(WxMaCodeCommitRequest commitRequest) throws WxErrorException {
this.wxMaService.post(COMMIT_URL, commitRequest.toJson());
}

@Override
public byte[] getQrCode() throws WxErrorException {
String appId = this.wxMaService.getWxMaConfig().getAppid();
Path qrCodeFilePath = null;
try {
RequestExecutor<File, String> executor = BaseMediaDownloadRequestExecutor
.create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("weixin-java-tools-ma-" + appId).toFile());
qrCodeFilePath = this.wxMaService.execute(executor, GET_QRCODE_URL, null).toPath();
return Files.readAllBytes(qrCodeFilePath);
} catch (IOException e) {
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e);
} finally {
if (qrCodeFilePath != null) {
try {
// 及时删除二维码文件,避免积压过多缓存文件
Files.delete(qrCodeFilePath);
} catch (Exception ignored) {
}
}
}
}

@Override
public List<WxMaCategory> getCategory() throws WxErrorException {
String responseContent = this.wxMaService.get(GET_CATEGORY_URL, null);
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
boolean hasCategoryList = jsonObject.has("category_list");
if (hasCategoryList) {
return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("category_list"),
new TypeToken<List<WxMaCategory>>() {
}.getType());
} else {
return null;
}
}

@Override
public List<String> getPage() throws WxErrorException {
String responseContent = this.wxMaService.get(GET_PAGE_URL, null);
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
boolean hasPageList = jsonObject.has("page_list");
if (hasPageList) {
return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("page_list"),
new TypeToken<List<String>>() {
}.getType());
} else {
return null;
}
}

@Override
public long submitAudit(WxMaCodeSubmitAuditRequest auditRequest) throws WxErrorException {
String responseContent = this.wxMaService.post(SUBMIT_AUDIT_URL, auditRequest.toJson());
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
return GsonHelper.getLong(jsonObject, "auditid");
}

@Override
public WxMaCodeAuditStatus getAuditStatus(long auditId) throws WxErrorException {
JsonObject param = new JsonObject();
param.addProperty("auditid", auditId);
String responseContent = this.wxMaService.post(GET_AUDIT_STATUS_URL, param.toString());
return WxMaCodeAuditStatus.fromJson(responseContent);
}

@Override
public WxMaCodeAuditStatus getLatestAuditStatus() throws WxErrorException {
String responseContent = this.wxMaService.get(GET_LATEST_AUDIT_STATUS_URL, null);
return WxMaCodeAuditStatus.fromJson(responseContent);
}

@Override
public void release() throws WxErrorException {
this.wxMaService.post(RELEASE_URL, "{}");
}

@Override
public void changeVisitStatus(String action) throws WxErrorException {
JsonObject param = new JsonObject();
param.addProperty("action", action);
this.wxMaService.post(CHANGE_VISIT_STATUS_URL, param.toString());
}

@Override
public void revertCodeRelease() throws WxErrorException {
this.wxMaService.get(REVERT_CODE_RELEASE_URL, null);
}

@Override
public WxMaCodeVersionDistribution getSupportVersion() throws WxErrorException {
String responseContent = this.wxMaService.post(GET_SUPPORT_VERSION_URL, "{}");
return WxMaCodeVersionDistribution.fromJson(responseContent);
}

@Override
public void setSupportVersion(String version) throws WxErrorException {
JsonObject param = new JsonObject();
param.addProperty("version", version);
this.wxMaService.post(SET_SUPPORT_VERSION_URL, param.toString());
}

@Override
public void undoCodeAudit() throws WxErrorException {
this.wxMaService.get(UNDO_CODE_AUDIT_URL, null);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
package cn.binarywang.wx.miniapp.api.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.binarywang.wx.miniapp.api.WxMaCodeService;
import cn.binarywang.wx.miniapp.api.WxMaMediaService;
import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
Expand All @@ -35,6 +22,19 @@
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;

/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
Expand All @@ -51,6 +51,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
private WxMaUserService userService = new WxMaUserServiceImpl(this);
private WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this);
private WxMaTemplateService templateService = new WxMaTemplateServiceImpl(this);
private WxMaCodeService codeService = new WxMaCodeServiceImpl(this);

private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
Expand Down Expand Up @@ -290,4 +291,9 @@ public WxMaQrcodeService getQrcodeService() {
public WxMaTemplateService getTemplateService() {
return this.templateService;
}

@Override
public WxMaCodeService getCodeService() {
return this.codeService;
}
}
Loading