diff --git a/pages.json b/pages.json
index a5e596e837a3adc7814a9ca1b1e4fc1b0bc66ab4..b6e8b1c9d3029574284f45d8d34774fc906cb381 100644
--- a/pages.json
+++ b/pages.json
@@ -99,6 +99,20 @@
"enablePullDownRefresh": false
}
},
+ {
+ "path": "pages/storage/space-storage",
+ "style": {
+ "navigationBarTitleText": "空间内置云存储",
+ "enablePullDownRefresh": false
+ }
+ },
+ {
+ "path": "pages/storage/ext-storage-qiniu",
+ "style": {
+ "navigationBarTitleText": "扩展存储-七牛云",
+ "enablePullDownRefresh": false
+ }
+ },
{
"path": "pages/schema2code/schema2code",
"style": {
diff --git a/pages/storage/ext-storage-qiniu.vue b/pages/storage/ext-storage-qiniu.vue
new file mode 100644
index 0000000000000000000000000000000000000000..4b280d8632d805758046fe0a8c0c6f36490c0f76
--- /dev/null
+++ b/pages/storage/ext-storage-qiniu.vue
@@ -0,0 +1,168 @@
+
+
+ 直接上传文件到扩展存储-七牛云
+
+
+ 各个小程序平台运行时,网络相关的 API 在使用前需要配置域名白名单。
+
+
+
+
+
+ 先调用uni.chooseImage选完文件/图片/视频后用uni.uploadFile方法上传
+
+ 上传完成后设置为私有文件
+
+
+
+
+
+
+
+
diff --git a/pages/storage/space-storage.vue b/pages/storage/space-storage.vue
new file mode 100644
index 0000000000000000000000000000000000000000..e8cdeb79935cb903182bc7f1d05296f2babfeb24
--- /dev/null
+++ b/pages/storage/space-storage.vue
@@ -0,0 +1,195 @@
+
+
+ 直接上传文件到云存储
+
+
+ 各个小程序平台运行时,网络相关的 API 在使用前需要配置域名白名单。
+
+
+
+
+
+ 先调用uni.chooseImage选完文件/图片/视频后用uniCloud.uploadFile方法上传
+
+ 调用uniCloud.chooseAndUploadFile方法选择文件/图片/视频直接上传
+
+
+
+
+
+
+
diff --git a/pages/storage/storage.vue b/pages/storage/storage.vue
index 1aa986dbb83b5502090cd016091643ba0d7043a4..33014b02c662cc4fce3345caccb3212a93c24465 100644
--- a/pages/storage/storage.vue
+++ b/pages/storage/storage.vue
@@ -1,17 +1,10 @@
- 直接上传文件到云存储
-
-
- 各个小程序平台运行时,网络相关的 API 在使用前需要配置域名白名单。
-
-
-
-
- 先调用chooseImage选完文件/图片/视频后用uploadFile方法上传
-
- 调用chooseAndUploadFile方法选择文件/图片/视频直接上传
+
+ 演示空间自带的云储存
+
+ 演示扩展存储-七牛云
@@ -23,141 +16,10 @@
},
mounted() {},
methods: {
- chooseAndUploadFile(file) {
- uni.showLoading({
- title: '文件上传中...'
- })
- uniCloud.chooseAndUploadFile({
- type: 'image',
- onChooseFile:(res)=> {
- console.log(res);
- const processAll = []
- for (let i = 0; i < res.tempFiles.length; i++) {
- processAll.push(this.cropImg(res.tempFiles[i]))
- }
- return Promise.all(processAll).then((fileList) => {
- let result = {
- tempFilePaths: []
- }
- result.tempFiles = fileList.map((fileItem, index) => {
- result.tempFilePaths.push(fileItem.path)
- return {
- path: fileItem.path,
- cloudPath: '' + Date.now() + index + '.' + fileItem.ext, // 云端路径,这里随便生成了一个
- fileType: fileItem.fileType
- }
- })
- return result
- })
- }
- }).then(res => {
- console.log(res)
- uni.showModal({
- content: JSON.stringify(res),
- showCancel: false
- });
- }).catch((err) => {
- console.log(err);
- uni.showModal({
- content: JSON.stringify(err),
- showCancel: false
- });
- }).finally(() => {
- uni.hideLoading()
- })
- },
- cropImg(file) {
- return new Promise((resolve, reject) => {
- let ext
- let filePathProcessed = file.path // 处理结果
- // #ifdef H5
- ext = file.name.split('.').pop()
- resolve({
- path: filePathProcessed,
- ext,
- fileType: file.fileType
- })
- // #endif
- // #ifndef H5
- uni.getImageInfo({
- src: file.path,
- success(info) {
- ext = info.type.toLowerCase()
- resolve({
- path: filePathProcessed,
- ext,
- fileType: file.fileType
- })
- },
- fail(err) {
- reject(new Error(err.errMsg || '未能获取图片类型'))
- }
- })
- // #endif
- })
- },
- upload() {
- new Promise((resolve, reject) => {
- uni.chooseImage({
- count: 1,
- success: res => {
- const path = res.tempFilePaths[0]
- let ext
- // #ifdef H5
- ext = res.tempFiles[0].name.split('.').pop()
- const options = {
- filePath: path,
- cloudPath: Date.now() + '.' + ext
- }
- resolve(options)
- // #endif
- // #ifndef H5
- uni.getImageInfo({
- src: path,
- success(info) {
- const options = {
- filePath: path,
- cloudPath: Date.now() + '.' + info.type.toLowerCase()
- }
- resolve(options)
- },
- fail(err) {
- reject(new Error(err.errMsg || '未能获取图片类型'))
- }
- })
- // #endif
- },
- fail: () => {
- reject(new Error('Fail_Cancel'))
- }
- })
- }).then((options) => {
- uni.showLoading({
- title: '文件上传中...'
- })
- return uniCloud.uploadFile({
- ...options,
- onUploadProgress(e) {
- console.log(e)
- }
- })
- }).then(res => {
- uni.hideLoading()
- console.log(res);
- uni.showModal({
- content: '图片上传成功,fileId为:' + res.fileID,
- showCancel: false
- })
- }).catch((err) => {
- uni.hideLoading()
- console.log(err);
- if (err.message !== 'Fail_Cancel') {
- uni.showModal({
- content: `图片上传失败,错误信息为:${err.message}`,
- showCancel: false
- })
- }
- })
+ pageTo(url){
+ uni.navigateTo({
+ url
+ });
}
}
}
@@ -168,13 +30,6 @@
padding-bottom: 30px;
}
- .title {
- font-weight: bold;
- text-align: center;
- padding: 20px 0px;
- font-size: 20px;
- }
-
.tips {
color: #999999;
font-size: 14px;
@@ -189,7 +44,4 @@
margin-top: 20px;
}
- .upload-preview {
- width: 100%;
- }
diff --git a/uniCloud-aliyun/cloudfunctions/ext-storage-co/index.obj.js b/uniCloud-aliyun/cloudfunctions/ext-storage-co/index.obj.js
new file mode 100644
index 0000000000000000000000000000000000000000..bb817d7f8b46a0c4cc0f905944472d954f429ab2
--- /dev/null
+++ b/uniCloud-aliyun/cloudfunctions/ext-storage-co/index.obj.js
@@ -0,0 +1,64 @@
+// 扩展存储自定义域名
+const domain = "qiniu01.dcloud.net.cn";
+module.exports = {
+ _before() {},
+ getUploadFileOptions(data = {}) {
+ let {
+ cloudPath, // 前端传过来的文件路径
+ } = data;
+ // 可以在此先判断下此路径是否允许上传等逻辑
+ // ...
+
+ // 然后获取 extStorageManager 对象实例
+ const extStorageManager = uniCloud.getExtStorageManager({
+ provider: "qiniu", // 扩展存储供应商
+ domain: domain, // 带http协议头的域名地址
+ });
+ // 最后调用 extStorageManager.getUploadFileOptions
+ let uploadFileOptionsRes = extStorageManager.getUploadFileOptions({
+ cloudPath: cloudPath,
+ allowUpdate: false, // 是否允许覆盖更新,如果返回前端,建议设置false,代表仅新增,不可覆盖
+ });
+ return uploadFileOptionsRes;
+ },
+ // 设置文件为私有权限
+ async setFilePrivate(data = {}) {
+ let {
+ fileID
+ } = data;
+ // 可以在此先判断下此路径是否允许设置为私有权限
+ // ...
+
+ // 然后获取 extStorageManager 对象实例
+ const extStorageManager = uniCloud.getExtStorageManager({
+ provider: "qiniu", // 扩展存储供应商
+ domain: domain, // 带http协议头的域名地址
+ });
+ let res = await extStorageManager.updateFileStatus({
+ fileID, // 私有文件id
+ isPrivate: true, // true 私有 false 公共
+ });
+ console.log('updateFileStatus: ', res);
+ return res;
+ },
+ // 获取临时下载链接
+ async getTempFileURL(data = {}) {
+ let {
+ fileList
+ } = data;
+ // 可以在此先判断下是否有权限访问这些私有文件
+ // ...
+
+ // 然后获取 extStorageManager 对象实例
+ const extStorageManager = uniCloud.getExtStorageManager({
+ provider: "qiniu", // 扩展存储供应商
+ domain: domain, // 带http协议头的域名地址
+ });
+ let res = extStorageManager.getTempFileURL({
+ fileList
+ });
+ console.log('getTempFileURL: ', res);
+ return res;
+ },
+
+}
\ No newline at end of file
diff --git a/uniCloud-aliyun/cloudfunctions/ext-storage-co/package.json b/uniCloud-aliyun/cloudfunctions/ext-storage-co/package.json
new file mode 100644
index 0000000000000000000000000000000000000000..73cb5b1b54e8a4f16afeca0b3312b8bedb61c33b
--- /dev/null
+++ b/uniCloud-aliyun/cloudfunctions/ext-storage-co/package.json
@@ -0,0 +1,7 @@
+{
+ "name": "ext-storage-co",
+ "dependencies": {},
+ "extensions": {
+ "uni-cloud-ext-storage": {}
+ }
+}
\ No newline at end of file