Browse Source

feat(toast): 调整到中间

曾志翔 2 years ago
parent
commit
7d4e0d5393
3 changed files with 32 additions and 20 deletions
  1. 7 2
      nuxt.config.js
  2. 24 17
      plugins/axios.js
  3. 1 1
      plugins/toast/toast.vue

+ 7 - 2
nuxt.config.js

@@ -69,7 +69,6 @@ export default {
 
   // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
   plugins: [
-    '~/plugins/axios',
     // '~/plugins/api',
     '~/plugins/file-upload',
     // '~/plugins/message',
@@ -160,7 +159,13 @@ export default {
     },
   },
   auth: {
-    plugins: ['~/plugins/auth', '~/plugins/baidu-tongji'],
+    plugins: ['~/plugins/axios', '~/plugins/auth', '~/plugins/baidu-tongji'],
+    redirect: {
+      login: '/login',
+      logout: '/login',
+      callback: '/login',
+      home: '/login',
+    },
     strategies: {
       password: {
         scheme: '~/schemes/password',

+ 24 - 17
plugins/axios.js

@@ -1,32 +1,39 @@
 import AxiosError from 'axios/lib/core/AxiosError';
 
-export default function ({ $axios, redirect }) {
+export default function ({ $axios, $auth, redirect }) {
   // $axios.defaults.dataKey = 'data';
 
   $axios.onRequest((config) => {
     config.headers.client = 7;
     return config;
   });
-  $axios.onResponse((response) => {
-    if (response.data.status !== 0) {
-      // const err = new Error(
-      //   response.data.msg || response.data.data || '未知错误',
-      // );
-      // Object.assign(err, response);
+  $axios.onResponse(async (response) => {
+    if (Object.prototype.toString.call(response.data) === '[object Object]') {
+      if ([2, 4, 6013, 6014].includes(response.data.status)) {
+        await $auth.logout();
+        // await $auth.setUserToken(null);
+      }
+      if (response.data.status !== 0) {
+        // const err = new Error(
+        //   response.data.msg || response.data.data || '未知错误',
+        // );
+        // Object.assign(err, response);
 
-      // return Promise.reject(err);
+        // return Promise.reject(err);
 
-      // if (response.data.status === 502) {
-      // }
+        // if (response.data.status === 502) {
+        // }
 
-      throw new AxiosError(
-        response.data.msg || response.data.data || '未知错误',
-        null,
-        response.config,
-        response.request,
-        response,
-      );
+        throw new AxiosError(
+          response.data.msg || response.data.data || '未知错误',
+          null,
+          response.config,
+          response.request,
+          response,
+        );
+      }
     }
+
     // else if (response.config.dataKey) {
     //   response.data = response.data[response.config.dataKey];
     //   return response;

+ 1 - 1
plugins/toast/toast.vue

@@ -1,7 +1,7 @@
 <template>
   <v-snackbar
     app
-    bottom
+    centered
     class="toast"
     :value.sync="value"
     color="rgba(0,0,0,.8)"