Skip to content

Commit 0247486

Browse files
charmingohbinarywang
authored andcommitted
#562 小程序增加代码管理相关 API
* 微信开放平台:1. WxOpenInRedisConfigStorage 支持 JedisPool/JedisSentinelPool 等 Pool<Jedis> 的子类;2. WxOpenInRedisConfigStorage 增加 keyPrefix 以支持可配置的前缀; * 微信开放平台:增加小程序代码模板库管理 * 小程序:增加代码管理相关 API
1 parent 75c038d commit 0247486

18 files changed

+1027
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import cn.binarywang.wx.miniapp.bean.code.WxMaCategory;
4+
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeAuditStatus;
5+
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeCommitRequest;
6+
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeSubmitAuditRequest;
7+
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeVersionDistribution;
8+
import me.chanjar.weixin.common.exception.WxErrorException;
9+
10+
import java.util.List;
11+
12+
/**
13+
* 小程序代码管理相关 API(大部分只能是第三方平台调用)
14+
* 文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1489140610_Uavc4&token=&lang=zh_CN
15+
*
16+
* @author <a href="https://github.com/charmingoh">Charming</a>
17+
* @since 2018-04-26 19:43
18+
*/
19+
public interface WxMaCodeService {
20+
/**
21+
* 为授权的小程序帐号上传小程序代码
22+
*/
23+
String COMMIT_URL = "https://api.weixin.qq.com/wxa/commit";
24+
String GET_QRCODE_URL = "https://api.weixin.qq.com/wxa/get_qrcode";
25+
String GET_CATEGORY_URL = "https://api.weixin.qq.com/wxa/get_category";
26+
String GET_PAGE_URL = "https://api.weixin.qq.com/wxa/get_page";
27+
String SUBMIT_AUDIT_URL = "https://api.weixin.qq.com/wxa/submit_audit";
28+
String GET_AUDIT_STATUS_URL = "https://api.weixin.qq.com/wxa/get_auditstatus";
29+
String GET_LATEST_AUDIT_STATUS_URL = "https://api.weixin.qq.com/wxa/get_latest_auditstatus";
30+
String RELEASE_URL = "https://api.weixin.qq.com/wxa/release";
31+
String CHANGE_VISIT_STATUS_URL = "https://api.weixin.qq.com/wxa/change_visitstatus";
32+
String REVERT_CODE_RELEASE_URL = "https://api.weixin.qq.com/wxa/revertcoderelease";
33+
String GET_SUPPORT_VERSION_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/getweappsupportversion";
34+
String SET_SUPPORT_VERSION_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/setweappsupportversion";
35+
String UNDO_CODE_AUDIT_URL = "https://api.weixin.qq.com/wxa/undocodeaudit";
36+
37+
/**
38+
* 为授权的小程序帐号上传小程序代码(仅仅支持第三方开放平台)
39+
*
40+
* @param commitRequest 参数
41+
* @throws WxErrorException 上传失败时抛出,具体错误码请看类注释文档
42+
*/
43+
void commit(WxMaCodeCommitRequest commitRequest) throws WxErrorException;
44+
45+
/**
46+
* 获取体验小程序的体验二维码
47+
*
48+
* @return 二维码 bytes
49+
* @throws WxErrorException 上传失败时抛出,具体错误码请看类注释文档
50+
*/
51+
byte[] getQrCode() throws WxErrorException;
52+
53+
/**
54+
* 获取授权小程序帐号的可选类目
55+
*
56+
* @return List<WxMaCategory>
57+
* @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
58+
*/
59+
List<WxMaCategory> getCategory() throws WxErrorException;
60+
61+
/**
62+
* 获取小程序的第三方提交代码的页面配置(仅供第三方开发者代小程序调用)
63+
*
64+
* @return page_list 页面配置列表
65+
* @throws WxErrorException 获取失败时返回,具体错误码请看此接口的注释文档
66+
*/
67+
List<String> getPage() throws WxErrorException;
68+
69+
/**
70+
* 将第三方提交的代码包提交审核(仅供第三方开发者代小程序调用)
71+
*
72+
* @param auditRequest 提交审核参数
73+
* @return 审核编号
74+
* @throws WxErrorException 提交失败时返回,具体错误码请看此接口的注释文档
75+
*/
76+
long submitAudit(WxMaCodeSubmitAuditRequest auditRequest) throws WxErrorException;
77+
78+
/**
79+
* 查询某个指定版本的审核状态(仅供第三方代小程序调用)
80+
*
81+
* @param auditId 提交审核时获得的审核id
82+
* @return 审核状态
83+
* @throws WxErrorException 查询失败时返回,具体错误码请看此接口的注释文档
84+
*/
85+
WxMaCodeAuditStatus getAuditStatus(long auditId) throws WxErrorException;
86+
87+
/**
88+
* 查询最新一次提交的审核状态(仅供第三方代小程序调用)
89+
*
90+
* @return 审核状态
91+
* @throws WxErrorException 查询失败时返回,具体错误码请看此接口的注释文档
92+
*/
93+
WxMaCodeAuditStatus getLatestAuditStatus() throws WxErrorException;
94+
95+
/**
96+
* 发布已通过审核的小程序(仅供第三方代小程序调用)
97+
*
98+
* @throws WxErrorException 发布失败时抛出,具体错误码请看此接口的注释文档
99+
*/
100+
void release() throws WxErrorException;
101+
102+
/**
103+
* 修改小程序线上代码的可见状态(仅供第三方代小程序调用)
104+
*
105+
* @param action 设置可访问状态,发布后默认可访问,close为不可见,open为可见
106+
* @throws WxErrorException 发布失败时抛出,具体错误码请看此接口的注释文档
107+
*/
108+
void changeVisitStatus(String action) throws WxErrorException;
109+
110+
/**
111+
* 小程序版本回退(仅供第三方代小程序调用)
112+
*
113+
* @throws WxErrorException 失败时抛出,具体错误码请看此接口的注释文档
114+
*/
115+
void revertCodeRelease() throws WxErrorException;
116+
117+
/**
118+
* 查询当前设置的最低基础库版本及各版本用户占比 (仅供第三方代小程序调用)
119+
*
120+
* @return 小程序版本分布信息
121+
* @throws WxErrorException 失败时抛出,具体错误码请看此接口的注释文档
122+
*/
123+
WxMaCodeVersionDistribution getSupportVersion() throws WxErrorException;
124+
125+
/**
126+
* 设置最低基础库版本(仅供第三方代小程序调用)
127+
*
128+
* @param version 版本
129+
* @throws WxErrorException 失败时抛出,具体错误码请看此接口的注释文档
130+
*/
131+
void setSupportVersion(String version) throws WxErrorException;
132+
133+
/**
134+
* 小程序审核撤回
135+
* 单个帐号每天审核撤回次数最多不超过1次,一个月不超过10次
136+
*
137+
* @throws WxErrorException 失败时抛出,具体错误码请看此接口的注释文档
138+
*/
139+
void undoCodeAudit() throws WxErrorException;
140+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java

+7
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ public interface WxMaService {
135135
*/
136136
WxMaTemplateService getTemplateService();
137137

138+
/**
139+
* 返回代码操作相关的 API
140+
*
141+
* @return WxMaCodeService
142+
*/
143+
WxMaCodeService getCodeService();
144+
138145
/**
139146
* 初始化http请求对象.
140147
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaCodeService;
4+
import cn.binarywang.wx.miniapp.api.WxMaService;
5+
import cn.binarywang.wx.miniapp.bean.code.WxMaCategory;
6+
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeAuditStatus;
7+
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeCommitRequest;
8+
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeSubmitAuditRequest;
9+
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeVersionDistribution;
10+
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
11+
import com.google.gson.JsonObject;
12+
import com.google.gson.JsonParser;
13+
import com.google.gson.reflect.TypeToken;
14+
import me.chanjar.weixin.common.bean.result.WxError;
15+
import me.chanjar.weixin.common.exception.WxErrorException;
16+
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
17+
import me.chanjar.weixin.common.util.http.RequestExecutor;
18+
import me.chanjar.weixin.common.util.json.GsonHelper;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.util.List;
25+
26+
/**
27+
* @author <a href="https://github.com/charmingoh">Charming</a>
28+
* @since 2018-04-26 20:00
29+
*/
30+
public class WxMaCodeServiceImpl implements WxMaCodeService {
31+
private static final JsonParser JSON_PARSER = new JsonParser();
32+
private WxMaService wxMaService;
33+
34+
public WxMaCodeServiceImpl(WxMaService wxMaService) {
35+
this.wxMaService = wxMaService;
36+
}
37+
38+
@Override
39+
public void commit(WxMaCodeCommitRequest commitRequest) throws WxErrorException {
40+
this.wxMaService.post(COMMIT_URL, commitRequest.toJson());
41+
}
42+
43+
@Override
44+
public byte[] getQrCode() throws WxErrorException {
45+
String appId = this.wxMaService.getWxMaConfig().getAppid();
46+
Path qrCodeFilePath = null;
47+
try {
48+
RequestExecutor<File, String> executor = BaseMediaDownloadRequestExecutor
49+
.create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("weixin-java-tools-ma-" + appId).toFile());
50+
qrCodeFilePath = this.wxMaService.execute(executor, GET_QRCODE_URL, null).toPath();
51+
return Files.readAllBytes(qrCodeFilePath);
52+
} catch (IOException e) {
53+
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e);
54+
} finally {
55+
if (qrCodeFilePath != null) {
56+
try {
57+
// 及时删除二维码文件,避免积压过多缓存文件
58+
Files.delete(qrCodeFilePath);
59+
} catch (Exception ignored) {
60+
}
61+
}
62+
}
63+
}
64+
65+
@Override
66+
public List<WxMaCategory> getCategory() throws WxErrorException {
67+
String responseContent = this.wxMaService.get(GET_CATEGORY_URL, null);
68+
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
69+
boolean hasCategoryList = jsonObject.has("category_list");
70+
if (hasCategoryList) {
71+
return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("category_list"),
72+
new TypeToken<List<WxMaCategory>>() {
73+
}.getType());
74+
} else {
75+
return null;
76+
}
77+
}
78+
79+
@Override
80+
public List<String> getPage() throws WxErrorException {
81+
String responseContent = this.wxMaService.get(GET_PAGE_URL, null);
82+
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
83+
boolean hasPageList = jsonObject.has("page_list");
84+
if (hasPageList) {
85+
return WxMaGsonBuilder.create().fromJson(jsonObject.getAsJsonArray("page_list"),
86+
new TypeToken<List<String>>() {
87+
}.getType());
88+
} else {
89+
return null;
90+
}
91+
}
92+
93+
@Override
94+
public long submitAudit(WxMaCodeSubmitAuditRequest auditRequest) throws WxErrorException {
95+
String responseContent = this.wxMaService.post(SUBMIT_AUDIT_URL, auditRequest.toJson());
96+
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
97+
return GsonHelper.getLong(jsonObject, "auditid");
98+
}
99+
100+
@Override
101+
public WxMaCodeAuditStatus getAuditStatus(long auditId) throws WxErrorException {
102+
JsonObject param = new JsonObject();
103+
param.addProperty("auditid", auditId);
104+
String responseContent = this.wxMaService.post(GET_AUDIT_STATUS_URL, param.toString());
105+
return WxMaCodeAuditStatus.fromJson(responseContent);
106+
}
107+
108+
@Override
109+
public WxMaCodeAuditStatus getLatestAuditStatus() throws WxErrorException {
110+
String responseContent = this.wxMaService.get(GET_LATEST_AUDIT_STATUS_URL, null);
111+
return WxMaCodeAuditStatus.fromJson(responseContent);
112+
}
113+
114+
@Override
115+
public void release() throws WxErrorException {
116+
this.wxMaService.post(RELEASE_URL, "{}");
117+
}
118+
119+
@Override
120+
public void changeVisitStatus(String action) throws WxErrorException {
121+
JsonObject param = new JsonObject();
122+
param.addProperty("action", action);
123+
this.wxMaService.post(CHANGE_VISIT_STATUS_URL, param.toString());
124+
}
125+
126+
@Override
127+
public void revertCodeRelease() throws WxErrorException {
128+
this.wxMaService.get(REVERT_CODE_RELEASE_URL, null);
129+
}
130+
131+
@Override
132+
public WxMaCodeVersionDistribution getSupportVersion() throws WxErrorException {
133+
String responseContent = this.wxMaService.post(GET_SUPPORT_VERSION_URL, "{}");
134+
return WxMaCodeVersionDistribution.fromJson(responseContent);
135+
}
136+
137+
@Override
138+
public void setSupportVersion(String version) throws WxErrorException {
139+
JsonObject param = new JsonObject();
140+
param.addProperty("version", version);
141+
this.wxMaService.post(SET_SUPPORT_VERSION_URL, param.toString());
142+
}
143+
144+
@Override
145+
public void undoCodeAudit() throws WxErrorException {
146+
this.wxMaService.get(UNDO_CODE_AUDIT_URL, null);
147+
}
148+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
package cn.binarywang.wx.miniapp.api.impl;
22

