axios.js 1.61 KB
/*
 * @Date: 2023-10-27 11:12:24
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2023-11-30 18:12:27
 * @FilePath: /vue-flow-editor/doc/axios.js
 * @Description: 文件描述
 */
import axios from 'axios';
import { ElMessageBox } from 'element-plus'

axios.defaults.params = {
  m: 'mod',
  p: 'authority_my',
};

/**
 * @description 请求拦截器
 */
axios.interceptors.request.use(
  config => {
    // const url_params = parseQueryString(location.href);
    // GET请求默认打上时间戳,避免从缓存中拿数据。
    const timestamp = config.method === 'get' ? (new Date()).valueOf() : '';
    /**
     * POST PHP需要修改数据格式
     * 序列化POST请求时需要屏蔽上传相关接口,上传相关接口序列化后报错
     */
    // 绑定默认请求头
    config.params = { ...config.params, timestamp }
    return config;
  },
  error => {
    // 请求错误处理
    return Promise.reject(error);
  });

/**
 * @description 响应拦截器
 */
axios.interceptors.response.use(
  response => {
    // 默认显示错误提示
    response.data.show = true;
    if (response.data.msg === '登录失效') {
      ElMessageBox.alert('登录失效!将跳转到登录页面。', '温馨提示', {
        confirmButtonText: '确定',
        callback: action => {
          // session 失效需要重新登录
          if (action === 'confirm') {
            window.parent.location.href = location.origin + '/admin/' + window.parent.location.search;
          }
        }
      });
    }
    return response;
  },
  error => {
    return Promise.reject(error);
  });

export default axios;