Jacob
4 天以前 f564a64d3c99b4d300e54e038446227a440bba48
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import {
   HTTP_REQUEST_URL,
   HEADER,
   TOKENNAME,
   HEADERPARAMS
} from '@/config/app';
import {
   toLogin,
   checkLogin
} from '../libs/login';
import store from '../store';
 
// ========== 接口请求日志开关与工具 ==========
// 是否启用接口详细日志(true 开启,false 关闭;上线前可置为 false)
const ENABLE_REQUEST_LOG = true;
 
/**
 * 统一打印接口请求日志(请求结束后调用)
 * 包含:请求地址、请求方法、请求参数、响应状态、响应报文、耗时
 */
function printRequestLog({
   url,
   method,
   data,
   status,
   response,
   isError,
   startTime,
   errMsg
}) {
   if (!ENABLE_REQUEST_LOG) return;
 
   const baseUrl = HTTP_REQUEST_URL || 'http://api.front.hdq.xbdzz.cn';
   const fullUrl = baseUrl + '/api/front/' + url;
   const costTime = startTime ? (Date.now() - startTime) + 'ms' : 'N/A';
   const tag = isError ? '✘ 接口请求失败' : '✔ 接口请求完成';
   const time = new Date().toLocaleString();
 
   console.log('\n┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓');
   console.log('┃  ' + tag + '   [' + time + ']');
   console.log('┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫');
   console.log('┃  请求地址: ' + fullUrl);
   console.log('┃  请求方法: ' + ((method || 'GET').toUpperCase()));
   console.log('┃  请求参数:');
   console.log(data || {});
   console.log('┃  响应状态: ' + (status !== undefined ? status : (isError ? '请求失败' : 'N/A')));
   console.log('┃  响应报文:');
   console.log(response);
   if (isError && errMsg) {
       console.log('┃  错误信息: ' + errMsg);
   }
   console.log('┃  请求耗时: ' + costTime);
   console.log('┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n');
}
 
function filterData(data) {
   if (!data) return {}
   if (Array.isArray(data)) return data
   if (typeof data == 'object') {
       let result = {}
       Object.keys(data).forEach(key => {
           if (data[key] || data[key] == 0) {
               result[key] = data[key]
           }
       })
       return result
   }
}
/**
 * 发送请求
 */
function baseRequest(url, method, data, {
   noAuth = false,
   noVerify = false
}, params, isForm) {
   let Url = HTTP_REQUEST_URL,
       header = HEADER
   if (params != undefined) {
       header = HEADERPARAMS;
   }
   if (!noAuth) {
       //登录过期自动登录
       if (!store.state.app.token && !checkLogin()) {
           console.log('needAuth', url)
           uni.showToast({
               'title': '未登录'
           });
           return Promise.reject({
               msg: '未登录'
           });
       }
   }
   if (store.state.app.token) header[TOKENNAME] = store.state.app.token;
   if (isForm) {
       header['content-type'] = 'multipart/form-data'
   }
   return new Promise((resolve, reject) => {
       Url = HTTP_REQUEST_URL || 'http://api.front.hdq.xbdzz.cn';
       let app = getApp();
       const startTime = Date.now();
       const reqData = filterData(data);
       uni.request({
           url: Url + '/api/front/' + url,
           method: method || 'GET',
           header: header,
           data: reqData,
           success: (res) => {
               // if(url.indexOf('pre/order')>=0){
               //     res.data.code=601;
               // }
               // 成功 / 非致命分支:统一打印一份日志(gio/log 与 noVerify 走白名单,不打日志)
               if (url.indexOf('gio/log') >= 0) {
                   resolve(res.data, res);
               } else if (noVerify) {
                   resolve(res.data, res);
               } else if (res.data.code == 200) {
                   printRequestLog({
                       url,
                       method,
                       data: reqData,
                       status: res.statusCode,
                       response: res.data,
                       isError: false,
                       startTime
                   });
                   resolve(res.data, res);
               } else if ([410000, 410001, 410002, 401].indexOf(res.data.code) !== -1) {
                   //接口返回需要登录
                   printRequestLog({
                       url,
                       method,
                       data: reqData,
                       status: res.statusCode,
                       response: res.data,
                       isError: true,
                       startTime,
                       errMsg: '需要登录 code=' + res.data.code
                   });
                   console.log('需要登录 url ' + url, res.data)
                   uni.$emit('needLogin');
                   uni.showToast({
                       'title': '未登录'
                   });
                   reject(res.data);
               } else {
                   printRequestLog({
                       url,
                       method,
                       data: reqData,
                       status: res.statusCode,
                       response: res.data,
                       isError: true,
                       startTime,
                       errMsg: res.data.message || '系统错误'
                   });
                   if (app) {
                       uni.$emit("showTips", {
                           'title': res.data.message || '系统错误',
                           'icon': 'none'
                       });
                   } else {
                       uni.showToast({
                           'icon': 'none',
                           'title': res.data.message || '系统错误'
                       });
                   }
                   console.log('系统错误 url ' + url, res.data)
                   reject(res.data);
               }
           },
           fail: (msg) => {
               printRequestLog({
                   url,
                   method,
                   data: reqData,
                   status: undefined,
                   response: msg,
                   isError: true,
                   startTime,
                   errMsg: '网络请求失败'
               });
               console.log(url, msg);
               if (app) {
                   uni.$emit("showTips", {
                       'title': '请求失败',
                       'icon': 'none'
                   });
               } else {
                   uni.showToast({
                       'icon': 'none',
                       'title': '请求失败'
                   });
               }
               reject('请求失败');
           }
       })
   });
}
 
const request = {};
 
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
   request[method] = (api, data, opt, params, isForm) => baseRequest(api, method, data, opt || {}, params,
       isForm)
});
 
 
 
export default request;