Forráskód Böngészése

添加单点登录功能

wkw 6 hónapja
szülő
commit
44838f34cf
2 módosított fájl, 45 hozzáadás és 2 törlés
  1. 25 0
      common/queue.js
  2. 20 2
      pages/msg/im.vue

+ 25 - 0
common/queue.js

@@ -649,6 +649,7 @@ module.exports = {
 			success(res) {
 				// 这里是接口调用成功的回调,不是连接成功的回调,请注意
 				console.log('连接成功',res)
+				that.onSocketMsg();
 			},
 			fail(err) {
 				console.log('连接失败',res)
@@ -656,6 +657,30 @@ module.exports = {
 			}
 		})
 	},
+	onSocketMsg(){
+		let that = this
+		uni.onSocketMessage((res) => {
+		  if (res.data === 'HeartBeat') return // 忽略心跳包
+		  let msg
+		  try {
+		    msg = JSON.parse(res.data)
+		  } catch (e) {
+		    // console.log('消息不是JSON格式:', res.data)
+		    return
+		  }
+		  if (msg.type === 'kickOut') {
+		    uni.showModal({
+		      title: '提示',
+		      content: msg.content,
+		      showCancel: false,
+		      success() {
+		        that.logout();
+		        uni.reLaunch({ url: '/pages/public/loginphone?type=1' })
+		      }
+		    })
+		  }
+		})
+	},
 	sendImMessage(userId,content,messageType,chatConversationId){
 		let data = {
 			userId,

+ 20 - 2
pages/msg/im.vue

@@ -2556,8 +2556,26 @@
 					if (res.data == 'HeartBeat') { //如果是心跳回推则不进行处理
 						return
 					}
-					this.getTimeOrListItem1();
-					console.log('onMessage', res)
+					let msg
+					try {
+					  msg = JSON.parse(res.data)
+					} catch (e) {
+					  that.getTimeOrListItem1();
+					  return;
+					}
+					if (msg.type === 'kickOut') {
+					  uni.showModal({
+					    title: '提示',
+					    content: msg.content,
+					    showCancel: false,
+					    success() {
+					      that.$queue.logout();
+					      uni.reLaunch({ url: '/pages/public/loginphone?type=1' })
+					    }
+					  })
+					}else{
+						that.getTimeOrListItem1();
+					}
 				})
 				uni.onSocketClose((res) => {
 					uni.hideLoading()