Blame view

src/utils/request.js 3.18 KB
9b7e125f   monkeyhouyi   属地页面
1
2
3
4
5
  import axios from "axios";
  import { getToken, removeToken } from "@/utils/auth";
  import { Notification, MessageBox, Message, Loading } from 'element-ui'
  import errorCode from '@/utils/errorCode'
  import { tansParams, blobValidate } from "@/utils/index";
5330d757   monkeyhouyi   公司信息管理完成
6
  import define from '@/utils/define'
9b7e125f   monkeyhouyi   属地页面
7
8
9
10
11
12
13
  // 是否显示重新登录
  export let isRelogin = { show: false };
  
  axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
  
  const service = axios.create({
      // axios中请求配置有baseURL选项,表示请求URL公共部分
89ab0203   monkeyhouyi   修改个人信息弹框
14
      baseURL: '/api',
5330d757   monkeyhouyi   公司信息管理完成
15
      withCredentials: false,
9b7e125f   monkeyhouyi   属地页面
16
      // 超时
5330d757   monkeyhouyi   公司信息管理完成
17
      // timeout: define.timeout
9b7e125f   monkeyhouyi   属地页面
18
19
20
21
22
  })
  
  //拦截器(请求,响应)
  //请求
  service.interceptors.request.use(config => {
b61eb1ed   monkeyhouyi   上报线索研判
23
      if (config.url.indexOf('http') > -1) config.baseURL = ''
9b7e125f   monkeyhouyi   属地页面
24
25
26
      // 是否需要设置 token
      const isToken = (config.headers || {}).isToken === false
      // 是否需要防止数据重复提交
ecc43230   monkeyhouyi   优化首页,应用管理
27
      const isRepeatSubmit = (config.headers || {}).repeatSubmit === false
9b7e125f   monkeyhouyi   属地页面
28
29
30
31
      // 判断token
      if (getToken() && !isToken) {
          config.headers['Authorization'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
      }
ecc43230   monkeyhouyi   优化首页,应用管理
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      if (config.method === 'get' && config.params) {
          let url = config.url + '?' + tansParams(config.params);
          url = url.slice(0, -1);
          config.params = {};
          config.url = url;
      }
      if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) {
          const requestObj = {
              url: config.url,
              data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data,
              time: new Date().getTime()
          }
          const requestSize = Object.keys(JSON.stringify(requestObj)).length; // 请求数据大小
          const limitSize = 5 * 1024 * 1024; // 限制存放数据5M
          if (requestSize >= limitSize) {
              console.warn(`[${config.url}]: ` + '请求数据大小超出允许的5M限制,无法进行防重复提交验证。')
              return config;
          }
9b7e125f   monkeyhouyi   属地页面
50
      }
b61eb1ed   monkeyhouyi   上报线索研判
51
      return config
9b7e125f   monkeyhouyi   属地页面
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  })
  //响应
  service.interceptors.response.use(res => {
      const code = res.data.code || 200;
      const msg = errorCode[code] || res.data.msg || errorCode['default']
  
      if (code === 401 || code == 600) {
          if (!isRelogin.show) {
              isRelogin.show = true;
              MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => {
                  isRelogin.show = false;
                  removeToken();
                  location.href = '#/login';
              }).catch(() => {
                  isRelogin.show = false;
              });
          }
      } else if (code === 500) {
          Message({ message: msg, type: 'error' })
          return Promise.reject(new Error(msg))
      } else if (code === 601) {
          Message({ message: msg, type: 'warning' })
          return Promise.reject('error')
      } else if (code !== 200) {
          Notification.error({ title: msg })
          return Promise.reject('error')
      } else {
          return res.data
      }
  }, err => {
      //失败
      return Promise.reject(err)
  })
  
  
  export default service;