From 5d7985444198a05809669775a87731fe7ca549f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B9=81=E6=98=9F=E5=85=A5=E6=A2=A6?= <1738464117@qq.com> Date: Fri, 18 Aug 2023 16:05:58 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utlis/myUploadAdapter.js | 104 ++++-------------- src/views/customer/components/ConsultItem.vue | 100 +++++------------ src/views/customer/components/Operation.vue | 79 ++++++++++++- 3 files changed, 125 insertions(+), 158 deletions(-) diff --git a/src/utlis/myUploadAdapter.js b/src/utlis/myUploadAdapter.js index e7f4517..4ba8eb8 100644 --- a/src/utlis/myUploadAdapter.js +++ b/src/utlis/myUploadAdapter.js @@ -1,3 +1,5 @@ +import axios from 'axios'; + function MyUploadAdapter(loader) { let reader; @@ -5,76 +7,18 @@ function MyUploadAdapter(loader) { upload: async () => { reader = new FileReader(); const file = await loader.file; - - // console.log(loader,file,reader,'loader,file,reader') - - const result = await new Promise((resolve, reject) => { - reader.addEventListener('load', () => { - // 检查图片大小de - if (file.size > 2 * 1024 * 1024) { - // 2MB - // 图片大小超出限制,不执行上传 - reject('图片大小超过限制'); - } else { - // 加载图片 - const img = new Image(); - img.src = reader.result; - img.onload = () => { - const maxDimension = 300; - // 最大尺寸maxDimension,可以根据需要调整这个值来控制压缩后的图片尺寸 - let newWidth = img.width; - let newHeight = img.height; - - if (newWidth > newHeight) { - if (newWidth > maxDimension) { - newHeight *= maxDimension / newWidth; - newWidth = maxDimension; - } - } else { - if (newHeight > maxDimension) { - newWidth *= maxDimension / newHeight; - newHeight = maxDimension; - } - } - const canvas = document.createElement('canvas'); - //设置canvas尺寸为压缩后的尺寸 - canvas.width = newWidth; - canvas.height = newHeight; - const ctx = canvas.getContext('2d'); - // canvas中,png转jpg会变黑底,所以先给canvas铺一张白底 - ctx.fillStyle = '#fff'; - // fillRect()方法绘制一个填充了内容的矩形,这个矩形的开始点(左上点)在 - // (x, y) ,它的宽度和高度分别由width 和 height 确定,填充样式由当前的fillStyle 决定。 - ctx.fillRect(0, 0, canvas.width, canvas.height); - // 绘制图像 - ctx.drawImage(img, 0, 0, newWidth, newHeight); - // canvas转图片达到图片压缩效果 - // 返回一个包含图片展示的 data URI base64 在指定图片格式为 image/jpeg 或 image/webp的情况下, - // 可以从 0 到 1 的区间内选择图片的质量。如果超出取值范围,将会使用默认值 0.92。其他参数会被忽略。 - const dataUrl = canvas.toDataURL('image/jpeg', 0.8); - - // base64格式文件转成Blob文件格式 - // const blobFile = dataURLtoBlob(dataUrl); - // 拿到这个blobFile文件也可以作为返回值 - - resolve({ default: dataUrl }); - }; - } - }); - - reader.addEventListener('error', (err) => { - reject(err); - }); - - reader.addEventListener('abort', () => { - reject(); - }); - // readAsDataURL 方法会读取指定的 Blob 或 File 对象。读取操作完成的时候,readyState 会变成已完成DONE,并触发 loadend (en-US) 事件, - // 同时 result 属性将包含一个data:URL格式的字符串(base64编码)以表示所读取文件的内容。 - reader.readAsDataURL(file); + console.log('------------------'); + console.log(file); + const formData = new FormData(); + formData.append('file', file); + const res = await axios.post('/api/conversation/img/save', formData, { + headers: { + 'Content-Type': 'multipart/form-data', + }, }); - - return result; + const imgUrl = `http://localhost:19000/${res.data.message}`; + console.log('imgUrl', imgUrl); + return imgUrl; }, abort: () => { reader.abort(); @@ -83,17 +27,17 @@ function MyUploadAdapter(loader) { } // canvas生成的格式为base64,如果需要Blob格式可按如下进行转化 -// function dataURLtoBlob(dataurl) { -// const arr = dataurl.split(","), -// mime = arr[0].match(/:(.*?);/)[1], -// bstr = atob(arr[1]); -// let n = bstr.length; -// const u8arr = new Uint8Array(n); -// while (n--) { -// u8arr[n] = bstr.charCodeAt(n); -// } -// return new Blob([u8arr], { type: mime }); -// } +function dataURLtoBlob(dataurl) { + const arr = dataurl.split(','), + mime = arr[0].match(/:(.*?);/)[1], + bstr = atob(arr[1]); + let n = bstr.length; + const u8arr = new Uint8Array(n); + while (n--) { + u8arr[n] = bstr.charCodeAt(n); + } + return new Blob([u8arr], { type: mime }); +} function MyAdapterPlugin(editor) { editor.plugins.get('FileRepository').createUploadAdapter = (loader) => { diff --git a/src/views/customer/components/ConsultItem.vue b/src/views/customer/components/ConsultItem.vue index 1a75df6..9b0d244 100644 --- a/src/views/customer/components/ConsultItem.vue +++ b/src/views/customer/components/ConsultItem.vue @@ -1,4 +1,3 @@ -