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

393 lines
11 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-08 18:10:51 +08:00
import {PageEvents,ContactChooseTypes } from "@/constant";
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";
2025-12-08 18:10:51 +08:00
import IMSDK, {MessageType} from "openim-uniapp-polyfill";
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: [],
forwardMerge: false
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([
"storeCurrentConversation",
2025-12-08 18:10:51 +08:00
]),
2025-12-05 16:10:52 +08:00
},
2025-11-07 09:56:20 +08:00
onLoad(options) {
2025-12-05 16:10:52 +08:00
//console.log("onload");
2025-11-07 09:56:20 +08:00
this.setPageListener();
if (options?.back2Tab) {
this.back2Tab = JSON.parse(options.back2Tab);
}
},
onUnload() {
2025-12-05 16:10:52 +08:00
//console.log("unload");
2025-11-07 09:56:20 +08:00
this.disposePageListener();
markConversationAsRead({
...this.$store.getters.storeCurrentConversation,
},
true
);
this.resetConversationState();
this.resetMessageState();
},
methods: {
2025-12-08 18:10:51 +08:00
...mapActions("message", ["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)
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
//this.scrollToBottom();
//console.log(message);
//console.log(user_id,group_id);
IMSDK.asyncApi(IMSDK.IMMethods.SendMessage, IMSDK.uuid(), {
recvID: user_id,
groupID: group_id,
message,
offlinePushInfo,
})
.then(({data}) => {
console.log(data);
this.pushNewMessage(message);
})
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);
});
},
2025-12-08 18:10:51 +08:00
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-05 16:10:52 +08:00
let founded = false;
let arr = [];
for (var index = 0; index < this.selectItems.length; index++) {
var v = this.selectItems[index];
2025-12-08 18:10:51 +08:00
if (v.clientMsgID == data.clientMsgID) {
2025-12-05 16:10:52 +08:00
founded = true;
2025-12-08 18:10:51 +08:00
} else {
2025-12-05 16:10:52 +08:00
arr.push(v);
}
}
2025-12-08 18:10:51 +08:00
if (!founded) {
2025-12-05 16:10:52 +08:00
arr.push(data);
}
console.log(arr.length);
this.selectItems = [...arr];
}
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 == 'deleteMsg') {
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];
2025-12-08 18:10:51 +08:00
IMSDK.asyncApi('deleteMessageFromLocalStorage', IMSDK.uuid(), {
2025-12-05 16:10:52 +08:00
conversationID: _this.storeCurrentConversation.conversationID,
clientMsgID: element.clientMsgID
2025-12-08 18:10:51 +08:00
}).then(res => {
2025-12-05 16:10:52 +08:00
//console.log(res);
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);
})
}
this.selectItems = [];
this.$refs.chatingListRef.loadMessageList();
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 => {
2025-12-05 16:10:52 +08:00
console.log(res);
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
}
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;
if (this.isSender && diff < 120000) {
2025-12-05 16:10:52 +08:00
menu.push('撤回')
}
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;
default:
break;
}
}
})
2025-12-08 18:10:51 +08:00
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>