【新增】 IM基础模块
This commit is contained in:
276
pages/im/detail.vue
Normal file
276
pages/im/detail.vue
Normal file
@@ -0,0 +1,276 @@
|
||||
<template>
|
||||
<div>
|
||||
<scroll-view ref="scrollview" :scroll-y="true" class="scroll" :scroll-into-view="scrollIntoID"
|
||||
:scroll-with-animation="false">
|
||||
<view v-for="(item,index) in messages" :id="'chatId_'+index">
|
||||
<div :class="item.messageDirection == 1 ? 'right' : 'left'">
|
||||
<div class="avatar" v-if="item.messageDirection == 2">
|
||||
<u-avatar :src="userInfo.portraitUrl" @click="showFriend" shape="square" />
|
||||
</div>
|
||||
<div class="msg">
|
||||
{{ item.content.content }}
|
||||
<div class="status" v-if="item.messageDirection == 1">
|
||||
<u-icon v-if="item.sentStatus == 50" name="checkbox-mark" />
|
||||
<span v-else>未读</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="avatar" v-if="item.messageDirection == 1">
|
||||
<u-avatar text="Me" @click="showMine" shape="square" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="time">{{ item.sentTime|timeCustomCN }}</div>
|
||||
</view>
|
||||
</scroll-view>
|
||||
|
||||
<div class="footer">
|
||||
<div class="left">
|
||||
<u-icon name="volume" />
|
||||
</div>
|
||||
<div class="middle">
|
||||
<u--input autoBlur confirmHold confirmType="send" @confirm="send" v-model="inputTxt" />
|
||||
</div>
|
||||
<div class="right">
|
||||
<u-button type="primary" v-if="showSendButton" class="custom-style" text="发送" @click="send"></u-button>
|
||||
<u-icon v-else name="plus" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import * as RongIMLib from '@rongcloud/imlib-uni'
|
||||
import im from '@/utils/im/index.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
targetId: '',
|
||||
scrollIntoID: 'chatID_0',
|
||||
inputTxt: '',
|
||||
messages: [],
|
||||
conversationType: 1,
|
||||
totalCount: 0,
|
||||
userInfo: {
|
||||
name: '',
|
||||
userId: '',
|
||||
portraitUrl: ''
|
||||
}
|
||||
}
|
||||
},
|
||||
onLoad(e) {
|
||||
this.targetId = e.targetId
|
||||
this.conversationType = e.conversationType // 会话类型
|
||||
// 消息总数量
|
||||
RongIMLib.getMessageCount(this.conversationType, this.targetId, ({
|
||||
code,
|
||||
count
|
||||
}) => {
|
||||
if (code == 0) {
|
||||
this.totalCount = count
|
||||
}
|
||||
})
|
||||
this.userInfo = this.$store.getters.userInfo(this.targetId)
|
||||
uni.setNavigationBarTitle({
|
||||
title: this.$store.getters.userInfo(this.targetId).name
|
||||
})
|
||||
|
||||
RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, new Date().getTime())
|
||||
im.setNotifyBadge()
|
||||
RongIMLib.sendReadReceiptMessage(this.conversationType, this.targetId, new Date().getTime())
|
||||
|
||||
this.getMessageList()
|
||||
|
||||
// 监听消息回执
|
||||
RongIMLib.addReadReceiptReceivedListener((result) => {
|
||||
const res = result.data.message
|
||||
if (res.targetId == this.targetId) {
|
||||
this.getMessageList()
|
||||
}
|
||||
})
|
||||
},
|
||||
onUnload() {
|
||||
RongIMLib.clearReadReceiptReceivedListener()
|
||||
},
|
||||
computed: {
|
||||
showSendButton() {
|
||||
return this.inputTxt.length > 0
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
'$store.getters.newMessage': function(msg) {
|
||||
if (msg.targetId == this.targetId) {
|
||||
RongIMLib.clearMessagesUnreadStatus(msg.conversationType, msg.targetId, msg.sentTime)
|
||||
RongIMLib.sendReadReceiptMessage(msg.conversationType, msg.targetId, msg.sentTime)
|
||||
this.getMessageList()
|
||||
im.setNotifyBadge()
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
getMessageList() {
|
||||
// 获取消息列表
|
||||
const objectNames = [
|
||||
'RC:TxtMsg',
|
||||
'RC:VcMsg',
|
||||
'RC:HQVCMsg',
|
||||
'RC:ImgMsg',
|
||||
'RC:GIFMsg',
|
||||
'RC:ImgTextMsg',
|
||||
'RC:FileMsg',
|
||||
'RC:LBSMsg',
|
||||
'RC:SightMsg',
|
||||
'RC:ReferenceMsg',
|
||||
'RC:CombineMsg'
|
||||
]
|
||||
const timeStamp = new Date().getTime()
|
||||
const count = 20 // 获取的消息数量
|
||||
const isForward = true // 是否向前获取
|
||||
RongIMLib.getHistoryMessagesByTimestamp(this.conversationType, this.targetId, objectNames, timeStamp,
|
||||
count,
|
||||
isForward,
|
||||
({
|
||||
code,
|
||||
messages
|
||||
}) => {
|
||||
if (code === 0) {
|
||||
this.messages = messages.reverse()
|
||||
this.scrollBottom()
|
||||
}
|
||||
}
|
||||
)
|
||||
},
|
||||
send() {
|
||||
im.sendMsg(this.conversationType, this.targetId, this.inputTxt, () => {
|
||||
this.getMessageList()
|
||||
this.inputTxt = ''
|
||||
})
|
||||
},
|
||||
showFriend() {
|
||||
uni.navigateTo({
|
||||
url: '/pages/im/friends/info?targetId=' + this.targetId
|
||||
})
|
||||
},
|
||||
showMine() {
|
||||
uni.navigateTo({
|
||||
url: '/pages/im/friends/mine'
|
||||
})
|
||||
},
|
||||
scrollBottom() {
|
||||
this.$nextTick(function() {
|
||||
setTimeout(() => {
|
||||
let len = this.messages.length
|
||||
if (len) {
|
||||
if (this.scrollIntoID == "chatId_" + (len - 1)) {
|
||||
this.scrollIntoID = "chatId_" + (len - 2)
|
||||
this.scrollBottom()
|
||||
} else if (this.scrollIntoID == "chatId_" + (len - 2)) {
|
||||
this.scrollIntoID = "chatId_" + (len - 1)
|
||||
} else {
|
||||
this.scrollIntoID = "chatId_" + (len - 1)
|
||||
}
|
||||
} else {
|
||||
this.scrollIntoID = "chatId_0";
|
||||
}
|
||||
}, 50)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
$footer-height: 55px;
|
||||
|
||||
.scroll {
|
||||
height: calc(100vh - 55px);
|
||||
padding: 0 $uni-spacing-col-lg;
|
||||
}
|
||||
|
||||
.footer {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
position: fixed;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
align-items: center;
|
||||
height: $footer-height;
|
||||
background-color: $uni-bg-color-grey;
|
||||
|
||||
.u-icon {
|
||||
padding: $uni-spacing-col-sm;
|
||||
border: 1px solid $u-content-color;
|
||||
border-radius: $uni-border-radius-circle;
|
||||
}
|
||||
|
||||
.left {
|
||||
padding: 0 $uni-spacing-col-lg;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.middle {
|
||||
flex: 1;
|
||||
|
||||
.u-input {
|
||||
background-color: $uni-bg-color;
|
||||
}
|
||||
}
|
||||
|
||||
.right {
|
||||
padding: 0 $uni-spacing-col-lg;
|
||||
justify-content: center;
|
||||
}
|
||||
}
|
||||
|
||||
.custom-style {
|
||||
height: 36px;
|
||||
}
|
||||
|
||||
.time {
|
||||
text-align: center;
|
||||
font-size: $uni-font-size-sm;
|
||||
color: $u-light-color;
|
||||
}
|
||||
|
||||
.left,
|
||||
.right {
|
||||
display: flex;
|
||||
align-items: top;
|
||||
|
||||
.avatar {
|
||||
margin-top: $uni-spacing-col-base;
|
||||
}
|
||||
|
||||
.msg {
|
||||
font-size: $uni-font-size-lg;
|
||||
margin: $uni-spacing-col-base;
|
||||
padding: $uni-spacing-col-base + 5rpx;
|
||||
word-wrap: break-word;
|
||||
width: 70%;
|
||||
border-radius: $uni-border-radius-base;
|
||||
position: relative;
|
||||
|
||||
.status {
|
||||
position: absolute;
|
||||
right: 8rpx;
|
||||
bottom: 5rpx;
|
||||
font-size: $uni-font-size-base / 1.5;
|
||||
color: $uni-text-color-grey;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.left {
|
||||
.msg {
|
||||
background-color: $uni-bg-color-grey;
|
||||
}
|
||||
}
|
||||
|
||||
.right {
|
||||
justify-content: flex-end;
|
||||
|
||||
.msg {
|
||||
background-color: $u-primary-disabled;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -1,188 +1,131 @@
|
||||
<template>
|
||||
<view class="content">
|
||||
<block v-if="messageArr.length < 1">
|
||||
<view class="vertical null-list">
|
||||
<u-empty
|
||||
icon="http://cdn.uviewui.com/uview/empty/message.png"
|
||||
textColor="#999"
|
||||
text="暂无好友消息"
|
||||
>
|
||||
<template>
|
||||
<view class="null-list-btn" @click="openChum">开启聊天</view>
|
||||
</template>
|
||||
</u-empty>
|
||||
</view>
|
||||
</block>
|
||||
<block v-else>
|
||||
<view v-for="(item, index) in messageArr" :key="index" class="mssage-box">
|
||||
<view class="mssage-action" @click="openChum">
|
||||
<block v-if="item.cover === ''">
|
||||
<u-avatar
|
||||
clsss="mssage-action-cover"
|
||||
size="44"
|
||||
:text="item.text"
|
||||
font-size="14"
|
||||
randomBgColor
|
||||
></u-avatar>
|
||||
</block>
|
||||
<block v-else>
|
||||
<u-avatar
|
||||
clsss="mssage-action-cover"
|
||||
:src="item.cover"
|
||||
size="44"
|
||||
></u-avatar>
|
||||
</block>
|
||||
<view class="mssage-action-content">
|
||||
<view class="mssage-header">
|
||||
<view class="header-name">{{item.name}}</view>
|
||||
<view class="header-time">{{item.time}}</view>
|
||||
</view>
|
||||
<view class="mssage-msg">{{item.message}}</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</block>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
// import { init, connect } from '@rongcloud/imlib-uni'
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
messageArr : [
|
||||
{
|
||||
cover: 'https://cdn.uviewui.com/uview/album/1.jpg',
|
||||
name: '班班五月',
|
||||
message: '咱们进直播间了吗?上课啦~今天教大家【拆解项目背后的逻辑】',
|
||||
time: '昨天'
|
||||
},
|
||||
{
|
||||
cover: 'https://cdn.uviewui.com/uview/album/4.jpg',
|
||||
name: '周磊',
|
||||
message: '[语音]',
|
||||
time: '周一'
|
||||
},
|
||||
{
|
||||
cover: '',
|
||||
text: 'S',
|
||||
name: 'SKY',
|
||||
message: '谢谢',
|
||||
time: '2022/1/5'
|
||||
},
|
||||
{
|
||||
cover: 'https://cdn.uviewui.com/uview/album/5.jpg',
|
||||
name: '俊少',
|
||||
message: '[语音]',
|
||||
time: '2022/1/4'
|
||||
},
|
||||
],
|
||||
optionsAction: [
|
||||
{
|
||||
text: '删除',
|
||||
style: {
|
||||
backgroundColor: '#e6576b'
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
},
|
||||
methods:{
|
||||
openChum(){
|
||||
uni.showToast({
|
||||
title: '聊聊系统正在开发中,敬请期待',
|
||||
icon : 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
onNavigationBarButtonTap(e) {
|
||||
|
||||
switch (e.index){
|
||||
case 1:
|
||||
uni.showToast({
|
||||
title: '聊聊好友系统正在开发中,敬请期待',
|
||||
icon : 'none'
|
||||
})
|
||||
break;
|
||||
case 0:
|
||||
uni.showToast({
|
||||
title: '聊聊扫一扫系统正在开发中,敬请期待',
|
||||
icon : 'none'
|
||||
})
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.content{
|
||||
background-color: $window-color;
|
||||
min-height: 100vh;
|
||||
.null-list{
|
||||
height: 100vh;
|
||||
text-align: center;
|
||||
&-btn{
|
||||
margin-top: $margin * 2;
|
||||
line-height: 70rpx;
|
||||
color: $main-color;
|
||||
border:solid 1rpx $main-color;
|
||||
padding: 0 ($padding*3);
|
||||
font-size: $title-size-m;
|
||||
border-radius: 35rpx;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
}
|
||||
.mssage-box{
|
||||
background: white;
|
||||
.mssage-action{
|
||||
position: relative;
|
||||
padding: 20rpx $padding;
|
||||
&::after{
|
||||
position: absolute;
|
||||
left: $padding + 108;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
content: " ";
|
||||
height: 1rpx;
|
||||
background: $border-color;
|
||||
}
|
||||
&-content{
|
||||
position: absolute;
|
||||
top: 20rpx;
|
||||
height: 44px;
|
||||
left: $padding + 108;
|
||||
right: $margin;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
.mssage-header{
|
||||
display: flex;
|
||||
font-size: $title-size;
|
||||
line-height: 40rpx;
|
||||
justify-content: space-between;
|
||||
.header-name{
|
||||
flex: 1;
|
||||
@extend .nowrap;
|
||||
}
|
||||
.header-time{
|
||||
padding-left: $padding;
|
||||
font-size: $title-size-sm - 2;
|
||||
color: $text-gray;
|
||||
}
|
||||
}
|
||||
.mssage-msg{
|
||||
font-size: $title-size-sm - 2;
|
||||
color: $text-gray;
|
||||
@extend .nowrap;
|
||||
}
|
||||
}
|
||||
}
|
||||
&-item:last-child{
|
||||
.mssage-action::after{
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
<template>
|
||||
<div>
|
||||
<u-popup overlay mode="left" closeable closeOnClickOverlay :show="showLeft" @close="showLeft = false">
|
||||
<leftMenu></leftMenu>
|
||||
</u-popup>
|
||||
|
||||
<div class="container">
|
||||
<div class="msg-item u-border-bottom" v-for="(item, index) in conversations" @click="toDetail(item)">
|
||||
<div class="avatar">
|
||||
<u-badge numberType="ellipsis" max="99" shape="horn" absolute :offset="[-7, -7]"
|
||||
:value="item.unreadMessageCount" />
|
||||
<u-avatar shape="square" :src="friend(item.targetId).portraitUrl"></u-avatar>
|
||||
</div>
|
||||
<div class="content ">
|
||||
<div class="name">
|
||||
<h3>{{ friend(item.targetId).name }}</h3>
|
||||
<span class="time">{{ item.sentTime|timeCustomCN }}</span>
|
||||
</div>
|
||||
<div class="u-line-1">{{ item.latestMessage.content }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<u-loadmore status="nomore" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import * as RongIMLib from '@rongcloud/imlib-uni'
|
||||
import im from '@/utils/im/index.js'
|
||||
import leftMenu from './components/left_menu.vue'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
leftMenu
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
isShown: true, // 当前页面显示状态
|
||||
conversations: [], // 会话列表
|
||||
showLeft: false
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
friend() {
|
||||
return function(targetId) {
|
||||
return this.$store.getters.userInfo(targetId)
|
||||
}
|
||||
}
|
||||
},
|
||||
onLoad() {},
|
||||
onShow() {
|
||||
this.getConversationList()
|
||||
this.isShown = true
|
||||
},
|
||||
onHide() {
|
||||
this.isShown = false
|
||||
},
|
||||
onNavigationBarButtonTap(e) {
|
||||
if (e.index == 0) {
|
||||
this.showLeft = true
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
'$store.getters.newMessage': function(n, o) {
|
||||
if (this.isShown) {
|
||||
this.getConversationList()
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
getFriend(targetId) {
|
||||
return this.$store.getters.userInfo(targetId)
|
||||
},
|
||||
getConversationList() {
|
||||
const count = 1000
|
||||
const timestamp = 0 // 会话的时间戳(获取这个时间戳之前的会话列表,0 表示从最新开始获取)会话类型
|
||||
RongIMLib.getConversationList(undefined, count, timestamp, (res) => {
|
||||
if (res.code === 0 && res.conversations.length > 0) {
|
||||
this.conversations = res.conversations
|
||||
}
|
||||
})
|
||||
},
|
||||
// 进入聊天的详情页面,清理未读消息数量
|
||||
toDetail(item) {
|
||||
uni.navigateTo({
|
||||
url: '/pages/im/detail?targetId=' + item.targetId +
|
||||
'&conversationType=' + item.conversationType
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.container {
|
||||
padding: 0 $uni-spacing-col-lg $uni-spacing-col-lg $uni-spacing-col-lg;
|
||||
}
|
||||
|
||||
.msg-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: $uni-spacing-col-lg 0;
|
||||
|
||||
.avatar {
|
||||
position: relative;
|
||||
margin-right: $uni-spacing-col-lg;
|
||||
|
||||
.u-badge {
|
||||
z-index: 9;
|
||||
}
|
||||
}
|
||||
|
||||
.content {
|
||||
flex: 1;
|
||||
|
||||
.name {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
|
||||
.time {
|
||||
font-size: $uni-font-size-sm;
|
||||
color: $uni-text-color-grey;
|
||||
}
|
||||
}
|
||||
|
||||
.u-line-1 {
|
||||
color: $u-info;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user