From 16e7531f7d295ee35a67bee77aa24075a51e9d27 Mon Sep 17 00:00:00 2001 From: sx <2427911852@qq.com> Date: Wed, 8 Jan 2025 21:01:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jiuyi2/api/news.js | 45 +++- jiuyi2/common/js/util.js | 2 +- jiuyi2/components/news/book.vue | 14 +- jiuyi2/pages/news/chat/chat.vue | 237 ++++++++++-------- .../components/news-temp-system/index.vue | 24 -- .../news/chat/components/news-temp/index.vue | 59 +++-- jiuyi2/pages/news/chat/jy-voice.vue | 7 +- jiuyi2/pages/news/news.vue | 3 +- jiuyi2/pages/release/video.vue | 4 + jiuyi2/readme.md | 15 +- jiuyi2/vite.config.js | 8 +- 11 files changed, 229 insertions(+), 189 deletions(-) delete mode 100644 jiuyi2/pages/news/chat/components/news-temp-system/index.vue diff --git a/jiuyi2/api/news.js b/jiuyi2/api/news.js index 2bc8f439..0e7bb9f2 100644 --- a/jiuyi2/api/news.js +++ b/jiuyi2/api/news.js @@ -45,8 +45,8 @@ export const news = { deleteFriend(param) { return util.request({ url: `/user/chat/relation/deleteFriend`, - method: 'POST', - data: param.data, + method: 'DELETE', + query: param.query, }) }, @@ -133,6 +133,7 @@ export const news = { }) }, + // grabredLog(param) { return util.request({ url: `/home/grabredLog`, @@ -147,7 +148,45 @@ export const news = { url: `/user/chat/relation/myFriends`, method: 'GET' }) - } + }, + + // 获取私聊聊天记录 + getUserMsgHistory(param) { + return util.request({ + url: `/user/chat/single/getMsgList`, + query: param.query, + method: 'GET' + }) + }, + + // 获取群聊聊天记录 + getGroupMsgHistory(param) { + return util.request({ + url: `/user/chat/group/getMsgList`, + query: param.query, + method: 'GET' + }) + }, + + // 私聊发消息 + sendUserMsg(param) { + return util.request({ + url: `/user/chat/single/sendMsg`, + query: param.query, + data: param.data, + method: 'POST' + }) + }, + + // 群聊发消息 + sendGroupMsg(param) { + return util.request({ + url: `/user/chat/group/sendMsg`, + query: param.query, + data: param.data, + method: 'POST' + }) + }, } export default news \ No newline at end of file diff --git a/jiuyi2/common/js/util.js b/jiuyi2/common/js/util.js index 7e48a5ce..a6ca6a00 100644 --- a/jiuyi2/common/js/util.js +++ b/jiuyi2/common/js/util.js @@ -1731,7 +1731,7 @@ const util = { * @param {Object} option * @param {Object} option.name 标题昵称 * @param {Object} option.msgId 聊天对象id - * @param {Object} option.type 聊天类型 + * @param {Object} option.type 聊天类型 C2C单聊 GROUP群聊 */ toChat(option) { uni.navigateTo({ diff --git a/jiuyi2/components/news/book.vue b/jiuyi2/components/news/book.vue index f6d841e5..17eeed78 100644 --- a/jiuyi2/components/news/book.vue +++ b/jiuyi2/components/news/book.vue @@ -223,8 +223,9 @@ api.news.getFriendList().then(rs => { if (rs.code == 200) { const result = rs.data + userList.data.length = 0 // - userList.data.push(...result.rows.map(item => { + userList.data.push(...result.map(item => { // 取拼音 let char = pinyin.getCamelChars(item.remark || item.userNickname); return { @@ -233,6 +234,7 @@ child: [item] } })) + console.log('userList', userList.data) return } util.alert({ @@ -312,8 +314,8 @@ }) }).then(rs => { api.news.deleteFriend({ - data: { - toUserId: user.userId, + query: { + toUserIds: user.userId, }, }).then(rs => { if (rs.code === 200) { @@ -370,8 +372,8 @@ */ function handleUser(user) { util.toChat({ - name: user.remark || user.profile.nick, - msgId: user.userID, + name: user.remark || user.userNickname, + msgId: user.userId, type: 'C2C', }) } @@ -393,7 +395,7 @@ - 好友申请 + 新朋友 diff --git a/jiuyi2/pages/news/chat/chat.vue b/jiuyi2/pages/news/chat/chat.vue index 4b2d0614..add8da1c 100644 --- a/jiuyi2/pages/news/chat/chat.vue +++ b/jiuyi2/pages/news/chat/chat.vue @@ -10,8 +10,6 @@ nextTick, onUnmounted, onMounted, - inject, - provide, computed, getCurrentInstance, watch, @@ -26,18 +24,16 @@ onPageScroll, onUnload } from "@dcloudio/uni-app" - import { - getHistoryMsg - } from './fn.js' - import newsTempSystem from './components/news-temp-system' + // 单条消息 import newsTemplate from './components/news-temp' - import NewsEmoji from '@/static/news-emoji.png' - import NewsPlus from '@/static/news-plus.png' - import NewsVoice from '@/static/news-voice.png' + // 表情 import emoji from './emoji.vue' + // 语音条 import JyVoice from './jy-voice.vue' + // 加号菜单 import JyPlus from './jy-plus.vue' + import { useStore } from 'vuex' @@ -55,18 +51,22 @@ }) // 输入的内容 const content = ref('') - + // 加载 const loading = ref(false) - const isLast = ref(false) // 用户信息 const userinfo = computed(() => { let result = store.state.userinfo return result }) - //显示的数据 - const list = ref([]) - // 消息分页id 腾讯云im需要 - const nextReqMessageID = ref(undefined) + // 列表数据 + const list = reactive({ + // 列表条数 + limit: 20, + //显示的数据 + data: [], + // + total: 0, + }) // 滚动条位置 const top = ref(0) // 工具条的高度 @@ -95,14 +95,17 @@ // 开启消息监听 addListener() // 获取历史消息 - getHistory(scrollToBottom) - // + getHistory({ + callback: scrollToBottom + }) + // #ifdef APP uni.onKeyboardHeightChange((rs) => { ghostBox.value.height = rs.height + 'px' nextTick(() => { scrollToBottom() }) }) + // #endif }) onReady(() => { @@ -118,15 +121,21 @@ }) onUnload(() => { + // #ifdef APP uni.offKeyboardHeightChange(() => {}) + // #endif videoContext.value.stop() }) // 开启监听消息 function addListener() { let onMessageReceived = function(event) { + console.log('TencentCloudChat.EVENT.MESSAGE_RECEIVED', event) + return + // 获取历史记录 + getHistory() // - list.value.push(...event.data) + list.data.push(...event.data) } uni.$chat.on(TencentCloudChat.EVENT.MESSAGE_RECEIVED, onMessageReceived); @@ -139,67 +148,76 @@ // 获取更多消息记录 function getMoreHistroy() { - if (nextReqMessageID.value) getHistory() + // 获取第一条消息记录 + if (list.total <= list.data.length) return + getHistory({ + param: list.data[0].id + }) } - // 获取历史 - function getHistory(callback = () => {}) { + /** + * 获取历史记录 + * @param {Object} param + */ + function getHistory(param = {}) { // 验证sdk是否准备完毕 let isReady = uni.$chat.isReady(); // - if (!isReady && userinfo.value.userId) { + if (!isReady && userinfo.value.id) { setTimeout(function() { - getHistory(callback) + getHistory(param) }, 200); return } + // 如果没有msgId + // if(!param.msgId) if (loading.value) { // 提示加载中 util.showToastAndRedirect("加载中") return } - if (!isLast.value) { - loading.value = true - // 获取历史记录 - getHistoryMsg({ - msgId: msg.id, - chatType: msg.type, - nextReqMessageID: nextReqMessageID.value, - }).then(res => { + + // + loading.value = true + + // 获取单聊聊天记录 + let request = api.news.getUserMsgHistory + // 如果是群聊 获取群聊聊天记录 + if (msg.type === 'GROUP') api.news.getGroupMsgHistory + + // 获取历史记录 + request({ + query: { + msgId: param.msgId || '', + fromId: userinfo.value.id, + toId: msg.id, + limit: param.limit || list.limit, + }, + }).then(res => { + if (res.code === 200) { + // 价格 const result = res.data - if (page.value == 1) list.value.length = 0 - // 消息列表 - list.value.unshift(...result.messageList.map(item => { - try { - // 1普通消息 2系统消息 - item.messageType = 1 - // console.log('check 1', item.type, TencentCloudChat.TYPES.MSG_CUSTOM, item.payload.data) - // 如果是自定义消息 - if (item.type === TencentCloudChat.TYPES.MSG_CUSTOM && item.payload.data) { - if (item.payload.data) item.payload.data = JSON.parse(item.payload.data) - if (item.payload.data.data) item.payload.data.data = JSON.parse(item.payload.data.data) - // 视频消息类型 新建群聊 - if (['1', 'group_create'].includes(item.payload.data.businessID)) { - item.messageType = 2 - } - } - } catch (err) { - console.log('map catch', err) - } + // 追加 + list.data.unshift(...result.list.map(item => { + item.callbackData = JSON.parse(item.callbackJson) return item })) - page.value++ - console.log('getHistoryMsg then', result.messageList.length, list.value, res) - // 用于续拉,分页续拉时需传入该字段。 - nextReqMessageID.value = result.nextReqMessageID || undefined; + // 总数 + list.total = result.totalCount + console.log('list', list.data) nextTick(() => { - callback() + param.callback && param.callback() }) - }).finally(() => { - loading.value = false + return + } + util.alert({ + content: res.msg, + showCancel: false, }) - } + }).finally(() => { + loading.value = false + }) } // 滚动至底部 @@ -236,22 +254,15 @@ // 点击发送 function handleSend() { - // 创建文本消息 - let message = uni.$chat.createTextMessage({ - to: msg.id, - conversationType: msg.type, - payload: { - text: content.value - }, - // 消息已读回执 - needReadReceipt: true, - // 消息自定义数据(云端保存,会发送到对端,程序卸载重装后还能拉取到) - // cloudCustomData: 'content.value', - }); - // 发送消息 sendMsg({ - message, + query: { + toUserId: msg.id, + msgType: TencentCloudChat.TYPES.MSG_TEXT, + }, + data: { + text: content.value + }, success: () => { // 清空已发送的消息 content.value = '' @@ -275,12 +286,27 @@ * @param {Object} param */ function sendMsg(param) { + // + const request = api.news.sendUserMsg + // + if (msg.type == 'group') request = api.news.sendGroupMsg // 发送消息 - uni.$chat.sendMessage(param.message).then((rs) => { - console.log('rs', rs) - param.success ? param.success() : '' - // - getHistory(scrollToBottom()) + request({ + query: param.query, + data: param.data, + }).then((rs) => { + if (rs.code == 200) { + param.success ? param.success() : '' + // + getHistory({ + callback: scrollToBottom, + }) + return + } + util.alert({ + content: rs.msg, + showCancel: false, + }) }).catch((rs) => { console.log('sendMsg error:', rs); }) @@ -407,7 +433,6 @@ videoUrl.value = item.payload.videoUrl // 进入全屏 videoContext.value.requestFullScreen() - videoContext.value.requestFullScreen() } // 监听视频是否全屏 @@ -419,37 +444,29 @@