jiuyiUniapp/jiuyi2/components/news/msgList.vue

391 lines
8.9 KiB
Vue
Raw Normal View History

2024-12-18 15:46:27 +08:00
<script setup>
2025-02-15 16:09:39 +08:00
/**
* 消息列表
*/
import api from '@/api/index.js'
import {
ref,
computed,
reactive,
onMounted,
onUnmounted,
getCurrentInstance,
} from 'vue'
// 腾讯云聊天
import TencentCloudChat from '@tencentcloud/chat';
// 工具库
import util from '@/common/js/util.js'
import {
useStore,
} from 'vuex'
const {
proxy
} = getCurrentInstance()
const store = useStore()
const userinfo = computed(() => {
let result = store.state.userinfo
return result
})
// 右滑菜单
const rightOption = [{
text: '删除',
style: {
backgroundColor: '#F85050'
},
fn: (item) => delMsg(item)
}, {
text: '设为已读',
style: {
backgroundColor: '#00ADEE'
},
fn: (item) => setRead(item)
},]
// 系统
const systemRightOption = [{
text: '删除',
style: {
backgroundColor: '#F85050'
},
}, {
text: '设为已读',
style: {
backgroundColor: '#00ADEE'
},
},]
2025-02-19 16:36:12 +08:00
// 传入参数
const props = defineProps({
// 类型
type: {
type: Number,
default: 0,
},
})
2025-02-15 16:09:39 +08:00
// 用户列表
const list = reactive([])
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
onMounted(() => {
2025-02-19 16:36:12 +08:00
if (props.type == 0) {
getList()
} else if(props.type == 1) {
// 视频消息
} else if (props.type == 2) {
// 商城消息
}
2025-02-15 16:09:39 +08:00
addListener()
})
onUnmounted(() => {
2025-02-19 16:36:12 +08:00
// removeListener()
2025-02-15 16:09:39 +08:00
})
// 开启监听消息
function addListener() {
let onMessageReceived = function (event) {
2024-12-18 15:46:27 +08:00
getList()
2025-02-15 16:09:39 +08:00
}
uni.$chat.on(TencentCloudChat.EVENT.CONVERSATION_LIST_UPDATED, onMessageReceived);
}
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
// 移除监听
function removeListener() {
uni.$chat.on(TencentCloudChat.EVENT.CONVERSATION_LIST_UPDATED);
}
2025-02-19 16:36:12 +08:00
// 获取消息列表
2025-02-15 16:09:39 +08:00
function getList() {
api.news.getMessageList({
query: {
userId: userinfo.value.id,
}
}).then(rs => {
if (rs.code == 200) {
list.data = handleList(rs.data);
return
}
util.alert({
content: rs.msg,
showCancel: false,
})
2024-12-18 15:46:27 +08:00
})
2025-02-19 16:36:12 +08:00
// // 验证sdk是否准备完毕
// let isReady = uni.$chat.isReady();
// if (!isReady) {
// setTimeout(function () {
// getList();
// }, 1000);
// return
// }
// uni.$chat.getMessageList({})
2025-02-15 16:09:39 +08:00
}
/**
* 消息列表
* @param {Object} item
*/
function handleList(list) {
list.forEach(item => {
item.callbackJson = JSON.parse(item.callbackJson);
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
if (item.groupId != null) {
item.unreadCount = getGroupNoReadNum(item.groupId)
2024-12-18 15:46:27 +08:00
}
2025-02-18 11:38:36 +08:00
if (item.callbackJson.callback_json.length) {
let msgType = item.callbackJson.callback_json[0].MsgType;
if (msgType == TencentCloudChat.TYPES.MSG_TEXT) {
item.chatText = item.callbackJson.callback_json[0].MsgContent.Text
} else if (msgType == TencentCloudChat.TYPES.MSG_IMAGE) {
item.chatText = '[图片]'
} else if (msgType == TencentCloudChat.TYPES.MSG_AUDIO) {
item.chatText = '[语音]'
} else if (msgType == TencentCloudChat.TYPES.MSG_VIDEO) {
item.chatText = '[视频]'
} else if (msgType == TencentCloudChat.TYPES.MSG_CUSTOM) {
if (item.callbackJson.callback_json[0].businessType == 'redPacket') {
item.chatText = `[红包] ${item.callbackJson.blessing}`
} else if (item.callbackJson.callback_json[0].businessType == '1') {
// if (item.callbackJson.callback_json[0].call_type == '1') {
// item.chatText = '[语音通话]'
// } else if (item.callbackJson.callback_json[0].call_type == '2') {
// item.chatText = '[视频通话]'
// }
}
}
}
2025-02-15 16:09:39 +08:00
})
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
return list
}
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
// 查询群聊未读数量
function getGroupNoReadNum(groupId) {
// 验证sdk是否准备完毕
let isReady = uni.$chat.isReady();
if (!isReady) {
setTimeout(function () {
getGroupNoReadNum(groupId);
2025-02-19 16:36:12 +08:00
}, 800);
2025-02-15 16:09:39 +08:00
return
2024-12-18 15:46:27 +08:00
}
2025-02-15 16:09:39 +08:00
const unreadCount = uni.$chat.getTotalUnreadMessageCount('GROUP', groupId);
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
return unreadCount;
}
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
// // 点击用户
// function handleUser(item) {
// uni.navigateTo({
// url: util.setUrl('/pages/index/videoHome', {
// userId: item.userId,
// })
// })
// }
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
// // 跳转
// function navigateToPage(path) {
// uni.navigateTo({
// url: path
// });
// }
/**
* 去聊天
* @param {Object} item
*/
function handleChat(item) {
let param = {};
// 单聊
if (item.groupId == null) {
param.type = 'C2C'
param.name = `${item.callbackJson.from_name}`
param.msgId = `${item.callbackJson.from_id}`
} else {
2024-12-18 15:46:27 +08:00
// 群聊
2025-02-15 16:09:39 +08:00
param.type = 'GROUP'
param.name = `${item.groupChatDTO.name}`
param.msgId = `${item.groupId}`
param.num = `${item.groupChatDTO.memberCount}`
2024-12-18 15:46:27 +08:00
}
2025-02-19 16:36:12 +08:00
setRead(item)
2025-02-15 16:09:39 +08:00
util.toChat(param)
}
/**
* 菜单
* @param {Object} ev 默认事件
* @param {Object} item 单项
*/
function handleMenu(ev, item) {
console.log('ev', ev, item)
ev.content.fn(item)
proxy.$refs.swipeAction.closeAll()
}
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
// 删除会话
function delMsg(item) {
// 验证sdk是否准备完毕
let isReady = uni.$chat.isReady();
if (!isReady) {
setTimeout(function () {
delMsg(item);
2025-02-19 16:36:12 +08:00
}, 800);
2025-02-15 16:09:39 +08:00
return
2024-12-18 15:46:27 +08:00
}
2025-02-15 16:09:39 +08:00
let conversationId = item.groupId == null ? `C2C${item.fromId}` : `GROUP${item.groupId}`;
2025-02-18 11:38:36 +08:00
uni.$chat.deleteConversation(conversationId).then(rs => {
2025-02-15 16:09:39 +08:00
getList()
})
}
// 标为已读
function setRead(item) {
// 验证sdk是否准备完毕
let isReady = uni.$chat.isReady();
if (!isReady) {
setTimeout(function () {
setRead(item);
2025-02-19 16:36:12 +08:00
}, 800);
2025-02-15 16:09:39 +08:00
return
2024-12-18 15:46:27 +08:00
}
2025-02-15 16:09:39 +08:00
let conversationId = item.groupId == null ? `C2C${item.fromId}` : `GROUP${item.groupId}`;
uni.$chat.setMessageRead({
conversationID: conversationId,
}).then(rs => {
getList()
})
}
2024-12-18 15:46:27 +08:00
</script>
<template>
<view class="firendBox pr">
<scroll-view scroll-y="true" class="scroll">
<uni-swipe-action ref="swipeAction">
<view class="list pb30">
<!-- <uni-swipe-action-item :right-options="systemRightOption">
<view class="item rows ptb20 plr30">
<view class="image wh90 pr">
2025-02-15 16:09:39 +08:00
<image class="cir wh90" src="/static/msg1.png" mode="aspectFill" />
2024-12-18 15:46:27 +08:00
<view class="dot pa t0 r0 cir" v-if="1"></view>
</view>
<view class="col f1 ml20">
<view class="rows">
<view class="name f1 thd c333 f32">新的关注</view>
</view>
<view class="desc thd mt10 c666 f24">新增一位关注快看看是谁吧~</view>
</view>
</view>
2025-02-15 16:09:39 +08:00
</uni-swipe-action-item>
<uni-swipe-action-item :right-options="systemRightOption">
2024-12-18 15:46:27 +08:00
<view class="item rows ptb20 plr30">
<view class="image wh90 pr">
<image class="cir wh90" src="/static/msg1.png" mode="aspectFill" />
<view class="dot pa t0 r0 cir" v-if="1"></view>
</view>
<view class="col f1 ml20">
<view class="rows">
<view class="name f1 thd c333 f32">互动消息</view>
</view>
<view class="desc thd mt10 c666 f24">快去看看吧~</view>
</view>
</view>
2025-02-15 16:09:39 +08:00
</uni-swipe-action-item>
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
<uni-swipe-action-item :right-options="systemRightOption">
2024-12-18 15:46:27 +08:00
<view class="item rows ptb20 plr30">
<view class="image wh90 pr">
2025-02-15 16:09:39 +08:00
<image class="cir wh90" src="/static/msg2.png" mode="aspectFill" />
2024-12-18 15:46:27 +08:00
<view class="dot pa t0 r0 cir" v-if="0"></view>
</view>
<view class="col f1 ml20">
<view class="rows">
<view class="name f1 thd c333 f32">系统消息</view>
<view class="datetime c999 f22">16:06</view>
</view>
<view class="desc thd mt10 c666 f24">平台福利上新快来玩转平台</view>
</view>
</view>
</uni-swipe-action-item> -->
2025-02-15 16:09:39 +08:00
<uni-swipe-action-item :right-options="rightOption" v-for="(item, index) in list.data" :key="index"
@click="handleMenu($event, item)">
2025-02-19 16:36:12 +08:00
<view class="item rows ptb20 plr30" @click.stop="handleChat(item)">
2024-12-18 15:46:27 +08:00
<view class="image wh90 pr">
2025-02-15 16:09:39 +08:00
<template v-if="item.groupId == null">
<image class="cir wh90" :src="item.callbackJson.from_url" mode="aspectFill" />
2024-12-18 15:46:27 +08:00
</template>
2025-02-15 16:09:39 +08:00
<template v-else>
<image class="cir wh90" :src="item.groupChatDTO.groupFaceUrl" mode="aspectFill" />
2024-12-18 15:46:27 +08:00
</template>
2025-02-15 16:09:39 +08:00
<view class="mark pa t0 r0 cfff f22 cir" v-if="item.unreadCount">{{ item.unreadCount }}
</view>
2024-12-18 15:46:27 +08:00
</view>
<view class="col f1 ml20">
<view class="rows">
2025-02-15 16:09:39 +08:00
<template v-if="item.groupId == null">
<view class="name f1 thd c333 f32">{{ item.callbackJson.from_name }}</view>
</template>
<template v-else>
<view class="name f1 thd c333 f32">{{ item.groupChatDTO.name }}</view>
</template>
2025-02-12 09:05:14 +08:00
<view class="datetime c999 f22">
2025-02-15 16:09:39 +08:00
{{ util.formatTime('MM-dd HH:mm', item.createTime) }}</view>
2024-12-18 15:46:27 +08:00
</view>
2025-02-18 11:38:36 +08:00
<view class="desc thd mt10 c666 f24">{{ item.chatText }}</view>
2024-12-18 15:46:27 +08:00
</view>
</view>
</uni-swipe-action-item>
<view class="mtb20 tac c999 f20">到底啦~</view>
</view>
</uni-swipe-action>
</scroll-view>
</view>
</template>
<style lang="scss">
2025-02-15 16:09:39 +08:00
// 朋友列表
.firendBox {
height: 100%;
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
.scroll {
height: 100%;
}
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
// 列表
.list {
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
// 消息提示点
.mark,
.dot {
background-color: #FF6B17;
text-align: center;
line-height: 30rpx;
}
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
.mark {
width: 30rpx;
height: 30rpx;
}
2024-12-18 15:46:27 +08:00
2025-02-15 16:09:39 +08:00
.dot {
width: 20rpx;
height: 20rpx;
2024-12-18 15:46:27 +08:00
}
}
2025-02-15 16:09:39 +08:00
}
2024-12-18 15:46:27 +08:00
</style>