uniapp接融云即時通訊IM

因為融云含ui的聊天室并不適用于uniapp切厘,所以uniapp中所有東西就需要自己來實現(xiàn),好在融云給的接口還是很全的等浊,因為項目的差異性我只介紹聊天室的代碼(因為沒做任何優(yōu)化屯曹,可能代碼可讀性并不強(qiáng),而且只實現(xiàn)了發(fā)送文字信息的功能吓坚,分享出來只是方便自己看撵幽,接不通可以私信我)
在開始聊天之前需要先連接融云的im(main.js中)


//引入融云SDK
const RongIMib = require('./common/libs/RongIMLib-3.0.7.1-dev.js')
const RongIMLib = require('./common/libs/RongEmoji-2.2.9.js')

//初始化融云SDK
let im = RongIMib.init({
    appkey: ""http://申請的key
})
Vue.prototype.$Im = im
//掛載融云到Vue原型
Vue.prototype.$RongIMib = RongIMib
Vue.prototype.$RongIMLib=RongIMLib

可以在首頁打印下判斷是否連接成功
我是在首頁連接的融云,連接的話只一次就可以礁击,可以在登錄成功后連接盐杂,我感覺在登錄成功后連接更好(剛開始就連接就好,不用監(jiān)聽哆窿,在會話列表里已經(jīng)監(jiān)聽過了) 注意:連接融云需要token链烈,這時候需要讀后端的接口拿到

rongyunInit() {
            //初始化融云
            // let _that = this;
            //設(shè)置監(jiān)聽
            // let conversationList = []; //當(dāng)前存在的會話列表
            // _that.$Im.watch({
            //  //對會話列表進(jìn)行監(jiān)聽
            //  conversation: event => {
            //      console.log(event);
            //      let updatedConversationList = event.updatedConversationList; //更新的會話列表
            //      console.log('upload:', updatedConversationList);
            //      let sendOut = {
            //          content: event.updatedConversationList[0].latestMessage.content.content,
            //          sentTime: event.updatedConversationList[0].latestMessage.sentTime
            //      };
            //      uni.setStorageSync('sendOut', sendOut);
            //      if (event.updatedConversationList[0].latestMessage.messageDirection == 2) {
            //          this.chat.push({
            //              type: event.updatedConversationList[0].latestMessage.messageDirection,
            //              time: event.updatedConversationList[0].latestMessage.sentTime,
            //              msg: event.updatedConversationList[0].latestMessage.content.content,
            //              avatar: uni.getStorageSync('user_info').avatar
            //          });
            //      }
            //  },
            //  status: function(event) {
            //      let status = event.status;
            //      console.log('status num:', status);
            //  }
            // });
            /**
             * 3.連接融云
             *   1) 需要先調(diào)用服務(wù)端接口,獲取Token 然后進(jìn)行連接
             */
            //進(jìn)行連接
            this.$Im
                .connect({
                    token: uni.getStorageSync('rong_token')
                })
                .then(user => {
                    console.log('success id: ', user.id);
                })
                .catch(error => {
                    console.log('error: ', error.code, error.msg);
                });
        },

聊天室代碼.js文件

