loulijun2021
2023-05-06 cbcf2f774d7bd505502cf05a09eb38ee553f7c34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getCookie, getToken, removeCookie, setCookie } from '@/utils/auth'
import { handleDatetime } from '@/utils/global'
// create an axios instance
 
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 10000 // request timeout
})
// axios.defaults.withCredentials = true
 
// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
 
    if (getCookie('admin')) {
    // let each request carry token
    // ['X-Token'] is a custom headers key
    // please modify it according to the actual situation
    // config.headers['X-Token'] = getToken()
    //   config.headers['Authorization'] = 'admin=' + getCookie('admin') + ';navTabId=' + getCookie('navTabId')
    //   config.headers['Cookie'] = 'admin=' + getCookie('admin') + ';navTabId=' + getCookie('navTabId')
    //   document.cookie = 'admin=' + getCookie('admin') + ';navTabId=' + getCookie('navTabId')
    //   document.cookie = 'admin'
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)
 
// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */
 
  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response.data
 
    // if the custom code is not 20000, it is judged as an error.
    if (res.code === '300' || res.code === '303') {
      Message({
        message: res.Message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
      if (res.code === '303') {
        removeCookie('ruleCode')
        removeCookie('username')
        removeCookie('admin')
        removeCookie('navTabId')
        removeCookie('usertype')
        removeCookie('userid')
        removeCookie('rediskey')
        removeCookie('guid')
        removeCookie('code')
 
        if (getCookie('cloud')) {
          removeCookie('cloud')
          setTimeout(() => {
            sessionStorage.removeItem('tabViews')
            window.location.href = 'http://182.61.13.206/choose'
          }, 1000 * 2)
        } else {
          setTimeout(() => {
            sessionStorage.removeItem('tabViews')
            window.location.reload()
          }, 1000 * 2)
        }
      }
      // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
      // if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
      //   // to re-login
      //   MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
      //     confirmButtonText: 'Re-Login',
      //     cancelButtonText: 'Cancel',
      //     type: 'warning'
      //   }).then(() => {
      //     store.dispatch('user/resetToken').then(() => {
      //       location.reload()
      //     })
      //   })
      // }
      store.state.app.buttonIsDisabled = false // 将按钮变回可点击
      return Promise.reject(new Error(res.message || 'Error'))
    }
    // else if (res.code === '303') {
    //   console.log(res.code, 888)
    //   Message({
    //     message: res.Message || 'Error',
    //     type: 'error',
    //     duration: 5 * 1000
    //   })
    //   removeCookie('ruleCode')
    //   removeCookie('username')
    //   removeCookie('admin')
    //   removeCookie('navTabId')
    //   removeCookie('usertype')
    //   removeCookie('userid')
    //   removeCookie('code')
    //   this.$router.push({ path: this.redirect || '/' })
    // }
    else {
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)
 
export default service