2024-10-25 18:09:59 +08:00
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
import moduleHelper from './module-helper';
|
|
|
|
import { uid, formatJsonStr, formatResponse } from './utils';
|
2024-10-23 09:14:01 +08:00
|
|
|
const CloudIDObject = {};
|
2024-10-25 18:09:59 +08:00
|
|
|
const CDNObject = {};
|
|
|
|
function fixCallFunctionData(data) {
|
|
|
|
Object.keys(data).forEach((key) => {
|
2024-10-23 09:14:01 +08:00
|
|
|
if (typeof data[key] === 'object') {
|
2024-10-25 18:09:59 +08:00
|
|
|
fixCallFunctionData(data[key]);
|
2024-10-23 09:14:01 +08:00
|
|
|
}
|
2024-10-25 18:09:59 +08:00
|
|
|
else if (typeof data[key] === 'string') {
|
|
|
|
if (CloudIDObject[data[key]]) {
|
|
|
|
data[key] = CloudIDObject[data[key]];
|
|
|
|
}
|
|
|
|
if (CDNObject[data[key]]) {
|
|
|
|
data[key] = CDNObject[data[key]];
|
|
|
|
}
|
2024-10-23 09:14:01 +08:00
|
|
|
}
|
2024-10-25 18:09:59 +08:00
|
|
|
});
|
2024-10-23 09:14:01 +08:00
|
|
|
}
|
2024-10-25 18:09:59 +08:00
|
|
|
const CloudList = {};
|
2024-10-23 09:14:01 +08:00
|
|
|
export default {
|
2024-10-25 18:09:59 +08:00
|
|
|
WX_CloudCloud(option) {
|
|
|
|
const config = formatJsonStr(option);
|
|
|
|
// @ts-ignore
|
|
|
|
const cloud = new wx.cloud.Cloud(config);
|
|
|
|
CloudList[config.resourceEnv] = cloud;
|
|
|
|
},
|
|
|
|
WX_CloudInit(option) {
|
|
|
|
const config = formatJsonStr(option);
|
|
|
|
if (config.env === '_default_') {
|
|
|
|
wx.cloud.init();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CloudList[config.env].init(config);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
WX_CloudCallFunction(env, conf, callbackId) {
|
2024-10-23 09:14:01 +08:00
|
|
|
const config = formatJsonStr(conf);
|
2024-10-25 18:09:59 +08:00
|
|
|
if (config.data) {
|
|
|
|
fixCallFunctionData(config.data);
|
|
|
|
}
|
|
|
|
let targetCloud;
|
|
|
|
if (env === '_default_') {
|
|
|
|
targetCloud = wx.cloud;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
targetCloud = CloudList[env];
|
|
|
|
}
|
|
|
|
targetCloud.callFunction({
|
|
|
|
...config,
|
|
|
|
success(res) {
|
|
|
|
formatResponse('CallFunctionResult', res);
|
|
|
|
moduleHelper.send('_CloudCallFunctionCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'success', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
fail(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudCallFunctionCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'fail', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
complete(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudCallFunctionCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'complete', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
2024-10-23 09:14:01 +08:00
|
|
|
});
|
|
|
|
},
|
2024-10-25 18:09:59 +08:00
|
|
|
WX_CloudCloudID(env, cloudID) {
|
|
|
|
let targetCloud;
|
|
|
|
if (env === '_default_') {
|
|
|
|
targetCloud = wx.cloud;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
targetCloud = CloudList[env];
|
|
|
|
}
|
|
|
|
const res = targetCloud.CloudID(cloudID);
|
|
|
|
const id = `CloudID-${uid()}`;
|
|
|
|
CloudIDObject[id] = res;
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
WX_CloudCDN(env, target) {
|
|
|
|
let targetCloud;
|
|
|
|
if (env === '_default_') {
|
|
|
|
targetCloud = wx.cloud;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
targetCloud = CloudList[env];
|
|
|
|
}
|
|
|
|
const res = targetCloud.CDN(target);
|
|
|
|
const id = `CDN-${uid()}`;
|
|
|
|
CDNObject[id] = res;
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
WX_CloudCallContainer(env, conf, callbackId) {
|
|
|
|
const config = formatJsonStr(conf);
|
|
|
|
let targetCloud;
|
|
|
|
if (env === '_default_') {
|
|
|
|
targetCloud = wx.cloud;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
targetCloud = CloudList[env];
|
|
|
|
}
|
|
|
|
targetCloud.callContainer({
|
|
|
|
...config,
|
|
|
|
success(res) {
|
|
|
|
formatResponse('CallContainerResult', res);
|
|
|
|
moduleHelper.send('_CloudCallContainerCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'success', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
fail(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudCallContainerCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'fail', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
complete(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudCallContainerCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'complete', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
WX_CloudUploadFile(env, conf, callbackId) {
|
|
|
|
const config = formatJsonStr(conf);
|
|
|
|
let targetCloud;
|
|
|
|
if (env === '_default_') {
|
|
|
|
targetCloud = wx.cloud;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
targetCloud = CloudList[env];
|
|
|
|
}
|
|
|
|
targetCloud.uploadFile({
|
|
|
|
...config,
|
|
|
|
success(res) {
|
|
|
|
formatResponse('UploadFileResult', res);
|
|
|
|
moduleHelper.send('_CloudUploadFileCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'success', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
fail(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudUploadFileCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'fail', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
complete(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudUploadFileCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'complete', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
WX_CloudDownloadFile(env, conf, callbackId) {
|
|
|
|
const config = formatJsonStr(conf);
|
|
|
|
let targetCloud;
|
|
|
|
if (env === '_default_') {
|
|
|
|
targetCloud = wx.cloud;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
targetCloud = CloudList[env];
|
|
|
|
}
|
|
|
|
targetCloud.downloadFile({
|
|
|
|
...config,
|
|
|
|
success(res) {
|
|
|
|
formatResponse('DownloadFileResult', res);
|
|
|
|
moduleHelper.send('_CloudDownloadFileCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'success', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
fail(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudDownloadFileCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'fail', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
complete(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudDownloadFileCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'complete', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
WX_CloudGetTempFileURL(env, conf, callbackId) {
|
|
|
|
const config = formatJsonStr(conf);
|
|
|
|
let targetCloud;
|
|
|
|
if (env === '_default_') {
|
|
|
|
targetCloud = wx.cloud;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
targetCloud = CloudList[env];
|
|
|
|
}
|
|
|
|
targetCloud.getTempFileURL({
|
|
|
|
...config,
|
|
|
|
success(res) {
|
|
|
|
formatResponse('GetTempFileURLResult', res);
|
|
|
|
moduleHelper.send('_CloudGetTempFileURLCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'success', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
fail(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudGetTempFileURLCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'fail', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
complete(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudGetTempFileURLCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'complete', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
WX_CloudDeleteFile(env, conf, callbackId) {
|
|
|
|
const config = formatJsonStr(conf);
|
|
|
|
let targetCloud;
|
|
|
|
if (env === '_default_') {
|
|
|
|
targetCloud = wx.cloud;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
targetCloud = CloudList[env];
|
|
|
|
}
|
|
|
|
targetCloud.deleteFile({
|
|
|
|
...config,
|
|
|
|
success(res) {
|
|
|
|
formatResponse('DeleteFileResult', res);
|
|
|
|
moduleHelper.send('_CloudDeleteFileCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'success', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
fail(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudDeleteFileCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'fail', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
complete(res) {
|
|
|
|
formatResponse('GeneralCallbackResult', res);
|
|
|
|
moduleHelper.send('_CloudDeleteFileCallback', JSON.stringify({
|
|
|
|
callbackId, type: 'complete', res: JSON.stringify(res),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
});
|
2024-10-23 09:14:01 +08:00
|
|
|
},
|
|
|
|
};
|