export default {
    name: 'index',
    data() {
        return {
            chat: [],
            sentText: '',
            doctorInfo: '',
            height: '0rpx',
            scrollTop: 0,
            count: 7,
            uuid: '',
            avatar: '',
            list: [],
            style: {
                pageHeight: 0,
                contentViewHeight: 0,
                footViewHeight: 90,
                mitemHeight: 0
            },
        };
    },
    onLoad(options) {
        this.uuid = options.uuid
        this.avatar = options.avatar
        this.rongyunInit();
        this.getlist(new Date())
        const res = uni.getSystemInfoSync(); //獲取手機(jī)可使用窗口高度     api為獲取系統(tǒng)信息同步接口
        this.style.pageHeight = res.windowHeight;
        this.style.contentViewHeight = res.windowHeight - uni.getSystemInfoSync().screenWidth / 750 * (100); //像素   因為給出的是像素高度 然后我們用的是upx  所以換算一下 
    },
    onShow() {
        this.chat = []
        this.clear()
    },
    methods: {
        read(uuid, time) {
            let conversation = this.$Im.Conversation.get({
                targetId: this.uuid,
                type: this.$RongIMib.CONVERSATION_TYPE.PRIVATE
            });
            let messageUId = uuid; // 消息唯一 Id, message 中的 messageUid
            let lastMessageSendTime = time; // 最后一條消息的發(fā)送時間
            let type = '1'; // 備用挚躯,默認(rèn)賦值 1 即可
            // 以上 3 個屬性在會話的最后一條消息中可以獲得
            conversation.send({
                messageType: 'RC:ReadNtf',
                content: {
                    messageUId: messageUId,
                    lastMessageSendTime: lastMessageSendTime,
                    type: type
                }
            }).then((message) => {
                console.log('發(fā)送已讀通知消息成功', message);
            });
        },
        scrolltoupper() {
            this.$u.throttle(this.getlist(this.chat[0].time, 1), 500)
        },
        // 清空未讀數(shù)
        clear() {
            this.$Im.Conversation.getTotalUnreadCount().then(function(totalUnreadCount) {
              console.log('獲取未讀總數(shù)成功', totalUnreadCount);
            });
            let conversation = this.$Im.Conversation.get({
                targetId: this.uuid,
                type: this.$RongIMib.CONVERSATION_TYPE.PRIVATE
            });
            conversation.read().then(()=> {
                console.log('清除未讀數(shù)成功'); // im.watch conversation 將被觸發(fā)
            });
        
        },
        scrollToBottom() {
            let that = this;
            let query = uni.createSelectorQuery();
            query.selectAll('.m-item').boundingClientRect();
            query.select('#scrollview').boundingClientRect();
            query.exec((res) => {
                console.log(res)
                that.style.mitemHeight = 0;
                res[0].forEach((rect) => that.style.mitemHeight = that.style.mitemHeight + rect.height + 40) //獲取所有內(nèi)部子元素的高度
                // 因為vue的虛擬DOM 每次生成的新消息都是之前的强衡,所以采用異步setTimeout    主要就是添加了這定時器
                setTimeout(() => {
                    if (that.style.mitemHeight > (that.style.contentViewHeight - 100)) { //判斷子元素高度是否大于顯示高度
                        console.log(3333)
                        that.scrollTop = that.style.mitemHeight - that.style.contentViewHeight //用子元素的高度減去顯示的高度就獲益獲得序言滾動的高度
                    }
                }, 100)
            })
        },
        // #ifdef APP-PLUS
        hgit() {
            this.height = '50rpx'
        },
        // #endif
        // #ifdef H5
        hgit() {
            this.height = '500rpx'
        },
        // #endif
        blur() {
            this.height = '0rpx'
        },
        rongyunInit() {
            //初始化融云
            let _that = this;
            //設(shè)置監(jiān)聽
            let conversationList = []; //當(dāng)前存在的會話列表
            _that.$Im.watch({
                //對會話列表進(jìn)行監(jiān)聽
                conversation: (event) => {
                    let updatedConversationList = event.updatedConversationList; //更新的會話列表
                    console.log('upload用戶端:', updatedConversationList);
                    let sendOut = {
                        content: event.updatedConversationList[0].latestMessage.content.content,
                        sentTime: event.updatedConversationList[0].latestMessage.sentTime
                    }
                    uni.setStorageSync('sendOut', sendOut)
                    if (event.updatedConversationList[0].latestMessage.messageDirection == 2) {
                        console.log('執(zhí)行了')
                        this.chat.push({
                            type: event.updatedConversationList[0].latestMessage.messageDirection,
                            time: event.updatedConversationList[0].latestMessage.sentTime,
                            msg: event.updatedConversationList[0].latestMessage.content.content,
                            avatar: uni.getStorageSync('user_info').avatar
                        })
                    }
                },
                status: function(event) {
                    let status = event.status;
                    console.log('status num:', status);
                }
            });
            /**
             * 3.連接融云
             *   1) 需要先調(diào)用服務(wù)端接口,獲取Token 然后進(jìn)行連接
             */
            //進(jìn)行連接
            // this.$Im
            //  .connect({
            //      token: uni.getStorageSync('rong_token')
            //  })
            //  .then(user => {
            //      console.log('success id: ', user.id);
            //  })
            //  .catch(error => {
            //      console.log('error: ', error.code, error.msg);
            //  });
        },
        // 獲取歷史信息
        getlist(time, status) {
            console.log(time, status)
            let conversation = this.$Im.Conversation.get({
                targetId: this.uuid,
                type: this.$RongIMib.CONVERSATION_TYPE.PRIVATE
            });
            let option = {
                timestrap: +time,
                count: this.count
            };
            conversation.getMessages(option).then((result) => {
                this.list = result.list; // 歷史消息列表
                let hasMore = result.hasMore; // 是否還有歷史消息可以獲取
                console.log('獲取歷史消息成功', this.list, hasMore);
                // this.read(this.list.length.messageUId, this.list.length.sentTime)
                this.read(this.list[this.list.length-1].messageUId, this.list[this.list.length-1].sentTime)
                // data == 1下拉加載
                if (status == 1) {
                    let a = []
                    this.list.forEach(item => {
                        if (item.messageDirection == 1) {
                            a.push({
                                type: item.messageDirection,
                                time: item.sentTime,
                                msg: item.content.content,
                                avatar: uni.getStorageSync('user_info').avatar
                            })
                        } else {
                            a.push({
                                type: item.messageDirection,
                                time: item.sentTime,
                                msg: item.content.content,
                                avatar: this.avatar
                            })
                        }
                    })
                    console.log(2342342,a)
                    this.chat.unshift(...a)
                    console.log(this.chat)
                }else{
                    this.list.forEach(item => {
                        if (item.messageDirection == 1) {
                            this.chat.push({
                                type: item.messageDirection,
                                time: item.sentTime,
                                msg: item.content.content,
                                avatar: uni.getStorageSync('user_info').avatar
                            })
                        } else {
                            this.chat.push({
                                type: item.messageDirection,
                                time: item.sentTime,
                                msg: item.content.content,
                                avatar: this.avatar
                            })
                        }
                    })
                }
                console.log(this.chat)
            });
        },
        //融云發(fā)送消息方法
        sendMessage(type, content) {
            //4.連接融云成功后進(jìn)行發(fā)送消息
            this.conversation = this.$Im.Conversation.get({
                targetId: this.uuid,
                type: this.$RongIMib.CONVERSATION_TYPE.PRIVATE
            });
            this.conversation.send({
                messageType: this.$RongIMib.MESSAGE_TYPE[type],
                content
            }).then((msg) => {
                this.scrollToBottom(); //創(chuàng)建后調(diào)用回到底部方法
                console.log('發(fā)送消息成功', msg);
                this.chat.push({
                    type: msg.messageDirection,
                    time: msg.sentTime,
                    msg: msg.content.content,
                    avatar: uni.getStorageSync('user_info').avatar
                })
                this.sentText = []
            }).catch(err => {
                console.log('消息發(fā)送失敗', err)
            })
        },
    }
};

