diff --git a/pages/storage/ext-storage-qiniu.test.js b/pages/storage/ext-storage-qiniu.test.js new file mode 100644 index 0000000000000000000000000000000000000000..fb2ff9c6e0682abcd91912f358aa9ee8a5fd33fc --- /dev/null +++ b/pages/storage/ext-storage-qiniu.test.js @@ -0,0 +1,42 @@ +describe('pages/storage/ext-storage-qiniu.vue', () => { + let page + beforeAll(async () => { + // 重新reLaunch至首页,并获取首页page对象(其中 program 是uni-automator自动注入的全局对象) + page = await program.navigateTo('/pages/storage/ext-storage-qiniu') + await page.waitFor('view') + await page.setData({'isTest':true}) + }) + it('qiniu-storage-上传文件', async () => { + expect.assertions(2); + const res = await page.callMethod('uploadFile', { + filePath: '../../static/logo.png', + cloudPath: Date.now() + 'test-qiniu.png', + isPrivate: false + }) + console.log('res: ---1', res); + expectText(res.fileID,'qiniu://') + expectText(res.fileURL,'https://') + }) + it('qiniu-私有文件-上传', async () => { + expect.assertions(2); + const res = await page.callMethod('uploadFile', { + filePath: '../../static/play.png', + // cloudPath: Date.now() + 'test-qiniu.png', + isPrivate: true + }) + console.log('res: ----2', res); + expectText(res.fileID,'qiniu://') + expectText(res.fileURL,'https://') + }) + it('获取私有文件临时下载链接', async () => { + expect.assertions(3); + expectText(await page.data('privateFileID'),'qiniu://jest') + const res = await page.callMethod('getTempFileURL') + console.log('res: ----3', res); + expectText(res,'&token') + expectText(res,'https://') + }) +}) +function expectText(value,expectValue){ + expect(value).toEqual(expect.stringContaining(expectValue)); +} \ No newline at end of file diff --git a/pages/storage/ext-storage-qiniu.vue b/pages/storage/ext-storage-qiniu.vue index 513ef2cf31d47247003b6cdb1fbf79014f03ff98..80609784e7c23b4b2d8e606c71d5bd058b6fcce8 100644 --- a/pages/storage/ext-storage-qiniu.vue +++ b/pages/storage/ext-storage-qiniu.vue @@ -21,7 +21,8 @@ export default { data() { return { - privateFileID: "" + privateFileID: "", + isTest:false } }, mounted() {}, @@ -76,6 +77,7 @@ }) }, async uploadFile(options) { + console.log('options: ',options); uni.showLoading({ title: '文件上传中...' }) @@ -83,53 +85,60 @@ customUI: true }); const uploadFileOptionsRes = await uniCloudStorageExtCo.getUploadFileOptions({ - cloudPath: `test/${Date.now()}.jpg`, // 支持自定义目录 + cloudPath: `jest/${Date.now()}.jpg`, // 支持自定义目录 }); console.log('uploadFileOptionsRes: ', uploadFileOptionsRes) - const uploadTask = uni.uploadFile({ - ...uploadFileOptionsRes.uploadFileOptions, // 上传文件所需参数 - filePath: options.filePath, // 本地文件路径 - success: async () => { - const res = { - cloudPath: uploadFileOptionsRes.cloudPath, // 文件云端路径 - fileID: uploadFileOptionsRes.fileID, // 文件ID - fileURL: uploadFileOptionsRes.fileURL, // 文件URL(如果是私有权限,则此URL是无法直接访问的) - }; - // 上传成功后的逻辑 - console.log(res); - if (options.isPrivate) { - // 设为私有文件 - const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co", { - customUI: true - }); - await uniCloudStorageExtCo.setFilePrivate({ - fileID: res.fileID - }); - this.privateFileID = res.fileID; - } - uni.showModal({ - content: '图片上传成功,fileID为:' + res.fileID, - showCancel: false - }) - }, - fail: (err) => { - // 上传失败后的逻辑 - console.log(err); - if (err.message !== 'Fail_Cancel') { + + let testRes = new Promise((resolve,reject)=>{ + const uploadTask = uni.uploadFile({ + ...uploadFileOptionsRes.uploadFileOptions, // 上传文件所需参数 + filePath: options.filePath, // 本地文件路径 + success: async () => { + const res = { + cloudPath: uploadFileOptionsRes.cloudPath, // 文件云端路径 + fileID: uploadFileOptionsRes.fileID, // 文件ID + fileURL: uploadFileOptionsRes.fileURL, // 文件URL(如果是私有权限,则此URL是无法直接访问的) + }; + // 上传成功后的逻辑 + console.log(res); + if (options.isPrivate) { + // 设为私有文件 + const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co", { + customUI: true + }); + await uniCloudStorageExtCo.setFilePrivate({ + fileID: res.fileID + }); + this.privateFileID = res.fileID; + } uni.showModal({ - content: `图片上传失败,错误信息为:${err.message}`, + content: '图片上传成功,fileID为:' + res.fileID, showCancel: false }) + resolve(res) + }, + fail: (err) => { + // 上传失败后的逻辑 + console.log(err); + if (err.message !== 'Fail_Cancel') { + uni.showModal({ + content: `图片上传失败,错误信息为:${err.message}`, + showCancel: false + }) + } + reject(err) + }, + complete: () => { + uni.hideLoading() } - }, - complete: () => { - uni.hideLoading() - } - }); - // 监听上传进度 - uploadTask.onProgressUpdate((res) => { - console.log("监听上传进度", res); - }); + }); + // 监听上传进度 + uploadTask.onProgressUpdate((res) => { + console.log("监听上传进度", res); + }); + }) + console.log("testRes",testRes) + return testRes }, async getTempFileURL() { const uniCloudStorageExtCo = uniCloud.importObject("ext-storage-co"); @@ -141,6 +150,9 @@ content: '图片临时下载链接为:' + tempFileURL, showCancel: false }) + if(this.isTest){ + return tempFileURL + } } } } diff --git a/uniCloud-aliyun/cloudfunctions/ext-storage-co/index.obj.js b/uniCloud-aliyun/cloudfunctions/ext-storage-co/index.obj.js index bb817d7f8b46a0c4cc0f905944472d954f429ab2..3cb2aedbafee0e347d633c82d58ceef5a8b85e48 100644 --- a/uniCloud-aliyun/cloudfunctions/ext-storage-co/index.obj.js +++ b/uniCloud-aliyun/cloudfunctions/ext-storage-co/index.obj.js @@ -1,5 +1,5 @@ // 扩展存储自定义域名 -const domain = "qiniu01.dcloud.net.cn"; +const domain = "jest-ext-storage-aliyun.dcloud.net.cn"; module.exports = { _before() {}, getUploadFileOptions(data = {}) {