@x-9lab/xlab 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/@types/components/common/index.d.ts +0 -38
- package/@types/components/index.d.ts +0 -20
- package/@types/global.d.ts +2 -1
- package/dist/@config/config.js +2 -1
- package/dist/components/common/index.js +0 -80
- package/dist/components/cron/index.js +1 -1
- package/dist/components/uuid/index.js +2 -2
- package/dist/middleware/service-mark.js +2 -1
- package/dist/middlewares.js +1 -24
- package/dist/router.js +7 -2
- package/dist/server.js +1 -2
- package/package.json +5 -13
- package/dist/business/utils/cookie/1.1.0/clean/index.js +0 -35
- package/dist/business/utils/cookie/1.1.0/clean/js/@tpls/js.tpl +0 -32
- package/dist/business/utils/cookie/1.1.0/clean/js/index.js +0 -30
- package/dist/components/cache/index.js +0 -9
- package/dist/components/cache/lru.js +0 -93
- package/dist/components/html-processor/index.js +0 -95
- package/dist/components/injection/index.js +0 -83
- package/dist/components/js-processor/index.js +0 -65
- package/dist/components/md5/index.js +0 -19
- package/dist/components/platform/index.js +0 -103
- package/dist/components/proxy/index.js +0 -62
- package/dist/components/querystring/index.js +0 -60
- package/dist/components/redirect/index.js +0 -79
- package/dist/components/request/index.js +0 -159
- package/dist/components/request/resolve-uri.js +0 -28
- package/dist/components/version/index.js +0 -96
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
init: ()=>init,
|
|
13
|
-
htmlProcessor: ()=>htmlProcessor
|
|
14
|
-
});
|
|
15
|
-
const _utils = require("@x-drive/utils");
|
|
16
|
-
const _cache = require("../cache");
|
|
17
|
-
const _injection = require("../injection");
|
|
18
|
-
const _md5 = require("../md5");
|
|
19
|
-
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
20
|
-
const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
|
|
21
|
-
function _interopRequireDefault(obj) {
|
|
22
|
-
return obj && obj.__esModule ? obj : {
|
|
23
|
-
default: obj
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
var conf;
|
|
27
|
-
var logger;
|
|
28
|
-
const html_cache = (0, _cache.local)("HTML_CACHE", {
|
|
29
|
-
"maxAge": 1000 * 60 * 60,
|
|
30
|
-
"max": 10
|
|
31
|
-
});
|
|
32
|
-
function processorVar(html, req) {
|
|
33
|
-
return (0, _injection.inject)(html, req);
|
|
34
|
-
}
|
|
35
|
-
// 处理 HTML
|
|
36
|
-
// 读取 HTML 文件,如果没有缓存则缓存
|
|
37
|
-
// 填充相关字段
|
|
38
|
-
// 根据配置可以设置为清除本地缓存
|
|
39
|
-
// 设置缓存头
|
|
40
|
-
// 根据配置设置相应头部
|
|
41
|
-
function htmlProcessor(pathname, options, context) {
|
|
42
|
-
var filename = _path.default.resolve(conf.root, pathname);
|
|
43
|
-
// ua = req.get('user-agent'),
|
|
44
|
-
var cacheKey;
|
|
45
|
-
var html;
|
|
46
|
-
var route;
|
|
47
|
-
// 发送访问统计
|
|
48
|
-
// serverTrack(req, 'normal');
|
|
49
|
-
// 取出参数
|
|
50
|
-
if (options) {
|
|
51
|
-
route = options.route;
|
|
52
|
-
}
|
|
53
|
-
// 生成缓存 key
|
|
54
|
-
cacheKey = (0, _md5.md5)(pathname);
|
|
55
|
-
try {
|
|
56
|
-
html = html_cache.get(cacheKey);
|
|
57
|
-
if (!html) {
|
|
58
|
-
html = _fs.default.readFileSync(filename, "utf8");
|
|
59
|
-
html_cache.set(cacheKey, html);
|
|
60
|
-
}
|
|
61
|
-
} catch (e) {
|
|
62
|
-
logger.error("HTML Process Error", e);
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
// 无法正确读取 HTML
|
|
66
|
-
// 交给后续的 404 处理
|
|
67
|
-
if (!html) {
|
|
68
|
-
return null;
|
|
69
|
-
}
|
|
70
|
-
// 进行参数处理
|
|
71
|
-
html = processorVar(html, context);
|
|
72
|
-
// 按照配置清空 localStorage
|
|
73
|
-
if (conf.clearLocalStorage || context && context.query.no_cache) {
|
|
74
|
-
html = html.replace('<head>', '<head><script>if(window.localStorage) {window.localStorage.clear()}<\/script>');
|
|
75
|
-
}
|
|
76
|
-
// 读取 route 的配置并设置对应的 headers 信息
|
|
77
|
-
var headers = route && route.headers;
|
|
78
|
-
if (headers && context) {
|
|
79
|
-
Object.keys(headers).forEach((key)=>{
|
|
80
|
-
let val;
|
|
81
|
-
if ((0, _utils.isFunction)(headers[key])) {
|
|
82
|
-
val = headers[key]();
|
|
83
|
-
} else {
|
|
84
|
-
val = headers[key];
|
|
85
|
-
}
|
|
86
|
-
context.set(key, val);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
// 返回网页
|
|
90
|
-
return html;
|
|
91
|
-
}
|
|
92
|
-
function init() {
|
|
93
|
-
conf = getSysConfig();
|
|
94
|
-
logger = log.getLogger("html-processor");
|
|
95
|
-
}
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
inject: ()=>inject,
|
|
13
|
-
add: ()=>add,
|
|
14
|
-
modify: ()=>modify
|
|
15
|
-
});
|
|
16
|
-
const _utils = require("@x-drive/utils");
|
|
17
|
-
const conf = getSysConfig();
|
|
18
|
-
// 注入字段
|
|
19
|
-
const injectVars = conf.injection;
|
|
20
|
-
// 错误代码管理模块
|
|
21
|
-
const returnCode = requireMod("return-code").getCodeDetail();
|
|
22
|
-
/**
|
|
23
|
-
* 注入数据存储对象
|
|
24
|
-
*/ var INJECTIONS = {
|
|
25
|
-
"errCode": {},
|
|
26
|
-
"apis": conf.apis,
|
|
27
|
-
"biServer": conf.biServer ? conf.protocol + "://" + conf.biServer : "",
|
|
28
|
-
"navMap": {}
|
|
29
|
-
};
|
|
30
|
-
Object.keys(returnCode).forEach(function(key) {
|
|
31
|
-
let code = returnCode[key];
|
|
32
|
-
INJECTIONS.errCode[code.errorcode] = code.msg;
|
|
33
|
-
});
|
|
34
|
-
/**
|
|
35
|
-
* 数据注入方法
|
|
36
|
-
* @param html 待处理的字符串
|
|
37
|
-
* @param req 请求对象
|
|
38
|
-
* @return 处理完的字符串
|
|
39
|
-
*/ function inject(html, req) {
|
|
40
|
-
if ((0, _utils.isArray)(injectVars) && injectVars.length) {
|
|
41
|
-
let processorData = {};
|
|
42
|
-
injectVars.forEach((key)=>{
|
|
43
|
-
// 优先在模块內的缓存对象中找
|
|
44
|
-
let dat = INJECTIONS[key];
|
|
45
|
-
// 找不到再去配置中找
|
|
46
|
-
if ((0, _utils.isUndefined)(dat) || (0, _utils.isNull)(dat)) {
|
|
47
|
-
dat = conf[key];
|
|
48
|
-
}
|
|
49
|
-
// 如果找出来的是个函数,则尝试执行函数得到返回结果
|
|
50
|
-
if ((0, _utils.isFunction)(dat)) {
|
|
51
|
-
dat = dat(key, req);
|
|
52
|
-
}
|
|
53
|
-
if ((0, _utils.isObject)(dat) || Array.isArray(dat)) {
|
|
54
|
-
dat = JSON.stringify(dat);
|
|
55
|
-
}
|
|
56
|
-
processorData[key] = dat;
|
|
57
|
-
});
|
|
58
|
-
return (0, _utils.labelReplace)(html, processorData, true);
|
|
59
|
-
}
|
|
60
|
-
return html;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* 添加一个字段到缓存对象
|
|
64
|
-
* @param key 字段名
|
|
65
|
-
* @param val 数据
|
|
66
|
-
* @return 存储的数据
|
|
67
|
-
*/ function add(key, val) {
|
|
68
|
-
INJECTIONS[key] = val;
|
|
69
|
-
injectVars.push(key);
|
|
70
|
-
return INJECTIONS[key];
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* 修改一个字段
|
|
74
|
-
* @param key 字段名
|
|
75
|
-
* @param val 数据
|
|
76
|
-
* @return 存储的数据
|
|
77
|
-
*/ function modify(key, val) {
|
|
78
|
-
var re = INJECTIONS.hasOwnProperty(key);
|
|
79
|
-
if (re) {
|
|
80
|
-
INJECTIONS[key] = val;
|
|
81
|
-
}
|
|
82
|
-
return re;
|
|
83
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
init: ()=>init,
|
|
13
|
-
jsProcessor: ()=>jsProcessor
|
|
14
|
-
});
|
|
15
|
-
const _cache = require("../cache");
|
|
16
|
-
const _injection = require("../injection");
|
|
17
|
-
const _md5 = require("../md5");
|
|
18
|
-
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
19
|
-
const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
|
|
20
|
-
function _interopRequireDefault(obj) {
|
|
21
|
-
return obj && obj.__esModule ? obj : {
|
|
22
|
-
default: obj
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
var conf;
|
|
26
|
-
var logger;
|
|
27
|
-
var js_cache = (0, _cache.local)("JS_CACHE", {
|
|
28
|
-
"maxAge": 1000 * 60 * 60 * 24,
|
|
29
|
-
"max": 20
|
|
30
|
-
});
|
|
31
|
-
function processorVar(html, req) {
|
|
32
|
-
return (0, _injection.inject)(html, req);
|
|
33
|
-
}
|
|
34
|
-
// 处理 JS 文件
|
|
35
|
-
function jsProcessor(pathname, context) {
|
|
36
|
-
const filename = _path.default.resolve(conf.root + pathname);
|
|
37
|
-
var cacheKey;
|
|
38
|
-
var js;
|
|
39
|
-
// 生成缓存 key
|
|
40
|
-
cacheKey = (0, _md5.md5)(pathname);
|
|
41
|
-
// 检查是否有缓存
|
|
42
|
-
// 如果有缓存直接返回
|
|
43
|
-
try {
|
|
44
|
-
js = js_cache.get(cacheKey);
|
|
45
|
-
if (!js) {
|
|
46
|
-
js = _fs.default.readFileSync(filename, "utf8");
|
|
47
|
-
js_cache.set(cacheKey, js);
|
|
48
|
-
}
|
|
49
|
-
} catch (e) {
|
|
50
|
-
logger.error("JS Process Error", e);
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
// 无法正确读取 JS
|
|
54
|
-
// 交给后续的 404 处理
|
|
55
|
-
if (!js) {
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
// 进行参数处理
|
|
59
|
-
js = processorVar(js, context);
|
|
60
|
-
return js;
|
|
61
|
-
}
|
|
62
|
-
function init() {
|
|
63
|
-
conf = getSysConfig();
|
|
64
|
-
logger = log.getLogger("js-processor");
|
|
65
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "md5", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>md5
|
|
8
|
-
});
|
|
9
|
-
const _crypto = /*#__PURE__*/ _interopRequireDefault(require("crypto"));
|
|
10
|
-
function _interopRequireDefault(obj) {
|
|
11
|
-
return obj && obj.__esModule ? obj : {
|
|
12
|
-
default: obj
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
function md5(str) {
|
|
16
|
-
var md5 = _crypto.default.createHash("md5");
|
|
17
|
-
md5.update(str);
|
|
18
|
-
return md5.digest("hex");
|
|
19
|
-
}
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "check", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: ()=>check
|
|
8
|
-
});
|
|
9
|
-
const REGEXP = {
|
|
10
|
-
"wechat": /MicroMessenger\/([\d\.]+)/,
|
|
11
|
-
"alipay": /AlipayClient\/([\d\.]+)/,
|
|
12
|
-
"weibo": /(__weibo__|weibo\/)([\d\.]+)/i,
|
|
13
|
-
"android": /Android[\s]?([\d+\.]+)/i,
|
|
14
|
-
"ios": /iPad|iPhone/i
|
|
15
|
-
};
|
|
16
|
-
const COMMON_OS_REGEXP = /\([^\)]*\)/;
|
|
17
|
-
// window 平台内部版本与发行版的映射关系
|
|
18
|
-
const windowVersionMap = {
|
|
19
|
-
"5.0": "Windows 2000",
|
|
20
|
-
"5.1": "Windows XP",
|
|
21
|
-
"6.0": "Windows Vista",
|
|
22
|
-
"6.1": "Windows 7",
|
|
23
|
-
"6.2": "Windows 8",
|
|
24
|
-
"6.3": "Windows 8.1",
|
|
25
|
-
"10": "Windows 10",
|
|
26
|
-
"10.0": "Windows 10"
|
|
27
|
-
};
|
|
28
|
-
const unknownStr = "Unknown";
|
|
29
|
-
function check(ua) {
|
|
30
|
-
ua = String(ua);
|
|
31
|
-
const plat = {
|
|
32
|
-
"is": ""
|
|
33
|
-
};
|
|
34
|
-
for(var n in REGEXP){
|
|
35
|
-
plat[n] = ua.match(REGEXP[n]);
|
|
36
|
-
if (plat[n]) {
|
|
37
|
-
switch(true){
|
|
38
|
-
case n === "ios":
|
|
39
|
-
plat.is = "mobile";
|
|
40
|
-
const iosVer = ua.match(/Version\/([\d+\.]+)/i);
|
|
41
|
-
plat[n] = iosVer ? iosVer[1] || "Unknonw" : "Unknonw";
|
|
42
|
-
break;
|
|
43
|
-
case n === "android":
|
|
44
|
-
if (!plat.wechat && !plat.alipay && !plat.weibo) {
|
|
45
|
-
plat.is = "mobile";
|
|
46
|
-
}
|
|
47
|
-
plat[n] = plat[n][1];
|
|
48
|
-
break;
|
|
49
|
-
default:
|
|
50
|
-
plat[n] = plat[n][1];
|
|
51
|
-
plat.is = n;
|
|
52
|
-
}
|
|
53
|
-
} else {
|
|
54
|
-
plat[n] = false;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
if (plat.is === "") {
|
|
58
|
-
let deviceArr = ua.match(COMMON_OS_REGEXP);
|
|
59
|
-
let device = deviceArr && deviceArr[0];
|
|
60
|
-
deviceArr = null;
|
|
61
|
-
let osInfo;
|
|
62
|
-
let osVInfo;
|
|
63
|
-
if (device) {
|
|
64
|
-
osInfo = device.replace(/[\(|\)]+/, "").split(/;[\s]?/);
|
|
65
|
-
if (osInfo[1] === "U") {
|
|
66
|
-
osInfo.splice(1, 1);
|
|
67
|
-
}
|
|
68
|
-
switch(true){
|
|
69
|
-
case device.indexOf("Win") !== -1:
|
|
70
|
-
plat.os = "Windows";
|
|
71
|
-
var winOs = "";
|
|
72
|
-
while(osInfo.length){
|
|
73
|
-
winOs = osInfo.shift();
|
|
74
|
-
if (winOs.indexOf("Win") !== -1) {
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
if (winOs) {
|
|
79
|
-
osVInfo = winOs.match(/[\d\.]+/);
|
|
80
|
-
osVInfo = osVInfo && osVInfo[0] ? windowVersionMap[osVInfo[0]] || unknownStr : unknownStr;
|
|
81
|
-
} else {
|
|
82
|
-
osVInfo = unknownStr;
|
|
83
|
-
}
|
|
84
|
-
plat.win = osVInfo;
|
|
85
|
-
plat.is = "desktop";
|
|
86
|
-
break;
|
|
87
|
-
case device.indexOf("Mac OS") !== -1:
|
|
88
|
-
osVInfo = device.match(/[\d_]+/);
|
|
89
|
-
osVInfo = osVInfo && osVInfo[0] || unknownStr;
|
|
90
|
-
osVInfo = osVInfo.replace(/_/g, ".");
|
|
91
|
-
plat.mac = osVInfo;
|
|
92
|
-
plat.is = "desktop";
|
|
93
|
-
break;
|
|
94
|
-
case device.indexOf("Linux") !== -1:
|
|
95
|
-
plat.os = "Linux";
|
|
96
|
-
plat.linux = unknownStr;
|
|
97
|
-
plat.is = "desktop";
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return plat;
|
|
103
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
set: ()=>set,
|
|
13
|
-
web: ()=>web
|
|
14
|
-
});
|
|
15
|
-
const _httpProxy = /*#__PURE__*/ _interopRequireDefault(require("http-proxy"));
|
|
16
|
-
function _interopRequireDefault(obj) {
|
|
17
|
-
return obj && obj.__esModule ? obj : {
|
|
18
|
-
default: obj
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
const proxy = _httpProxy.default.createProxy();
|
|
22
|
-
/**
|
|
23
|
-
* proxy 配置
|
|
24
|
-
*/ const proxyOptions = {};
|
|
25
|
-
/**
|
|
26
|
-
* 设置代理配置
|
|
27
|
-
* @param type 代理配置名称
|
|
28
|
-
* @param options 配置信息
|
|
29
|
-
*/ function set(type, options) {
|
|
30
|
-
if (!proxyOptions[type]) {
|
|
31
|
-
if (typeof options === "string") {
|
|
32
|
-
options = {
|
|
33
|
-
"target": options,
|
|
34
|
-
"host": options.replace("http://", "")
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
proxyOptions[type] = options;
|
|
38
|
-
}
|
|
39
|
-
return proxyOptions[type];
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* web 代理
|
|
43
|
-
*/ function web(reqObj, headers) {
|
|
44
|
-
let options = proxyOptions[reqObj.req.proxyType];
|
|
45
|
-
if (options.host) {
|
|
46
|
-
reqObj.req.headers.host = options.host;
|
|
47
|
-
}
|
|
48
|
-
if (headers) {
|
|
49
|
-
reqObj.req.headers = Object.assign(reqObj.req.headers, headers);
|
|
50
|
-
}
|
|
51
|
-
return new Promise(function(resolve, reject) {
|
|
52
|
-
proxy.web(reqObj.req, reqObj.res, options, function(err) {
|
|
53
|
-
if (err) {
|
|
54
|
-
console.log("proxy err ", err);
|
|
55
|
-
err.name = "Proxy";
|
|
56
|
-
reject(err);
|
|
57
|
-
} else {
|
|
58
|
-
resolve(null);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
query2string: ()=>query2string,
|
|
13
|
-
string2query: ()=>string2query
|
|
14
|
-
});
|
|
15
|
-
const _querystring = /*#__PURE__*/ _interopRequireDefault(require("querystring"));
|
|
16
|
-
function _interopRequireDefault(obj) {
|
|
17
|
-
return obj && obj.__esModule ? obj : {
|
|
18
|
-
default: obj
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* 将 URL 参数转换为字符串
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* query2string({"pf":"145","ss":"320x240"});
|
|
26
|
-
* query2string({"pf":"145","ss":"320x240"},["pf"]);
|
|
27
|
-
* query2string({"pf":"145","ss":"320x240"},{"fr":"android"});
|
|
28
|
-
* query2string({"pf":"145","ss":"320x240"},{"fr":"android"},["fr"]);
|
|
29
|
-
* ```
|
|
30
|
-
*/ function query2string(...args) {
|
|
31
|
-
var withouts;
|
|
32
|
-
const lastIndex = args.length - 1;
|
|
33
|
-
// 检测是否有排除参数
|
|
34
|
-
if (Array.isArray(args[lastIndex])) {
|
|
35
|
-
withouts = args[lastIndex];
|
|
36
|
-
args.pop();
|
|
37
|
-
}
|
|
38
|
-
// 需要生成的参数对象
|
|
39
|
-
var qsObj = {};
|
|
40
|
-
args.forEach(function(item) {
|
|
41
|
-
Object.keys(item).forEach(function(key) {
|
|
42
|
-
// 原有的逻辑不是相同的key替换,而是后面的key跟现有的有冲突时会被忽略掉
|
|
43
|
-
if (!qsObj[key] && (!withouts || withouts.indexOf(key) === -1)) {
|
|
44
|
-
qsObj[key] = encodeURIComponent(item[key]);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
return _querystring.default.stringify(qsObj);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* 字符串转成参数对象
|
|
52
|
-
* @param qString 参数字符串
|
|
53
|
-
* @return 处理完的对象
|
|
54
|
-
* @example
|
|
55
|
-
* ```ts
|
|
56
|
-
* string2query("pf=145&ss=320x240");
|
|
57
|
-
* ```
|
|
58
|
-
*/ function string2query(qString) {
|
|
59
|
-
return _querystring.default.parse(qString);
|
|
60
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: all[name]
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
by302: ()=>by302,
|
|
13
|
-
byJS: ()=>byJS,
|
|
14
|
-
byMeta: ()=>byMeta,
|
|
15
|
-
makeRedirectUrl: ()=>makeRedirectUrl
|
|
16
|
-
});
|
|
17
|
-
const _querystring = require("../querystring");
|
|
18
|
-
// 302 重定向
|
|
19
|
-
// 将用户访问302重定向到新的页面中去
|
|
20
|
-
function by302(context, url) {
|
|
21
|
-
var newUrl = makeRedirectUrl(context.query, url);
|
|
22
|
-
// 重定向到处理后的地址
|
|
23
|
-
context.redirect(newUrl);
|
|
24
|
-
}
|
|
25
|
-
// 添加公参重定向
|
|
26
|
-
// 由于直接 302 重定向各版本的客户端都可能存在兼容性的问题
|
|
27
|
-
// 所以利用页面 meta 或者 location.href 进行重定向
|
|
28
|
-
function byJS(context, url, msg, origin) {
|
|
29
|
-
msg = msg || "正在为您跳转...";
|
|
30
|
-
var newUrl = origin ? url : makeRedirectUrl(context.query, url);
|
|
31
|
-
// 返回重定向页面
|
|
32
|
-
context.body = `<html><head><title>跳转</title><noscript><meta http-equiv="refresh" content="0; url=${newUrl}"></noscript><script>window.location.href = "${newUrl}";</script></head><body>${msg}</body></html>`;
|
|
33
|
-
}
|
|
34
|
-
function byMeta(context, url) {
|
|
35
|
-
var newUrl = makeRedirectUrl(context.query, url);
|
|
36
|
-
// 返回重定向页面
|
|
37
|
-
context.body = '<html><head><title>跳转</title><noscript><meta http-equiv="refresh" content="0; url=' + newUrl + '"></noscript><script>window.location.href = "' + newUrl + '";</script></head><body>正在为您跳转...</body></html>';
|
|
38
|
-
}
|
|
39
|
-
// 重新整理重定向链接
|
|
40
|
-
function makeRedirectUrl(query, url) {
|
|
41
|
-
var withouts = [];
|
|
42
|
-
var queryString = "?";
|
|
43
|
-
const hashIndex = url.indexOf("#");
|
|
44
|
-
const urlpath = hashIndex > -1 ? url.substring(0, hashIndex) : url;
|
|
45
|
-
const queryIndex = urlpath.indexOf('?');
|
|
46
|
-
// 查找参数在新地址中是否已存在,存在则从原地址中转移到新地址
|
|
47
|
-
for(var name in query){
|
|
48
|
-
if (url.indexOf(name + '=') > 0) {
|
|
49
|
-
withouts.push(name);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
// 拼接 URL 参数
|
|
53
|
-
queryString += (0, _querystring.query2string)(query, withouts);
|
|
54
|
-
// 已经有参数,插入原参数位置
|
|
55
|
-
if (queryIndex > 0) {
|
|
56
|
-
// 如果已有参数则需要在替换字符串最后加上 &
|
|
57
|
-
// /detail/?id=1
|
|
58
|
-
// /detail/?id=1#123
|
|
59
|
-
if (queryIndex < url.length - 1 && (hashIndex < 0 || hashIndex - queryIndex > 1)) {
|
|
60
|
-
queryString += '&';
|
|
61
|
-
}
|
|
62
|
-
url = url.replace('?', queryString);
|
|
63
|
-
// 还没有参数,自己找位置合适的位置
|
|
64
|
-
} else {
|
|
65
|
-
// if (queryString === "?") {
|
|
66
|
-
// queryString = "";
|
|
67
|
-
// }
|
|
68
|
-
// 有 # ,将参数放在 # 前
|
|
69
|
-
if (hashIndex > 0) {
|
|
70
|
-
var arr = url.split("#");
|
|
71
|
-
arr.splice(1, 0, queryString, "#");
|
|
72
|
-
url = arr.join("");
|
|
73
|
-
// 无 # ,直接将参数拼接在最后
|
|
74
|
-
} else {
|
|
75
|
-
url += queryString;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return url;
|
|
79
|
-
}
|