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) }) } }) }, } })