You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.5 KiB
150 lines
4.5 KiB
var userAgent = navigator.userAgent;
|
|
var isApp = userAgent.indexOf('isapp') !== -1;
|
|
var hubUrl = "/IoTCenter/hub?group=page";
|
|
var connection = new signalR.HubConnectionBuilder().withUrl(hubUrl).build();
|
|
connection.on('Connected', function (id) {
|
|
connectionId = id;
|
|
console.log(connectionId);
|
|
});
|
|
connection.on("ServerToClient", function (method, json, to, from) {
|
|
console.log(method + ':' + json);
|
|
});
|
|
function connect() {
|
|
if (connection.state === signalR.HubConnectionState.Disconnected) {
|
|
connection.start().then(function () {
|
|
console.log('客户端与服务器连接成功!');
|
|
}).catch(function (err) {
|
|
console.log(err);
|
|
setTimeout(connect, 15 * 1000);
|
|
});
|
|
}
|
|
}
|
|
///
|
|
//var request = function (url, data, success, method) {
|
|
// this.url = url;
|
|
// this.data = data;
|
|
// this.method = method || 'get';
|
|
// this.success = success;
|
|
// if (this.method == 'get') {
|
|
// axios.get(url, data)
|
|
// }
|
|
// else {
|
|
|
|
// }
|
|
// request.then(function (response) {
|
|
// this.success(response);
|
|
// }).catch(function (error) {
|
|
// this.catch(error);
|
|
// }).finally(function () {
|
|
// this.finally();
|
|
// });
|
|
//}
|
|
var token = localStorage.getItem('accessToken');
|
|
var refreshToken = localStorage.getItem('refreshToken');
|
|
|
|
axios.interceptors.request.use(
|
|
function (config) {
|
|
if (config.url.indexOf('.html') === -1 && config.url.indexOf('getToken') === -1) {
|
|
if (token) {
|
|
config.withCredentials = true;
|
|
config.headers['x-requested-with'] = 'XMLHttpRequest';
|
|
config.headers['Authorization'] = 'Bearer ' + token;
|
|
console.log(config.headers['Authorization']);
|
|
}
|
|
}
|
|
return config;
|
|
},
|
|
function (error) {
|
|
console.log('axios.interceptors.request.error:');
|
|
console.log(error);
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
axios.interceptors.response.use(function (response) {
|
|
console.log('axios.interceptors.response:');
|
|
console.log(response);
|
|
return response;
|
|
}, function (error) {
|
|
console.log('axios.interceptors.response.error:');
|
|
console.log(error.response);
|
|
if (error.response.status === 401) {
|
|
var url = '/UserCenter/api/v1/token/refreshToken';
|
|
var data = refreshToken:refreshToken};
|
|
axios.post(url, data)
|
|
.then(function (response) {
|
|
token = response.data.accessToken;
|
|
localStorage.setItem("accessToken", token);
|
|
refreshToken = response.data.refreshToken;
|
|
localStorage.setItem("refreshToken", refreshToken);
|
|
config.headers['Authorization'] = 'Bearer ' + token;
|
|
console.log('after refresh token,send request again');
|
|
axios.request(error.config);
|
|
})
|
|
.catch(function (error) {
|
|
if (error.response.status === 401) {
|
|
console.log('refreshToken invalid');
|
|
router.push('/login')
|
|
}
|
|
});
|
|
}
|
|
return Promise.reject(error);
|
|
});
|
|
///
|
|
const routes = [
|
|
];
|
|
var routeList = routes.concat();
|
|
const router = new VueRouter({
|
|
routes
|
|
});
|
|
router.beforeEach((to, from, next) => {
|
|
$('#loadingToast').show();
|
|
if (!token && to.path !== '/login') {
|
|
router.push('/login');
|
|
return;
|
|
}
|
|
if (token) {
|
|
connect();
|
|
}
|
|
var route;
|
|
for (var i = 0; i < routeList.length; i++) {
|
|
if (routeList[i].path == to.path) {
|
|
route = routeList[i];
|
|
}
|
|
}
|
|
if (!route) {
|
|
var path = to.path === '/' ? '/pchome' : to.path;
|
|
var url = path + '.js';
|
|
var method = path.replace(/\//g, '_').substr(1) + "()";
|
|
console.log('preRoute:'+path+ '|'+url+'|' + method);
|
|
$.getScript(url, function () {
|
|
var route = {
|
|
path: to.path,
|
|
component: eval(method),
|
|
};
|
|
router.addRoutes([route]);
|
|
routeList.push(route);
|
|
router.push(to.fullPath);
|
|
});
|
|
}
|
|
else {
|
|
console.log('route from:'+from.path +' to:'+to.path);
|
|
next();
|
|
}
|
|
});
|
|
router.afterEach((route, redirect) => {
|
|
Vue.nextTick(() => {
|
|
$('#loadingToast').fadeOut(100);
|
|
})
|
|
})
|
|
///
|
|
const app = new Vue({
|
|
router,
|
|
data: {
|
|
},
|
|
mounted: function () {
|
|
console.log('mounted:app');
|
|
},
|
|
methods: {
|
|
}
|
|
}).$mount('#app');
|