From e1bcfc00c54f891433ab5a1bfef7578b8a82019e Mon Sep 17 00:00:00 2001 From: A1300399510 Date: Wed, 20 Sep 2023 15:51:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0socket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 0 src/App.vue | 59 ++++++++++++++++++++++++++++++++++++++++---- src/store/index.js | 5 ---- src/utils/request.js | 2 +- 4 files changed, 55 insertions(+), 11 deletions(-) mode change 100755 => 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json old mode 100755 new mode 100644 diff --git a/src/App.vue b/src/App.vue index aabb00d..2353bc3 100755 --- a/src/App.vue +++ b/src/App.vue @@ -15,24 +15,75 @@ export default { pathname: "", parameterObject: {}, isPc: false, + socketTask: null, } }, watch: { }, mounted() { - this.$store.dispatch('fetchHistoricalSearch') // 获取历史搜索记录 this.$store.dispatch('getAllForum', this) // 获取全部板块 this.isJudgePc() - // this.$skipUrl("gggg", false) + // setTimeout(() => { + // // this.hasMiucmsSessionCookie() + // // this.useSocket + // console.log(this.$store.state['user']); + // }, 1000) + + let timer = setInterval(() => { + if (Object.keys(this.$store.state.user).length !== 0) { + this.useSocket() + clearInterval(timer) + } + }, 1000); + }, methods: { - isJudgePc() { + // 开启socket + useSocket() { + let token = this.getMiucmsSessionCookie() || '' + this.socketTask = new WebSocket(`wss://app.gter.net/socket?token=${token}`); + this.socketTask.onopen = () => { + let user = this.$store.state.user || {}; + if (user && token) { + this.socketTask.send(JSON.stringify({ + type: 'bind', + data: { + token, + uid: user.uid || 0 + } + })) + } + setTimeout(() => this.timedTransmission(), 50000); + }; + this.socketTask.onclose = () => { + console.log('socket关闭了', new Date()); + }; + }, + // 定时发送 + timedTransmission() { + if (this.socketTask.readyState != 1) return + this.socketTask.send(JSON.stringify({ type: 'ping' })) + + setTimeout(() => this.timedTransmission(), 50000) + }, + + getMiucmsSessionCookie() { + let cookies = document.cookie.split(";"); + for (let i = 0; i < cookies.length; i++) { + let cookie = cookies[i].split("="); + if (cookie[0].trim() === "miucms_session") { + return cookie[1]; + } + } + return null; + }, + isJudgePc() { const userAgentInfo = navigator.userAgent; const Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; let flag = true; @@ -43,8 +94,6 @@ export default { } } - - if (flag) { let pathname = window.location.pathname diff --git a/src/store/index.js b/src/store/index.js index f75745b..8d56fda 100755 --- a/src/store/index.js +++ b/src/store/index.js @@ -72,17 +72,12 @@ export default new Vuex.Store({ // 获取全部板块的数据 getAllForum({ commit }, that) { - // getAllForum(commit, that) { - // that.$startupUnderLoading(that) that.$http.get("/api/home/allForum").then(res => { if (res.code != 200) return; let allForumList = res.data commit('setAllForumList', allForumList) - }).catch(err => { that.$message.error(err.message) - }).finally(() => { - // that.$closeUnderLoading(that) }) }, diff --git a/src/utils/request.js b/src/utils/request.js index b7ceaca..22727aa 100755 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -31,7 +31,7 @@ service.interceptors.request.use(config => { // config['headers']['authorization'] = "qj2q1qk1on0curclipghjtv5ja1g9eq2" // config['headers']['authorization'] = "661aiz52k5e6vqgmkxnz0wvbv8nciz8h" - if (process.env.NODE_ENV == "development") config['headers']['authorization'] = "uqw2cz1zdqf2uvcd22kxxy5gtsrrctiz" + if (process.env.NODE_ENV == "development") config['headers']['authorization'] = "0h870ovk2xckoqfsh8a3t3sg4sg5z7eg" return config }, error => {