Blame view

src/utils/request.js 2.42 KB
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
1
  import axios from 'axios'
ff70d5b6   wuxw   v1.9 业主重置密码不显示问题
2
  import {  Loading } from 'element-ui'
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
3
4
5
  import config from '@/conf/config'
  import { getHeader } from './header'
  
6db3ce8f   wuxw   优化 post 显示动画
6
7
8
  // 创建loading实例
  let loadingInstance = null
  
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
9
10
  // 创建axios实例
  const service = axios.create({
f9988111   wuxw   优化系统
11
    baseURL: '/', // 基础URL
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
12
13
14
    timeout: config.apiTimeout // 请求超时时间
  })
  
6db3ce8f   wuxw   优化 post 显示动画
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  // 显示加载动画
  const showLoading = () => {
    loadingInstance = Loading.service({
      lock: true,
      text: '加载中...',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    })
  }
  
  // 隐藏加载动画
  const hideLoading = () => {
    if (loadingInstance) {
      loadingInstance.close()
      loadingInstance = null
    }
  }
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
32
33
34
35
36
37
38
39
40
41
  
  // 请求拦截器
  service.interceptors.request.use(
    config => {
      let _header = getHeader();
      // 在发送请求之前做些什么
      config.headers['Content-Type'] = 'application/json'
      config.headers['Accept'] = 'application/json'
      config.headers['app-id'] = _header['app-id']
      config.headers['TRANSACTION-ID'] = _header['TRANSACTION-ID']
6db3ce8f   wuxw   优化 post 显示动画
42
43
44
      config.headers['REQ-TIME'] = _header['REQ-TIME']
      config.headers['SIGN'] = _header['SIGN']
      config.headers['user-id'] = _header['user-id']
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
45
46
47
      config.headers['X-Requested-With'] = 'XMLHttpRequest'
      // 这里可以添加token等认证信息
      config.headers['Authorization'] = _header['Authorization']
6db3ce8f   wuxw   优化 post 显示动画
48
      if (config.url.indexOf('/app/') == -1 && config.url.indexOf('/callComponent/') == -1) {
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
49
50
        config.baseURL = "/app"
      }
6db3ce8f   wuxw   优化 post 显示动画
51
52
53
54
55
      // GET请求不显示加载动画,其他请求显示
      if (config.method !== 'get' && config.showLoading !== false) {
        showLoading()
      }
  
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
56
57
58
59
      return config
    },
    error => {
      console.log(error)
6db3ce8f   wuxw   优化 post 显示动画
60
61
      // 请求错误时也要隐藏加载动画
      hideLoading()
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
62
63
64
65
66
67
68
      return Promise.reject(error)
    }
  )
  
  // 响应拦截器
  service.interceptors.response.use(
    response => {
6db3ce8f   wuxw   优化 post 显示动画
69
70
      // 隐藏加载动画
      hideLoading()
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
71
        return response
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
72
73
74
    },
    error => {
      console.log(error)
6db3ce8f   wuxw   优化 post 显示动画
75
76
77
78
  
      // 隐藏加载动画
      hideLoading()
  
5e59e7c3   wuxw   支持回话过期自动登录
79
80
81
82
83
84
      // 判断是否为401未授权错误
      if (error.response && error.response.status === 401) {
        // 清除本地存储的token
        localStorage.removeItem('token')
        // 跳转到登录页面
        window.location.href = '/#/views/user/login'
6db3ce8f   wuxw   优化 post 显示动画
85
  
468b84d2   wuxw   searchOwner 删除多余的
86
87
88
89
90
        // Message({
        //   message: '登录已过期,请重新登录',
        //   type: 'error',
        //   duration: 5 * 1000
        // })
5e59e7c3   wuxw   支持回话过期自动登录
91
      }
6db3ce8f   wuxw   优化 post 显示动画
92
  
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
93
94
95
96
97
      return Promise.reject(error)
    }
  )
  
  export default service