zs 2 vuotta sitten
vanhempi
commit
7cafd0b292
3 muutettua tiedostoa jossa 35 lisäystä ja 1 poistoa
  1. 18 0
      pages/index/index.vue
  2. 7 0
      pages/message/index.vue
  3. 10 1
      pagesMessage/message/info.vue

+ 18 - 0
pages/index/index.vue

@@ -18,8 +18,26 @@
 		onLoad: async function() {
 			const that = this;
 			await that.searchConfig();
+			await that.watchlogin();
 		},
 		methods: {
+			watchlogin() {
+				const that = this;
+				if (that.$store.state.socketTask == null) {
+					uni.getStorage({
+						key: 'token',
+						success: function(res) {
+							let user = that.$jwt(res.data);
+							if (user._id) {
+								//当websocket收到后端发送的消息时,触发
+								let config = that.$config.wsUrl;
+								// 开启websocket
+								that.$store.dispatch('websocketInit', config + `/${user._id}`);
+							}
+						}
+					})
+				}
+			},
 			// 查询基本设置
 			async searchConfig() {
 				const that = this;

+ 7 - 0
pages/message/index.vue

@@ -83,6 +83,13 @@
 						if (user) {
 							that.$set(that, `user`, user)
 							that.search();
+							if (that.$store.state.socketTask == null) {
+								//当websocket收到后端发送的消息时,触发
+								let config = that.$config.wsUrl;
+								// 开启websocket
+								that.$store.dispatch('websocketInit', config +
+									`/${user._id}`);
+							}
 						}
 					}
 				})

+ 10 - 1
pagesMessage/message/info.vue

@@ -111,7 +111,16 @@
 					key: 'token',
 					success: function(res) {
 						let user = that.$jwt(res.data);
-						if (user) that.$set(that, `user`, user)
+						if (user) {
+							that.$set(that, `user`, user)
+							if (that.$store.state.socketTask == null) {
+								//当websocket收到后端发送的消息时,触发
+								let config = that.$config.wsUrl;
+								// 开启websocket
+								that.$store.dispatch('websocketInit', config +
+									`/${user._id}`);
+							}
+						}
 					}
 				})
 			},