// utils/upload.js import config from '@/config' import store from '@/store' import {getOssToken} from "./api"; /** * 通用上传图片函数 * @param {String} uploadUrl - 上传接口地址(如 '/app/common/uploadImg') * @param {Object} options - 可选参数 * options.quality 压缩质量 (H5) * options.maxWidth 最大宽度 (H5) * @returns Promise<{url: string, name: string}> */ export function uploadImage(imgUrl, isHk) { // const tempFilePaths = e.tempFilePaths; // const imgUrl = tempFilePaths[0]; return new Promise((resolve, reject) => { // if (process.env.UNI_PLATFORM === 'h5') { // // H5 压缩后上传 // compressImage(imgUrl, quality, maxWidth) // .then((file) =>{ // // uploadCompressedFile(file, uploadUrl, resolve, reject) // const compressedUrl = URL.createObjectURL(file); // uploadFile(compressedUrl, uploadUrl, resolve, reject); // }) // .catch(reject); // } else { uni.compressImage({ src: imgUrl, quality:75, success: (res) => { // uploadFile(res.tempFilePath, uploadUrl, resolve, reject); const compressedUrl = res.tempFilePath; uploadFile(compressedUrl,isHk, resolve, reject); }, fail: reject, }); // } }); } /** * H5:canvas 压缩图片 */ function compressImage(filePath, quality = 0.6, maxWidth = 1080) { return new Promise((resolve, reject) => { const img = new Image(); img.src = filePath; img.crossOrigin = 'anonymous'; img.onload = function () { const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); const w = img.width; const h = img.height; const scale = w > maxWidth ? maxWidth / w : 1; canvas.width = w * scale; canvas.height = h * scale; ctx.drawImage(img, 0, 0, canvas.width, canvas.height); canvas.toBlob( (blob) => { const compressedFile = new File([blob], 'compressed.jpg', { type: 'image/jpeg', }); resolve(compressedFile); }, 'image/jpeg', quality ); }; img.onerror = reject; }); } /** * H5 上传压缩后的文件 */ function uploadCompressedFile(file, uploadUrl, resolve, reject) { uni.showLoading({ title: '上传中...', icon: 'none' }); const formData = new FormData(); formData.append('file', file); uni.request({ url: config.baseUrl + uploadUrl, method: 'POST', header: { 'Content-Type': 'multipart/form-data' }, data: formData, success: (res) => { uni.hideLoading(); let resData = res.data; if (typeof resData === 'string') resData = JSON.parse(resData); resolve({ url: resData.url, name: resData.fileName }); }, fail: (err) => { uni.hideLoading(); reject(err); }, }); } /** * App / 小程序端上传文件 */ function uploadFile(imgUrl, isHk, resolve, reject) { uni.showLoading({ title: '上传中...', icon: 'none' }); if (isHk){ getOssToken().then(res=>{ let foData = res ||{}; let filename = imgUrl.substring(imgUrl.lastIndexOf('/')); foData.key = foData.key +filename; uni.uploadFile({ url: config.aliOssUrl, filePath: imgUrl, name: 'file', formData:foData, success: (res2) => { uni.hideLoading(); resolve({ url: config.aliOssUrl+"/"+foData.key, name: filename }); }, fail: (err) => { uni.hideLoading(); uni.showToast({ title: "上传失败", icon: "error", duration: 2000 }) reject(err); }, }); }) }else{ uploadLocal(imgUrl,resolve, reject); } } const uploadLocal =(imgUrl, resolve, reject)=>{ uni.uploadFile({ url: config.host + `app/car/uploadImg`, filePath: imgUrl, name: 'file', header: { "Authorization": store.state.sso_user_token }, success: (res) => { uni.hideLoading(); let resData = JSON.parse(res.data); resolve({ url: resData.url, name: resData.fileName }); }, fail: (err) => { uni.hideLoading(); uni.showToast({ title: "上传失败", icon: "error", duration: 2000 }) reject(err); }, }); }