//operate.js文件内容//export const api = 'http://192.168.0.7:8080/'import {api} from '@/utils/operate.js' //封装上传单个图片export const uploadFile = (config = {}) => {const {url = `${api}/app/upload/image`,filePath = null,name = 'file',header = {"Content-Type": "multipart/form-data",},showToast = true} = configreturn new Promise((resolve, reject) => {uni.uploadFile({url, //文件服务器地址filePath, //文件路径name,header,success(res) {showToast && uni.showToast({title: '上传成功',icon: "none"});resolve(JSON.parse(res.data))},fail(err) {showToast && uni.showToast({title: '上传失败',icon: "none"});reject(err)}});})} //封装上传多张图片 export const uploadFileMore = (config = {}) => {const {urls = [],showOnlyOrBatch = 'Only'} = configlet fileIndex = 0;let successList = []return new Promise((resolve, reject) => {(function uploadFileFun() {uploadFile({filePath: urls[fileIndex].url,showToast: false}).then(res => {if (showOnlyOrBatch != 'None' && (showOnlyOrBatch == 'Only' ||showOnlyOrBatch == 'All')) {uni.showToast({title: `正在上传: ${fileIndex+1}/${urls.length}`,icon: "none"})}fileIndex += 1successList.push(res.data)if (fileIndex > urls.length - 1) {fileIndex = 0if (showOnlyOrBatch != 'None' && (showOnlyOrBatch == 'Batch' ||showOnlyOrBatch == 'All')) {uni.showToast({title: '批量上传成功',icon: "none"})}resolve(successList)return}setTimeout(() => {uploadFileFun(urls)}, 1000)}).catch((error) => {console.log('error', error);fileIndex = 0uni.showToast({title: '批量上传失败',icon: "none"});reject(false)})})()})}
来源地址:https://blog.csdn.net/weixin_51014776/article/details/131307891