From 6b16395a9a9babb18d01cf7213add0b84863b2b1 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: Thu, 10 Aug 2023 14:13:41 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E9=83=A8=E5=88=86api=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 2 +- src/apis/sendMessage.js | 11 ++ src/apis/user.js | 2 +- src/stores/ckeditor.js | 11 ++ src/utlis/myUploadAdapter.js | 104 ++++++++++++++++++ src/views/customer/LoginView.vue | 32 +++--- src/views/customer/components/Chat.vue | 6 +- src/views/customer/components/JimiLeft.vue | 2 +- .../customer/components/JimiLeftHeader.vue | 11 +- src/views/customer/components/Operation.vue | 67 +++++------ 10 files changed, 183 insertions(+), 65 deletions(-) create mode 100644 src/apis/sendMessage.js create mode 100644 src/stores/ckeditor.js create mode 100644 src/utlis/myUploadAdapter.js diff --git a/package-lock.json b/package-lock.json index 73f84ae..b7cf98e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4098,7 +4098,7 @@ "version": "5.1.6", "resolved": "https://registry.npmmirror.com/typescript/-/typescript-5.1.6.tgz", "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", - "dev": true, + "devOptional": true, "peer": true, "bin": { "tsc": "bin/tsc", diff --git a/src/apis/sendMessage.js b/src/apis/sendMessage.js new file mode 100644 index 0000000..df2fe5b --- /dev/null +++ b/src/apis/sendMessage.js @@ -0,0 +1,11 @@ +import axios from '../axios/axios'; + +// 获取用户信息 + +export function sendMessage(file) { + return axios.post('/conversation/img/save', file, { + headers: { + 'Content-Type': 'multipart/form-data', + }, + }); +} diff --git a/src/apis/user.js b/src/apis/user.js index 3a32dee..dede786 100644 --- a/src/apis/user.js +++ b/src/apis/user.js @@ -9,7 +9,7 @@ import axios from '../axios/axios'; export function login(userName, password) { return axios.post( - '/chat/user/save', + '/login', { userName, password, diff --git a/src/stores/ckeditor.js b/src/stores/ckeditor.js new file mode 100644 index 0000000..a3da722 --- /dev/null +++ b/src/stores/ckeditor.js @@ -0,0 +1,11 @@ +import { defineStore } from 'pinia'; +import { ref } from 'vue'; + +const useCkeditorStore = defineStore('ckeditor', () => { + const editorResult = ref(''); + return { + editorResult, + }; +}); + +export default useCkeditorStore; diff --git a/src/utlis/myUploadAdapter.js b/src/utlis/myUploadAdapter.js new file mode 100644 index 0000000..e7f4517 --- /dev/null +++ b/src/utlis/myUploadAdapter.js @@ -0,0 +1,104 @@ +function MyUploadAdapter(loader) { + let reader; + + return { + 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); + }); + + return result; + }, + abort: () => { + reader.abort(); + }, + }; +} + +// 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 MyAdapterPlugin(editor) { + editor.plugins.get('FileRepository').createUploadAdapter = (loader) => { + return new MyUploadAdapter(loader); + }; +} + +export default MyAdapterPlugin; diff --git a/src/views/customer/LoginView.vue b/src/views/customer/LoginView.vue index 6350168..4bff4c4 100644 --- a/src/views/customer/LoginView.vue +++ b/src/views/customer/LoginView.vue @@ -9,8 +9,8 @@ const loginFormRef = ref(); const router = useRouter(); const formData = reactive({ form: { - username: 'aaaa', - password: '123456', + username: 'admin', + password: 'admin', }, rules: { username: [ @@ -53,24 +53,18 @@ const onSubmit = async () => { const res = await login(formData.form.username, formData.form.password); console.log(res); // 响应成功的处理 - if (res.data.code === 200) { - const jsonData = JSON.parse(res.data.data); + if (res.data.code === 19000200) { + const jsonData = res.data.data; + console.log(jsonData); - console.log(jsonData.username); - console.log(jsonData.url); - console.log(jsonData.auth); - username = jsonData.username; - console.log(username); - if (jsonData.auth === '0') { - router.push({ name: 'admin' }); - } else { - router.push({ - name: 'home', - query: { - username: username, - }, - }); - } + router.push({ + name: 'home', + query: { + userId: res.data.data.userId, + username: res.data.data.userName, + avatar: res.data.data.avatar, + }, + }); } else { alert(res.data.msg); } diff --git a/src/views/customer/components/Chat.vue b/src/views/customer/components/Chat.vue index 4e1ce8c..dbd60e6 100644 --- a/src/views/customer/components/Chat.vue +++ b/src/views/customer/components/Chat.vue @@ -101,7 +101,6 @@ li { } .message-right { margin-top: 10px; - margin-left: 10px; width: 100%; display: flex; align-items: flex-start; @@ -165,4 +164,9 @@ p { .name { font-size: smaller; } + +.message-l img { + width: 200px; + height: 200px; +} diff --git a/src/views/customer/components/JimiLeft.vue b/src/views/customer/components/JimiLeft.vue index 078a841..b7d333f 100644 --- a/src/views/customer/components/JimiLeft.vue +++ b/src/views/customer/components/JimiLeft.vue @@ -38,7 +38,7 @@ const contracts = reactive([ }, ]); onMounted(() => { - gteAllList(1); + // gteAllList(1); }); async function gteAllList(userId) { diff --git a/src/views/customer/components/JimiLeftHeader.vue b/src/views/customer/components/JimiLeftHeader.vue index 137eb15..7ae0322 100644 --- a/src/views/customer/components/JimiLeftHeader.vue +++ b/src/views/customer/components/JimiLeftHeader.vue @@ -1,9 +1,9 @@