| | |
| | | data: { |
| | | currentFiles: [], |
| | | showPreview: false, |
| | | previewImageUrls: [] |
| | | previewImageUrls: [], |
| | | groups: [{ |
| | | text: '选择图片', |
| | | value: 0 |
| | | }, { |
| | | text: '从聊天记录选择文件', |
| | | value: 1 |
| | | }], |
| | | }, |
| | | |
| | | ready() {}, |
| | | |
| | | methods: { |
| | | showActionSheet() { |
| | | this.setData({ |
| | | showSheet: true |
| | | }) |
| | | }, |
| | | previewImage(e) { |
| | | const { |
| | | index |
| | |
| | | showPreview: true |
| | | }); |
| | | }, |
| | | previewFile(e) { |
| | | const { |
| | | index |
| | | } = e.currentTarget.dataset; |
| | | const file = this.data.files[index] |
| | | wx.openDocument({ |
| | | filePath: file.url, |
| | | }); |
| | | }, |
| | | actionClick(e) { |
| | | switch (e.detail.value) { |
| | | case 0: |
| | | this.chooseImage() |
| | | break; |
| | | case 1: |
| | | this.chooseMessageFile() |
| | | break; |
| | | default: |
| | | this.chooseImage() |
| | | break; |
| | | } |
| | | this.setData({ |
| | | showSheet: false |
| | | }) |
| | | }, |
| | | chooseImage() { |
| | | if (this.uploading) return; |
| | | wx.chooseImage({ |
| | | wx.chooseMedia({ |
| | | count: this.data.maxCount - this.data.files.length, |
| | | sizeType:['compressed'], |
| | | sizeType: ['compressed'], |
| | | mediaType: ['image'], |
| | | success: res => { |
| | | console.log('chooseImage resp', res) |
| | | // 首先检查文件大小 |
| | | let invalidIndex = -1; // @ts-ignore |
| | | res.tempFilePaths = res.tempFiles.map(item => item.tempFilePath) |
| | | res.tempFiles.forEach((item, index) => { |
| | | if (item.size > this.data.maxSize) { |
| | | invalidIndex = index; |
| | |
| | | }, {}); |
| | | return; |
| | | } // 获取文件内容 |
| | | |
| | | |
| | | |
| | | const mgr = wx.getFileSystemManager(); |
| | | const contents = res.tempFilePaths.map(item => { |
| | |
| | | }; // 触发选中的事件,开发者根据内容来上传文件,上传了把上传的结果反馈到files属性里面 |
| | | |
| | | this.triggerEvent('select', obj, {}); |
| | | // const files = res.tempFilePaths.map((item, i) => ({ |
| | | // loading: true, |
| | | // // @ts-ignore |
| | | // url: `data:image/jpg;base64,${wx.arrayBufferToBase64(contents[i])}` |
| | | // })); |
| | | // var locationPath = [] |
| | | // res.tempFilePaths.forEach(item =>{ |
| | | // wx.getImageInfo({ |
| | | // src: item, |
| | | // success(data){ |
| | | // console.log(data) |
| | | // locationPath = data.path |
| | | // } |
| | | // }) |
| | | // }) |
| | | |
| | | let files = res.tempFilePaths.map((item, i) => ({ |
| | | |
| | | let files = res.tempFiles.map((item, i) => ({ |
| | | loading: true, |
| | | url: item |
| | | url: item.tempFilePath, |
| | | type: item.fileType, |
| | | })) |
| | | //压缩图片 |
| | | wx.compressImage({ |
| | | src:files[0].url, |
| | | quality:20, |
| | | success(data){ |
| | | src: files[0].url, |
| | | quality: 20, |
| | | success(data) { |
| | | console.log('compressImage', data) |
| | | }, |
| | | fail(){ |
| | | fail() { |
| | | |
| | | } |
| | | }) |
| | | |
| | | |
| | | |
| | | |
| | | if (!files || !files.length) return; |
| | | const newFiles = this.data.files.concat(files); |
| | |
| | | } |
| | | }); |
| | | }, |
| | | chooseMessageFile() { |
| | | if (this.uploading) return; |
| | | wx.chooseMessageFile({ |
| | | count: this.data.maxCount - this.data.files.length, |
| | | type: 'file', |
| | | extension: ['xls', 'xlsx', 'csv', 'doc', 'docx', 'pdf', 'ppt', 'pptx'], |
| | | success: res => { |
| | | console.log('chooseMessageFile', res) |
| | | // 首先检查文件大小 |
| | | let invalidIndex = -1; // @ts-ignore |
| | | res.tempFilePaths = res.tempFiles.map(item => item.path) |
| | | res.tempFiles.forEach((item, index) => { |
| | | if (item.size > this.data.maxSize) { |
| | | invalidIndex = index; |
| | | } |
| | | }); |
| | | if (typeof this.data.select === 'function') { |
| | | const ret = this.data.select(res); |
| | | if (ret === false) { |
| | | return; |
| | | } |
| | | } |
| | | if (invalidIndex >= 0) { |
| | | this.triggerEvent('fail', { |
| | | type: 1, |
| | | errMsg: `chooseMessageFile:fail size exceed ${this.data.maxSize}`, |
| | | total: res.tempFilePaths.length, |
| | | index: invalidIndex |
| | | }, {}); |
| | | return; |
| | | } // 获取文件内容 |
| | | |
| | | |
| | | const mgr = wx.getFileSystemManager(); |
| | | const contents = res.tempFilePaths.map(item => { |
| | | // @ts-ignore |
| | | const fileContent = mgr.readFileSync(item); |
| | | return fileContent; |
| | | }); |
| | | const obj = { |
| | | tempFilePaths: res.tempFilePaths, |
| | | tempFiles: res.tempFiles, |
| | | contents |
| | | }; // 触发选中的事件,开发者根据内容来上传文件,上传了把上传的结果反馈到files属性里面 |
| | | |
| | | this.triggerEvent('select', obj, {}); |
| | | |
| | | let files = res.tempFiles.map((item, i) => { |
| | | // 判断文件类型,对应不同样式 |
| | | let extensionClass = '' |
| | | switch (item.name.split('.')[1]) { |
| | | case 'xls': |
| | | case 'xlsx': |
| | | case 'csv': |
| | | extensionClass = 'file_xlsx' |
| | | break; |
| | | case 'doc': |
| | | case 'docx': |
| | | extensionClass = 'file_word' |
| | | break; |
| | | case 'pdf': |
| | | extensionClass = 'file_pdf' |
| | | break; |
| | | case 'ppt': |
| | | case 'pptx': |
| | | extensionClass = 'file_ppt' |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | // 拆分文件名称,实现文本过长时,中间省略 |
| | | const ellipsisName = { |
| | | start: item.name.slice(0, -5), |
| | | // 取末尾5个字符常态显示 |
| | | end: item.name.substr(-5, 5) |
| | | } |
| | | // 文件大小格式化 |
| | | let size = parseInt(item.size / 1024) |
| | | if (size > 1024) { |
| | | size = parseInt(size / 1024) + ' MB' |
| | | } else { |
| | | size += ' KB' |
| | | } |
| | | return { |
| | | loading: true, |
| | | url: item.path, |
| | | name: item.name, |
| | | ellipsisName, |
| | | styleClass: extensionClass, |
| | | type: item.type, |
| | | time: item.time, |
| | | size |
| | | } |
| | | }) |
| | | if (!files || !files.length) return; |
| | | const newFiles = this.data.files.concat(files); |
| | | this.setData({ |
| | | files: newFiles, |
| | | currentFiles: newFiles |
| | | }); |
| | | this.triggerEvent('uploadImg', { |
| | | newFiles |
| | | }, {}) |
| | | } |
| | | }) |
| | | }, |
| | | deletePic(e) { |
| | | const index = e.detail.index; |
| | | const files = this.data.files; |
| | |
| | | index, |
| | | item: file[0] |
| | | }); |
| | | } |
| | | }, |
| | | |
| | | |
| | | } |
| | | }); |