Blame view

merchant-web-master/src/utils/requestnew.js 3.18 KB
b1d468f9   李宇   1
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
  import axios from 'axios'
  import {
    MessageBox,
    Message
  } from 'element-ui'
  import store from '@/store'
  import router from '@/router'
  import {
    getToken,
    getShopId,
    removeToken
  } from '@/utils/auth'
  
  //
  // const baseURL = process.env.VUE_APP_DOMAIN_PREFIX
  const host = window.location.host;
  let PREFIX;
  if (host == '172.16.61.48' || host == '172.16.61.49:5173') {
     PREFIX = 'https://jy.scjysm.asia:18086/merchant-business';
  }else if( host == 'localhost:9528' || host == '8.130.38.56:8027' || host == 'localhost:9529'){
    // 其他情况的默认值
    // PREFIX = 'http://8.130.38.56:8027/business-server';
    //  PREFIX = 'https://jy.scjysm.asia:18086/merchant-business';
    PREFIX = 'http://8.130.38.56:9003'
  }else{
    PREFIX = '/merchant-business';
  }
  const baseURL = PREFIX
  // const baseURL = 'http://10.0.0.14:9004'
  // create an axios instance
  const service = axios.create({
    baseURL,
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 300000 // request timeout
  })
  
  // export const uploadUrl = 'http://192.168.1.102:8005/file/upload'
  export const uploadUrl = `${baseURL}/file/upload`
  export const WXuploadUrl = `${baseURL}/file/uploadWxMedia` // 直播上传专用
  //export const QYuploadUrl = `${baseURL}/file/uploadQyMedia` // 微信客服上传专用
  export const QYuploadUrl = `${baseURL}/file/upload` // 微信客服上传专用
  
  // request interceptor
  service.interceptors.request.use(
    config => {
      if (store.getters.token) {
        config.headers['Authorization-business'] = getToken()
        config.headers['shopId'] = getShopId()
        config.headers['Content-Type'] = 'application/json; charset=UTF-8'
        // config.headers['type'] = ' business'
      }
      return config
    },
    error => {
      console.log(error)
      return Promise.reject(error)
    }
  )
  
  // response interceptor
  service.interceptors.response.use(
    response => {
      const res = response.data
      if (response.config.responseType === 'blob') {
        return response.data
      }
      if (res.code !== '') {
        Message({
          message: res.message || 'Error',
          type: 'error',
          duration: 5 * 1000
        })
        // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
        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) {
          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 => {
      console.log(error)
      Message({
        message: '服务器暂无响应,请稍后重试',
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(error)
    }
  )
  export default service