.vue文件

<template>
    <view class="mh100p pd20 pb100" :style="{height: 100+'%'}">
        <share title="即時通訊" />
        <scroll-view id="scrollview" @scrolltoupper='scrolltoupper' class="chat-window pb100" scroll-y="true" :style="{ height: style.contentViewHeight + 'px' }" :scroll-with-animation="true" :scroll-top="scrollTop">
            <view class="my20 m-item" v-for="(item, index) in chat" :key="index">
                <view class="cl999 ftr26 mb20 u-text-center">{{ item.time | date('yyyy.mm.dd hh:MM:ss') }}</view>
                <view class="yc" :class="item.type == 1 ? 'rever' : ''">
                    <u-avatar mode="square" :src="item.avatar"></u-avatar>
                    <view class="chat-box">{{ item.msg }}</view>
                </view>
            </view>
        </scroll-view>
        <view class="fix-wrap bgfff px30 py20 pb50" :style="{ bottom: height }">
            <u-search
                placeholder="請輸入發(fā)送內(nèi)容"
                search-icon="edit-pen"
                action-text="發(fā)送"
                v-model="sentText"
                @focus="hgit"
                @blur="blur"
                @custom="sendMessage('TEXT', { content: sentText })"
                @search="sendMessage('TEXT', { content: sentText })"
            ></u-search>
        </view>
    </view>
