@@ -463,6 +463,8 @@ public enum WxMaErrorMsgEnum { | |||
CODE_85003(85003, "微信号绑定的小程序体验者达到上限"), | |||
CODE_85004(85004, "微信号已经绑定"), | |||
// CODE_504002(-504002, "云函数未找到 Function not found"), | |||
; | |||
private int code; | |||
@@ -0,0 +1,317 @@ | |||
package cn.binarywang.wx.miniapp.api; | |||
import cn.binarywang.wx.miniapp.bean.cloud.*; | |||
import com.google.gson.JsonArray; | |||
import me.chanjar.weixin.common.error.WxErrorException; | |||
import java.util.List; | |||
/** | |||
* 云开发相关接口. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-22 | |||
*/ | |||
public interface WxMaCloudService { | |||
String INVOKE_CLOUD_FUNCTION_URL = "https://api.weixin.qq.com/tcb/invokecloudfunction?env=%s&name=%s"; | |||
String DATABASE_COLLECTION_GET_URL = "https://api.weixin.qq.com/tcb/databasecollectionget"; | |||
String DATABASE_COLLECTION_DELETE_URL = "https://api.weixin.qq.com/tcb/databasecollectiondelete"; | |||
String DATABASE_COLLECTION_ADD_URL = "https://api.weixin.qq.com/tcb/databasecollectionadd"; | |||
String GET_QCLOUD_TOKEN_URL = "https://api.weixin.qq.com/tcb/getqcloudtoken"; | |||
String BATCH_DELETE_FILE_URL = "https://api.weixin.qq.com/tcb/batchdeletefile"; | |||
String UPLOAD_FILE_URL = "https://api.weixin.qq.com/tcb/uploadfile"; | |||
String DATABASE_MIGRATE_QUERY_INFO_URL = "https://api.weixin.qq.com/tcb/databasemigratequeryinfo"; | |||
String DATABASE_MIGRATE_EXPORT_URL = "https://api.weixin.qq.com/tcb/databasemigrateexport"; | |||
String DATABASE_MIGRATE_IMPORT_URL = "https://api.weixin.qq.com/tcb/databasemigrateimport"; | |||
String UPDATE_INDEX_URL = "https://api.weixin.qq.com/tcb/updateindex"; | |||
String DATABASE_COUNT_URL = "https://api.weixin.qq.com/tcb/databasecount"; | |||
String DATABASE_AGGREGATE_URL = "https://api.weixin.qq.com/tcb/databaseaggregate"; | |||
String DATABASE_QUERY_URL = "https://api.weixin.qq.com/tcb/databasequery"; | |||
String DATABASE_UPDATE_URL = "https://api.weixin.qq.com/tcb/databaseupdate"; | |||
String DATABASE_DELETE_URL = "https://api.weixin.qq.com/tcb/databasedelete"; | |||
String DATABASE_ADD_URL = "https://api.weixin.qq.com/tcb/databaseadd"; | |||
/** | |||
* <pre> | |||
* 触发云函数。注意:HTTP API 途径触发云函数不包含用户信息。 | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/functions/invokeCloudFunction.html | |||
* | |||
* 请求地址 | |||
* POST https://api.weixin.qq.com/tcb/invokecloudfunction?access_token=ACCESS_TOKEN&env=ENV&name=FUNCTION_NAME | |||
* | |||
* </pre> | |||
* | |||
* @param env string 是 云开发环境ID | |||
* @param name string 是 云函数名称 | |||
* @param body string 是 云函数的传入参数,具体结构由开发者定义。 | |||
* @return resp_data string 云函数返回的buffer | |||
* @throws WxErrorException . | |||
*/ | |||
String invokeCloudFunction(String env, String name, String body) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 数据库插入记录 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseAdd.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/databaseadd?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param query 数据库操作语句 | |||
* @return 插入成功的数据集合主键_id | |||
* @throws WxErrorException . | |||
*/ | |||
JsonArray databaseAdd(String env, String query) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 数据库删除记录 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseDelete.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/databasedelete?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param query 数据库操作语句 | |||
* @return 删除记录数量 | |||
* @throws WxErrorException . | |||
*/ | |||
int databaseDelete(String env, String query) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 数据库更新记录 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseUpdate.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/databaseupdate?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param query 数据库操作语句 | |||
* @return . | |||
* @throws WxErrorException . | |||
*/ | |||
WxCloudDatabaseUpdateResult databaseUpdate(String env, String query) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 数据库查询记录 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseQuery.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/databasequery?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param query 数据库操作语句 | |||
* @return . | |||
* @throws WxErrorException . | |||
*/ | |||
WxCloudDatabaseQueryResult databaseQuery(String env, String query) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 数据库聚合记录 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseAggregate.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/databaseaggregate?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param query 数据库操作语句 | |||
* @return . | |||
* @throws WxErrorException . | |||
*/ | |||
JsonArray databaseAggregate(String env, String query) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 统计集合记录数或统计查询语句对应的结果记录数 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCount.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/databasecount?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param query 数据库操作语句 | |||
* @return 记录数量 | |||
* @throws WxErrorException . | |||
*/ | |||
Long databaseCount(String env, String query) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 变更数据库索引 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/updateIndex.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/updateindex?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param collectionName 集合名称 | |||
* @param createIndexes 新增索引对象 | |||
* @param dropIndexNames 要删除的索引的名字 | |||
* @throws WxErrorException . | |||
*/ | |||
void updateIndex(String env, String collectionName, List<WxCloudDatabaseCreateIndexRequest> createIndexes, | |||
List<String> dropIndexNames) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 数据库导入 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseMigrateImport.html | |||
* 请求地址: POST https://api.weixin.qq.com/tcb/databasemigrateimport?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param collectionName 导入collection名 | |||
* @param filePath 导入文件路径(导入文件需先上传到同环境的存储中,可使用开发者工具或 HTTP API的上传文件 API上传) | |||
* @param fileType 导入文件类型, 1 JSON, 2 CSV | |||
* @param stopOnError 是否在遇到错误时停止导入 | |||
* @param conflictMode 冲突处理模式 : 1 INSERT , 2 UPSERT | |||
* @return jobId | |||
* @throws WxErrorException . | |||
*/ | |||
Long databaseMigrateImport(String env, String collectionName, String filePath, int fileType, boolean stopOnError, | |||
int conflictMode) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 数据库导出 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseMigrateExport.html | |||
* 请求地址: POST https://api.weixin.qq.com/tcb/databasemigrateexport?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param filePath 导出文件路径(文件会导出到同环境的云存储中,可使用获取下载链接 API 获取下载链接) | |||
* @param fileType 导出文件类型, 1 JSON, 2 CSV | |||
* @param query 导出条件 | |||
* @return jobId | |||
* @throws WxErrorException . | |||
*/ | |||
Long databaseMigrateExport(String env, String filePath, int fileType, String query) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 数据库迁移状态查询 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseMigrateQueryInfo.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/databasemigratequeryinfo?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param jobId 迁移任务ID | |||
* @return . | |||
* @throws WxErrorException . | |||
*/ | |||
WxCloudCloudDatabaseMigrateQueryInfoResult databaseMigrateQueryInfo(String env, Long jobId) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 获取文件上传链接 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/uploadFile.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/uploadfile?access_token=ACCESS_TOKEN | |||
* | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param path 上传路径 | |||
* @return 上传结果 | |||
* @throws WxErrorException . | |||
*/ | |||
WxCloudUploadFileResult uploadFile(String env, String path) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 获取文件下载链接 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDownloadFile.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/batchdownloadfile?access_token=ACCESS_TOKEN | |||
* | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param fileIds 文件ID列表 | |||
* @param maxAges 下载链接有效期列表,对应文件id列表 | |||
* @return 下载链接信息 | |||
* @throws WxErrorException . | |||
*/ | |||
WxCloudBatchDownloadFileResult batchDownloadFile(String env, String[] fileIds, long[] maxAges) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 删除文件 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDeleteFile.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/batchdeletefile?access_token=ACCESS_TOKEN | |||
* | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param fileIds 文件ID列表 | |||
* @return 下载链接信息 | |||
* @throws WxErrorException . | |||
*/ | |||
WxCloudBatchDeleteFileResult batchDeleteFile(String env, String[] fileIds) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 获取腾讯云API调用凭证 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/utils/getQcloudToken.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/getqcloudtoken?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param lifeSpan 有效期(单位为秒,最大7200) | |||
* @return . | |||
* @throws WxErrorException . | |||
*/ | |||
WxCloudGetQcloudTokenResult getQcloudToken(long lifeSpan) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 新增集合 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionAdd.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/databasecollectionadd?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param collectionName 集合名称 | |||
* @throws WxErrorException . | |||
*/ | |||
void databaseCollectionAdd(String env, String collectionName) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 删除集合 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionDelete.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/databasecollectionadd?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param collectionName 集合名称 | |||
* @throws WxErrorException . | |||
*/ | |||
void databaseCollectionDelete(String env, String collectionName) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 获取特定云环境下集合信息 | |||
* | |||
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionGet.html | |||
* 请求地址:POST https://api.weixin.qq.com/tcb/databasecollectionget?access_token=ACCESS_TOKEN | |||
* </pre> | |||
* | |||
* @param env 云环境ID | |||
* @param limit 获取数量限制,默认值:10 | |||
* @param offset 偏移量,默认值:0 | |||
* @return . | |||
* @throws WxErrorException . | |||
*/ | |||
WxCloudDatabaseCollectionGetResult databaseCollectionGet(String env, Long limit, Long offset) throws WxErrorException; | |||
} |
@@ -242,4 +242,9 @@ public interface WxMaService { | |||
* @return | |||
*/ | |||
WxMaExpressService getExpressService(); | |||
/** | |||
* 获取云开发接口服务对象 | |||
*/ | |||
WxMaCloudService getCloudService(); | |||
} |
@@ -0,0 +1,178 @@ | |||
package cn.binarywang.wx.miniapp.api.impl; | |||
import cn.binarywang.wx.miniapp.api.WxMaCloudService; | |||
import cn.binarywang.wx.miniapp.api.WxMaService; | |||
import cn.binarywang.wx.miniapp.bean.cloud.*; | |||
import com.google.common.collect.ImmutableMap; | |||
import com.google.common.collect.Lists; | |||
import com.google.gson.JsonArray; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonParser; | |||
import lombok.RequiredArgsConstructor; | |||
import lombok.extern.slf4j.Slf4j; | |||
import me.chanjar.weixin.common.error.WxErrorException; | |||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
import java.io.Serializable; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
/** | |||
* 云开发相关接口实现类. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-22 | |||
*/ | |||
@Slf4j | |||
@RequiredArgsConstructor | |||
public class WxMaCloudServiceImpl implements WxMaCloudService { | |||
private static final JsonParser JSON_PARSER = new JsonParser(); | |||
private final WxMaService wxMaService; | |||
@Override | |||
public String invokeCloudFunction(String env, String name, String body) throws WxErrorException { | |||
final String response = this.wxMaService.post(String.format(INVOKE_CLOUD_FUNCTION_URL, env, name), body); | |||
return JSON_PARSER.parse(response).getAsJsonObject().get("resp_data").getAsString(); | |||
} | |||
@Override | |||
public JsonArray databaseAdd(String env, String query) throws WxErrorException { | |||
String response = this.wxMaService.post(DATABASE_ADD_URL, ImmutableMap.of("env", env, "query", query)); | |||
return JSON_PARSER.parse(response).getAsJsonObject().get("id_list").getAsJsonArray(); | |||
} | |||
@Override | |||
public int databaseDelete(String env, String query) throws WxErrorException { | |||
String response = this.wxMaService.post(DATABASE_DELETE_URL, ImmutableMap.of("env", env, "query", query)); | |||
return JSON_PARSER.parse(response).getAsJsonObject().get("deleted").getAsInt(); | |||
} | |||
@Override | |||
public WxCloudDatabaseUpdateResult databaseUpdate(String env, String query) throws WxErrorException { | |||
String response = this.wxMaService.post(DATABASE_UPDATE_URL, ImmutableMap.of("env", env, "query", query)); | |||
return WxGsonBuilder.create().fromJson(response, WxCloudDatabaseUpdateResult.class); | |||
} | |||
@Override | |||
public WxCloudDatabaseQueryResult databaseQuery(String env, String query) throws WxErrorException { | |||
String response = this.wxMaService.post(DATABASE_QUERY_URL, ImmutableMap.of("env", env, "query", query)); | |||
return WxGsonBuilder.create().fromJson(response, WxCloudDatabaseQueryResult.class); | |||
} | |||
@Override | |||
public JsonArray databaseAggregate(String env, String query) throws WxErrorException { | |||
String response = this.wxMaService.post(DATABASE_AGGREGATE_URL, ImmutableMap.of("env", env, "query", query)); | |||
return JSON_PARSER.parse(response).getAsJsonObject().get("data").getAsJsonArray(); | |||
} | |||
@Override | |||
public Long databaseCount(String env, String query) throws WxErrorException { | |||
String response = this.wxMaService.post(DATABASE_COUNT_URL, ImmutableMap.of("env", env, "query", query)); | |||
return JSON_PARSER.parse(response).getAsJsonObject().get("count").getAsLong(); | |||
} | |||
@Override | |||
public void updateIndex(String env, String collectionName, List<WxCloudDatabaseCreateIndexRequest> createIndexes, | |||
List<String> dropIndexNames) throws WxErrorException { | |||
List<Map<String, String>> dropIndexes = Lists.newArrayList(); | |||
if (dropIndexNames != null) { | |||
for (String index : dropIndexNames) { | |||
dropIndexes.add(ImmutableMap.of("name", index)); | |||
} | |||
} | |||
this.wxMaService.post(UPDATE_INDEX_URL, ImmutableMap.of("env", env, | |||
"collection_name", collectionName, "create_indexes", createIndexes, "drop_indexes", dropIndexes)); | |||
} | |||
@Override | |||
public Long databaseMigrateImport(String env, String collectionName, String filePath, int fileType, | |||
boolean stopOnError, int conflictMode) throws WxErrorException { | |||
JsonObject params = new JsonObject(); | |||
params.addProperty("env", env); | |||
params.addProperty("collection_name", collectionName); | |||
params.addProperty("file_path", filePath); | |||
params.addProperty("file_type", fileType); | |||
params.addProperty("stop_on_error", stopOnError); | |||
params.addProperty("conflict_mode", conflictMode); | |||
String response = this.wxMaService.post(DATABASE_MIGRATE_IMPORT_URL, params.toString()); | |||
return JSON_PARSER.parse(response).getAsJsonObject().get("job_id").getAsLong(); | |||
} | |||
@Override | |||
public Long databaseMigrateExport(String env, String filePath, int fileType, String query) throws WxErrorException { | |||
JsonObject params = new JsonObject(); | |||
params.addProperty("env", env); | |||
params.addProperty("file_path", filePath); | |||
params.addProperty("file_type", fileType); | |||
params.addProperty("query", query); | |||
String response = this.wxMaService.post(DATABASE_MIGRATE_EXPORT_URL, params.toString()); | |||
return JSON_PARSER.parse(response).getAsJsonObject().get("job_id").getAsLong(); | |||
} | |||
@Override | |||
public WxCloudCloudDatabaseMigrateQueryInfoResult databaseMigrateQueryInfo(String env, Long jobId) throws WxErrorException { | |||
String response = this.wxMaService.post(DATABASE_MIGRATE_QUERY_INFO_URL, ImmutableMap.of("env", env, "job_id", jobId)); | |||
return WxGsonBuilder.create().fromJson(response, WxCloudCloudDatabaseMigrateQueryInfoResult.class); | |||
} | |||
@Override | |||
public WxCloudUploadFileResult uploadFile(String env, String path) throws WxErrorException { | |||
String response = this.wxMaService.post(UPLOAD_FILE_URL, ImmutableMap.of("env", env, "path", path)); | |||
return WxGsonBuilder.create().fromJson(response, WxCloudUploadFileResult.class); | |||
} | |||
@Override | |||
public WxCloudBatchDownloadFileResult batchDownloadFile(String env, String[] fileIds, long[] maxAges) throws WxErrorException { | |||
List<Map<String, Serializable>> fileList = Lists.newArrayList(); | |||
int i = 0; | |||
for (String fileId : fileIds) { | |||
fileList.add(ImmutableMap.of("fileid", fileId, "max_age", (Serializable) maxAges[i++])); | |||
} | |||
String response = this.wxMaService.post(GET_QCLOUD_TOKEN_URL, ImmutableMap.of("env", env, "file_list", fileList)); | |||
return WxGsonBuilder.create().fromJson(response, WxCloudBatchDownloadFileResult.class); | |||
} | |||
@Override | |||
public WxCloudBatchDeleteFileResult batchDeleteFile(String env, String[] fileIds) throws WxErrorException { | |||
String response = this.wxMaService.post(BATCH_DELETE_FILE_URL, ImmutableMap.of("env", env, "fileid_list", fileIds)); | |||
return WxGsonBuilder.create().fromJson(response, WxCloudBatchDeleteFileResult.class); | |||
} | |||
@Override | |||
public WxCloudGetQcloudTokenResult getQcloudToken(long lifeSpan) throws WxErrorException { | |||
String response = this.wxMaService.post(GET_QCLOUD_TOKEN_URL, ImmutableMap.of("lifespan", lifeSpan)); | |||
return WxGsonBuilder.create().fromJson(response, WxCloudGetQcloudTokenResult.class); | |||
} | |||
@Override | |||
public void databaseCollectionAdd(String env, String collectionName) throws WxErrorException { | |||
this.wxMaService.post(DATABASE_COLLECTION_ADD_URL, ImmutableMap.of("env", env, "collection_name", collectionName)); | |||
} | |||
@Override | |||
public void databaseCollectionDelete(String env, String collectionName) throws WxErrorException { | |||
this.wxMaService.post(DATABASE_COLLECTION_DELETE_URL, ImmutableMap.of("env", env, "collection_name", collectionName)); | |||
} | |||
@Override | |||
public WxCloudDatabaseCollectionGetResult databaseCollectionGet(String env, Long limit, Long offset) throws WxErrorException { | |||
Map<String, Object> params = new HashMap<>(2); | |||
params.put("env", env); | |||
if (limit != null) { | |||
params.put("limit", limit); | |||
} | |||
if (offset != null) { | |||
params.put("offset", offset); | |||
} | |||
String response = this.wxMaService.post(DATABASE_COLLECTION_GET_URL, params); | |||
return WxGsonBuilder.create().fromJson(response, WxCloudDatabaseCollectionGetResult.class); | |||
} | |||
} |
@@ -57,6 +57,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||
private WxMaPluginService pluginService = new WxMaPluginServiceImpl(this); | |||
private WxMaExpressService expressService = new WxMaExpressServiceImpl(this); | |||
private WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(this); | |||
private WxMaCloudService cloudService = new WxMaCloudServiceImpl(this); | |||
private int retrySleepMillis = 1000; | |||
private int maxRetryTimes = 5; | |||
@@ -383,4 +384,9 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||
public WxMaExpressService getExpressService() { | |||
return this.expressService; | |||
} | |||
@Override | |||
public WxMaCloudService getCloudService() { | |||
return this.cloudService; | |||
} | |||
} |
@@ -0,0 +1,44 @@ | |||
package cn.binarywang.wx.miniapp.bean.cloud; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Data; | |||
import java.io.Serializable; | |||
import java.util.List; | |||
/** | |||
* 文件删除结果. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-27 | |||
*/ | |||
@Data | |||
public class WxCloudBatchDeleteFileResult implements Serializable { | |||
private static final long serialVersionUID = -1133274298839868115L; | |||
@SerializedName("delete_list") | |||
private List<FileDownloadInfo> fileList; | |||
@Data | |||
public static class FileDownloadInfo implements Serializable { | |||
private static final long serialVersionUID = 5812969045277862211L; | |||
/** | |||
* fileid string 文件ID | |||
*/ | |||
@SerializedName("fileid") | |||
private String fileId; | |||
/** | |||
* status number 状态码 | |||
*/ | |||
@SerializedName("status") | |||
private Integer status; | |||
/** | |||
* errmsg string 该文件错误信息 | |||
*/ | |||
@SerializedName("errmsg") | |||
private String errMsg; | |||
} | |||
} |
@@ -0,0 +1,50 @@ | |||
package cn.binarywang.wx.miniapp.bean.cloud; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Data; | |||
import java.io.Serializable; | |||
import java.util.List; | |||
/** | |||
* 获取文件下载链接结果. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-27 | |||
*/ | |||
@Data | |||
public class WxCloudBatchDownloadFileResult implements Serializable { | |||
private static final long serialVersionUID = 646423661372964402L; | |||
@SerializedName("file_list") | |||
private List<FileDownloadInfo> fileList; | |||
@Data | |||
public static class FileDownloadInfo implements Serializable { | |||
private static final long serialVersionUID = 5812969045277862211L; | |||
/** | |||
* fileid string 文件ID | |||
*/ | |||
@SerializedName("fileid") | |||
private String fileId; | |||
/** | |||
* download_url string 下载链接 | |||
*/ | |||
@SerializedName("download_url") | |||
private String downloadUrl; | |||
/** | |||
* status number 状态码 | |||
*/ | |||
@SerializedName("status") | |||
private Integer status; | |||
/** | |||
* errmsg string 该文件错误信息 | |||
*/ | |||
@SerializedName("errmsg") | |||
private String errMsg; | |||
} | |||
} |
@@ -0,0 +1,47 @@ | |||
package cn.binarywang.wx.miniapp.bean.cloud; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Data; | |||
import java.io.Serializable; | |||
/** | |||
* 云开发数据库迁移状态查询结果. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-26 | |||
*/ | |||
@Data | |||
public class WxCloudCloudDatabaseMigrateQueryInfoResult implements Serializable { | |||
private static final long serialVersionUID = 2014197503355968243L; | |||
/** | |||
* status string 导出状态 | |||
*/ | |||
private String status; | |||
/** | |||
* record_success number 导出成功记录数 | |||
*/ | |||
@SerializedName("record_success") | |||
private Integer recordSuccess; | |||
/** | |||
* record_fail number 导出失败记录数 | |||
*/ | |||
@SerializedName("record_fail") | |||
private Integer recordFail; | |||
/** | |||
* err_msg string 导出错误信息 | |||
*/ | |||
@SerializedName("err_msg") | |||
private String errMsg; | |||
/** | |||
* file_url string 导出文件下载地址 | |||
*/ | |||
@SerializedName("file_url") | |||
private String fileUrl; | |||
} |
@@ -0,0 +1,86 @@ | |||
package cn.binarywang.wx.miniapp.bean.cloud; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Data; | |||
import java.io.Serializable; | |||
/** | |||
* 云开发获取集合接口的结果. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-28 | |||
*/ | |||
@Data | |||
public class WxCloudDatabaseCollectionGetResult implements Serializable { | |||
private static final long serialVersionUID = 3702855196387039823L; | |||
/** | |||
* 分页信息 | |||
*/ | |||
private WxCloudDatabaseQueryResult.Pager pager; | |||
/** | |||
* 查询结果 | |||
*/ | |||
private CollectionInfo[] collections; | |||
@Data | |||
public static class CollectionInfo implements Serializable { | |||
private static final long serialVersionUID = -3280126948752330438L; | |||
/** | |||
* name string 集合名 | |||
*/ | |||
@SerializedName("name") | |||
private String name; | |||
/** | |||
* count number 表中文档数量 | |||
*/ | |||
@SerializedName("count") | |||
private Long count; | |||
/** | |||
* size number 表的大小(即表中文档总大小),单位:字节 | |||
*/ | |||
@SerializedName("size") | |||
private Long size; | |||
/** | |||
* index_count number 索引数量 | |||
*/ | |||
@SerializedName("index_count") | |||
private Long indexCount; | |||
/** | |||
* index_size number 索引占用大小,单位:字节 | |||
*/ | |||
@SerializedName("index_size") | |||
private Long indexSize; | |||
} | |||
@Data | |||
public static class Pager implements Serializable { | |||
private static final long serialVersionUID = 5045727687673687839L; | |||
/** | |||
* Offset number 偏移 | |||
*/ | |||
@SerializedName("Offset") | |||
private Long offset; | |||
/** | |||
* Limit number 单次查询限制 | |||
*/ | |||
@SerializedName("Limit") | |||
private Long limit; | |||
/** | |||
* Total number 符合查询条件的记录总数 | |||
*/ | |||
@SerializedName("Total") | |||
private Long total; | |||
} | |||
} |
@@ -0,0 +1,59 @@ | |||
package cn.binarywang.wx.miniapp.bean.cloud; | |||
import lombok.Builder; | |||
import lombok.Data; | |||
import lombok.experimental.Accessors; | |||
import java.io.Serializable; | |||
import java.util.List; | |||
/** | |||
* 云开发新增索引的请求对象. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-26 | |||
*/ | |||
@Accessors(chain = true) | |||
@Data | |||
public class WxCloudDatabaseCreateIndexRequest implements Serializable { | |||
private static final long serialVersionUID = -8308393731157121109L; | |||
/** | |||
* name string 是 索引名 | |||
*/ | |||
private String name; | |||
/** | |||
* unique boolean 是 是否唯一 | |||
*/ | |||
private boolean unique; | |||
/** | |||
* keys Array.<Object> 是 索引字段 | |||
*/ | |||
private List<IndexKey> keys; | |||
@Data | |||
@Accessors(chain = true) | |||
public static class IndexKey implements Serializable { | |||
private static final long serialVersionUID = -252641130547960325L; | |||
/** | |||
* name string 是 字段名 | |||
*/ | |||
private String name; | |||
/** | |||
* direction string 是 字段排序 | |||
* <pre> | |||
* direction 的合法值 | |||
* 值 说明 | |||
* "1" 升序 | |||
* "-1" 降序 | |||
* "2dsphere" 地理位置 | |||
* | |||
* </pre> | |||
*/ | |||
private String direction; | |||
} | |||
} |
@@ -0,0 +1,51 @@ | |||
package cn.binarywang.wx.miniapp.bean.cloud; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Data; | |||
import java.io.Serializable; | |||
/** | |||
* 云开发数据库查询记录接口请求结果. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-26 | |||
*/ | |||
@Data | |||
public class WxCloudDatabaseQueryResult implements Serializable { | |||
private static final long serialVersionUID = 8291820029137872536L; | |||
/** | |||
* 分页信息 | |||
*/ | |||
private Pager pager; | |||
/** | |||
* 查询结果 | |||
*/ | |||
private String[] data; | |||
@Data | |||
public static class Pager implements Serializable{ | |||
private static final long serialVersionUID = 8556239063823985674L; | |||
/** | |||
* Offset number 偏移 | |||
*/ | |||
@SerializedName("Offset") | |||
private Long offset; | |||
/** | |||
* Limit number 单次查询限制 | |||
*/ | |||
@SerializedName("Limit") | |||
private Long limit; | |||
/** | |||
* Total number 符合查询条件的记录总数 | |||
*/ | |||
@SerializedName("Total") | |||
private Long total; | |||
} | |||
} |
@@ -0,0 +1,32 @@ | |||
package cn.binarywang.wx.miniapp.bean.cloud; | |||
import lombok.Data; | |||
import java.io.Serializable; | |||
/** | |||
* 云开发数据库更新记录接口请求结果. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-26 | |||
*/ | |||
@Data | |||
public class WxCloudDatabaseUpdateResult implements Serializable { | |||
private static final long serialVersionUID = -3905429931117999004L; | |||
/** | |||
* matched number 更新条件匹配到的结果数 | |||
*/ | |||
private Long matched; | |||
/** | |||
* modified number 修改的记录数,注意:使用set操作新插入的数据不计入修改数目 | |||
*/ | |||
private Long modified; | |||
/** | |||
* id string 新插入记录的id,注意:只有使用set操作新插入数据时这个字段会有值 | |||
*/ | |||
private String id; | |||
} |
@@ -0,0 +1,41 @@ | |||
package cn.binarywang.wx.miniapp.bean.cloud; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Data; | |||
import java.io.Serializable; | |||
/** | |||
* 获取腾讯云API调用凭证结果. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-27 | |||
*/ | |||
@Data | |||
public class WxCloudGetQcloudTokenResult implements Serializable { | |||
private static final long serialVersionUID = -1505786395531138286L; | |||
/** | |||
* secretid | |||
*/ | |||
@SerializedName("secretid") | |||
private String secretId; | |||
/** | |||
* secretkey | |||
*/ | |||
@SerializedName("secretkey") | |||
private String secretKey; | |||
/** | |||
* token | |||
*/ | |||
@SerializedName("token") | |||
private String token; | |||
/** | |||
* 过期时间戳 | |||
*/ | |||
@SerializedName("expired_time") | |||
private Long expiredTime; | |||
} |
@@ -0,0 +1,47 @@ | |||
package cn.binarywang.wx.miniapp.bean.cloud; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Data; | |||
import java.io.Serializable; | |||
/** | |||
* 云开发文件上传接口响应结果. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-27 | |||
*/ | |||
@Data | |||
public class WxCloudUploadFileResult implements Serializable { | |||
private static final long serialVersionUID = 787346474470048318L; | |||
/** | |||
* 上传url | |||
*/ | |||
@SerializedName("url") | |||
private String url; | |||
/** | |||
* token | |||
*/ | |||
@SerializedName("token") | |||
private String token; | |||
/** | |||
* authorization | |||
*/ | |||
@SerializedName("authorization") | |||
private String authorization; | |||
/** | |||
* 文件ID | |||
*/ | |||
@SerializedName("file_id") | |||
private String fileId; | |||
/** | |||
* cos文件ID | |||
*/ | |||
@SerializedName("cos_file_id") | |||
private String cosFileId; | |||
} |
@@ -0,0 +1,224 @@ | |||
package cn.binarywang.wx.miniapp.api.impl; | |||
import cn.binarywang.wx.miniapp.api.WxMaService; | |||
import cn.binarywang.wx.miniapp.bean.cloud.*; | |||
import cn.binarywang.wx.miniapp.test.ApiTestModule; | |||
import com.google.common.collect.Lists; | |||
import com.google.gson.JsonArray; | |||
import com.google.inject.Inject; | |||
import me.chanjar.weixin.common.error.WxErrorException; | |||
import org.testng.annotations.Guice; | |||
import org.testng.annotations.Test; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
/** | |||
* 测试类. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2020-01-22 | |||
*/ | |||
@Guice(modules = ApiTestModule.class) | |||
public class WxMaCloudServiceImplTest { | |||
@Inject | |||
private WxMaService wxMaService; | |||
@Test | |||
public void testInvokeCloudFunction() throws WxErrorException { | |||
final String result = this.wxMaService.getCloudService().invokeCloudFunction("rcn", "login", "{}"); | |||
assertThat(result).isNotNull(); | |||
} | |||
@Test | |||
public void testDatabaseAdd() throws WxErrorException { | |||
JsonArray array = this.wxMaService.getCloudService().databaseAdd("rcn", "db.collection(\"geo\").add({\n" + | |||
" data: [{\n" + | |||
" description: \"item1\",\n" + | |||
" due:\n" + | |||
" new Date(\"2019-09-09\"),\n" + | |||
" tags: [\n" + | |||
" \"cloud\",\n" + | |||
" \"database\"\n" + | |||
" ],\n" + | |||
" location:\n" + | |||
" new db.Geo.Point(113, 23),\n" + | |||
" done:false\n" + | |||
" },\n" + | |||
" {\n" + | |||
" description: \"item2\",\n" + | |||
" due:\n" + | |||
" new Date(\"2019-09-09\"),\n" + | |||
" tags: [\n" + | |||
" \"cloud\",\n" + | |||
" \"database\"\n" + | |||
" ],\n" + | |||
" location:\n" + | |||
" new db.Geo.Point(113, 23),\n" + | |||
" done:false\n" + | |||
" }\n" + | |||
" ]\n" + | |||
" })"); | |||
System.out.println(array); | |||
assertThat(array).isNotEmpty(); | |||
} | |||
@Test | |||
public void testDatabaseDelete() throws WxErrorException { | |||
final int result = this.wxMaService.getCloudService().databaseDelete("rcn", | |||
"db.collection(\"geo\").doc(\"056120a7-c89e-4616-95bf-dfc9a11daa3b\").remove()"); | |||
assertThat(result).isEqualTo(0); | |||
} | |||
@Test | |||
public void testDatabaseUpdate() throws WxErrorException { | |||
final WxCloudDatabaseUpdateResult result = this.wxMaService.getCloudService().databaseUpdate("rcn", | |||
"db.collection(\"geo\").where({description:\"item1\"}).update({data:{age: _.inc(1)}})"); | |||
assertThat(result).isNotNull(); | |||
assertThat(result.getMatched()).isGreaterThan(0); | |||
assertThat(result.getId()).isEmpty(); | |||
assertThat(result.getModified()).isGreaterThan(0); | |||
} | |||
@Test | |||
public void testDatabaseQuery() throws WxErrorException { | |||
final WxCloudDatabaseQueryResult result = this.wxMaService.getCloudService().databaseQuery("rcn", | |||
"db.collection(\"geo\").where({done:false}).limit(10).skip(1).get()"); | |||
assertThat(result).isNotNull(); | |||
assertThat(result.getPager()).isNotNull(); | |||
assertThat(result.getPager().getLimit()).isEqualTo(10); | |||
assertThat(result.getPager().getOffset()).isEqualTo(1); | |||
assertThat(result.getPager().getTotal()).isGreaterThan(0); | |||
assertThat(result.getData().length).isGreaterThan(0); | |||
} | |||
@Test | |||
public void testDatabaseAggregate() throws WxErrorException { | |||
final JsonArray result = this.wxMaService.getCloudService().databaseAggregate("rcn", | |||
"db.collection(\"geo\").aggregate().match({tags:\"cloud\"}).limit(10).end()"); | |||
assertThat(result).isNotNull(); | |||
} | |||
@Test | |||
public void testDatabaseCount() throws WxErrorException { | |||
final Long result = this.wxMaService.getCloudService().databaseCount("rcn", | |||
"db.collection(\"geo\").where({done:false}).count()"); | |||
assertThat(result).isGreaterThan(0); | |||
} | |||
@Test | |||
public void testUpdateIndex() throws WxErrorException { | |||
this.wxMaService.getCloudService().updateIndex("rcn", "geo", | |||
Lists.newArrayList(new WxCloudDatabaseCreateIndexRequest() | |||
.setName("drop_index") | |||
.setUnique(true) | |||
.setKeys(Lists.newArrayList(new WxCloudDatabaseCreateIndexRequest.IndexKey().setDirection("2dsphere").setName("test")) | |||
)), | |||
Lists.newArrayList("add_index2")); | |||
} | |||
@Test | |||
public void testDatabaseMigrateImport() throws WxErrorException { | |||
final Long result = this.wxMaService.getCloudService().databaseMigrateImport("rcn", "geo", "test.json", | |||
1, true, 1); | |||
assertThat(result).isGreaterThan(0); | |||
} | |||
@Test | |||
public void testDatabaseMigrateExport() throws WxErrorException { | |||
final Long result = this.wxMaService.getCloudService().databaseMigrateExport("rcn", "test.json", 1, | |||
"const Point = db.Geo.Point;db.collection('geo').where({age: _.gt(1)}).limit(10).skip(1).orderBy('age','asc').orderBy('name', 'desc')" + | |||
".field({ name: true }).get()"); | |||
assertThat(result).isGreaterThan(0); | |||
} | |||
@Test | |||
public void testDatabaseMigrateQueryInfo() throws WxErrorException { | |||
final WxCloudCloudDatabaseMigrateQueryInfoResult result = this.wxMaService.getCloudService() | |||
.databaseMigrateQueryInfo("rcn", 476629L); | |||
assertThat(result).isNotNull(); | |||
System.out.println(result.getFileUrl()); | |||
} | |||
@Test | |||
public void testUploadFile() throws WxErrorException { | |||
final WxCloudUploadFileResult result = this.wxMaService.getCloudService().uploadFile("rcn", "E:\\MyDocs\\Desktop\\test.json"); | |||
assertThat(result).isNotNull(); | |||
assertThat(result.getAuthorization()).isNotNull(); | |||
assertThat(result.getToken()).isNotNull(); | |||
assertThat(result.getUrl()).isNotNull(); | |||
assertThat(result.getFileId()).isNotNull(); | |||
assertThat(result.getCosFileId()).isNotNull(); | |||
} | |||
@Test | |||
public void testBatchDownloadFile() throws WxErrorException { | |||
final WxCloudBatchDownloadFileResult result = this.wxMaService.getCloudService().batchDownloadFile("rcn", | |||
new String[]{"cloud://rcn.7263-rcn-1258788140/Snipaste_2019-11-13_00-21-27.png", "cloud://rcn.7263-rcn-1258788140/avatar.jpg"}, | |||
new long[]{7200, 6800}); | |||
assertThat(result).isNotNull(); | |||
assertThat(result.getFileList()).isNotNull(); | |||
assertThat(result.getFileList().size()).isGreaterThan(0); | |||
assertThat(result.getFileList().get(0).getDownloadUrl()).isNotNull(); | |||
assertThat(result.getFileList().get(0).getErrMsg()).isEqualTo("ok"); | |||
assertThat(result.getFileList().get(0).getStatus()).isEqualTo(0); | |||
assertThat(result.getFileList().get(0).getFileId()).isNotNull(); | |||
} | |||
@Test | |||
public void testBatchDeleteFile() throws WxErrorException { | |||
final WxCloudBatchDeleteFileResult result = this.wxMaService.getCloudService().batchDeleteFile("rcn", | |||
new String[]{"cloud://rcn.7263-rcn-1258788140/test.json"}); | |||
assertThat(result).isNotNull(); | |||
assertThat(result.getFileList()).isNotNull(); | |||
assertThat(result.getFileList().size()).isGreaterThan(0); | |||
assertThat(result.getFileList().get(0).getErrMsg()).isEqualTo("ok"); | |||
assertThat(result.getFileList().get(0).getStatus()).isEqualTo(0); | |||
assertThat(result.getFileList().get(0).getFileId()).isNotNull(); | |||
} | |||
@Test | |||
public void testGetQcloudToken() throws WxErrorException { | |||
final WxCloudGetQcloudTokenResult result = this.wxMaService.getCloudService().getQcloudToken(1800); | |||
assertThat(result).isNotNull(); | |||
assertThat(result.getSecretId()).isNotNull(); | |||
assertThat(result.getSecretKey()).isNotNull(); | |||
assertThat(result.getToken()).isNotNull(); | |||
assertThat(result.getExpiredTime()).isNotNull(); | |||
} | |||
@Test | |||
public void testDatabaseCollectionAdd() throws WxErrorException { | |||
this.wxMaService.getCloudService().databaseCollectionAdd("rcn","test_add_collection"); | |||
} | |||
@Test | |||
public void testDatabaseCollectionDelete() throws WxErrorException { | |||
this.wxMaService.getCloudService().databaseCollectionAdd("rcn","test_delete_collection"); | |||
this.wxMaService.getCloudService().databaseCollectionDelete("rcn","test_delete_collection"); | |||
} | |||
@Test | |||
public void testDatabaseCollectionGet() throws WxErrorException { | |||
final WxCloudDatabaseCollectionGetResult result = this.wxMaService.getCloudService().databaseCollectionGet("rcn", null, null); | |||
assertThat(result).isNotNull(); | |||
assertThat(result.getPager()).isNotNull(); | |||
assertThat(result.getPager().getLimit()).isEqualTo(10); | |||
assertThat(result.getPager().getOffset()).isEqualTo(0); | |||
assertThat(result.getPager().getTotal()).isGreaterThan(0); | |||
assertThat(result.getCollections().length).isGreaterThan(0); | |||
assertThat(result.getCollections()[0].getCount()).isGreaterThan(0); | |||
assertThat(result.getCollections()[0].getName()).isNotNull(); | |||
assertThat(result.getCollections()[0].getSize()).isGreaterThan(0); | |||
assertThat(result.getCollections()[0].getIndexCount()).isGreaterThan(0); | |||
assertThat(result.getCollections()[0].getIndexSize()).isGreaterThan(0); | |||
} | |||
} |