无人机管理后台前端(已迁走)
张含笑
2025-07-21 3be2e099bbc05e902dfcfabc403ade0a2aba8ef4
src/axios.js
@@ -5,6 +5,7 @@
 * isSerialize是否开启form表单提交
 * isToken是否需要token
 */
import axios from 'axios';
import store from '@/store/';
import router from '@/router/';
@@ -18,11 +19,13 @@
import { Base64 } from 'js-base64';
import { baseUrl } from '@/config/env';
import crypto from '@/utils/crypto';
const adminUrl = import.meta.env.VITE_APP_DASHBOARD_URL;
// 全局未授权错误提示状态,只提示一次
let isErrorShown = false;
axios.defaults.timeout = 10000;
let _retry = false;
// 超时时间设置为10分钟,部分接口上传比较慢,如固件上传
axios.defaults.timeout = 600000;
//返回其他状态码
axios.defaults.validateStatus = function (status) {
  return status >= 200 && status <= 500; // 默认的
@@ -45,6 +48,7 @@
      config.url = baseUrl + config.url;
    }
    //安全请求header
    config.headers['areaCode'] = store?.state?.user?.userInfo?.detail?.areaCode;
    config.headers['Blade-Requested-With'] = 'BladeHttpRequest';
    //headers判断是否需要
    const authorization = config.authorization === false;
@@ -67,16 +71,16 @@
        : 'bearer ' + token;
    }
    // 开启报文加密
    if (cryptoData) {
      if (config.params) {
        const data = crypto.encryptAES(JSON.stringify(config.params), crypto.aesKey);
        config.params = { data };
      }
      if (config.data) {
        config.text = true;
        config.data = crypto.encryptAES(JSON.stringify(config.data), crypto.aesKey);
      }
    }
    // if (cryptoData) {
    //   if (config.params) {
    //     const data = crypto.encryptAES(JSON.stringify(config.params), crypto.aesKey);
    //     config.params = { data };
    //   }
    //   if (config.data) {
    //     config.text = true;
    //     config.data = crypto.encryptAES(JSON.stringify(config.data), crypto.aesKey);
    //   }
    // }
    //headers中配置text请求
    if (config.text === true) {
      config.headers['Content-Type'] = 'text/plain';
@@ -97,15 +101,32 @@
    NProgress.done();
    const status = res.data.error_code || res.data.code || res.status;
    const statusWhiteList = website.statusWhiteList || [];
    const message = res.data.msg || res.data.error_description || '系统错误';
    const message = res.data.msg || res.data.error_description || res.data.message || '系统错误';
    const config = res.config;
    const cryptoData = config.cryptoData === true;
    if (status === 511) {
      ElMessage({
        message: '请联系运维人员上传密钥证书!',
        type: 'error',
      });
      // 获取当前路由名称
      const currentRouteName = router.currentRoute.value.name;
      // 如果当前路由不是登录页,则跳转到登录页
      if (currentRouteName !== '登录页') {
        store.dispatch('FedLogOut').then(() =>
          router.push({
            path: '/login',
          })
        );
      }
      return Promise.reject(new Error(message));
    }
    //如果在白名单里则自行catch逻辑处理
    if (statusWhiteList.includes(status)) return Promise.reject(res);
    // 如果是401并且没有重试过,尝试刷新token
    if (status === 401 && !config._retry) {
    if (status === 401 && !_retry) {
      // 标记此请求已尝试刷新token
      config._retry = true;
      _retry = true;
      // 调用RefreshToken action来刷新token
      return store
        .dispatch('RefreshToken')
@@ -128,20 +149,30 @@
          if (!isErrorShown) {
            isErrorShown = true;
            ElMessage({
              message: '用户令牌过期,请重新登录',
              message: message,
              type: 'error',
            });
          }
          // 清除token信息
          removeToken();
          removeRefreshToken();
          const env = import.meta.env.VITE_APP_ENV;
          // 重定向到登录页
          store.dispatch('FedLogOut').then(() => router.push({ path: '/login' }));
          // store.dispatch('FedLogOut').then(() => router.push({
          //    path: '/login'
          // }));
          env === 'development'
            ? store.dispatch('FedLogOut').then(() =>
                router.push({
                  path: '/login',
                })
              )
            : store.dispatch('FedLogOut').then(() => window.location.replace(`${adminUrl}#/login`));
          return Promise.reject(new Error(message));
        });
    }
    // 如果是401并且已经重试过,直接跳转到登录页面
    if (status === 401 && config._retry) {
    if (status === 401 && _retry) {
      // 首次报错时提示
      if (!isErrorShown) {
        isErrorShown = true;
@@ -154,7 +185,17 @@
      removeToken();
      removeRefreshToken();
      // 重定向到登录页
      store.dispatch('FedLogOut').then(() => router.push({ path: '/login' }));
      //   store.dispatch('FedLogOut').then(() => router.push({
      //      path: '/login'
      //   }));
      const env = import.meta.env.VITE_APP_ENV;
      env === 'development'
        ? store.dispatch('FedLogOut').then(() =>
            router.push({
              path: '/login',
            })
          )
        : store.dispatch('FedLogOut').then(() => window.location.replace(`${adminUrl}#/login`));
      return Promise.reject(new Error(message));
    }
    // 如果请求为oauth2错误码则首次报错时提示
@@ -170,11 +211,13 @@
      return Promise.reject(new Error(message));
    }
    // 如果请求为非200默认统一处理
    if (status !== 200) {
      ElMessage({
        message: message,
        type: 'error',
      });
    if (status !== 200 && status !== 0) {
      if (!isErrorShown) {
        ElMessage({
          message: message,
          type: 'error',
        });
      }
      return Promise.reject(new Error(message));
    }
    // 解析加密报文