</template>

<script>
import index from './index.js';
export default index;
</script>

<style lang="scss" scope>
@import './index.scss';
</style>

.css文件(用的ui庫是uview)

.chat-box {
    padding: 20upx;
    margin: 0 16upx;
    background-color: #ffffff;
    border-radius: 12upx;
}
.rever {
    flex-direction: row-reverse;
    .chat-box {
        background-color: #f8e271;
    }
}
.fix-wrap {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100upx;
}
/deep/ .u-input__input {
    background-color: #f4f4f4;
    border-radius: 20upx;
    padding: 0 20upx;
}

即時通訊接起來還是挺麻煩的码荔,走一遍之后就還好漩勤,該有的api都有所提供,因為我是直接拿的項目里的缩搅,可能并不適用于你們

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末越败,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子硼瓣,更是在濱河造成了極大的恐慌究飞,老刑警劉巖,帶你破解...
    沈念sama閱讀 223,126評論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異亿傅,居然都是意外死亡媒峡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評論 3 400
  • 文/潘曉璐 我一進(jìn)店門葵擎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谅阿,“玉大人,你說我怎么就攤上這事坪蚁”即” “怎么了?”我有些...
    開封第一講書人閱讀 169,941評論 0 366
  • 文/不壞的土叔 我叫張陵敏晤,是天一觀的道長贱田。 經(jīng)常有香客問我,道長嘴脾,這世上最難降的妖魔是什么男摧? 我笑而不...
    開封第一講書人閱讀 60,294評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮译打,結(jié)果婚禮上耗拓,老公的妹妹穿的比我還像新娘。我一直安慰自己奏司,他們只是感情好乔询,可當(dāng)我...
    茶點故事閱讀 69,295評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著韵洋,像睡著了一般竿刁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上搪缨,一...
    開封第一講書人閱讀 52,874評論 1 314
  • 那天食拜,我揣著相機(jī)與錄音,去河邊找鬼副编。 笑死负甸,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的痹届。 我是一名探鬼主播呻待,決...
    沈念sama閱讀 41,285評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼队腐!你這毒婦竟也來了蚕捉?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,249評論 0 277
  • 序言:老撾萬榮一對情侶失蹤香到,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體悠就,經(jīng)...
    沈念sama閱讀 46,760評論 1 321
  • 正文 獨居荒郊野嶺守林人離奇死亡千绪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,840評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了梗脾。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片荸型。...
    茶點故事閱讀 40,973評論 1 354
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖炸茧,靈堂內(nèi)的尸體忽然破棺而出瑞妇,到底是詐尸還是另有隱情,我是刑警寧澤梭冠,帶...
    沈念sama閱讀 36,631評論 5 351
  • 正文 年R本政府宣布辕狰,位于F島的核電站,受9級特大地震影響控漠,放射性物質(zhì)發(fā)生泄漏蔓倍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,315評論 3 336
  • 文/蒙蒙 一盐捷、第九天 我趴在偏房一處隱蔽的房頂上張望偶翅。 院中可真熱鬧,春花似錦碉渡、人聲如沸聚谁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽形导。三九已至,卻和暖如春铭段,著一層夾襖步出監(jiān)牢的瞬間骤宣,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評論 1 275
  • 我被黑心中介騙來泰國打工序愚, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留憔披,地道東北人。 一個月前我還...
    沈念sama閱讀 49,431評論 3 379
  • 正文 我出身青樓爸吮,卻偏偏與公主長得像芬膝,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子形娇,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,982評論 2 361

推薦閱讀更多精彩內(nèi)容