abler-api 0.1.17 → 0.1.20
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/README.md +7 -2
- package/dist/cjs/pp-util.js +233 -60
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ API服务相关工具
|
|
|
5
5
|
安装
|
|
6
6
|
|
|
7
7
|
```
|
|
8
|
-
npm i abler-api
|
|
8
|
+
npm i abler-api@latest -s
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
|
|
@@ -13,8 +13,13 @@ npm i abler-api-util@latest -s
|
|
|
13
13
|
引用
|
|
14
14
|
|
|
15
15
|
```
|
|
16
|
-
const {apiUtil} = require("abler-api");
|
|
16
|
+
const {apiUtil, preconditions} = require("abler-api");
|
|
17
17
|
apiUtil.config(conf,errCfg,dbSql);
|
|
18
18
|
...
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### preconditions
|
|
24
|
+
|
|
25
|
+
API服务启动时前置条件检查,使用
|
package/dist/cjs/pp-util.js
CHANGED
|
@@ -4,6 +4,8 @@ var require$$0 = require('crypto');
|
|
|
4
4
|
var require$$1 = require('abler-util');
|
|
5
5
|
var require$$2 = require('abler-db');
|
|
6
6
|
var require$$3 = require('abler-net');
|
|
7
|
+
var require$$0$1 = require('fs');
|
|
8
|
+
var require$$1$1 = require('path');
|
|
7
9
|
|
|
8
10
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
11
|
|
|
@@ -11,15 +13,17 @@ var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);
|
|
|
11
13
|
var require$$1__default = /*#__PURE__*/_interopDefaultLegacy(require$$1);
|
|
12
14
|
var require$$2__default = /*#__PURE__*/_interopDefaultLegacy(require$$2);
|
|
13
15
|
var require$$3__default = /*#__PURE__*/_interopDefaultLegacy(require$$3);
|
|
16
|
+
var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$1);
|
|
17
|
+
var require$$1__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$1$1);
|
|
14
18
|
|
|
15
19
|
const crypto = require$$0__default["default"];
|
|
16
|
-
const ppUtil$
|
|
20
|
+
const ppUtil$2 = require$$1__default["default"].ppUtil;
|
|
17
21
|
const {
|
|
18
22
|
dbUtil,
|
|
19
23
|
kvStorage
|
|
20
24
|
} = require$$2__default["default"];
|
|
21
25
|
const netUtil = require$$3__default["default"];
|
|
22
|
-
let conf, appSetting, err, dbSql;
|
|
26
|
+
let conf$1, appSetting, err$1, dbSql;
|
|
23
27
|
const pnToken = "access_token",
|
|
24
28
|
hnToken = pnToken,
|
|
25
29
|
pnApiKey = "apiKey",
|
|
@@ -27,20 +31,20 @@ const pnToken = "access_token",
|
|
|
27
31
|
// 我们接收到的 headers 中的字段名总是全小写的
|
|
28
32
|
pnApiSecret = "apiSecret",
|
|
29
33
|
hnApiSecret = pnApiSecret.toLowerCase();
|
|
30
|
-
const MD5 = ppUtil$
|
|
31
|
-
moveProperty = ppUtil$
|
|
34
|
+
const MD5 = ppUtil$2.MD5,
|
|
35
|
+
moveProperty = ppUtil$2.moveProperty;
|
|
32
36
|
|
|
33
37
|
class apiUtil$1 {
|
|
34
|
-
static debugFlag = ppUtil$
|
|
38
|
+
static debugFlag = ppUtil$2.newGuid(); //应用必须设置,否则谁也不知道是啥
|
|
35
39
|
|
|
36
|
-
static testFlag = ppUtil$
|
|
40
|
+
static testFlag = ppUtil$2.newGuid();
|
|
37
41
|
static envId_dev = "?"; // static apiCallRecSaver;
|
|
38
42
|
|
|
39
43
|
static config(appConfig, appErrCfg, appDbSql) {
|
|
40
|
-
ppUtil$
|
|
41
|
-
conf = appConfig;
|
|
42
|
-
appSetting = conf?.appSetting;
|
|
43
|
-
err = appErrCfg, dbSql = appDbSql;
|
|
44
|
+
ppUtil$2.config(appConfig, appErrCfg);
|
|
45
|
+
conf$1 = appConfig;
|
|
46
|
+
appSetting = conf$1?.appSetting;
|
|
47
|
+
err$1 = appErrCfg, dbSql = appDbSql;
|
|
44
48
|
apiUtil$1.debugFlag = appSetting?.debugFlag || apiUtil$1.debugFlag;
|
|
45
49
|
apiUtil$1.testFlag = appSetting?.testFlag || apiUtil$1.testFlag; // apiUtil.apiCallRecSaver = apiCallRecSaver;
|
|
46
50
|
} //#region ===== 需要应用系统重写的方法
|
|
@@ -53,7 +57,7 @@ class apiUtil$1 {
|
|
|
53
57
|
|
|
54
58
|
|
|
55
59
|
static _getApiSecret(companyId) {
|
|
56
|
-
return ppUtil$
|
|
60
|
+
return ppUtil$2.newGuid();
|
|
57
61
|
}
|
|
58
62
|
/**
|
|
59
63
|
* 保存 API 调用记录,应用系统必须重写此方法
|
|
@@ -156,18 +160,18 @@ class apiUtil$1 {
|
|
|
156
160
|
|
|
157
161
|
|
|
158
162
|
static setParamsFunctions(params) {
|
|
159
|
-
ppUtil$
|
|
163
|
+
ppUtil$2.defUnenumProp(params, "asNumber", function (name, minValue, maxValue, defaultValue) {
|
|
160
164
|
// if (!this.hasOwnProperty(name)) // {} 定义的对象没有 hasOwnProperty 函数
|
|
161
165
|
if (this[name] === undefined) return defaultValue;
|
|
162
166
|
let result = +this[name];
|
|
163
167
|
|
|
164
168
|
if (typeof result !== "number" || result < minValue || result > maxValue) {
|
|
165
|
-
throw [err.INVALID_PARAM, `参数 ${name} 必须是 ${minValue} ~ ${maxValue} 之间的数字`];
|
|
169
|
+
throw [err$1.INVALID_PARAM, `参数 ${name} 必须是 ${minValue} ~ ${maxValue} 之间的数字`];
|
|
166
170
|
}
|
|
167
171
|
|
|
168
172
|
return result;
|
|
169
173
|
});
|
|
170
|
-
ppUtil$
|
|
174
|
+
ppUtil$2.defUnenumProp(params, "asBool", function (name, defaultValue) {
|
|
171
175
|
if (this[name] === undefined) return defaultValue;
|
|
172
176
|
let result = JSON.parse(this[name]);
|
|
173
177
|
return !!result;
|
|
@@ -232,7 +236,7 @@ class apiUtil$1 {
|
|
|
232
236
|
|
|
233
237
|
static apiFail(error, req) {
|
|
234
238
|
configNeeded();
|
|
235
|
-
let response = err.ERROR(error, err.errorLangParamFlag + ppUtil$
|
|
239
|
+
let response = err$1.ERROR(error, err$1.errorLangParamFlag + ppUtil$2.getMsgLang(req));
|
|
236
240
|
response.datetime = new Date();
|
|
237
241
|
|
|
238
242
|
if (req && req.headers) {
|
|
@@ -453,12 +457,12 @@ class apiUtil$1 {
|
|
|
453
457
|
delete params.token;
|
|
454
458
|
} else {
|
|
455
459
|
// 如果没有token,则返回错误
|
|
456
|
-
console.log(err.TOKEN_NEEDED);
|
|
460
|
+
console.log(err$1.TOKEN_NEEDED);
|
|
457
461
|
|
|
458
462
|
if (res) {
|
|
459
|
-
res.send(err.ERROR(err.TOKEN_NEEDED));
|
|
463
|
+
res.send(err$1.ERROR(err$1.TOKEN_NEEDED));
|
|
460
464
|
} else if (!noErr) {
|
|
461
|
-
throw err.TOKEN_NEEDED;
|
|
465
|
+
throw err$1.TOKEN_NEEDED;
|
|
462
466
|
}
|
|
463
467
|
}
|
|
464
468
|
}
|
|
@@ -480,7 +484,7 @@ class apiUtil$1 {
|
|
|
480
484
|
const params = apiUtil$1.extractParams(req);
|
|
481
485
|
req.accessToken = req.accessToken || apiUtil$1.extractToken(req);
|
|
482
486
|
|
|
483
|
-
if (req.accessToken === MD5(ppUtil$
|
|
487
|
+
if (req.accessToken === MD5(ppUtil$2.idNumDisturbing)) {
|
|
484
488
|
//todo: 检查IP
|
|
485
489
|
params.__internal__ = true;
|
|
486
490
|
req.ignoreToken = true; // if (params.idNum || params.personId) {
|
|
@@ -501,7 +505,7 @@ class apiUtil$1 {
|
|
|
501
505
|
return true;
|
|
502
506
|
} else {
|
|
503
507
|
// console.log('token error.', req.accessToken);
|
|
504
|
-
throw err.TOKEN_INVALID;
|
|
508
|
+
throw err$1.TOKEN_INVALID;
|
|
505
509
|
}
|
|
506
510
|
}
|
|
507
511
|
/**
|
|
@@ -537,7 +541,7 @@ class apiUtil$1 {
|
|
|
537
541
|
|
|
538
542
|
if (dbgToken) {
|
|
539
543
|
let envId = process.env.ECS_DEPLOY_ID;
|
|
540
|
-
req.__debug__ = dbgToken && dbgToken === MD5(req.headers.timestamp, ppUtil$
|
|
544
|
+
req.__debug__ = dbgToken && dbgToken === MD5(req.headers.timestamp, ppUtil$2.commonHashDisturbing) && (envId === "florist_longdan" || envId === "shuzi");
|
|
541
545
|
} else {
|
|
542
546
|
req.__debug__ = false;
|
|
543
547
|
} // console.log("__debug__:", req.__debug__, "dbgToken:", dbgToken);
|
|
@@ -717,7 +721,7 @@ class apiUtil$1 {
|
|
|
717
721
|
};
|
|
718
722
|
|
|
719
723
|
if (rec.secret !== "") {
|
|
720
|
-
rec.secret = ppUtil$
|
|
724
|
+
rec.secret = ppUtil$2.getEncAse192(rec.secret);
|
|
721
725
|
}
|
|
722
726
|
|
|
723
727
|
req.apiCallRec = rec;
|
|
@@ -784,7 +788,7 @@ class apiUtil$1 {
|
|
|
784
788
|
return certInfo;
|
|
785
789
|
}).catch(e => {
|
|
786
790
|
console.log('获取企业证书失败:\n', e);
|
|
787
|
-
throw [err.ACCESS_REFUSED, `获取企业证书失败:${err.ERROR(e).message}`];
|
|
791
|
+
throw [err$1.ACCESS_REFUSED, `获取企业证书失败:${err$1.ERROR(e).message}`];
|
|
788
792
|
});
|
|
789
793
|
}
|
|
790
794
|
/**
|
|
@@ -798,7 +802,7 @@ class apiUtil$1 {
|
|
|
798
802
|
static async verifyApiSignature(tokenData, req) {
|
|
799
803
|
try {
|
|
800
804
|
if (!req.headers.timestamp) {
|
|
801
|
-
throw [err.ACCESS_REFUSED, `必须在请求头中设置时戳`];
|
|
805
|
+
throw [err$1.ACCESS_REFUSED, `必须在请求头中设置时戳`];
|
|
802
806
|
}
|
|
803
807
|
|
|
804
808
|
let timestamp = parseInt(req.headers.timestamp);
|
|
@@ -806,13 +810,13 @@ class apiUtil$1 {
|
|
|
806
810
|
let maxTimeDiff = apiUtil$1.isDebugMode(req) ? 7 * 24 * 3600 : apiUtil$1.isTestMode(req) ? 4 * 3600 : 100; // 100
|
|
807
811
|
|
|
808
812
|
if (timestamp.toString() == "NaN" || currentTime < timestamp - 5000 || currentTime - timestamp > maxTimeDiff * 1000) {
|
|
809
|
-
throw [err.ACCESS_REFUSED, `时戳(${timestamp})无效,当前时戳:${currentTime},时差 ${(currentTime - timestamp) / 1000}秒`];
|
|
813
|
+
throw [err$1.ACCESS_REFUSED, `时戳(${timestamp})无效,当前时戳:${currentTime},时差 ${(currentTime - timestamp) / 1000}秒`];
|
|
810
814
|
}
|
|
811
815
|
|
|
812
816
|
let signature = req.headers.signature;
|
|
813
817
|
|
|
814
818
|
if (!signature) {
|
|
815
|
-
throw [err.ACCESS_REFUSED, `必须在请求头中提供签名`];
|
|
819
|
+
throw [err$1.ACCESS_REFUSED, `必须在请求头中提供签名`];
|
|
816
820
|
}
|
|
817
821
|
|
|
818
822
|
let signData = apiUtil$1.extractToken(req, null, true) || tokenData.apiKey;
|
|
@@ -867,7 +871,7 @@ class apiUtil$1 {
|
|
|
867
871
|
|
|
868
872
|
if (!tokenData.certPublicKeySpare) {
|
|
869
873
|
throw {
|
|
870
|
-
eo: err.ACCESS_REFUSED,
|
|
874
|
+
eo: err$1.ACCESS_REFUSED,
|
|
871
875
|
msgArgv: "签名验证异常",
|
|
872
876
|
data: e
|
|
873
877
|
};
|
|
@@ -916,7 +920,7 @@ class apiUtil$1 {
|
|
|
916
920
|
} catch (e) {
|
|
917
921
|
console.log("SPO服务签名验证异常(备用证书), ", e);
|
|
918
922
|
throw {
|
|
919
|
-
eo: err.ACCESS_REFUSED,
|
|
923
|
+
eo: err$1.ACCESS_REFUSED,
|
|
920
924
|
msgArgv: "签名验证异常(spare)",
|
|
921
925
|
data: e
|
|
922
926
|
};
|
|
@@ -925,7 +929,7 @@ class apiUtil$1 {
|
|
|
925
929
|
|
|
926
930
|
if (!verifyOK) {
|
|
927
931
|
console.log("SPO服务签名验证失败, tokenData: ", tokenData);
|
|
928
|
-
throw [err.ACCESS_REFUSED, "签名验证未通过"];
|
|
932
|
+
throw [err$1.ACCESS_REFUSED, "签名验证未通过"];
|
|
929
933
|
}
|
|
930
934
|
|
|
931
935
|
return verifyOK;
|
|
@@ -943,7 +947,7 @@ class apiUtil$1 {
|
|
|
943
947
|
if (!tokenData.companyInfo) {
|
|
944
948
|
tokenData.companyInfo = await apiUtil$1.queryCompanyInfo(tokenData.apiKey, true).catch(e => {
|
|
945
949
|
console.log("执行企业信息查询发生异常", e);
|
|
946
|
-
throw [err.API_KEY_INVALID, "执行企业信息查询发生异常:" + e.message || ""];
|
|
950
|
+
throw [err$1.API_KEY_INVALID, "执行企业信息查询发生异常:" + e.message || ""];
|
|
947
951
|
});
|
|
948
952
|
}
|
|
949
953
|
|
|
@@ -952,7 +956,7 @@ class apiUtil$1 {
|
|
|
952
956
|
if (companyInfo != null) {
|
|
953
957
|
let secret = apiUtil$1._getApiSecret(tokenData.apiKey);
|
|
954
958
|
|
|
955
|
-
if (secret !== tokenData.apiSecret) throw err.API_SCREPT_INVALID; // 不再检查 IP 白名单,改为验证证书签名
|
|
959
|
+
if (secret !== tokenData.apiSecret) throw err$1.API_SCREPT_INVALID; // 不再检查 IP 白名单,改为验证证书签名
|
|
956
960
|
// console.log("fromIp:", tokenData.clientIp, "ipWhiteList:", companyInfo.ipWhiteList);
|
|
957
961
|
// let whiteList = companyInfo.ipWhiteList || "";
|
|
958
962
|
// // 没有设置ip白名单的就不检查了
|
|
@@ -966,7 +970,7 @@ class apiUtil$1 {
|
|
|
966
970
|
});
|
|
967
971
|
}
|
|
968
972
|
|
|
969
|
-
throw [err.API_KEY_INVALID, tokenData.apiKey];
|
|
973
|
+
throw [err$1.API_KEY_INVALID, tokenData.apiKey];
|
|
970
974
|
}
|
|
971
975
|
/**
|
|
972
976
|
* 检查令牌数据是否存在
|
|
@@ -1017,7 +1021,7 @@ class apiUtil$1 {
|
|
|
1017
1021
|
paramExists = typeof params[paramName] != 'undefined' || typeof params[paramName + paramSufix] != 'undefined';
|
|
1018
1022
|
}
|
|
1019
1023
|
|
|
1020
|
-
if (!paramExists) return ppUtil$
|
|
1024
|
+
if (!paramExists) return ppUtil$2.errorPormise(err$1.ERROR(err$1.PARAMETER_NEEDED, paramName));
|
|
1021
1025
|
}
|
|
1022
1026
|
|
|
1023
1027
|
return Promise.resolve(paramSufix);
|
|
@@ -1051,14 +1055,9 @@ class apiUtil$1 {
|
|
|
1051
1055
|
|
|
1052
1056
|
static getEnvHosts(params) {
|
|
1053
1057
|
configNeeded();
|
|
1054
|
-
let envType = ppUtil$1.getEnvType();
|
|
1055
|
-
let x = conf.envHosts[envType] || {
|
|
1056
|
-
hosts: ["localhost"]
|
|
1057
|
-
};
|
|
1058
|
-
let port = +params.port || 8443;
|
|
1059
1058
|
return {
|
|
1060
|
-
hosts:
|
|
1061
|
-
port: x.port ||
|
|
1059
|
+
hosts: appSetting.serverHosts || ["localhost"],
|
|
1060
|
+
port: x.listenPort || +params.port || 8443
|
|
1062
1061
|
};
|
|
1063
1062
|
}
|
|
1064
1063
|
/**
|
|
@@ -1086,9 +1085,9 @@ class apiUtil$1 {
|
|
|
1086
1085
|
configNeeded();
|
|
1087
1086
|
const params = options.replacements;
|
|
1088
1087
|
const myEnvHosts = apiUtil$1.getEnvHosts(params);
|
|
1089
|
-
const myIp = ppUtil$
|
|
1088
|
+
const myIp = ppUtil$2.getMyIp();
|
|
1090
1089
|
const host = params.host || myIp;
|
|
1091
|
-
if (!myEnvHosts.hosts.contains(host)) throw [err.ACCESS_REFUSED, `环境${conf.envId}貌似无此主机:${params.host}`];
|
|
1090
|
+
if (!myEnvHosts.hosts.contains(host)) throw [err$1.ACCESS_REFUSED, `环境${conf$1.envId}貌似无此主机:${params.host}`];
|
|
1092
1091
|
|
|
1093
1092
|
if (!apiUtil$1.hostIsMySelf(host, options._res.req)) {
|
|
1094
1093
|
let result = await apiUtil$1.forwardsTo(host, myEnvHosts.port, params, options._res.req, myIp);
|
|
@@ -1112,7 +1111,7 @@ class apiUtil$1 {
|
|
|
1112
1111
|
|
|
1113
1112
|
|
|
1114
1113
|
static async forwardsTo(anotherIp, port, params, req, lanClient) {
|
|
1115
|
-
let url = anotherIp.indexOf("://") > 0 ? anotherIp : `${
|
|
1114
|
+
let url = anotherIp.indexOf("://") > 0 ? anotherIp : `${req.headers.scheme}://${anotherIp}:${port}${req.originalUrl}`;
|
|
1116
1115
|
|
|
1117
1116
|
if (url.indexOf("?") > 0) {
|
|
1118
1117
|
url = url.substring(0, url.indexOf("?"));
|
|
@@ -1127,7 +1126,7 @@ class apiUtil$1 {
|
|
|
1127
1126
|
|
|
1128
1127
|
if (result.status !== 200) {
|
|
1129
1128
|
console.log(`访问 ${reqOptions.url} 出错`, result.status, result.res.statusMessage);
|
|
1130
|
-
throw [err.EXCEPTION, `[${result.status}] - ${result.res.statusMessage}`];
|
|
1129
|
+
throw [err$1.EXCEPTION, `[${result.status}] - ${result.res.statusMessage}`];
|
|
1131
1130
|
}
|
|
1132
1131
|
|
|
1133
1132
|
return result.data;
|
|
@@ -1143,7 +1142,7 @@ class apiUtil$1 {
|
|
|
1143
1142
|
let keys = Array.isArray(keyPath) ? keyPath : keyPath.split(".");
|
|
1144
1143
|
let parentKey = "";
|
|
1145
1144
|
let key = "config";
|
|
1146
|
-
let value = conf;
|
|
1145
|
+
let value = conf$1;
|
|
1147
1146
|
let parent = null;
|
|
1148
1147
|
|
|
1149
1148
|
for (let i = 0; i < keys.length; i++) {
|
|
@@ -1153,7 +1152,7 @@ class apiUtil$1 {
|
|
|
1153
1152
|
value = parent[key];
|
|
1154
1153
|
|
|
1155
1154
|
if (value === undefined) {
|
|
1156
|
-
throw [err.INVALID_PARAM, `无此配置项: ${key}`];
|
|
1155
|
+
throw [err$1.INVALID_PARAM, `无此配置项: ${key}`];
|
|
1157
1156
|
}
|
|
1158
1157
|
}
|
|
1159
1158
|
|
|
@@ -1176,7 +1175,7 @@ class apiUtil$1 {
|
|
|
1176
1175
|
await apiUtil$1.checkRequestToken(req);
|
|
1177
1176
|
await next();
|
|
1178
1177
|
} catch (e) {
|
|
1179
|
-
res.send(err.ERROR(e));
|
|
1178
|
+
res.send(err$1.ERROR(e));
|
|
1180
1179
|
}
|
|
1181
1180
|
|
|
1182
1181
|
return false;
|
|
@@ -1224,7 +1223,7 @@ class apiUtil$1 {
|
|
|
1224
1223
|
|
|
1225
1224
|
return true;
|
|
1226
1225
|
} catch (e) {
|
|
1227
|
-
res.send(err.ERROR(e));
|
|
1226
|
+
res.send(err$1.ERROR(e));
|
|
1228
1227
|
return false;
|
|
1229
1228
|
}
|
|
1230
1229
|
}
|
|
@@ -1274,11 +1273,11 @@ class apiUtil$1 {
|
|
|
1274
1273
|
let errResponse = null;
|
|
1275
1274
|
|
|
1276
1275
|
if (!tokenData.clientIp) {
|
|
1277
|
-
errResponse = apiUtil$1.spoApiFail(err.GET_CLIENTIP_FAIL, req);
|
|
1276
|
+
errResponse = apiUtil$1.spoApiFail(err$1.GET_CLIENTIP_FAIL, req);
|
|
1278
1277
|
} else if (!tokenData.apiKey) {
|
|
1279
|
-
errResponse = apiUtil$1.spoApiFail([err.PARAMETER_NEEDED, "apiKey"], req);
|
|
1278
|
+
errResponse = apiUtil$1.spoApiFail([err$1.PARAMETER_NEEDED, "apiKey"], req);
|
|
1280
1279
|
} else if (!tokenData.apiSecret) {
|
|
1281
|
-
errResponse = apiUtil$1.spoApiFail([err.PARAMETER_NEEDED, "apiSecret"], req);
|
|
1280
|
+
errResponse = apiUtil$1.spoApiFail([err$1.PARAMETER_NEEDED, "apiSecret"], req);
|
|
1282
1281
|
}
|
|
1283
1282
|
|
|
1284
1283
|
if (errResponse != null) {
|
|
@@ -1314,7 +1313,7 @@ class apiUtil$1 {
|
|
|
1314
1313
|
let accessToken = apiUtil$1.extractToken(req);
|
|
1315
1314
|
|
|
1316
1315
|
if (!accessToken) {
|
|
1317
|
-
res.send(apiUtil$1.spoApiFail(err.TOKEN_NEEDED, req)); // apiUtil.sendOrRedirect(req, res, spoApiFail(err.TOKEN_NEEDED, req));
|
|
1316
|
+
res.send(apiUtil$1.spoApiFail(err$1.TOKEN_NEEDED, req)); // apiUtil.sendOrRedirect(req, res, spoApiFail(err.TOKEN_NEEDED, req));
|
|
1318
1317
|
|
|
1319
1318
|
return;
|
|
1320
1319
|
}
|
|
@@ -1335,7 +1334,7 @@ class apiUtil$1 {
|
|
|
1335
1334
|
req.tokenData = p;
|
|
1336
1335
|
return apiUtil$1.$checkApiKeyOld(req, res, next);
|
|
1337
1336
|
}).catch(e => {
|
|
1338
|
-
apiUtil$1.sendOrRedirect(req, res, apiUtil$1.spoApiFail(err.TOKEN_INVALID, req));
|
|
1337
|
+
apiUtil$1.sendOrRedirect(req, res, apiUtil$1.spoApiFail(err$1.TOKEN_INVALID, req));
|
|
1339
1338
|
});
|
|
1340
1339
|
}
|
|
1341
1340
|
/**
|
|
@@ -1388,7 +1387,7 @@ class apiUtil$1 {
|
|
|
1388
1387
|
if (await apiUtil$1.checkForwardsHost(options)) return true;
|
|
1389
1388
|
let c = apiUtil$1.parseCfgPath(item);
|
|
1390
1389
|
delete c.parent;
|
|
1391
|
-
c.host = ppUtil$
|
|
1390
|
+
c.host = ppUtil$2.getMyIp();
|
|
1392
1391
|
return c;
|
|
1393
1392
|
}
|
|
1394
1393
|
/**
|
|
@@ -1409,14 +1408,14 @@ class apiUtil$1 {
|
|
|
1409
1408
|
let c = apiUtil$1.parseCfgPath(item);
|
|
1410
1409
|
|
|
1411
1410
|
if (typeof c.value === "object") {
|
|
1412
|
-
throw [err.ACCESS_REFUSED, `暂不支持修改复杂配置`];
|
|
1411
|
+
throw [err$1.ACCESS_REFUSED, `暂不支持修改复杂配置`];
|
|
1413
1412
|
} // c.oldValue = c.value;
|
|
1414
1413
|
|
|
1415
1414
|
|
|
1416
1415
|
c.newValue = params.value;
|
|
1417
1416
|
c.parent[c.key] = c.newValue;
|
|
1418
1417
|
delete c.parent;
|
|
1419
|
-
c.host = ppUtil$
|
|
1418
|
+
c.host = ppUtil$2.getMyIp();
|
|
1420
1419
|
return c; // 应用层自行处理多机同步
|
|
1421
1420
|
} //#endregion
|
|
1422
1421
|
|
|
@@ -1424,19 +1423,193 @@ class apiUtil$1 {
|
|
|
1424
1423
|
}
|
|
1425
1424
|
|
|
1426
1425
|
function configNeeded() {
|
|
1427
|
-
if (!conf || !err) {
|
|
1426
|
+
if (!conf$1 || !err$1) {
|
|
1427
|
+
ppUtil$2.configNeeded();
|
|
1428
|
+
conf$1 = ppUtil$2.appConfig;
|
|
1429
|
+
appSetting = conf$1.appSetting;
|
|
1430
|
+
err$1 = ppUtil$2.appErrCfg;
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
var ppUtilApi = apiUtil$1;
|
|
1435
|
+
|
|
1436
|
+
function commonjsRequire(path) {
|
|
1437
|
+
throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
const fs = require$$0__default$1["default"];
|
|
1441
|
+
const path = require$$1__default$1["default"];
|
|
1442
|
+
const ppUtil$1 = require$$1__default["default"].ppUtil;
|
|
1443
|
+
const redisUtil = require$$2__default["default"].redisUtil;
|
|
1444
|
+
let conf, err; // const dbCheck = require('../dbupdate/dd-version');
|
|
1445
|
+
|
|
1446
|
+
/**
|
|
1447
|
+
* API服务启动时前置条件检查
|
|
1448
|
+
* ok = await preconditions
|
|
1449
|
+
* .setAppName(conf.thisApp)
|
|
1450
|
+
* .addCheckPromise(preconditions.checkExEnvSetting())
|
|
1451
|
+
* .addCheckPromise(preconditions.checkRedis())
|
|
1452
|
+
* .addCheckPromise(checkDatabase())
|
|
1453
|
+
* .addChecker(myChecker, args)
|
|
1454
|
+
* .checkAll()
|
|
1455
|
+
*/
|
|
1456
|
+
|
|
1457
|
+
class preconditions$1 {
|
|
1458
|
+
static appName;
|
|
1459
|
+
static checkFunctions = [];
|
|
1460
|
+
static checkPromises = [];
|
|
1461
|
+
|
|
1462
|
+
static setAppName(aName) {
|
|
1428
1463
|
ppUtil$1.configNeeded();
|
|
1429
1464
|
conf = ppUtil$1.appConfig;
|
|
1430
|
-
appSetting = conf.appSetting;
|
|
1431
1465
|
err = ppUtil$1.appErrCfg;
|
|
1466
|
+
preconditions$1.appName = aName || conf.thisApp;
|
|
1467
|
+
return preconditions$1;
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
static addCheckPromise(p) {
|
|
1471
|
+
preconditions$1.checkPromises.push(p);
|
|
1472
|
+
return preconditions$1;
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
static addChecker(checkFunc, args) {
|
|
1476
|
+
preconditions$1.checkFunctions.push({
|
|
1477
|
+
check: checkFunc,
|
|
1478
|
+
args: args
|
|
1479
|
+
});
|
|
1480
|
+
return preconditions$1;
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
static async checkAll() {
|
|
1484
|
+
for (let i = 0; i < preconditions$1.checkFunctions.length; i++) {
|
|
1485
|
+
const f = preconditions$1.checkFunctions[i];
|
|
1486
|
+
preconditions$1.addCheckPromise(f.check(f.args));
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
for (let i = 0; i < preconditions$1.checkPromises.length; i++) {
|
|
1490
|
+
let msg = await preconditions$1.checkPromises[i];
|
|
1491
|
+
|
|
1492
|
+
if (msg) {
|
|
1493
|
+
if (!preconditions$1.messages) {
|
|
1494
|
+
preconditions$1.messages = [];
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
preconditions$1.messages.push(`${preconditions$1.messages.length + 1}. ${msg}`);
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
let text = "";
|
|
1502
|
+
|
|
1503
|
+
if (preconditions$1.messages) {
|
|
1504
|
+
text = `*** 由于以下原因,${preconditions$1.appName} 尚不能正常提供服务:\n\n` + preconditions$1.messages.join("\n\n") + "\n\n*** 请检查以上各项条件,修复后重新启动服务";
|
|
1505
|
+
console.log("\n" + text); // for (let i = 0; i < preconditions.messages.length; i++) {
|
|
1506
|
+
// console.log(preconditions.messages[i]);
|
|
1507
|
+
// }
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
if (!(await fs.async_exists("../log"))) {
|
|
1511
|
+
await fs.async_mkdir("../log");
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
await fs.async_writeFile(`../log/preconditions`, text);
|
|
1515
|
+
return !text;
|
|
1516
|
+
} // 检查器
|
|
1517
|
+
|
|
1518
|
+
/**
|
|
1519
|
+
* 检查本机外部配置文件版本
|
|
1520
|
+
*/
|
|
1521
|
+
|
|
1522
|
+
|
|
1523
|
+
static async checkExEnvSetting() {
|
|
1524
|
+
return await checkExEnvSetting();
|
|
1432
1525
|
}
|
|
1526
|
+
/**
|
|
1527
|
+
* redis 可用性检查
|
|
1528
|
+
*/
|
|
1529
|
+
|
|
1530
|
+
|
|
1531
|
+
static async checkRedis() {
|
|
1532
|
+
return await checkRedis();
|
|
1533
|
+
} // /**
|
|
1534
|
+
// * 数据库可用性及业务数据版本版本检查
|
|
1535
|
+
// */
|
|
1536
|
+
// static async checkDatabase() {
|
|
1537
|
+
// return await checkDatabase();
|
|
1538
|
+
// }
|
|
1539
|
+
|
|
1540
|
+
|
|
1433
1541
|
}
|
|
1434
1542
|
|
|
1435
|
-
|
|
1543
|
+
async function createEnvSettingFile(fileName) {
|
|
1544
|
+
try {
|
|
1545
|
+
const cfgText = await ppUtil$1.doCreateEnvSettingFile(fileName);
|
|
1546
|
+
|
|
1547
|
+
if (cfgText.indexOf(`cfgVersion: "${conf._envSetting.cfgVersion}"`) > 0) {
|
|
1548
|
+
console.log(`\n*** 已創建新的本機環境配置文件\n${fileName}\n请檢查各項內容是否正确(此项更新服务可正常运行)\n\n`);
|
|
1549
|
+
return "";
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
return `已創建新的本機環境配置文件\n${fileName}\n请檢查並正確設置各項內容,然後將配置版本號修改為${conf._envSettings.default.cfgVersion}`;
|
|
1553
|
+
} catch (e) {
|
|
1554
|
+
return `創建本機環境配置文件失敗\n《${fileName}》\n${err.ERROR(e).message}`;
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
async function checkExEnvSetting() {
|
|
1559
|
+
if (!conf._envSetting.localSettingImported) {
|
|
1560
|
+
let fileName = conf.getEnvSettingFileName();
|
|
1561
|
+
|
|
1562
|
+
if (!fs.existsSync(fileName)) {
|
|
1563
|
+
return await createEnvSettingFile(fileName);
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
let envSettingEx = commonjsRequire(fileName);
|
|
1567
|
+
|
|
1568
|
+
if (envSettingEx.cfgVersion !== conf._envSettings.default.cfgVersion) {
|
|
1569
|
+
if (envSettingEx.cfgVersion !== "0.0") {
|
|
1570
|
+
let fp = path.parse(fileName);
|
|
1571
|
+
let newFileName = path.join(fp.dir, fp.name + envSettingEx.cfgVersion + fp.ext);
|
|
1572
|
+
|
|
1573
|
+
if (!fs.existsSync(newFileName)) {
|
|
1574
|
+
fs.renameSync(fileName, newFileName);
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
return await createEnvSettingFile(fileName);
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
} // async function checkDatabase() {
|
|
1582
|
+
// return dbCheck.check().then(info => info.updateMessage);
|
|
1583
|
+
// }
|
|
1584
|
+
|
|
1585
|
+
|
|
1586
|
+
async function checkRedis() {
|
|
1587
|
+
if (!conf.redis.enabled) return "";
|
|
1588
|
+
const redis = redisUtil.newClient("io"); // console.log(`checkRedis begin:`);
|
|
1589
|
+
|
|
1590
|
+
await ppUtil$1.wait(15 * 1000, param => {
|
|
1591
|
+
if (redis.ready && redis.connected || redis.error) {
|
|
1592
|
+
return true;
|
|
1593
|
+
}
|
|
1594
|
+
}).catch(e => {
|
|
1595
|
+
redis.error = e;
|
|
1596
|
+
});
|
|
1597
|
+
|
|
1598
|
+
if (redis.error) {
|
|
1599
|
+
console.log(`未能成功連接 redis: ${redis.error},請檢查相關設置`);
|
|
1600
|
+
return `未能成功連接 redis: ${redis.error},請檢查相關設置`;
|
|
1601
|
+
} // console.log(`checkRedis done:`);
|
|
1602
|
+
// console.log(redis);
|
|
1603
|
+
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
var ppPrecond = preconditions$1;
|
|
1436
1607
|
|
|
1437
1608
|
const apiUtil = ppUtilApi;
|
|
1609
|
+
const preconditions = ppPrecond;
|
|
1438
1610
|
var ppUtil = {
|
|
1439
|
-
apiUtil
|
|
1611
|
+
apiUtil,
|
|
1612
|
+
preconditions
|
|
1440
1613
|
};
|
|
1441
1614
|
|
|
1442
1615
|
module.exports = ppUtil;
|