Merge pull request 'xbx' (#134) from xbx into master
Reviewed-on: http://git.luyuan.tk/luyuan/deming/pulls/134
This commit is contained in:
		
						commit
						f32f7df14a
					
				
							
								
								
									
										4
									
								
								App.vue
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								App.vue
									
									
									
									
									
								
							@ -1,10 +1,14 @@
 | 
			
		||||
<script>
 | 
			
		||||
	import { mapMutations, mapState } from 'vuex';
 | 
			
		||||
	export default {
 | 
			
		||||
		globalData: {  
 | 
			
		||||
			im: {}  
 | 
			
		||||
		},  
 | 
			
		||||
        computed: {
 | 
			
		||||
            ...mapState(["hasLogin"])
 | 
			
		||||
        },
 | 
			
		||||
		onLaunch() {
 | 
			
		||||
			getApp().globalData.im = this.imService
 | 
			
		||||
			// 缓存token
 | 
			
		||||
			uni.getStorage({
 | 
			
		||||
				key: "token",
 | 
			
		||||
 | 
			
		||||
@ -388,7 +388,10 @@ export default {
 | 
			
		||||
            that.list= res.data.data
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
    },6 * 1000)
 | 
			
		||||
          that.$forceUpdate();
 | 
			
		||||
          console.log(that.room)
 | 
			
		||||
 | 
			
		||||
    },60 * 1000)
 | 
			
		||||
    uni.request({
 | 
			
		||||
      url:"https://dmmall.sdbairui.com/api/Specialci/livingUserInfo",
 | 
			
		||||
      data:{
 | 
			
		||||
@ -424,30 +427,14 @@ export default {
 | 
			
		||||
              id : a.rid + "",
 | 
			
		||||
              name : a.rid + ""
 | 
			
		||||
          };
 | 
			
		||||
           that.chatRoomService = getApp().globalData.im
 | 
			
		||||
          console.log(room,currentUser)
 | 
			
		||||
          //构造chatRoomService
 | 
			
		||||
            that.chatRoomService = new ChatRoomService(room, currentUser);
 | 
			
		||||
 | 
			
		||||
            that.chatRoomService.initialWhenNewMessage(that.whenNewMessage);
 | 
			
		||||
            that.chatRoomService.connectGoEasyIM();
 | 
			
		||||
 | 
			
		||||
			//获取当前聊天室数据
 | 
			
		||||
            that.room = that.chatRoomService.room;
 | 
			
		||||
            setTimeout(function() {
 | 
			
		||||
              that.chatRoomService.quitRoom();
 | 
			
		||||
              that.chatRoomService = {}
 | 
			
		||||
               
 | 
			
		||||
            }, 1000);
 | 
			
		||||
            setTimeout(function(){
 | 
			
		||||
              that.chatRoomService = new ChatRoomService(room, currentUser);
 | 
			
		||||
 | 
			
		||||
            that.chatRoomService.initialWhenNewMessage(that.whenNewMessage);
 | 
			
		||||
            that.chatRoomService.connectGoEasyIM();
 | 
			
		||||
 | 
			
		||||
			//获取当前聊天室数据
 | 
			
		||||
            that.room = that.chatRoomService.room;
 | 
			
		||||
            console.log(that.room.onlineUsers)
 | 
			
		||||
            }, 2000);
 | 
			
		||||
          that.chatRoomService.subscribeRoomMessage(room,currentUser)
 | 
			
		||||
          that.chatRoomService.initialWhenNewMessage(that.whenNewMessage);
 | 
			
		||||
          //获取当前聊天室数据
 | 
			
		||||
          that.room = that.chatRoomService.room;
 | 
			
		||||
          console.log(that.room)
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
@ -465,7 +452,7 @@ export default {
 | 
			
		||||
		},
 | 
			
		||||
  methods:{
 | 
			
		||||
    sendMessage (messageType, content) {//发送消息
 | 
			
		||||
    console.log(123)
 | 
			
		||||
    console.log(this.room,this.room.id,messageType, content)
 | 
			
		||||
				if(content == "" && messageType == 0) return;
 | 
			
		||||
				var message = {
 | 
			
		||||
					senderNickname : this.room.currentUser.nickname ,
 | 
			
		||||
 | 
			
		||||
@ -69,6 +69,14 @@ function IMService() {
 | 
			
		||||
    this.onFriendListChange = function (friends) {};
 | 
			
		||||
    //群列表发生改变
 | 
			
		||||
    this.onGroupListChange = function (groups) {};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    this.whenNewMessage = function () {
 | 
			
		||||
 | 
			
		||||
    };
 | 
			
		||||
    this.whenOnlineUserChange = function () {
 | 
			
		||||
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//登录
 | 
			
		||||
@ -354,23 +362,23 @@ IMService.prototype.initialIMListeners = function () {
 | 
			
		||||
        this.onNewPrivateMessageReceive(friendId, message);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    //监听群聊消息
 | 
			
		||||
    this.im.on(GoEasyIM.EVENT.GROUP_MESSAGE_RECEIVED, (message) => {
 | 
			
		||||
        let groupId = message.groupId;
 | 
			
		||||
        //群未读消息+1
 | 
			
		||||
        let group = this.groups[groupId];
 | 
			
		||||
        group.unReadMessage++;
 | 
			
		||||
        removeGroupPendingMessage(this, groupId, message);
 | 
			
		||||
        //如果页面传入了相应的listener,执行listener
 | 
			
		||||
        this.onGroupListChange(this.groups);
 | 
			
		||||
    // //监听群聊消息
 | 
			
		||||
    // this.im.on(GoEasyIM.EVENT.GROUP_MESSAGE_RECEIVED, (message) => {
 | 
			
		||||
    //     let groupId = message.groupId;
 | 
			
		||||
    //     //群未读消息+1
 | 
			
		||||
    //     let group = this.groups[groupId];
 | 
			
		||||
    //     group.unReadMessage++;
 | 
			
		||||
    //     removeGroupPendingMessage(this, groupId, message);
 | 
			
		||||
    //     //如果页面传入了相应的listener,执行listener
 | 
			
		||||
    //     this.onGroupListChange(this.groups);
 | 
			
		||||
 | 
			
		||||
        //更新群聊消息记录
 | 
			
		||||
        let groupMessages = this.getGroupMessages(groupId);
 | 
			
		||||
        let sentMessages = groupMessages.sentMessages;
 | 
			
		||||
        sentMessages.push(message);
 | 
			
		||||
        //如果页面传入了相应的listener,执行listener
 | 
			
		||||
        this.onNewGroupMessageReceive(groupId, message);
 | 
			
		||||
    })
 | 
			
		||||
    //     //更新群聊消息记录
 | 
			
		||||
    //     let groupMessages = this.getGroupMessages(groupId);
 | 
			
		||||
    //     let sentMessages = groupMessages.sentMessages;
 | 
			
		||||
    //     sentMessages.push(message);
 | 
			
		||||
    //     //如果页面传入了相应的listener,执行listener
 | 
			
		||||
    //     this.onNewGroupMessageReceive(groupId, message);
 | 
			
		||||
    // })
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -602,4 +610,240 @@ function removeGroupPendingMessage(imService,groupId, message){
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//下面是合并的
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//用户
 | 
			
		||||
function User(id, nickname, avatar) {
 | 
			
		||||
    this.id = id;
 | 
			
		||||
    this.nickname = nickname;
 | 
			
		||||
    this.avatar = avatar;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//消息
 | 
			
		||||
function Message(senderUserId, senderNickname, content, type) {
 | 
			
		||||
    this.senderNickname = senderNickname;
 | 
			
		||||
    this.senderUserId = senderUserId;
 | 
			
		||||
    this.content = content;
 | 
			
		||||
    this.type = type
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//聊天室
 | 
			
		||||
function Room(id, name, currentUser) {
 | 
			
		||||
 | 
			
		||||
    this.id = id;
 | 
			
		||||
    this.name = name;
 | 
			
		||||
    this.currentUser = currentUser;
 | 
			
		||||
 | 
			
		||||
    this.onlineUsers = {
 | 
			
		||||
        count: 0,
 | 
			
		||||
        users: []
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    this.messages = [];
 | 
			
		||||
 | 
			
		||||
    this.MessageType = {
 | 
			
		||||
        CHAT: 0,//文字聊天
 | 
			
		||||
        PROP: 1//道具
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    this.Prop = {
 | 
			
		||||
        HEART: 0,//桃心
 | 
			
		||||
        ROCKET: 1//火箭
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
IMService.prototype.initialWhenNewMessage = function (whenNewMessage) {
 | 
			
		||||
    this.whenNewMessage = whenNewMessage;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
IMService.prototype.initialWhenOnlineUserChange = function (whenOnlineUserChange) {
 | 
			
		||||
    this.whenOnlineUserChange = whenOnlineUserChange;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//监听新消息
 | 
			
		||||
IMService.prototype.listenerNewMessage = function () {
 | 
			
		||||
 | 
			
		||||
    this.im.on(GoEasyIM.EVENT.GROUP_MESSAGE_RECEIVED, (message) => {
 | 
			
		||||
		var content = JSON.parse(message.payload.text);
 | 
			
		||||
        this.addNewMessage(message);
 | 
			
		||||
        this.whenNewMessage(content);
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
IMService.prototype.addNewMessage = function (message) {
 | 
			
		||||
    var content = JSON.parse(message.payload.text);
 | 
			
		||||
    let messageContent = "";
 | 
			
		||||
    //聊天消息
 | 
			
		||||
    if (content.type == this.room.MessageType.CHAT) {
 | 
			
		||||
        messageContent = content.content;
 | 
			
		||||
    }
 | 
			
		||||
    //道具消息
 | 
			
		||||
    if (content.type == this.room.MessageType.PROP) {
 | 
			
		||||
        if (content.content == this.room.Prop.ROCKET) {
 | 
			
		||||
            messageContent = "送出了一枚大火箭";
 | 
			
		||||
        }
 | 
			
		||||
        if (content.content == this.room.Prop.HEART) {
 | 
			
		||||
            messageContent = "送出了一个大大的比心";
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    //添加消息
 | 
			
		||||
    let newMessage = new Message(message.senderId, content.senderNickname, messageContent);
 | 
			
		||||
    this.room.messages.push(newMessage);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
//监听用户上下线
 | 
			
		||||
IMService.prototype.listenerGroupPresence = function () {
 | 
			
		||||
    this.im.on(GoEasyIM.EVENT.GROUP_PRESENCE, (event) => {
 | 
			
		||||
        //更新在线用户数
 | 
			
		||||
        this.room.onlineUsers.count = event.groupOnlineCount;
 | 
			
		||||
 | 
			
		||||
        if (event.action == 'join' || event.action == 'online') {
 | 
			
		||||
            let userData = JSON.parse(event.userData);
 | 
			
		||||
            //添加新用户
 | 
			
		||||
            let user = new User(event.userId, userData.nickname, userData.avatar);
 | 
			
		||||
 | 
			
		||||
            //添加在线用户,避免用户重复
 | 
			
		||||
            if (!this.room.onlineUsers.users.find(item => item.id == event.userId)) {
 | 
			
		||||
                this.room.onlineUsers.users.push(user);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            //添加进入房间的消息
 | 
			
		||||
            let message = new Message(event.userId, userData.nickname, " 进入房间", this.room.MessageType.CHAT);
 | 
			
		||||
            this.room.messages.push(message);
 | 
			
		||||
        } else {
 | 
			
		||||
            let offlineUserIndex = this.room.onlineUsers.users.findIndex(item => item.id == event.userId);
 | 
			
		||||
            if (offlineUserIndex > -1) {
 | 
			
		||||
                //将离开的用户从onlineUsers中删掉
 | 
			
		||||
                let offlineUser = Object.assign(this.room.onlineUsers.users[offlineUserIndex]);
 | 
			
		||||
                this.room.onlineUsers.users.splice(offlineUserIndex, 1);
 | 
			
		||||
                //添加离开消息
 | 
			
		||||
                let message = new Message(offlineUser.id, offlineUser.nickname, " 离开房间", this.room.MessageType.CHAT)
 | 
			
		||||
                this.room.messages.push(message);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        this.whenOnlineUserChange(this.room.onlineUsers);
 | 
			
		||||
    })
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
//查询和初始化在线用户列表和在线用户数
 | 
			
		||||
IMService.prototype.initialOnlineUsers = function (roomId) {
 | 
			
		||||
    let self = this;
 | 
			
		||||
 | 
			
		||||
    //查询最新上线的用户列表
 | 
			
		||||
    this.im.groupHereNow(roomId)
 | 
			
		||||
        .then(result => {
 | 
			
		||||
            if (result.code == 200) {
 | 
			
		||||
                let users = [];
 | 
			
		||||
                result.content && result.content.map(function (onlineUser) {
 | 
			
		||||
                    let userData = JSON.parse(onlineUser.userData);
 | 
			
		||||
                    let user = new User(onlineUser.userId, userData.nickname, userData.avatar);
 | 
			
		||||
                    users.push(user);
 | 
			
		||||
                });
 | 
			
		||||
                self.room.onlineUsers = {
 | 
			
		||||
                    users: users
 | 
			
		||||
                };
 | 
			
		||||
            }
 | 
			
		||||
        }).catch(e => {
 | 
			
		||||
        if (e.code == 401) {
 | 
			
		||||
            console.log("您还没有开通用户在线状态提醒,登录goeasy->我的应用->查看详情->高级功能,自助开通.");
 | 
			
		||||
        } else {
 | 
			
		||||
            console.log(e);
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
    //获取聊天室在线用户数
 | 
			
		||||
    this.im.groupOnlineCount(roomId)
 | 
			
		||||
        .then(result => {
 | 
			
		||||
            this.room.onlineUsers.count = result.content.onlineCount;
 | 
			
		||||
        }).catch(e => {
 | 
			
		||||
        console.log(e)
 | 
			
		||||
    })
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
//订阅聊天室成员上下线
 | 
			
		||||
IMService.prototype.subscribePresence = function (roomId) {
 | 
			
		||||
    this.im.subscribeGroupPresence([roomId])
 | 
			
		||||
        .then(() => {
 | 
			
		||||
            console.log('成员上下线订阅成功')
 | 
			
		||||
        }).catch(e => {
 | 
			
		||||
        console.log(e)
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//订阅聊天室消息
 | 
			
		||||
IMService.prototype.subscribeRoomMessage = function (room, user) {
 | 
			
		||||
    this.room = new Room(room.id, room.name, user);
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    //监听上下线提醒
 | 
			
		||||
    this.listenerGroupPresence();
 | 
			
		||||
 | 
			
		||||
    //监听新消息
 | 
			
		||||
    this.listenerNewMessage();
 | 
			
		||||
     //订阅用户上下线事件
 | 
			
		||||
     this.subscribePresence(this.room.id);
 | 
			
		||||
     //订阅聊天室消息
 | 
			
		||||
    //  this.subscribeRoomMessage(this.room.id);
 | 
			
		||||
    this.im.subscribeGroup([this.room.id])
 | 
			
		||||
        .then(result => {
 | 
			
		||||
            console.log('消息订阅成功')
 | 
			
		||||
        }).catch(e => {
 | 
			
		||||
        console.log(e,'失败')
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//历史消息
 | 
			
		||||
IMService.prototype.initialChatHistory = function (roomId) {
 | 
			
		||||
    var self = this;
 | 
			
		||||
    this.im.history({
 | 
			
		||||
        groupId: roomId
 | 
			
		||||
    }).then(res => {
 | 
			
		||||
        res.content.forEach(function (message) {
 | 
			
		||||
            self.addNewMessage(message);
 | 
			
		||||
        })
 | 
			
		||||
    }).catch(function (error) {
 | 
			
		||||
        if (error.code == 401) {
 | 
			
		||||
            console.log("您还没有开通历史消息的权限,登录goeasy->我的应用->查看详情->高级功能,自助开通.");
 | 
			
		||||
        } else {
 | 
			
		||||
            console.log(error);
 | 
			
		||||
        }
 | 
			
		||||
    })
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
//发送消息
 | 
			
		||||
IMService.prototype.sendMessages = function (roomId, content) {
 | 
			
		||||
	var contentMessage = {
 | 
			
		||||
		text: JSON.stringify(content)
 | 
			
		||||
	};
 | 
			
		||||
	let message = this.im.createTextMessage(contentMessage);
 | 
			
		||||
    this.im.sendGroupMessage(roomId, message)
 | 
			
		||||
        .then(() => {
 | 
			
		||||
            console.log('消息发送成功')
 | 
			
		||||
        }).catch(e => {
 | 
			
		||||
        console.log(e);
 | 
			
		||||
    })
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
//退出聊天室
 | 
			
		||||
IMService.prototype.quitRoom = function (roomId) {
 | 
			
		||||
    // this.im.subscribeGroup([0])
 | 
			
		||||
    //     .then(result => {
 | 
			
		||||
    //         console.log('消息订阅成功')
 | 
			
		||||
    //     }).catch(e => {
 | 
			
		||||
    //     console.log(e,'失败')
 | 
			
		||||
    // })
 | 
			
		||||
    // this.subscribePresence(0);
 | 
			
		||||
    this.disconnect()
 | 
			
		||||
    this.connectIM()
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
export default IMService;
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user