export default { data() { return { uploadList: [] } }, computed: { }, methods: { // 处理文件上传 handleUpload() { // #ifdef H5 this.handleH5Upload(); // #endif // #ifdef APP-PLUS this.handleAppUpload(); // #endif }, // H5 平台文件上传 handleH5Upload() { console.log("H5 平台文件上传") // 根据fileType参数决定文件选择类型 const chooseType = this.fileType || 'all'; uni.chooseFile({ count: 100, // 最多可以选择100个文件 type: chooseType, // 根据fileType选择文件类型:all, image, video, audio, document success: async (res) => { const { tempFilePaths, tempFiles } = res; if (!tempFiles || tempFiles.length === 0) { return; } // 验证文件大小和类型 const validFiles = []; for (let i = 0; i < tempFiles.length; i++) { const filePath = tempFilePaths[i]; validFiles.push(filePath); } if (validFiles.length === 0) { return; } try { console.log("上传文件成功:1", validFiles) await Promise.all(validFiles.map(filePath => this.uploadFile(filePath))); this.handleUploadSuccess(); } catch (error) { console.error('上传失败:', error); uni.$u.toast('上传失败'); } }, fail: (err) => { console.error('选择文件失败:', err); uni.$u.toast('选择文件失败'); } }); }, // 上传文件 async uploadFile(fileUrl) { try { uni.showLoading({ title: '上传中...', mask: true }); // 调用统一的上传接口 const { data } = await uni.$u.api.uploadFile(fileUrl); const fileObj = { fileSize: data.fileSize, fileSuffix: data.fileSuffix, fileName: data.name, fileUrl: data.url, orderFileType: this.orderFileType }; this.uploadList.push(fileObj); this.$emit('update:fileList', this.uploadList); this.$emit('change', this.uploadList); uni.hideLoading(); } catch (error) { uni.hideLoading(); console.error('文件上传失败:', error); uni.$u.toast('上传失败,请重试'); } }, // App 平台文件上传 handleAppUpload() { const lemonjkFileSelect = uni.requireNativePlugin('lemonjk-FileSelect'); // 根据fileType参数设置文件选择类型 let mimeType = "*/*"; let utisType = "public.data"; switch (this.fileType) { case 'image': mimeType = "image/*"; utisType = "public.image"; break; case 'video': mimeType = "video/*"; utisType = "public.movie"; break; case 'audio': mimeType = "audio/*"; utisType = "public.audio"; break; case 'document': mimeType = "application/*"; utisType = "public.data"; break; default: // 'all' 或其他未识别的类型 mimeType = "*/*"; utisType = "public.data"; } //4.0.0+ 使用示例及高级筛选器配置示例 lemonjkFileSelect.showNativePicker({ pathScope: "/DCIM/Camera", mimeType: mimeType, utisType: utisType, multi: 'yes' }, async result => { const { code, files } = result; if (code == 0) { // 选择的是否全部为图片 const tempFilePaths = files.map(v => "file://" + v.filePath); Promise.all(tempFilePaths.map(v => this.uploadFile(v))).then(() => { this.handleUploadSuccess(); }).catch(() => { uni.$u.toast("上传失败"); }) } else if (result.code == 1001) { uni.showModal({ title: "需要文件访问权限", content: "您还未授权本应用读取文件。为保证您可以正常上传文件,请在权限设置页面打开文件访问权限(不同手机厂商表述可能略有差异)请根据自己手机品牌设置", confirmText: "去授权", cancelText: "算了", success(e) { if (e.confirm) { // 跳转到应用设置页 lemonjkFileSelect.gotoSetting(); } } }) } }) }, }, }