goeasy移植
This commit is contained in:
@@ -88,19 +88,19 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
|
||||
import { getMainUserData } from '@/utils/pk-mini/storage'
|
||||
import { TimestamptolocalTime } from '@/utils/pk-mini/timeConversion'
|
||||
// GoEasy 暂时禁用(订阅未续费)
|
||||
// import {
|
||||
// goEasyGetConversations,
|
||||
// goEasyGetMessages,
|
||||
// goEasySendMessage,
|
||||
// goEasySendImageMessage,
|
||||
// goEasyMessageRead,
|
||||
// getPkGoEasy
|
||||
// } from '@/utils/pk-mini/goeasy'
|
||||
// import GoEasy from 'goeasy'
|
||||
import { isGoEasyEnabled } from '@/config/pk-mini'
|
||||
import {
|
||||
goEasyGetConversations,
|
||||
goEasyGetMessages,
|
||||
goEasySendMessage,
|
||||
goEasySendImageMessage,
|
||||
goEasyMessageRead,
|
||||
getPkGoEasy,
|
||||
GoEasy
|
||||
} from '@/utils/pk-mini/goeasy'
|
||||
import PictureMessage from '@/components/pk-mini/chat/PictureMessage.vue'
|
||||
import PKMessage from '@/components/pk-mini/chat/PKMessage.vue'
|
||||
import VoiceMessage from '@/components/pk-mini/chat/VoiceMessage.vue'
|
||||
@@ -118,12 +118,34 @@ const fileInputRef = ref(null)
|
||||
const formatTime = TimestamptolocalTime
|
||||
|
||||
async function loadConversations() {
|
||||
// GoEasy 暂时禁用
|
||||
ElMessage.warning('消息功能暂时不可用(GoEasy 订阅未续费)')
|
||||
if (!isGoEasyEnabled()) {
|
||||
ElMessage.warning('消息功能暂时不可用(GoEasy 订阅未续费)')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await goEasyGetConversations()
|
||||
chatList.value = result?.content || []
|
||||
} catch (e) {
|
||||
console.error('加载会话列表失败', e)
|
||||
}
|
||||
}
|
||||
|
||||
async function selectChat(item) {
|
||||
// GoEasy 暂时禁用
|
||||
if (!isGoEasyEnabled()) return
|
||||
|
||||
selectedChat.value = item
|
||||
try {
|
||||
const messages = await goEasyGetMessages({ id: item.userId, timestamp: null })
|
||||
messagesList.value = messages || []
|
||||
await nextTick()
|
||||
scrollToBottom()
|
||||
// 标记消息已读
|
||||
goEasyMessageRead({ id: item.userId }).catch(() => {})
|
||||
item.unread = 0
|
||||
} catch (e) {
|
||||
console.error('加载消息失败', e)
|
||||
}
|
||||
}
|
||||
|
||||
function scrollToBottom() {
|
||||
@@ -133,22 +155,105 @@ function scrollToBottom() {
|
||||
}
|
||||
|
||||
async function sendMessage() {
|
||||
// GoEasy 暂时禁用
|
||||
ElMessage.warning('消息功能暂时不可用(GoEasy 订阅未续费)')
|
||||
if (!isGoEasyEnabled()) {
|
||||
ElMessage.warning('消息功能暂时不可用(GoEasy 订阅未续费)')
|
||||
return
|
||||
}
|
||||
|
||||
if (!inputText.value.trim()) return
|
||||
if (!selectedChat.value) return
|
||||
|
||||
try {
|
||||
const msg = await goEasySendMessage({
|
||||
text: inputText.value,
|
||||
id: selectedChat.value.userId,
|
||||
avatar: currentUser.value.headerIcon,
|
||||
nickname: currentUser.value.nickName
|
||||
})
|
||||
messagesList.value.push(msg)
|
||||
inputText.value = ''
|
||||
await nextTick()
|
||||
scrollToBottom()
|
||||
} catch (e) {
|
||||
console.error('发送消息失败', e)
|
||||
ElMessage.error('发送失败')
|
||||
}
|
||||
}
|
||||
|
||||
function handleSendImage() {
|
||||
// GoEasy 暂时禁用
|
||||
ElMessage.warning('消息功能暂时不可用(GoEasy 订阅未续费)')
|
||||
if (!isGoEasyEnabled()) {
|
||||
ElMessage.warning('消息功能暂时不可用(GoEasy 订阅未续费)')
|
||||
return
|
||||
}
|
||||
if (!selectedChat.value) {
|
||||
ElMessage.warning('请先选择一个会话')
|
||||
return
|
||||
}
|
||||
fileInputRef.value?.click()
|
||||
}
|
||||
|
||||
async function handleFileSelect(event) {
|
||||
const file = event.target.files?.[0]
|
||||
event.target.value = ''
|
||||
|
||||
if (!file || !isGoEasyEnabled()) return
|
||||
if (!selectedChat.value) return
|
||||
|
||||
try {
|
||||
const msg = await goEasySendImageMessage({
|
||||
imagefile: file,
|
||||
id: selectedChat.value.userId,
|
||||
avatar: currentUser.value.headerIcon,
|
||||
nickname: currentUser.value.nickName
|
||||
})
|
||||
messagesList.value.push(msg)
|
||||
await nextTick()
|
||||
scrollToBottom()
|
||||
} catch (e) {
|
||||
console.error('发送图片失败', e)
|
||||
ElMessage.error('发送图片失败')
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
currentUser.value = getMainUserData() || {}
|
||||
// GoEasy 暂时禁用,不加载会话
|
||||
if (isGoEasyEnabled()) {
|
||||
loadConversations()
|
||||
// 监听新消息
|
||||
const goeasy = getPkGoEasy()
|
||||
if (goeasy) {
|
||||
goeasy.im.on(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, onMessageReceived)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
function onMessageReceived(message) {
|
||||
if (!isGoEasyEnabled()) return
|
||||
// 更新会话列表中的未读数
|
||||
const conv = chatList.value.find(c => c.userId === message.senderId)
|
||||
if (conv) {
|
||||
conv.unread = (conv.unread || 0) + 1
|
||||
conv.lastMessage = message
|
||||
}
|
||||
// 如果当前正在查看该会话,添加消息到列表
|
||||
if (selectedChat.value && selectedChat.value.userId === message.senderId) {
|
||||
messagesList.value.push(message)
|
||||
nextTick(() => scrollToBottom())
|
||||
goEasyMessageRead({ id: message.senderId }).catch(() => {})
|
||||
}
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
if (isGoEasyEnabled()) {
|
||||
const goeasy = getPkGoEasy()
|
||||
if (goeasy) {
|
||||
try {
|
||||
goeasy.im.off(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, onMessageReceived)
|
||||
} catch (e) {
|
||||
console.warn('清理 GoEasy 监听器失败', e)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user