Browse Source

uniapp 输入@触发选择弹窗

master
xie.bx 2 years ago
parent
commit
6d04cdbd66
  1. 2
      im-uniapp/App.vue
  2. 48
      im-uniapp/pages/chat/chat-box.vue
  3. 33
      im-uniapp/store/chatStore.js

2
im-uniapp/App.vue

@ -73,7 +73,6 @@
this.insertPrivateMessage(friend,msgInfo); this.insertPrivateMessage(friend,msgInfo);
} }
}) })
store.commit("refreshChats");
if (msgInfos.length == 100) { if (msgInfos.length == 100) {
// //
this.loadPrivateMessage(msgInfos[99].id); this.loadPrivateMessage(msgInfos[99].id);
@ -96,7 +95,6 @@
this.insertGroupMessage(group,msgInfo); this.insertGroupMessage(group,msgInfo);
} }
}) })
store.commit("refreshChats");
if (msgInfos.length == 100) { if (msgInfos.length == 100) {
// //
this.loadGroupMessage(msgInfos[99].id); this.loadGroupMessage(msgInfos[99].id);

48
im-uniapp/pages/chat/chat-box.vue

@ -11,8 +11,8 @@
<view v-for="(msgInfo,idx) in chat.messages" :key="idx"> <view v-for="(msgInfo,idx) in chat.messages" :key="idx">
<chat-message-item v-if="idx>=showMinIdx" :headImage="headImage(msgInfo)" <chat-message-item v-if="idx>=showMinIdx" :headImage="headImage(msgInfo)"
:showName="showName(msgInfo)" @recall="onRecallMessage" @delete="onDeleteMessage" :showName="showName(msgInfo)" @recall="onRecallMessage" @delete="onDeleteMessage"
@longPressHead="onLongPressHead(msgInfo)" @longPressHead="onLongPressHead(msgInfo)" @download="onDownloadFile" :id="'chat-item-'+idx"
@download="onDownloadFile" :id="'chat-item-'+idx" :msgInfo="msgInfo"> :msgInfo="msgInfo">
</chat-message-item> </chat-message-item>
</view> </view>
</scroll-view> </scroll-view>
@ -31,14 +31,14 @@
<view class="send-text"> <view class="send-text">
<textarea class="send-text-area" v-model="sendText" auto-height :show-confirm-bar="false" <textarea class="send-text-area" v-model="sendText" auto-height :show-confirm-bar="false"
:adjust-position="false" @confirm="sendTextMessage()" @keyboardheightchange="onKeyboardheightchange" :adjust-position="false" @confirm="sendTextMessage()" @keyboardheightchange="onKeyboardheightchange"
confirm-type="send" confirm-hold :hold-keyboard="true"></textarea> @input="onTextInput" confirm-type="send" confirm-hold :hold-keyboard="true"></textarea>
</view> </view>
<view v-if="chat.type=='GROUP'" class="iconfont icon-at" @click="openAtBox()"></view> <view v-if="chat.type=='GROUP'" class="iconfont icon-at" @click="openAtBox()"></view>
<view class="iconfont icon-icon_emoji" @click="switchChatTabBox('emo',true)"></view> <view class="iconfont icon-icon_emoji" @click="switchChatTabBox('emo',true)"></view>
<view v-if="sendText==''" class="iconfont icon-add" @click="switchChatTabBox('tools',true)"> <view v-if="sendText==''" class="iconfont icon-add" @click="switchChatTabBox('tools',true)">
</view> </view>
<button v-if="sendText!=''||atUserIds.length>0" class="btn-send" type="primary" @touchend.prevent="sendTextMessage()" <button v-if="sendText!=''||atUserIds.length>0" class="btn-send" type="primary"
size="mini">发送</button> @touchend.prevent="sendTextMessage()" size="mini">发送</button>
</view> </view>
<view class="chat-tab-bar" v-show="chatTabBox!='none' ||showKeyBoard " :style="{height:`${keyboardHeight}px`}"> <view class="chat-tab-bar" v-show="chatTabBox!='none' ||showKeyBoard " :style="{height:`${keyboardHeight}px`}">
@ -119,8 +119,8 @@
onAtComplete(atUserIds) { onAtComplete(atUserIds) {
this.atUserIds = atUserIds; this.atUserIds = atUserIds;
}, },
onLongPressHead(msgInfo){ onLongPressHead(msgInfo) {
if(!msgInfo.selfSend && this.chat.type=="GROUP" && this.atUserIds.indexOf(msgInfo.sendId)<0){ if (!msgInfo.selfSend && this.chat.type == "GROUP" && this.atUserIds.indexOf(msgInfo.sendId) < 0) {
this.atUserIds.push(msgInfo.sendId); this.atUserIds.push(msgInfo.sendId);
} }
}, },
@ -141,7 +141,7 @@
} }
}, },
sendTextMessage() { sendTextMessage() {
if (!this.sendText.trim() && this.atUserIds.length==0) { if (!this.sendText.trim() && this.atUserIds.length == 0) {
return uni.showToast({ return uni.showToast({
title: "不能发送空白信息", title: "不能发送空白信息",
icon: "none" icon: "none"
@ -179,10 +179,10 @@
createAtText() { createAtText() {
let atText = ""; let atText = "";
this.atUserIds.forEach((id) => { this.atUserIds.forEach((id) => {
if(id==-1){ if (id == -1) {
atText += ` @全体成员`; atText += ` @全体成员`;
}else{ } else {
let member = this.groupMembers.find((m)=>m.userId==id); let member = this.groupMembers.find((m) => m.userId == id);
if (member) { if (member) {
atText += ` @${member.aliasName}`; atText += ` @${member.aliasName}`;
} }
@ -414,6 +414,16 @@
}) })
} }
}, },
onTextInput(e) {
let idx = e.detail.cursor - 1;
if (this.chat.type == 'GROUP' && e.detail.value[idx] == '@') {
this.openAtBox();
let sendText = e.detail.value.replace("@", '');
this.$nextTick(() => {
this.sendText = sendText;
})
}
},
readedMessage() { readedMessage() {
if (this.chat.type == "GROUP") { if (this.chat.type == "GROUP") {
var url = `/message/group/readed?groupId=${this.chat.targetId}` var url = `/message/group/readed?groupId=${this.chat.targetId}`
@ -491,15 +501,18 @@
unreadCount() { unreadCount() {
return this.chat.unreadCount; return this.chat.unreadCount;
}, },
atUserItems(){ atUserItems() {
let atUsers = []; let atUsers = [];
this.atUserIds.forEach((id)=>{ this.atUserIds.forEach((id) => {
if(id==-1){ if (id == -1) {
atUsers.push({id:-1,aliasName:"全体成员"}) atUsers.push({
id: -1,
aliasName: "全体成员"
})
return; return;
} }
let member = this.groupMembers.find((m)=>m.userId==id); let member = this.groupMembers.find((m) => m.userId == id);
if(member){ if (member) {
atUsers.push(member); atUsers.push(member);
} }
}) })
@ -612,6 +625,7 @@
.chat-at-scroll-box { .chat-at-scroll-box {
flex: 1; flex: 1;
width: 80%; width: 80%;
.chat-at-items { .chat-at-items {
display: flex; display: flex;
align-items: center; align-items: center;

33
im-uniapp/store/chatStore.js

@ -31,15 +31,12 @@ export default {
}, },
openChat(state, chatInfo) { openChat(state, chatInfo) {
let chat = null; let chat = null;
for (let i in state.chats) { for (let idx in state.chats) {
if (state.chats[i].type == chatInfo.type && if (state.chats[idx].type == chatInfo.type &&
state.chats[i].targetId === chatInfo.targetId) { state.chats[idx].targetId === chatInfo.targetId) {
chat = state.chats[i]; chat = state.chats[idx];
// 放置头部(这个操作非常耗资源,正在加载消息时不执行) // 放置头部
if(!state.loadingPrivateMsg && !state.loadingPrivateMsg){ this.commit("moveTop",idx)
state.chats.splice(i, 1);
state.chats.unshift(chat);
}
break; break;
} }
} }
@ -110,22 +107,27 @@ export default {
} }
}, },
moveTop(state, idx) { moveTop(state, idx) {
// 加载中不移动,很耗性能
if(state.loadingPrivateMsg || state.loadingGroupMsg){
return ;
}
if (idx > 0) {
let chat = state.chats[idx]; let chat = state.chats[idx];
// 放置头部
state.chats.splice(idx, 1); state.chats.splice(idx, 1);
state.chats.unshift(chat); state.chats.unshift(chat);
this.commit("saveToStorage");
}
}, },
insertMessage(state, msgInfo) { insertMessage(state, msgInfo) {
// 获取对方id或群id // 获取对方id或群id
let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE'; let type = msgInfo.groupId ? 'GROUP' : 'PRIVATE';
let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId; let targetId = msgInfo.groupId ? msgInfo.groupId : msgInfo.selfSend ? msgInfo.recvId : msgInfo.sendId;
let chat = null; let chat = null;
let chatIdx = -1;
for (let idx in state.chats) { for (let idx in state.chats) {
if (state.chats[idx].type == type && if (state.chats[idx].type == type &&
state.chats[idx].targetId === targetId) { state.chats[idx].targetId === targetId) {
chat = state.chats[idx]; chat = state.chats[idx];
chatIdx = idx; this.commit("moveTop", idx)
break; break;
} }
} }
@ -158,7 +160,6 @@ export default {
chat.atAll = true; chat.atAll = true;
} }
} }
// 记录消息的最大id // 记录消息的最大id
if (msgInfo.id && type == "PRIVATE" && msgInfo.id > state.privateMsgMaxId) { if (msgInfo.id && type == "PRIVATE" && msgInfo.id > state.privateMsgMaxId) {
state.privateMsgMaxId = msgInfo.id; state.privateMsgMaxId = msgInfo.id;
@ -246,9 +247,15 @@ export default {
}, },
loadingPrivateMsg(state, loadding) { loadingPrivateMsg(state, loadding) {
state.loadingPrivateMsg = loadding; state.loadingPrivateMsg = loadding;
if(!state.loadingPrivateMsg && !state.loadingGroupMsg){
this.commit("refreshChats")
}
}, },
loadingGroupMsg(state, loadding) { loadingGroupMsg(state, loadding) {
state.loadingGroupMsg = loadding; state.loadingGroupMsg = loadding;
if(!state.loadingPrivateMsg && !state.loadingGroupMsg){
this.commit("refreshChats")
}
}, },
refreshChats(state){ refreshChats(state){
state.chats.forEach((chat)=>{ state.chats.forEach((chat)=>{

Loading…
Cancel
Save