Browse Source

请求工具修改

yangxiao 3 years ago
parent
commit
c511f5e026
3 changed files with 25 additions and 20 deletions
  1. 7 2
      src/api/axios.js
  2. 3 3
      src/main.js
  3. 15 15
      src/router/index.js

+ 7 - 2
src/api/axios.js

@@ -61,7 +61,7 @@ export function requestData (options) {
     }
 
     // 发起请求
-    httpService({
+    XHRReq({
       url: options.subUrl,
       method: options.method || 'GET',
       params,
@@ -72,7 +72,12 @@ export function requestData (options) {
       if (response.code === 200) {
         options.success && options.success(resolve(response));
       } else {
-        Message.error(response.message || ("请求出错[" + response.code + "]"));
+        localStorage.removeItem('authToken');
+        Message.error((response.data && response.data.msg) || ("请求出错[" + response.data.code + "]"));
+        setTimeout(() => {
+          window.location.reload();
+          // window.__STATICVUE__.$router.replace('/login');
+        }, 1000);
       }
     }).catch(error => {
       if (options.showLoading) {

+ 3 - 3
src/main.js

@@ -18,7 +18,7 @@ Vue.prototype.BASE = base;
 
 import ECzm from "@tools/echartsToolzm"; // echarts处理 - zm
 Vue.prototype.ECZM = ECzm;
- 
+
 import echartsTool from "@tools/echartsTool"; // Echarts 工具类 - ylf
 Vue.prototype.ET = echartsTool;
 
@@ -35,7 +35,7 @@ Vue.prototype.Get_PDF = Get_PDF;
 import moment from 'moment';
 Vue.prototype.Moment = moment;
 // 滚动条
-import vuescroll from "vuescroll"; 
+import vuescroll from "vuescroll";
 Vue.use(vuescroll, {
   ops: {
     scrollPanel: {
@@ -68,7 +68,7 @@ import '@assets/css/font.scss'
 
 Vue.config.productionTip = false
 
-new Vue({
+window.__STATICVUE__ = new Vue({
   router,
   store,
   render: h => h(App)

+ 15 - 15
src/router/index.js

@@ -4,11 +4,11 @@ import store from '@/store/index';
 
 // 路由3+版本的异常处理
 const originalPush = VueRouter.prototype.push
-VueRouter.prototype.push = function push(location) {
+VueRouter.prototype.push = function push (location) {
   return originalPush.call(this, location).catch(err => err)
 }
 const originalReplace = VueRouter.prototype.replace;
-VueRouter.prototype.replace = function replace(location) {
+VueRouter.prototype.replace = function replace (location) {
   return originalReplace.call(this, location).catch(err => err);
 }
 
@@ -31,9 +31,9 @@ const routes = [
       requireAuth: true, // 添加该字段,表示进入这个路由是需要登录的
     },
     component: main,
-    children:[
+    children: [
       { // 首页
-        path:'/',
+        path: '/',
         component: r => require.ensure([], () => r(require('@views/index/index')), 'indexM')
       },
       ...yRouter,
@@ -50,9 +50,9 @@ const routes = [
     component: error404,
   },
   {
-    path: '/login', 
+    path: '/login',
     meta: {
-      requireAuth: true, 
+      requireAuth: true,
     },
     name: 'login',
     component: login,
@@ -72,18 +72,18 @@ const router = new VueRouter({
 /**
  * 路由的钩子函数,处理是否登录的判断
  * **/
- router.beforeEach((to, from, next) => {
-    // 登录后登录处理办法
-  if(to.path === '/login' && store.getters.authToken){
-    next({path: '/'})
-  }else if (to.matched.some(m => m.meta.requireAuth)) {    // 需要登录
-    if(window.localStorage.authToken){
+router.beforeEach((to, from, next) => {
+  // 登录后登录处理办法
+  if (to.path === '/login' && store.getters.authToken) {
+    next({ path: '/' })
+  } else if (to.matched.some(m => m.meta.requireAuth)) {    // 需要登录
+    if (window.localStorage.authToken) {
       next()
     } else if (to.path !== '/login') {
       let token = localStorage.getItem('authToken');
-      if (token === null || token === '' || token === undefined){
-          next({path: '/login'})
-      }else{next()}
+      if (token === null || token === '' || token === undefined) {
+        next({ path: '/login' })
+      } else { next() }
     } else {
       next()
     }