Blame view

wenjuan-pc/src/utils/request.js 3.93 KB
b1fef010   杨鑫   '最新代码'
1
2
3
4
5
6
7
8
9
10
11
12
  import Vue from 'vue'
  import axios from 'axios'
  import {
    MessageBox,
    Message
  } from 'element-ui'
  import store from '@/store'
  import router from '@/router'
  import {
    getToken, removeToken
  } from '@/utils/auth'
  
60e2cd8b   杨鑫   '最新'
13
  // const baseURL = process.env.VUE_APP_DOMAIN_PREFIX_1
24cb57ca   wesley88   1
14
15
  let baseURL = 'http://172.16.61.125:9003'
  // const baseURL = 'https://jy.scjysm.asia:18086/meserver/admin-server/'
9dce0ee1   杨鑫   '最新问卷'
16
  
b1fef010   杨鑫   '最新代码'
17
18
  // const baseURL = 'http://192.168.2.36:9003'
  // create an axios instance
24cb57ca   wesley88   1
19
20
21
22
23
  let host = window.location.host;
  let hostall = window.location.href;
  if(host === 'localhost:8080' || host === 'localhost:8081' || host === 'localhost:9528'|| host === '192.168.31.45:9528'|| host === 'localhost:9529'  ) {
  
    // baseURL = process.env.VUE_APP_DOMAIN_PREFIX_1;
77959fdb   杨鑫   最新2
24
25
    // baseURL = 'https://jy.scjysm.asia:18086/cdwlMall/meserver/admin-server';
    baseURL = 'http://128.10.249.21:9003';
24cb57ca   wesley88   1
26
27
28
29
30
31
32
33
34
35
36
37
38
    // baseURL = 'http://172.16.61.125:9003';
    // baseURL = 'http://192.168.2.36:9003';
    // baseURL = process.env.VUE_APP_DOMAIN_PREFIX_1;
    // baseURL = 'http://192.168.2.225:9003';
  
  } else {
    console.error('---------------------')
    console.error(hostall)
    let c1 = hostall.split('cdwlMall')[0];
    baseURL = c1 + 'cdwlMall/admin-server';
    console.error(baseURL)
  }
  
b1fef010   杨鑫   '最新代码'
39
40
41
42
43
44
45
46
47
48
  Vue.prototype.axios = axios
  axios.defaults.timeout = 1000000
  const service = axios.create({
    // baseURL:'/cdwlMall/meserver/admin-server/', // url = base url + request url
    baseURL, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 1000000 // request timeout
  })
  // export const upurl = baseURL
  // export const uploadUrl = `${baseURL}miniio/uploadQuestion`
24cb57ca   wesley88   1
49
   export const uploadUrl = `${baseURL}/miniio/uploadQuestion`
b1fef010   杨鑫   '最新代码'
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
  export const token = getToken()
  
  // request interceptor
  service.interceptors.request.use(
    config => {
      // console.log(config)
      if (store.getters.token) {
        config.headers['Authorization-admin'] = getToken()
        config.headers['Content-Type'] = 'application/json; charset=UTF-8'
        // config.headers['type'] = ' admin'
      }
      return config
    },
    error => {
      console.log(error) // for debug
      return Promise.reject(error)
    }
  )
  
  // response interceptor
  service.interceptors.response.use(
    response => {
      const res = response.data
      if (response.config.responseType === 'blob') {
        return response.data
      }
35280aea   “wangming”   1
76
      if (res.code !== '' && res.code !== 200&& res.code !== "200") {
b1fef010   杨鑫   '最新代码'
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
        Message({
          message: res.message || 'Error',
          type: 'error',
          duration: 5 * 1000
        })
  
        // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
        /**
          token为空 20003
          public static final String TOKEN_IS_NULL = "20003";
          token认证失败
          public static final String TOKEN_APPROVE_ERROR = "20004";
          请先登录
          public static final String USER_NOT_LOGIN = "20005";
         */
        const tokenerr = [20003, '20003', 20004, '20004', 20005, '20005']
        if (tokenerr.includes(res.code)) {
          localStorage.clear()
          removeToken()
          router.push({ path: '/login' })
          location.reload()
        }
        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 {
        return res
      }
    },
    // error => {
    //   if (!error.message.includes('timeout')) {
    //     // Message({
    //     //   message: '服务器暂无响应,请稍后重试',
    //     //   type: 'error',
    //     //   duration: 5 * 1000
    //     // })
    //   }
    //   return Promise.reject(error)
    // }
  )
  
  export default service