zhaoxiaoqiang
2023-08-25 9583630b27fdd2f2566995a78d8238ce504f3523
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
133
134
135
136
137
138
139
140
141
142
143
144
145
import axios from 'axios'
// import { MessageBox, Message } from 'element-ui'
import { Loading } from 'element-ui'
import { getSessionId } from '@/utils/auth'
import sign from '@/utils/loginSign';
import {showTipOfStatuCode} from './statusCodeManage'
 
 
let loading        //定义loading变量
function startLoading() {    //使用Element loading-start 方法
    loading = Loading.service({
        lock: true,
        text: '加载中……',
        background: 'rgba(0, 0, 0, 0.7)'
    })
}
function endLoading() {    //使用Element loading-close 方法
    loading.close()
}
//那么 showFullScreenLoading() tryHideFullScreenLoading() 要干的事儿就是将同一时刻的请求合并。
//声明一个变量 needLoadingRequestCount,每次调用showFullScreenLoading方法 needLoadingRequestCount + 1。
//调用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount为 0 时,结束 loading。
let needLoadingRequestCount = 0
export function showFullScreenLoading() {
    if (needLoadingRequestCount === 0) {
        startLoading()
    }
    needLoadingRequestCount++
}
 
export function tryHideFullScreenLoading() {
    if (needLoadingRequestCount <= 0) return
    needLoadingRequestCount--
    if (needLoadingRequestCount === 0) {
        endLoading()
    }
}
 
 
// import StatusCodeManage from './statusCodeManage';
// create an axios instance
const service = axios.create({
    baseURL: '/flp',
    timeout: 300000 // request timeout
})
 
// request interceptor
let configs
service.interceptors.request.use(
  config => {
        configs=config
        if (config.method === 'post') {
          // post 封装数据
          if (typeof config.data !== 'object') {
              config.data = {};
          }
        } else if (config.method === 'get') {
            // get 封装数据
            if (config.params === undefined) {
                config.params = {};
            }
            //        config.params['mblNo'] = config.mblNo;
        }
        let sessionID = getSessionId();
      // 验签 用于所有没有登录并传递有电话号码的接口验签
      if (sessionID === null) {
      } else {
          // 参数签名
          // 必须由http或https开头的请求,才会进行自动签名组装
          if (config.params) {
              config.params = sign.signForFormDataOrUrl(
                  config.params,
                  true,
                  sessionID
              );
          } else if (config.data) {
              config.data = sign.signForFormDataOrUrl(
                  config.data,
                  false,
                  sessionID
              );
          } else {
              if (config.method === 'GET') {
                  config.params = sign.signForFormDataOrUrl(
                      config.data,
                      false,
                      sessionID
                  );
              } else {
                  config.data = sign.signForFormDataOrUrl(
                      config.data,
                      false,
                      sessionID
                  );
              }
          }
      }
      //  ================end   ===================
    // if (store.getters.token) {
    //   // let each request carry token
    //   // ['X-Token'] is a custom headers key
    //   // please modify it according to the actual situation
    //   config.headers['X-Token'] = getSessionId()
    // }
    showFullScreenLoading()
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    tryHideFullScreenLoading()
    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;
    tryHideFullScreenLoading()
    if(configs.method=='get'){
        return response;
    }else{
        return res;
    }
 
  },
  error => {
    tryHideFullScreenLoading()
    showTipOfStatuCode(error);
    return Promise.reject(error);
}
)
 
export default service