|
@@ -3,12 +3,13 @@
|
|
import website from "@/config/index";
|
|
import website from "@/config/index";
|
|
import userApi from '~api/user/index';
|
|
import userApi from '~api/user/index';
|
|
import {getStorage, setStorage} from "@/utils/storage";
|
|
import {getStorage, setStorage} from "@/utils/storage";
|
|
- import {calcDate, getObjValue, isNullES} from "js-fast-way";
|
|
|
|
|
|
+ import {calcDate, getObjValue, isNullES, isString} from "js-fast-way";
|
|
|
|
+ import {getWssApiUrl} from "@/config/envApi";
|
|
|
|
+
|
|
export default {
|
|
export default {
|
|
data() {
|
|
data() {
|
|
return {
|
|
return {
|
|
- userInfo: {},
|
|
|
|
- refreshLock: false,
|
|
|
|
|
|
+ userInfo: {}, refreshLock: false, isSocket: false
|
|
}
|
|
}
|
|
},
|
|
},
|
|
onLaunch: function() {
|
|
onLaunch: function() {
|
|
@@ -24,6 +25,7 @@
|
|
const store = useAppStore()
|
|
const store = useAppStore()
|
|
this.userInfo = store.userInfo
|
|
this.userInfo = store.userInfo
|
|
this.getRefreshToken()
|
|
this.getRefreshToken()
|
|
|
|
+ this.setInitSocket()
|
|
},
|
|
},
|
|
methods:{
|
|
methods:{
|
|
//刷新token
|
|
//刷新token
|
|
@@ -32,7 +34,7 @@
|
|
setInterval(() => {
|
|
setInterval(() => {
|
|
const token = getStorage('token', true)
|
|
const token = getStorage('token', true)
|
|
const date = calcDate(token.datetime, new Date().getTime())
|
|
const date = calcDate(token.datetime, new Date().getTime())
|
|
- if (isNullES(date)) return
|
|
|
|
|
|
+ if (isNullES(date)) return false;
|
|
if (date.seconds >= website.tokenTime && !this.refreshLock) {
|
|
if (date.seconds >= website.tokenTime && !this.refreshLock) {
|
|
this.refreshLock = true
|
|
this.refreshLock = true
|
|
console.log('刷新token')
|
|
console.log('刷新token')
|
|
@@ -66,7 +68,57 @@
|
|
} else {
|
|
} else {
|
|
return Promise.reject(res)
|
|
return Promise.reject(res)
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ },
|
|
|
|
+ //长连接
|
|
|
|
+ setInitSocket() {
|
|
|
|
+ const _this = this
|
|
|
|
+ const timeId = setInterval(() => {
|
|
|
|
+ if (_this.isSocket) {
|
|
|
|
+ clearInterval(timeId)
|
|
|
|
+ } else {
|
|
|
|
+ _this.setAppSocket(timeId)
|
|
|
|
+ }
|
|
|
|
+ }, 3000)
|
|
|
|
+ },
|
|
|
|
+ setAppSocket() {
|
|
|
|
+ const {user_id} = this.userInfo
|
|
|
|
+ if (user_id) this.initWebSocket(user_id)
|
|
|
|
+ },
|
|
|
|
+ initWebSocket(user_id) {
|
|
|
|
+ const _this = this
|
|
|
|
+ uni.connectSocket({
|
|
|
|
+ url: getWssApiUrl() + user_id,
|
|
|
|
+ complete: ()=> {
|
|
|
|
+ console.log('websocket链接成功')
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ uni.onSocketOpen((res) => {
|
|
|
|
+ _this.isSocket = true
|
|
|
|
+ console.log('WebSocket连接已打开!');
|
|
|
|
+ _this.sendSocketMessage();
|
|
|
|
+ });
|
|
|
|
+ uni.onSocketError((res) => {
|
|
|
|
+ _this.isSocket = false
|
|
|
|
+ console.log('WebSocket连接打开失败,请检查!');
|
|
|
|
+ });
|
|
|
|
+ uni.onSocketClose((res) => {
|
|
|
|
+ _this.isSocket = false
|
|
|
|
+ console.log('WebSocket 已关闭!');
|
|
|
|
+ //关闭后在连接
|
|
|
|
+ //_this.setInitSocket()
|
|
|
|
+ });
|
|
|
|
+ //收到的消息
|
|
|
|
+ uni.onSocketMessage(({data}) => {
|
|
|
|
+ const countData = isString(data) ? JSON.parse(data) : {}
|
|
|
|
+ console.log(countData)
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ sendSocketMessage() {
|
|
|
|
+ const store = useAppStore()
|
|
|
|
+ uni.sendSocketMessage({
|
|
|
|
+ data: store.projectId + ',' + store.contractId
|
|
|
|
+ });
|
|
|
|
+ },
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
</script>
|