3-
import java.io.IOException;
4-
import java.util.HashMap;
5-
import java.util.Map;
6-
import java.util.concurrent.locks.Lock;
7-
8-
import org.apache.http.HttpHost;
9-
import org.apache.http.client.config.RequestConfig;
10-
import org.apache.http.client.methods.CloseableHttpResponse;
11-
import org.apache.http.client.methods.HttpGet;
12-
import org.apache.http.impl.client.BasicResponseHandler;
13-
import org.apache.http.impl.client.CloseableHttpClient;
14-
import org.slf4j.Logger;
15-
import org.slf4j.LoggerFactory;
16-
3+
import cn.binarywang.wx.miniapp.api.WxMaCodeService;
174
import cn.binarywang.wx.miniapp.api.WxMaMediaService;
185
import cn.binarywang.wx.miniapp.api.WxMaMsgService;
196
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
@@ -35,6 +22,19 @@
3522
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
3623
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
3724
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
25+
import org.apache.http.HttpHost;
26+
import org.apache.http.client.config.RequestConfig;
27+
import org.apache.http.client.methods.CloseableHttpResponse;
28+
import org.apache.http.client.methods.HttpGet;
29+
import org.apache.http.impl.client.BasicResponseHandler;
30+
import org.apache.http.impl.client.CloseableHttpClient;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
import java.io.IOException;
35+
import java.util.HashMap;
36+
import java.util.Map;
37+
import java.util.concurrent.locks.Lock;
3838

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

5556
private int retrySleepMillis = 1000;
5657
private int maxRetryTimes = 5;
@@ -290,4 +291,9 @@ public WxMaQrcodeService getQrcodeService() {
290291
public WxMaTemplateService getTemplateService() {
291292
return this.templateService;
292293
}
294+
295+
@Override
296+
public WxMaCodeService getCodeService() {
297+
return this.codeService;
298+
}
293299
}

0 commit comments

Comments
 (0)