Files
im/pages/conversation/chating/index.vue
T

491 lines
14 KiB
Vue
Raw Normal View History

2025-11-07 09:56:20 +08:00
<template>
2025-12-02 03:05:52 +08:00
<view class="chating_container">
2025-12-05 16:10:52 +08:00
<SelectHeader v-if="selectFlag" :count="selectItems.length" @leftClick="onUserMessageEvent({type:'cancelSelect'})" ref="selectHeaderRef" />
2025-12-08 18:10:51 +08:00
<ChatingHeader v-else @click="pageClick" ref="chatingHeaderRef" />
<ChatingList
2025-12-05 16:10:52 +08:00
@userEvent="onUserMessageEvent"
@click="pageClick"
:selectItems="selectItems"
:selectFlag="selectFlag"
ref="chatingListRef"
@initSuccess="initSuccess" />
<SelectFooter v-if="selectFlag" ref="selectFooterRef" @userEvent="onUserMessageEvent" />
2025-12-08 18:10:51 +08:00
<ChatingFooter v-else ref="chatingFooterRef" :footerOutsideFlag="footerOutsideFlag"
2025-11-07 09:56:20 +08:00
@scrollToBottom="scrollToBottom" />
<u-loading-page :loading="initLoading"></u-loading-page>
</view>
</template>
<script>
2025-12-05 16:10:52 +08:00
import {mapActions,mapGetters} from "vuex";
import {offlinePushInfo} from "@/util/imCommon";
2025-12-24 04:12:56 +08:00
import {PageEvents,ContactChooseTypes,noticeMessageTypes} from "@/constant";
2025-12-08 18:10:51 +08:00
import ChatingHeader from "./components/ChatingHeader";
import ChatingFooter from "./components/ChatingFooter/index";
import ChatingList from "./components/ChatingList";
import SelectHeader from "./components/SelectHeader";
import SelectFooter from "./components/SelectFooter";
2025-12-05 16:10:52 +08:00
import {markConversationAsRead} from "@/util/imCommon";
2026-01-15 22:50:35 +08:00
import IMSDK, {MessageType,GroupMemberRole,SessionType} from "openim-uniapp-polyfill";
2026-01-11 13:51:16 +08:00
import util from "@/util/index.js"
2025-11-07 09:56:20 +08:00
export default {
components: {
ChatingHeader,
ChatingFooter,
ChatingList,
2025-12-05 16:10:52 +08:00
SelectHeader,
SelectFooter
2025-11-07 09:56:20 +08:00
},
data() {
return {
listHeight: 0,
footerOutsideFlag: 0,
initLoading: true,
back2Tab: false,
2025-12-08 18:10:51 +08:00
selectFlag: false,
selectItems: [],
forwardItems: [],
2025-12-17 08:47:58 +08:00
forwardMerge: false,
Audio:null
2025-11-07 09:56:20 +08:00
};
},
2025-12-08 18:10:51 +08:00
computed: {
2025-12-05 16:10:52 +08:00
...mapGetters([
2026-01-09 20:22:25 +08:00
"storeHistoryMessageList",
"storeCurrentUserID",
2026-01-10 15:40:38 +08:00
"storeCurrentConversation",
"storeCurrentMsg",
'storeCurrentMsgID',
"storeCurrentGroup",
"storeCurrentMemberInGroup"
2025-12-08 18:10:51 +08:00
]),
2026-01-15 22:50:35 +08:00
isSingle() {
return (
this.storeCurrentConversation.conversationType === SessionType.Single
);
},
2026-01-10 15:40:38 +08:00
isOwner() {
return this.storeCurrentMemberInGroup.roleLevel === GroupMemberRole.Owner;
},
isAdmin() {
return this.storeCurrentMemberInGroup.roleLevel === GroupMemberRole.Admin;
},
2025-12-05 16:10:52 +08:00
},
2025-12-17 08:47:58 +08:00
created() {
this.Audio = uni.createInnerAudioContext(); //音频
/*
设置音频播放模式,可取值:
"ambient" - 不中止其他声音播放,不能后台播放,静音后无声音;
"soloAmbient" - 中止其他声音播放,不能后台播放,静音后无声音;
"playback" - 中止其他声音,可以后台播放,静音后有声音。
默认值为"playback"。
*/
this.Audio.sessionCategory="soloAmbient";
this.Audio.onError((r)=>{
console.log(r);
this.updateCurrentMsg({});
})
this.Audio.onEnded((r)=>{
console.log(r);
this.updateCurrentMsg({});
})
},
watch:{
storeCurrentMsgID(nv,ov){
if(nv!=ov && nv){
if(this.storeCurrentMsg.soundElem.soundPath){
this.Audio.src= this.storeCurrentMsg.soundElem.soundPath;
this.Audio.play();
return ;
}
this.updateCurrentMsg({});
}
console.log(nv,ov,this.Audio,this.storeCurrentMsg);
}
},
2025-11-07 09:56:20 +08:00
onLoad(options) {
2025-12-23 00:18:46 +08:00
//console.log("onload",this.storeCurrentConversation);
2025-11-07 09:56:20 +08:00
this.setPageListener();
if (options?.back2Tab) {
this.back2Tab = JSON.parse(options.back2Tab);
}
2026-01-12 18:07:21 +08:00
// IMSDK.subscribe(IMSDK.IMEvents.OnMsgDeleted,({data})=>{
// let list = this.storeHistoryMessageList;
// //console.log(data);
// list = list.filter((item)=>{
// return item.serverMsgID != data.serverMsgID;
// })
// this.$store.commit('message/SET_HISTORY_MESSAGE_LIST',list);
// });
2025-11-07 09:56:20 +08:00
},
onUnload() {
2026-01-12 18:07:21 +08:00
//IMSDK.unsubscribe(IMSDK.IMEvents.OnMsgDeleted);
2025-12-05 16:10:52 +08:00
//console.log("unload");
2025-11-07 09:56:20 +08:00
this.disposePageListener();
2025-12-17 08:47:58 +08:00
markConversationAsRead({...this.$store.getters.storeCurrentConversation,},true);
2025-11-07 09:56:20 +08:00
this.resetConversationState();
this.resetMessageState();
2025-12-17 08:47:58 +08:00
// #ifdef APP-PLUS
if (this.Audio) {
this.Audio.stop();
this.Audio.destroy();
2025-12-23 00:18:46 +08:00
this.Audio = null;
2025-12-17 08:47:58 +08:00
}
// #endif
},
onHide() {
// #ifdef APP-PLUS
if (this.Audio) {
this.Audio.stop();
}
// #endif
2025-11-07 09:56:20 +08:00
},
methods: {
2025-12-17 08:47:58 +08:00
...mapActions("message", ["updateCurrentMsg","resetMessageState", "deleteMessages", "pushNewMessage", "updateOneMessage"]),
2025-11-07 09:56:20 +08:00
...mapActions("conversation", ["resetConversationState"]),
scrollToBottom(isRecv = false) {
this.$refs.chatingListRef.scrollToBottom(false, isRecv);
},
pageClick(e) {
this.footerOutsideFlag += 1;
},
initSuccess() {
this.initLoading = false;
},
// page event
setPageListener() {
uni.$on(PageEvents.ScrollToBottom, this.scrollToBottom);
},
disposePageListener() {
uni.$off(PageEvents.ScrollToBottom, this.scrollToBottom);
},
2025-12-05 16:10:52 +08:00
//选择会话列表的回调
2025-12-08 18:10:51 +08:00
async onForwardTargetSelected(userList, groupList) {
2025-12-05 16:10:52 +08:00
const _this = this;
//console.log(userList,groupList);
2025-12-08 18:10:51 +08:00
if (this.forwardItems.length === 0) {
2025-12-05 16:10:52 +08:00
return false;
}
2025-12-08 18:10:51 +08:00
if (this.forwardMerge && this.forwardItems.length > 1) {
2025-12-05 16:10:52 +08:00
const res = await IMSDK.asyncApi(IMSDK.IMMethods.CreateMergerMessage, IMSDK.uuid(), {
messageList: _this.forwardItems,
title: "转发的消息",
2025-12-08 18:10:51 +08:00
summaryList: ["summaryList", "summaryList"]
2025-12-05 16:10:52 +08:00
});
//console.log(res );
2025-12-08 18:10:51 +08:00
if (res) {
await _this.doForwarMsg(userList, groupList, res);
2025-12-05 16:10:52 +08:00
}
2025-12-08 18:10:51 +08:00
} else {
2025-12-05 16:10:52 +08:00
for (var mi = 0; mi < this.forwardItems.length; mi++) {
var msg = this.forwardItems[mi];
const res = await IMSDK.asyncApi(IMSDK.IMMethods.CreateForwardMessage, IMSDK.uuid(), msg);
//console.log(res );
2025-12-08 18:10:51 +08:00
if (res) {
await _this.doForwarMsg(userList, groupList, res);
2025-12-05 16:10:52 +08:00
}
}
}
2025-12-08 18:10:51 +08:00
this.onUserMessageEvent({type: 'cancelSelect'});
2025-12-05 16:10:52 +08:00
},
2025-12-08 18:10:51 +08:00
async doForwarMsg(userList, groupList, msgiem)
2026-01-11 15:48:41 +08:00
{
2025-12-05 16:10:52 +08:00
//console.log(userList,groupList);
const _this = this;
for (var i = 0; i < userList.length; i++) {
2025-12-08 18:10:51 +08:00
this.sendMessage(msgiem, userList[i].userID, "");
2025-12-05 16:10:52 +08:00
}
for (var i = 0; i < groupList.length; i++) {
2025-12-08 18:10:51 +08:00
this.sendMessage(msgiem, "", groupList[i].groupID);
2025-12-05 16:10:52 +08:00
}
},
2025-12-08 18:10:51 +08:00
sendMessage(message, user_id, group_id) {
2025-12-05 16:10:52 +08:00
IMSDK.asyncApi(IMSDK.IMMethods.SendMessage, IMSDK.uuid(), {
recvID: user_id,
groupID: group_id,
message,
offlinePushInfo,
})
.then(({data}) => {
2025-12-24 04:12:56 +08:00
if(user_id && this.storeCurrentConversation.userID == user_id){
this.pushNewMessage(message);
this.scrollToBottom();
return ;
}
if(group_id && this.storeCurrentConversation.groupID == group_id){
this.pushNewMessage(message);
this.scrollToBottom();
return ;
}
2025-12-05 16:10:52 +08:00
})
2025-12-08 18:10:51 +08:00
.catch((res = {data,errCode,errMsg}) => {
2025-12-05 16:10:52 +08:00
console.log(res);
uni.$u.toast(errMsg);
});
},
2026-01-11 15:48:41 +08:00
async onUserMessageEvent(e, data) {
2025-12-05 16:10:52 +08:00
const _this = this;
2025-12-08 18:10:51 +08:00
if (e.type == 'select') {
2025-12-05 16:10:52 +08:00
this.selectFlag = true;
this.selectItems = [data];
this.$refs.chatingListRef.scrollToAnchor(`auchor${data.clientMsgID}`);
2025-12-08 18:10:51 +08:00
return;
2025-12-05 16:10:52 +08:00
}
2025-12-08 18:10:51 +08:00
if (e.type == 'cancelSelect') {
2025-12-05 16:10:52 +08:00
this.selectFlag = false;
this.selectItems = [];
2025-12-08 18:10:51 +08:00
return;
2025-12-05 16:10:52 +08:00
}
2025-12-08 18:10:51 +08:00
if (e.type == 'selected') {
if (this.selectFlag == true) {
2025-12-24 04:12:56 +08:00
if(noticeMessageTypes.includes(data.contentType)){
return ;
}
2026-01-09 20:22:25 +08:00
let arr = this.selectItems.filter((item)=>item.clientMsgID != data.clientMsgID);
if(arr.length === this.selectItems.length){
arr.push(data)
2025-12-05 16:10:52 +08:00
}
this.selectItems = [...arr];
}
2025-12-08 18:10:51 +08:00
return;
2025-12-05 16:10:52 +08:00
}
2026-01-10 15:40:38 +08:00
if (e.type == 'deleteMsg' || e.type == 'deleteServerMsg') {
let method = IMSDK.IMMethods.DeleteMessageFromLocalStorage;
if(e.type == 'deleteServerMsg'){
2026-01-11 15:48:41 +08:00
method = IMSDK.IMMethods.RevokeMessage
2026-01-10 15:40:38 +08:00
}
2026-01-12 18:07:21 +08:00
//console.log(method);
2025-12-05 16:10:52 +08:00
let deleteMsgs = [];
2025-12-08 18:10:51 +08:00
if (!data) {
2025-12-05 16:10:52 +08:00
deleteMsgs = [...this.selectItems];
2025-12-08 18:10:51 +08:00
} else {
2025-12-05 16:10:52 +08:00
deleteMsgs = [{...data}];
}
for (let i = 0; i < deleteMsgs.length; i++) {
let element = deleteMsgs[i];
2026-01-11 15:48:41 +08:00
let a = await IMSDK.asyncApi(method, IMSDK.uuid(), {
2025-12-05 16:10:52 +08:00
conversationID: _this.storeCurrentConversation.conversationID,
clientMsgID: element.clientMsgID
2026-01-10 15:40:38 +08:00
});
2026-01-12 18:07:21 +08:00
//console.log(a);
2025-12-05 16:10:52 +08:00
}
this.selectItems = [];
2025-12-08 18:10:51 +08:00
return;
2025-12-05 16:10:52 +08:00
}
2025-12-08 18:10:51 +08:00
if (e.type == 'forward') {
if (!data) {
2025-12-05 16:10:52 +08:00
this.forwardItems = [...this.selectItems];
2025-12-08 18:10:51 +08:00
let menu = ['逐条转发', '合并转发'];
2025-12-05 16:10:52 +08:00
uni.showActionSheet({
2025-12-08 18:10:51 +08:00
itemList: menu,
success({tapIndex}) {
if (menu[tapIndex] == '逐条转发') {
2025-12-05 16:10:52 +08:00
this.forwardMerge = false;
}
2025-12-08 18:10:51 +08:00
if (menu[tapIndex] == '合并转发') {
2025-12-05 16:10:52 +08:00
this.forwardMerge = true;
}
uni.navigateTo({
url: `/pages/common/contactChoose/choose?type=${ContactChooseTypes.GetList}&checkedUserIDList=[]`,
2025-12-08 18:10:51 +08:00
events: {
onSelectedConfirm(userList, groupList) {
2025-12-05 16:10:52 +08:00
//console.log(userList,groupList);
2025-12-08 18:10:51 +08:00
_this.onForwardTargetSelected(userList, groupList)
2025-12-05 16:10:52 +08:00
}
}
});
}
});
2025-12-08 18:10:51 +08:00
} else {
2025-12-05 16:10:52 +08:00
this.forwardItems = [{...data}];
this.forwardMerge = false;
uni.navigateTo({
url: `/pages/common/contactChoose/choose?type=${ContactChooseTypes.GetList}&checkedUserIDList=[]`,
2025-12-08 18:10:51 +08:00
events: {
onSelectedConfirm(userList, groupList) {
2025-12-05 16:10:52 +08:00
//console.log(userList,groupList);
2025-12-08 18:10:51 +08:00
_this.onForwardTargetSelected(userList, groupList)
2025-12-05 16:10:52 +08:00
}
}
});
}
2025-12-08 18:10:51 +08:00
return;
2025-12-05 16:10:52 +08:00
}
2025-12-08 18:10:51 +08:00
if (e.type == 'revokeMessage') {
2025-12-05 16:10:52 +08:00
IMSDK.asyncApi('revokeMessage', IMSDK.uuid(), {
conversationID: _this.storeCurrentConversation.conversationID,
clientMsgID: data.clientMsgID
2025-12-08 18:10:51 +08:00
}).then(res => {
2026-01-09 20:22:25 +08:00
let list = _this.storeHistoryMessageList;
list = list.filter((item)=>{
return item.clientMsgID != data.clientMsgID;
});
_this.$store.commit('message/SET_HISTORY_MESSAGE_LIST',list);
2025-12-08 18:10:51 +08:00
}).catch(res => {
2025-12-05 16:10:52 +08:00
console.log(res);
2025-12-08 18:10:51 +08:00
}).finally(() => {
2025-12-05 16:10:52 +08:00
console.log(arguments);
})
2025-12-08 18:10:51 +08:00
return;
2025-12-05 16:10:52 +08:00
}
2026-01-10 15:40:38 +08:00
if (e.type == 'audio_msg_click'){
2025-12-17 08:47:58 +08:00
if(_this.storeCurrentMsgID){
_this.updateCurrentMsg({clientMsgID:""});
}else{
_this.updateCurrentMsg(e.message);
}
return ;
}
2025-12-08 18:10:51 +08:00
if (e.type == 'longtapMsgContent') {
2025-12-05 16:10:52 +08:00
let menu = [];
2025-12-08 18:10:51 +08:00
if (data.contentType == MessageType.TextMessage) {
2025-12-05 16:10:52 +08:00
menu.push('复制')
}
menu.push('转发');
menu.push('多选');
menu.push('删除');
2025-12-08 18:10:51 +08:00
let nowTime = new Date().getTime();
let msgTime = data.createTime;
let diff = nowTime - msgTime;
2026-01-09 20:22:25 +08:00
if (_this.storeCurrentUserID == data.sendID && diff < 120000) {
2025-12-05 16:10:52 +08:00
menu.push('撤回')
}
2026-01-10 15:40:38 +08:00
if(_this.isAdmin | _this.isOwner){
2026-01-12 18:07:21 +08:00
menu.push('撤回(管理员功能)')
2026-01-10 15:40:38 +08:00
}
2025-12-05 16:10:52 +08:00
uni.showActionSheet({
2025-12-08 18:10:51 +08:00
itemList: menu,
success({tapIndex}) {
2025-12-05 16:10:52 +08:00
const title = menu[tapIndex];
switch (title) {
case "撤回":
2025-12-08 18:10:51 +08:00
_this.onUserMessageEvent({type: 'revokeMessage'}, data);
2025-12-05 16:10:52 +08:00
break;
case "复制":
let content = data.textElem?.content;
if (content) {
//let formatStr = this.replaceReseverEmoji(copyContent);
uni.setClipboardData({
2025-12-08 18:10:51 +08:00
data: content,
2025-12-05 16:10:52 +08:00
success: (res) => {
uni.showToast({
title: res,
icon: 'none'
})
},
error: (e) => {
uni.showToast({
title: e,
icon: 'none',
duration: 3000,
})
}
})
}
break;
case "转发":
2025-12-08 18:10:51 +08:00
_this.onUserMessageEvent({type: 'forward'}, data);
2025-12-05 16:10:52 +08:00
break;
case "多选":
2025-12-08 18:10:51 +08:00
_this.onUserMessageEvent({type: 'select'}, data);
2025-12-05 16:10:52 +08:00
break;
case "删除":
2025-12-08 18:10:51 +08:00
_this.onUserMessageEvent({type: 'deleteMsg'}, data);
2025-12-05 16:10:52 +08:00
break;
2026-01-12 18:07:21 +08:00
case "撤回(管理员功能)":
2026-01-10 15:40:38 +08:00
_this.onUserMessageEvent({type: 'deleteServerMsg'}, data);
break;
2025-12-05 16:10:52 +08:00
default:
break;
}
}
})
2025-12-08 18:10:51 +08:00
return;
2025-12-05 16:10:52 +08:00
}
2026-01-15 22:50:35 +08:00
if (e.type == 'avatarClick'){
if(data.sendID == this.storeCurrentUserID){
return ;
}
if(!_this.isSingle){
if(this.storeCurrentMemberInGroup.roleLevel < 60 && this.storeCurrentGroup.lookMemberInfo!=0){
return ;
}
}
uni.navigateTo({
url:"/pages/common/userCard/index?sourceID="+data.sendID
})
return ;
}
2025-12-05 16:10:52 +08:00
}
2025-11-07 09:56:20 +08:00
},
onBackPress() {
uni.switchTab({
url: "/pages/conversation/conversationList/index",
});
return true;
},
beforeDestroy() {
uni.switchTab({
url: "/pages/conversation/conversationList/index",
});
},
};
</script>
<style lang="scss" scoped>
.chating_container {
@include colBox(false);
height: 100vh;
overflow: hidden;
2025-12-02 03:05:52 +08:00
background-color: #ececec !important;
2025-11-07 09:56:20 +08:00
position: relative;
.watermark {
font-size: 16px;
/* 水印文字大小 */
color: rgba(0, 0, 0, 0.2);
/* 水印文字颜色,使用透明度控制可见度 */
position: absolute;
/* 水印相对定位 */
transform: rotate(-45deg);
pointer-events: none;
/* 防止水印文字干扰交互 */
/* 为不同的水印元素设置不同的偏移,以避免重叠 */
// transform-origin: top right;
// margin-top: 20px;
// margin-right: 20px;
}
// ::before {
// content: "Your Watermark Text"; /* 替换为你想要的水印文字 */
// font-size: 16px; /* 水印文字大小 */
// color: rgba(0, 0, 0, 0.2); /* 水印文字颜色,使用透明度控制可见度 */
// position: absolute; /* 水印相对定位 */
// top: 20px; /* 距离顶部的距离 */
// right: 20px; /* 距离右侧的距离 */
// transform: rotate(-45deg); /* 将水印旋转为倾斜状态 */
// pointer-events: none; /* 防止水印文字干扰交互 */
// z-index: -1; /* 将水印置于底层 */
// }
.mutiple_action_container {
display: flex;
border-top: 1px solid #eaebed;
background: #f0f2f6;
justify-content: space-evenly;
padding: 12px 24px;
.action_item {
@include centerBox();
flex-direction: column;
image {
width: 48px;
height: 48px;
}
}
}
}
</style>