加入websocket
This commit is contained in:
parent
16077129e8
commit
c750242da5
@ -13,7 +13,8 @@ const APP_CONFIG = {
|
|||||||
APP_LOGO: '',
|
APP_LOGO: '',
|
||||||
//接口地址
|
//接口地址
|
||||||
API_URL: 'https://ps.xiaoapi.com/admin',
|
API_URL: 'https://ps.xiaoapi.com/admin',
|
||||||
|
|
||||||
// 微信扫码登录
|
// 微信扫码登录
|
||||||
MY_SHOW_LOGIN_OAUTH:true,
|
MY_SHOW_LOGIN_OAUTH: true,
|
||||||
|
//
|
||||||
|
WEBSOCKET: 'wss://app.gter.net/socket',
|
||||||
}
|
}
|
@ -36,4 +36,4 @@ app.use(ElementPlus);
|
|||||||
app.use(i18n);
|
app.use(i18n);
|
||||||
app.use(x);
|
app.use(x);
|
||||||
//挂载 app
|
//挂载 app
|
||||||
app.mount('#app');
|
app.mount('#app');
|
@ -89,6 +89,16 @@ router.beforeEach(async (to, from, next) => {
|
|||||||
router.push(to.fullPath);
|
router.push(to.fullPath);
|
||||||
}
|
}
|
||||||
isGetRouter = true;
|
isGetRouter = true;
|
||||||
|
// 发送绑定
|
||||||
|
if (store.state.socket && store.state.socket.readyState === WebSocket.OPEN) {
|
||||||
|
store.state.socket.send(JSON.stringify({
|
||||||
|
type: 'bind',
|
||||||
|
data: {
|
||||||
|
token: token,
|
||||||
|
uid: response.data.user.uid
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
@ -155,7 +165,8 @@ function loadComponent(component) {
|
|||||||
function flatAsyncRoutes(routes, breadcrumb = []) {
|
function flatAsyncRoutes(routes, breadcrumb = []) {
|
||||||
let res = []
|
let res = []
|
||||||
routes.forEach(route => {
|
routes.forEach(route => {
|
||||||
const tmp = { ...route }
|
const tmp = { ...route
|
||||||
|
}
|
||||||
if (tmp.children) {
|
if (tmp.children) {
|
||||||
let childrenBreadcrumb = [...breadcrumb]
|
let childrenBreadcrumb = [...breadcrumb]
|
||||||
childrenBreadcrumb.push(route)
|
childrenBreadcrumb.push(route)
|
||||||
|
47
src/store/modules/socket.js
Normal file
47
src/store/modules/socket.js
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import config from '@/config';
|
||||||
|
import { ref } from 'vue';
|
||||||
|
let socket;
|
||||||
|
let reconnectAttempts = 1;
|
||||||
|
const maxReconnectAttempts = 5;
|
||||||
|
export function createWebSocket() {
|
||||||
|
let socketurl = config.WEBSOCKET || null;
|
||||||
|
if (socketurl) {
|
||||||
|
try {
|
||||||
|
socket = new WebSocket(socketurl);
|
||||||
|
socket.onopen = function() {
|
||||||
|
console.log('WebSocket连接成功');
|
||||||
|
reconnectAttempts = 0; // reset reconnect attempts
|
||||||
|
}
|
||||||
|
socket.onerror = function() {
|
||||||
|
console.log('WebSocket连接发生错误');
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
socket.onclose = function() {
|
||||||
|
console.log('WebSocket连接关闭');
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
// 更新 socketState 的值
|
||||||
|
socketState.value = socket;
|
||||||
|
} catch (e) {
|
||||||
|
console.log('WebSocket连接失败', e);
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function reconnect() {
|
||||||
|
if (maxReconnectAttempts > reconnectAttempts) {
|
||||||
|
setTimeout(function() {
|
||||||
|
createWebSocket();
|
||||||
|
console.log('WebSocket进行重连(' + reconnectAttempts);
|
||||||
|
reconnectAttempts++;
|
||||||
|
}, 2000);
|
||||||
|
} else {
|
||||||
|
console.log('WebSocket重连失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const socketState = ref(socket);
|
||||||
|
createWebSocket();
|
||||||
|
export default {
|
||||||
|
state: socketState
|
||||||
|
}
|
@ -31,6 +31,8 @@
|
|||||||
},
|
},
|
||||||
mounted(){
|
mounted(){
|
||||||
|
|
||||||
|
console.log(this.$socket)
|
||||||
|
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
onMounted(){
|
onMounted(){
|
||||||
|
@ -115,6 +115,7 @@
|
|||||||
this.$tool.data.remove("permissions")
|
this.$tool.data.remove("permissions")
|
||||||
this.$tool.data.remove("dashboardgrid")
|
this.$tool.data.remove("dashboardgrid")
|
||||||
this.$tool.data.remove("grid")
|
this.$tool.data.remove("grid")
|
||||||
|
this.$tool.store.remove("token")
|
||||||
this.$store.commit("clearViewTags")
|
this.$store.commit("clearViewTags")
|
||||||
this.$store.commit("clearKeepLive")
|
this.$store.commit("clearKeepLive")
|
||||||
this.$store.commit("clearIframeList")
|
this.$store.commit("clearIframeList")
|
||||||
|
@ -166,18 +166,21 @@ export default {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
loaddata(){
|
loaddata(){
|
||||||
this.loading = true;
|
|
||||||
this.$http.get('admin/upgrade/get').then((res) => {
|
console.log(this.$socket.send(1));
|
||||||
this.loading = false;
|
|
||||||
if (res.code == 200) {
|
this.loading = true;
|
||||||
this.userData = res.data.user;
|
this.$http.get('admin/upgrade/get').then((res) => {
|
||||||
this.cloudregister = res.data.cloudregister;
|
this.loading = false;
|
||||||
this.name = res.data.name;
|
if (res.code == 200) {
|
||||||
this.authorizationtime = res.data.authorizationtime;
|
this.userData = res.data.user;
|
||||||
return ;
|
this.cloudregister = res.data.cloudregister;
|
||||||
}
|
this.name = res.data.name;
|
||||||
this.$alert(res.message, "提示", {type: 'error'});
|
this.authorizationtime = res.data.authorizationtime;
|
||||||
});
|
return ;
|
||||||
|
}
|
||||||
|
this.$alert(res.message, "提示", {type: 'error'});
|
||||||
|
});
|
||||||
},
|
},
|
||||||
loginHandle(formName) {
|
loginHandle(formName) {
|
||||||
this.$refs[formName].validate((valid) => {
|
this.$refs[formName].validate((valid) => {
|
||||||
|
2
src/x.js
2
src/x.js
@ -28,6 +28,7 @@ import xQrCode from './components/xQrCode'
|
|||||||
import xPageHeader from './components/xPageHeader'
|
import xPageHeader from './components/xPageHeader'
|
||||||
import xSelect from './components/xSelect'
|
import xSelect from './components/xSelect'
|
||||||
import xWaterMark from './components/xWaterMark'
|
import xWaterMark from './components/xWaterMark'
|
||||||
|
import store from '@/store'
|
||||||
import {
|
import {
|
||||||
Flexbox,
|
Flexbox,
|
||||||
FlexboxItem
|
FlexboxItem
|
||||||
@ -50,6 +51,7 @@ export default {
|
|||||||
app.config.globalProperties.$api = api;
|
app.config.globalProperties.$api = api;
|
||||||
app.config.globalProperties.$auth = permission;
|
app.config.globalProperties.$auth = permission;
|
||||||
app.config.globalProperties.$role = rolePermission;
|
app.config.globalProperties.$role = rolePermission;
|
||||||
|
app.config.globalProperties.$socket = store.state.socket;
|
||||||
//注册全局组件
|
//注册全局组件
|
||||||
app.component('flexbox', Flexbox)
|
app.component('flexbox', Flexbox)
|
||||||
app.component('flexbox-item', FlexboxItem)
|
app.component('flexbox-item', FlexboxItem)
|
||||||
|
Loading…
Reference in New Issue
Block a user