yichenxi
2022-12-13 4e15972e4db30f68e4ea74759fd04f00e6e44be2
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
import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
// const baseUrl = 'http://47.99.64.149:8081/'    
// const baseUrl = 'http://192.168.0.42:8081/' // lzj
// const baseUrl = 'http://192.168.0.33:8081/' // yy
// const baseUrl = 'http://192.168.0.25:8081' // jj
// const baseUrl = 'http://192.168.0.33:8085/'// swb
// const baseUrl = 'http://192.168.0.33:8081/' // new swb
// const baseUrl = 'http://121.43.179.139:8080'
// const baseUrl = 'http://192.168.0.33:8085/'
const baseUrl = 'http://192.168.0.33:8081/'
 
// create an axios instance
const service = axios.create({
  // baseURL: 'http://47.99.64.149:8080/screen_api_v2/', // url = base url + request url
  baseURL: baseUrl,
  // baseURL: 'http://192.168.0.195:8081/',
  // baseURL: ' http://www.7drlb7.com:8081/',
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 50000 // request timeout
})
const reqServe = axios.create({ baseurl: 'http://121.43.179.139:8080', timeout: '5000' })
// const reqServe1 = axios.create({ baseurl: 'http://192.168.55.1:8081', timeout: '5000' })
 
// instance1这里用到的参数有 baseurl,timeout,method,url instance1.get('/userinfo').then(res=>{ console.log(res) }) //instance2这里用到的参数有 baseurl,timeout,method,url,params,并且对timeout进行了修改 instance1.get('/orderlist',{ timeout:'5000' params:{} }).then(res=>{ console.log(res) })
// 请求拦截器
service.interceptors.request.use(
  config => {
    // 在发送请求之前做 第一次不走if,直接登录返回config(token),第二次会拿token去比对是否正确
    if (store.getters.token) {
      // 让每个请求携带令牌
      // ['X-Token'] 是自定义令牌
      // please modify it according to the actual situation
      config.headers['token'] = getToken()
      config.headers['Authorization'] = getToken()
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)
 
// response interceptor 响应拦截器,对接收的数据进行判断。
service.interceptors.response.use(
  response => {
    const res = response.data
 
    // if the custom code is not 20000, it is judged as an error.
    if (res.accountId === -1) {
      Message({
        message: res.msg || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
 
      // 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()
          })
        })
      }
      return Promise.reject(new Error(res.message || 'Error'))
    } else {
      if (getToken()) {
        if (res.code === -3) {
          store.dispatch('user/resetToken').then(() => {
            Message({
              message: res.message,
              type: 'warning'
            })
            setTimeout(() => {
              location.reload()
            }, 2000)
          })
        }
      }
      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, baseUrl, reqServe }