forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globalLib.js
568 lines (526 loc) · 15.3 KB
/
globalLib.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import projectConfig from './config';
import $ from 'jquery';
import { Message } from '@alifd/next';
import { LOGINPAGE_ENABLED } from './constants';
function goLogin() {
const url = window.location.href;
localStorage.removeItem('token');
const base_url = url.split('#')[0];
console.log('base_url', base_url);
window.location = `${base_url}#/login`;
}
const global = window;
/**
* 获取cookie值
* @param {*String} keyName cookie名
*/
const aliwareGetCookieByKeyName = function(keyName) {
let result = '';
const cookieList = (document.cookie && document.cookie.split(';')) || [];
cookieList.forEach(str => {
const [name = '', value = ''] = str.split('=') || [];
if (name.trim().indexOf(keyName) !== -1) {
result = value;
}
});
return result.trim();
};
/**
* 监听事件对象
*/
const nacosEvent = (function(_global) {
const eventListObj = {};
const ignoreEventListObj = {};
return {
/**
* 只监听一次
*/
once(eventName, callback) {
this.listen.call(this, eventName, callback, true);
},
/**
* 监听事件<eventName: String 监听事件名, callback: function 回调函数, once: boolean 是否监听一次>
*/
listen(eventName, callback, once = false) {
if (!eventName || !callback) {
return;
}
!eventListObj[eventName] && (eventListObj[eventName] = []);
eventListObj[eventName].push({
callback,
once,
});
},
/**
* 监听事件, 之前未消费的消息也会进行触发<eventName: String 监听事件名, callback: function 回调函数, once: boolean 是否监听一次>
*/
listenAllTask(...args) {
const self = this;
const argsList = Array.prototype.slice.call(args);
const eventName = argsList[0];
if (!eventName) {
return;
}
// 监听事件
self.listen(...argsList);
// 判断是否有未消费的消息
if (ignoreEventListObj[eventName] && ignoreEventListObj[eventName].length > 0) {
const eventObj = ignoreEventListObj[eventName].pop();
self.trigger.apply(eventObj.self, eventObj.argsList);
}
},
/**
* 触发事件
*/
trigger(...args) {
const self = this;
const argsList = Array.prototype.slice.call(args);
const eventName = argsList.shift();
// 如果还没有订阅消息, 将其放到未消费队列里
if (!eventListObj[eventName]) {
!ignoreEventListObj[eventName] && (ignoreEventListObj[eventName] = []);
ignoreEventListObj[eventName].push({
argsList: Array.prototype.slice.call(args),
self,
});
return;
}
const newList = [];
eventListObj[eventName].forEach((_obj, index) => {
if (Object.prototype.toString.call(_obj.callback) !== '[object Function]') {
return;
}
_obj.callback.apply(self, argsList);
// 删除只触发一次的事件
if (!_obj.once) {
newList.push(_obj);
}
});
eventListObj[eventName] = newList;
},
/**
* 删除监听事件
*/
remove(eventName, callback) {
if (!eventName || !eventListObj[eventName]) {
return;
}
if (!callback) {
eventListObj[eventName] = null;
} else {
const newList = [];
eventListObj[eventName].forEach((_obj, index) => {
if (_obj.callback !== callback) {
newList.push(_obj);
}
});
eventListObj[eventName] = newList.length ? newList : null;
}
},
};
})(global);
/**
* nacos的工具类
*/
const nacosUtils = (function(_global) {
let loadingCount = 0;
let loadingState = {
visible: false,
shape: 'flower',
tip: 'loading...',
// color: "#333",
// style: { height: "100%", width: "100%" }
};
return {
/**
* 改变loading 的样式
*/
changeLoadingAttr(obj) {
if (Object.prototype.toString.call(obj) === '[object Object]') {
loadingState = Object.assign({}, loadingCount, obj);
}
},
/**
* 打开loading效果
*/
openLoading() {
loadingCount++;
nacosEvent.trigger(
'nacosLoadingEvent',
Object.assign(loadingState, {
visible: true,
spinning: true,
})
);
},
/**
* 尝试关闭loading, 只有当loadingCount小于0时才会关闭loading效果
*/
closeLoading() {
loadingCount--;
if (loadingCount <= 0) {
loadingCount = 0;
nacosEvent.trigger(
'nacosLoadingEvent',
Object.assign(loadingState, {
visible: false,
spinning: false,
})
);
}
},
/**
* 关闭loading效果
*/
closeAllLoading() {
loadingCount = 0;
nacosEvent.trigger(
'nacosLoadingEvent',
Object.assign(loadingState, {
visible: false,
spinning: false,
})
);
},
/**
* 获取资源地址, 如果资源需要静态化输出 请调用此方法
*/
getURISource(url) {
return url;
},
};
})(global);
/**
* 获取url中的参数
*/
const getParams = (function(_global) {
return function(name) {
const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i');
let result = [];
if (_global.location.hash !== '') {
result = _global.location.hash.split('?'); // 优先判别hash
} else {
result = _global.location.href.split('?');
}
if (result.length === 1) {
result = _global.parent.location.hash.split('?');
}
if (result.length > 1) {
const r = result[1].match(reg);
if (r != null) {
return decodeURIComponent(r[2]);
}
}
return null;
};
})(global);
/**
* 设置参数
*/
const setParams = (function(global) {
let _global = global;
const _originHref = _global.location.href.split('#')[0];
return function(name, value) {
if (!name) {
return;
}
let obj = {};
if (typeof name === 'string') {
obj = {
[name]: value,
};
}
if (Object.prototype.toString.call(name) === '[object Object]') {
obj = name;
}
let hashArr = [];
if (_global.location.hash) {
hashArr = _global.location.hash.split('?');
}
const paramArr = (hashArr[1] && hashArr[1].split('&')) || [];
let paramObj = {};
paramArr.forEach(val => {
const tmpArr = val.split('=');
paramObj[tmpArr[0]] = decodeURIComponent(tmpArr[1] || '');
});
paramObj = Object.assign({}, paramObj, obj);
const resArr =
Object.keys(paramObj).map(key => `${key}=${encodeURIComponent(paramObj[key] || '')}`) || [];
hashArr[1] = resArr.join('&');
const hashStr = hashArr.join('?');
if (_global.history.replaceState) {
const url = _originHref + hashStr;
_global.history.replaceState(null, '', url);
} else {
_global.location.hash = hashStr;
}
};
})(global);
/**
* 设置参数
*/
const setParam = function(...args) {
return setParams.apply(this, args);
};
/**
* 删除参数
*/
const removeParams = (function(global) {
let _global = global;
const _originHref = _global.location.href.split('#')[0];
return function(name) {
let removeList = [];
const nameType = Object.prototype.toString.call(name);
if (nameType === '[object String]') {
removeList.push(name);
} else if (nameType === '[object Array]') {
removeList = name;
} else if (nameType === '[object Object]') {
removeList = Object.keys(name);
} else {
return;
}
let hashArr = [];
if (_global.location.hash) {
hashArr = _global.location.hash.split('?');
}
let paramArr = (hashArr[1] && hashArr[1].split('&')) || [];
// let paramObj = {};
paramArr = paramArr.filter(val => {
const tmpArr = val.split('=');
return removeList.indexOf(tmpArr[0]) === -1;
});
hashArr[1] = paramArr.join('&');
const hashStr = hashArr.join('?');
if (_global.history.replaceState) {
const url = _originHref + hashStr;
_global.history.replaceState(null, '', url);
} else {
_global.location.hash = hashStr;
}
};
})(global);
/**
* 封装的ajax请求
*/
const request = (function(_global) {
const middlewareList = [];
const middlewareBackList = [];
const serviceMap = {};
const serviceList = [];
const methodList = [];
/**
* 获取真实url信息
*/
const NacosRealUrlMapper = (function() {
serviceList.forEach(obj => {
serviceMap[obj.registerName] = obj;
});
return function(registerName) {
const serviceObj = serviceMap[registerName];
if (!serviceObj) {
return null;
}
// 获取正确请求方式
serviceObj.methodType = methodList[serviceObj.method];
return serviceObj;
};
})();
/**
* 添加中间件函数
* @param {*function} callback 回调函数
*/
function middleWare(callback, isBack = true) {
if (isBack) {
middlewareBackList.push(callback);
} else {
middlewareList.push(callback);
}
return this;
}
/**
* 处理中间件
* @param {*Object} config ajax请求配置信息
*/
function handleMiddleWare(...allArgs) {
// 获取除config外传入的参数
let [config, ...args] = allArgs;
// 最后一个参数为middlewareList
const middlewareList = args.pop() || [];
if (middlewareList && middlewareList.length > 0) {
config = middlewareList.reduce((config, callback) => {
if (typeof callback === 'function') {
return callback.apply(this, [config, ...args]) || config;
}
return config;
}, config);
}
return config;
}
/**
* 处理自定义url
* @param {*Object} config ajax请求配置信息
*/
function handleCustomService(...args) {
let [config] = args;
// 只处理com.alibaba.开头的url
if (config && config.url && config.url.indexOf('com.alibaba.') === 0) {
const registerName = config.url;
const serviceObj = NacosRealUrlMapper(registerName);
if (serviceObj && serviceObj.url && serviceObj.url.replace) {
// 有mock数据 直接返回 生产环境失效
if (projectConfig.is_preview && serviceObj.is_mock && config.success) {
let code = null;
try {
code = JSON.parse(serviceObj.defaults);
} catch (error) {}
config.success(code);
return;
}
// 替换url中的占位符
config.url = serviceObj.url.replace(/{([^\}]+)}/g, ($1, $2) => config.$data[$2]);
try {
// 添加静态参数
if (serviceObj.is_param && typeof config.data === 'object') {
config.data = Object.assign({}, JSON.parse(serviceObj.params), config.data);
}
} catch (e) {}
// 替换请求方式
if (serviceObj.method && !config.type) {
config.type = serviceObj.methodType;
}
// 将请求参数变为json格式
if (serviceObj.isJsonData && typeof config.data === 'object') {
config.data = JSON.stringify(config.data);
config.processData = false;
config.dataType = 'json';
config.contentType = 'application/json';
}
try {
// 设置临时代理 生产环境失效
if (projectConfig.is_preview && serviceObj.is_proxy) {
const { beforeSend } = config;
config.beforeSend = function(xhr) {
serviceObj.cookie && xhr.setRequestHeader('tmpCookie', serviceObj.cookie);
serviceObj.header && xhr.setRequestHeader('tmpHeader', serviceObj.header);
serviceObj.proxy && xhr.setRequestHeader('tmpProxy', serviceObj.proxy);
beforeSend && beforeSend(xhr);
};
}
} catch (e) {}
// 设置自动loading效果
if (serviceObj.autoLoading) {
// nacosUtils.openLoading();
const prevComplete = config.complete;
config.complete = function() {
nacosUtils.closeLoading();
typeof prevComplete === 'function' &&
prevComplete.apply($, Array.prototype.slice.call(args));
};
}
// serviceObj = null;
}
}
return config;
}
function Request(...allArgs) {
// 除了config外的传参
let [config, ...args] = allArgs;
// 处理前置中间件
config = handleMiddleWare.apply(this, [config, ...args, middlewareList]);
// 处理自定义url
config = handleCustomService.apply(this, [config, ...args]);
if (!config) return;
// xsrf
if (
config.type &&
config.type.toLowerCase() === 'post' &&
config.data &&
Object.prototype.toString.call(config.data) === '[object Object]' &&
!config.data.sec_token
) {
const sec_token = aliwareGetCookieByKeyName('XSRF-TOKEN');
sec_token && (config.data.sec_token = sec_token);
}
// 处理后置中间件
config = handleMiddleWare.apply(this, [config, ...args, middlewareBackList]);
const [url, paramsStr] = config.url.split('?');
const params = paramsStr ? paramsStr.split('&') : [];
const _LOGINPAGE_ENABLED = localStorage.getItem(LOGINPAGE_ENABLED);
if (_LOGINPAGE_ENABLED !== 'false') {
let token = {};
try {
token = JSON.parse(localStorage.token);
} catch (e) {
console.log('Token Error', localStorage.token, e);
goLogin();
}
const { accessToken = '' } = token;
params.push(`accessToken=${accessToken}`);
}
return $.ajax(
Object.assign({}, config, {
type: config.type,
url: [url, params.join('&')].join('?'),
data: config.data || '',
dataType: config.dataType || 'json',
beforeSend(xhr) {
config.beforeSend && config.beforeSend(xhr);
},
headers: {
Authorization: localStorage.getItem('token'),
},
})
).then(
success => {},
error => {
// 处理403 forbidden
const { status, responseJSON = {} } = error || {};
if (responseJSON.message) {
Message.error(responseJSON.message);
}
if (
[401, 403].includes(status) &&
['unknown user!', 'token invalid!', 'token expired!'].includes(responseJSON.message)
) {
goLogin();
}
return error;
}
);
}
// 暴露方法
Request.handleCustomService = handleCustomService;
Request.handleMiddleWare = handleMiddleWare;
Request.NacosRealUrlMapper = NacosRealUrlMapper;
Request.serviceList = serviceList;
Request.serviceMap = serviceMap;
Request.middleWare = middleWare;
return Request;
})(global);
export {
nacosEvent,
nacosUtils,
aliwareGetCookieByKeyName,
removeParams,
getParams,
setParam,
setParams,
goLogin,
request,
};