Ver Fonte

文件上传

zhaoxiaohai há 3 anos atrás
pai
commit
ebfa7f731d

+ 125 - 0
components/My_upload/index.js

@@ -0,0 +1,125 @@
+const _Http = getApp().globalData.http;
+Component({
+    /**
+     * 组件的属性列表
+     */
+    properties: {
+        /* 最大上传数量 */
+        maxCount: {
+            type: String,
+            value: 9
+        },
+        /* 文件上传类型限制 all media image file video */
+        accept: {
+            type: String,
+            value: "all"
+        }
+    },
+
+    /**
+     * 组件的初始数据
+     */
+    data: {
+        fileList: []
+    },
+
+    /**
+     * 组件的方法列表
+     */
+    methods: {
+        /* 上传文件 */
+        afterRead(event) {
+            for (let i = 0; i < event.detail.file.length; i++) {
+                // 初始化数据
+                let that = this,
+                    data = this.requestType(event.detail.file[i]);
+                //发送请求
+                wx.getFileSystemManager().readFile({
+                    filePath: event.detail.file[i].url,
+                    success: result => {
+                        //返回临时文件路径
+                        const fileData = result.data;
+                        _Http.basic(data).then(res => {
+                            console.log("res", res)
+                            if (res.msg == "成功") {
+                                that.uploadFile(res.data, fileData)
+                            } else {
+                                wx.showToast({
+                                    title: `${fileData.filename}.${fileData.serialfilename}`,
+                                    icon: "none"
+                                })
+                            }
+                        })
+                    },
+                    fail: console.error
+                })
+            }
+        },
+        /* 请求类型 */
+        requestType(file) {
+            //获取文件后缀
+            var index = file.url.lastIndexOf(".");
+            var ext = file.url.substr(index + 1);
+            //文件名称
+            return {
+                "classname": "system.attachment.huawei.OBS",
+                "method": "getFileName",
+                "content": {
+                    "filename": file.name,
+                    "filetype": ext,
+                    "parentid": 2
+                }
+            }
+        },
+        /* 上传成功反馈 */
+        uploadFile(res, data) {
+            console.log('asd', res, data)
+            var that = this
+            wx.request({
+                url: res.uploadurl,
+                method: "PUT",
+                data: data,
+                header: {
+                    'content-type': 'application/octet-stream'
+                },
+                success(a) {
+                    console.log("a", a)
+                    _Http.basic({
+                        "classname": "system.attachment.huawei.OBS",
+                        "method": "uploadSuccess",
+                        "content": {
+                            "serialfilename": res.serialfilename
+                        }
+                    }).then(s => {
+                        return console.log(s);
+                        if (res.msg != "成功") return;
+                        let fileList = that.data.fileList;
+                        for (let i = 0; i < res.data.length; i++) {
+                            let arr = {
+                                url: res.data[i].fobsurl,
+                                ownerid: res.data[i].ownerid,
+                                tattachmentid: res.data[i].tattachmentid,
+                                ownertable: res.data[i].ownertable,
+                                fdocument: res.data[i].fdocument,
+                            }
+                            fileList.push(arr)
+                        };
+                        // 用户头像 先删除 在修改
+                        if (that.data.upType == "userImage" && fileList.length >= 2) {
+                            that.dleeteDealWith(0);
+                        }
+                        that.setData({
+                            fileList
+                        });
+                        /* 返回数据 */
+                        that.triggerEvent("imageChange", {
+                            fileList
+                        })
+                    }).catch(err => {
+                        console.log(err)
+                    })
+                }
+            })
+        },
+    }
+})

+ 0 - 0
components/My_uploadFiles/index.json → components/My_upload/index.json


+ 1 - 0
components/My_upload/index.scss

@@ -0,0 +1 @@
+/* components/My_upload/index.wxss */

+ 3 - 0
components/My_upload/index.wxml

@@ -0,0 +1,3 @@
+<van-uploader bind:after-read="afterRead" accept='{{accept}}' max-count="{{maxCount}}" preview-image='{{false}}' multiple>
+    <slot />
+</van-uploader>

+ 0 - 52
components/My_uploadFiles/index.js

@@ -1,52 +0,0 @@
-// components/My_uploadFiles/index.js
-Component({
-    /**
-     * 组件的属性列表
-     */
-    properties: {
-        fileList: {
-            type: Array,
-            value: []
-        }
-    },
-
-    /**
-     * 组件的初始数据
-     */
-    data: {
-
-    },
-
-    /**
-     * 组件的方法列表
-     */
-    methods: {
-        afterRead(event) {
-            const {
-                file
-            } = event.detail;
-            // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
-            wx.uploadFile({
-                url: 'https://example.weixin.qq.com/upload', // 仅为示例,非真实的接口地址
-                filePath: file.url,
-                name: 'file',
-                formData: {
-                    user: 'test'
-                },
-                success(res) {
-                    // 上传完成需要更新 fileList
-                    const {
-                        fileList = []
-                    } = this.data;
-                    fileList.push({
-                        ...file,
-                        url: res.data
-                    });
-                    this.setData({
-                        fileList
-                    });
-                },
-            });
-        },
-    }
-})

+ 0 - 1
components/My_uploadFiles/index.scss

@@ -1 +0,0 @@
-/* components/My_uploadFiles/index.wxss */

+ 0 - 3
components/My_uploadFiles/index.wxml

@@ -1,3 +0,0 @@
-<van-uploader file-list="{{ fileList }}" bind:after-read="afterRead">
-    
-</van-uploader>