From 392c9c1028cfc2db151ee7200e78188a6038fd18 Mon Sep 17 00:00:00 2001 From: lq Date: Wed, 24 Dec 2025 08:33:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=82=A3=E8=80=85?= =?UTF-8?q?=E5=B0=B1=E8=AF=8A=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- subPackages/chat/chat.vue | 83 ++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 22 deletions(-) diff --git a/subPackages/chat/chat.vue b/subPackages/chat/chat.vue index 69907a9..278b4bc 100644 --- a/subPackages/chat/chat.vue +++ b/subPackages/chat/chat.vue @@ -447,7 +447,11 @@ 拍摄 - + + + + 视频 + @@ -519,6 +523,11 @@ import { webSocketManager } from '@/utils/ws/websocket'; import { ChatManager } from '@/store/chat/chat.js'; import {getChatRegisterInfoApi, sendToUserApi, upLoadChatFileApi} from "@/request/api/im"; +import { checkDev } from '@/utils/utils'; + +// 根据环境获取上传URL +const isDev = checkDev('dev'); +const uploadBaseUrl = isDev ? 'http://127.0.0.1:18001/api/mobile' : 'https://api.xiaokang88.com/api/mobile'; /** * 辅助函数:按需解析消息内容 @@ -1032,7 +1041,7 @@ export default { uni.showLoading({ title: '上传中...', mask: true }); try { uni.uploadFile({ - url: 'http://127.0.0.1:18001/api/mobile/chat-friends/upload-chat-file', + url: `${uploadBaseUrl}/chat-friends/upload-chat-file`, filePath: tempFilePath, name: 'file', header: { @@ -1213,38 +1222,68 @@ export default { sourceType: ['album', 'camera'], success: res => { res.tempFilePaths.forEach(file => { - // 此处复用了上传音频的接口逻辑,可根据实际需求调整为图片上传接口 - uni.uploadFile({ - url: 'http://127.0.0.1:18001/api/mobile/chat-friends/upload-chat-file', - filePath: file, - name: 'file', - header: { "authorization": uni.getStorageSync('token') }, - formData: {}, - success: (uploadFileRes) => { - this.sendMessage({ - type: 'image', - content: JSON.parse(uploadFileRes.data).result.url - }); - }, - fail: (res) => { console.log(res, '上传失败'); } - }); + this.uploadAndSendFile(file, 'image'); }); }, fail: (e) => { console.log('错误', e); }, }); + } else if (type === 'video') { + // 选择视频 + uni.chooseVideo({ + sourceType: ['album', 'camera'], + maxDuration: 60, // 最长60秒 + camera: 'back', + success: res => { + this.uploadAndSendFile(res.tempFilePath, 'video'); + }, + fail: (e) => { console.log('选择视频错误', e); }, + }); } }, + + /** + * 通用文件上传并发送方法 + */ + uploadAndSendFile(filePath, type) { + uni.showLoading({ title: '上传中...', mask: true }); + uni.uploadFile({ + url: `${uploadBaseUrl}/chat-friends/upload-chat-file`, + filePath: filePath, + name: 'file', + header: { "authorization": uni.getStorageSync('token') }, + formData: {}, + success: (uploadFileRes) => { + uni.hideLoading(); + try { + const result = JSON.parse(uploadFileRes.data); + if (result.code === 0 && result.result && result.result.url) { + this.sendMessage({ + type: type, + content: result.result.url + }); + } else { + uni.showToast({ title: result.message || '上传失败', icon: 'none' }); + } + } catch (e) { + console.error('解析上传结果失败:', e); + uni.showToast({ title: '上传失败', icon: 'none' }); + } + }, + fail: (res) => { + uni.hideLoading(); + console.log(res, '上传失败'); + uni.showToast({ title: '上传失败', icon: 'none' }); + } + }); + }, openCamera() { uni.chooseImage({ count: 1, sourceType: ['camera'], success: res => { - // 这里可以复用上面的上传逻辑 - this.sendMessage({ - type: 'image', - content: res.tempFilePaths[0] // 注意:实际应用中也需要上传 - }); + // 使用通用上传方法上传拍摄的照片 + this.uploadAndSendFile(res.tempFilePaths[0], 'image'); } }); },