@fastcar/core 0.2.38

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.
Files changed (239) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +379 -0
  3. package/annotation.d.ts +157 -0
  4. package/db.d.ts +272 -0
  5. package/index.d.ts +262 -0
  6. package/package.json +53 -0
  7. package/src/FastCarApplication.ts +761 -0
  8. package/src/annotation/Application.ts +46 -0
  9. package/src/annotation/ExceptionMonitor.ts +15 -0
  10. package/src/annotation/bind/AddRequireModule.ts +18 -0
  11. package/src/annotation/bind/AliasInjection.ts +24 -0
  12. package/src/annotation/bind/Autowired.ts +11 -0
  13. package/src/annotation/bind/CallDependency.ts +24 -0
  14. package/src/annotation/data/DBType.ts +9 -0
  15. package/src/annotation/data/DS.ts +58 -0
  16. package/src/annotation/data/DSIndex.ts +7 -0
  17. package/src/annotation/data/Entity.ts +8 -0
  18. package/src/annotation/data/Field.ts +15 -0
  19. package/src/annotation/data/PrimaryKey.ts +7 -0
  20. package/src/annotation/data/SqlSession.ts +7 -0
  21. package/src/annotation/data/Table.ts +37 -0
  22. package/src/annotation/data/Transactional.ts +60 -0
  23. package/src/annotation/env/ApplicationSetting.ts +24 -0
  24. package/src/annotation/env/BaseFilePath.ts +12 -0
  25. package/src/annotation/env/BasePath.ts +12 -0
  26. package/src/annotation/env/ENV.ts +8 -0
  27. package/src/annotation/env/ResourcePath.ts +7 -0
  28. package/src/annotation/lifeCycle/AddLifeCycleItem.ts +25 -0
  29. package/src/annotation/lifeCycle/ApplicationDestory.ts +13 -0
  30. package/src/annotation/lifeCycle/ApplicationInit.ts +13 -0
  31. package/src/annotation/lifeCycle/ApplicationRunner.ts +5 -0
  32. package/src/annotation/lifeCycle/ApplicationStart.ts +23 -0
  33. package/src/annotation/lifeCycle/ApplicationStop.ts +18 -0
  34. package/src/annotation/property/Deprecate.ts +17 -0
  35. package/src/annotation/property/NotImplemented.ts +5 -0
  36. package/src/annotation/property/Override.ts +4 -0
  37. package/src/annotation/property/Readonly.ts +12 -0
  38. package/src/annotation/scan/ComponentInjection.ts +22 -0
  39. package/src/annotation/scan/ComponentScan.ts +7 -0
  40. package/src/annotation/scan/ComponentScanExclusion.ts +26 -0
  41. package/src/annotation/scan/Hotter.ts +6 -0
  42. package/src/annotation/stereotype/BeanName.ts +9 -0
  43. package/src/annotation/stereotype/Component.ts +6 -0
  44. package/src/annotation/stereotype/Configure.ts +12 -0
  45. package/src/annotation/stereotype/Controller.ts +7 -0
  46. package/src/annotation/stereotype/Injection.ts +10 -0
  47. package/src/annotation/stereotype/Log.ts +17 -0
  48. package/src/annotation/stereotype/Repository.ts +7 -0
  49. package/src/annotation/stereotype/Service.ts +7 -0
  50. package/src/annotation/valid/AddChildValid.ts +61 -0
  51. package/src/annotation/valid/DefaultVal.ts +8 -0
  52. package/src/annotation/valid/NotNull.ts +7 -0
  53. package/src/annotation/valid/Rule.ts +104 -0
  54. package/src/annotation/valid/Size.ts +13 -0
  55. package/src/annotation/valid/Type.ts +8 -0
  56. package/src/annotation/valid/ValidCustom.ts +21 -0
  57. package/src/annotation/valid/ValidForm.ts +146 -0
  58. package/src/annotation.ts +103 -0
  59. package/src/config/ApplicationConfig.ts +5 -0
  60. package/src/config/SysConfig.ts +28 -0
  61. package/src/constant/AppStatusEnum.ts +5 -0
  62. package/src/constant/BootPriority.ts +7 -0
  63. package/src/constant/CommonConstant.ts +14 -0
  64. package/src/constant/ComponentKind.ts +7 -0
  65. package/src/constant/DataTypes.ts +15 -0
  66. package/src/constant/FastCarMetaData.ts +25 -0
  67. package/src/constant/LifeCycleModule.ts +5 -0
  68. package/src/db.ts +182 -0
  69. package/src/index.ts +11 -0
  70. package/src/interface/ApplicationHook.ts +9 -0
  71. package/src/interface/ApplicationRunnerService.ts +3 -0
  72. package/src/interface/DataSourceManager.ts +14 -0
  73. package/src/interface/Logger.ts +9 -0
  74. package/src/model/BaseMapper.ts +115 -0
  75. package/src/model/DataMap.ts +103 -0
  76. package/src/model/FormRuleModel.ts +23 -0
  77. package/src/model/ValidError.ts +1 -0
  78. package/src/model/WinstonLogger.ts +119 -0
  79. package/src/type/ComponentDesc.ts +5 -0
  80. package/src/type/DesignMeta.ts +11 -0
  81. package/src/type/FileHotterDesc.ts +4 -0
  82. package/src/type/MapperType.ts +8 -0
  83. package/src/type/ProcessType.ts +12 -0
  84. package/src/type/SqlError.ts +3 -0
  85. package/src/type/WinstonLoggerType.ts +18 -0
  86. package/src/utils/ClassLoader.ts +72 -0
  87. package/src/utils/ClassUtils.ts +38 -0
  88. package/src/utils/CryptoUtil.ts +106 -0
  89. package/src/utils/DataFormat.ts +97 -0
  90. package/src/utils/DateUtil.ts +85 -0
  91. package/src/utils/FileUtil.ts +172 -0
  92. package/src/utils/FormatStr.ts +13 -0
  93. package/src/utils/Mix.ts +69 -0
  94. package/src/utils/ReflectUtil.ts +22 -0
  95. package/src/utils/TypeUtil.ts +56 -0
  96. package/src/utils/ValidationUtil.ts +138 -0
  97. package/src/utils.ts +13 -0
  98. package/target/FastCarApplication.js +661 -0
  99. package/target/annotation/AddRequireModule.js +21 -0
  100. package/target/annotation/Application.js +45 -0
  101. package/target/annotation/Autowired.js +15 -0
  102. package/target/annotation/Calldependency.js +18 -0
  103. package/target/annotation/ExceptionMonitor.js +16 -0
  104. package/target/annotation/bind/AddRequireModule.js +21 -0
  105. package/target/annotation/bind/AliasInjection.js +23 -0
  106. package/target/annotation/bind/Autowired.js +13 -0
  107. package/target/annotation/bind/CallDependency.js +23 -0
  108. package/target/annotation/data/DBType.js +11 -0
  109. package/target/annotation/data/DS.js +54 -0
  110. package/target/annotation/data/DSIndex.js +9 -0
  111. package/target/annotation/data/Entity.js +10 -0
  112. package/target/annotation/data/Field.js +18 -0
  113. package/target/annotation/data/PrimaryKey.js +9 -0
  114. package/target/annotation/data/SqlSession.js +9 -0
  115. package/target/annotation/data/Table.js +34 -0
  116. package/target/annotation/data/Transactional.js +52 -0
  117. package/target/annotation/env/ApplicationSetting.js +25 -0
  118. package/target/annotation/env/BaseFilePath.js +14 -0
  119. package/target/annotation/env/BasePath.js +14 -0
  120. package/target/annotation/env/ENV.js +10 -0
  121. package/target/annotation/env/ResourcePath.js +9 -0
  122. package/target/annotation/lifeCycle/AddLifeCycleItem.js +18 -0
  123. package/target/annotation/lifeCycle/ApplicationDestory.js +15 -0
  124. package/target/annotation/lifeCycle/ApplicationInit.js +15 -0
  125. package/target/annotation/lifeCycle/ApplicationRunner.js +7 -0
  126. package/target/annotation/lifeCycle/ApplicationStart.js +24 -0
  127. package/target/annotation/lifeCycle/ApplicationStop.js +20 -0
  128. package/target/annotation/property/Deprecate.js +19 -0
  129. package/target/annotation/property/NotImplemented.js +8 -0
  130. package/target/annotation/property/Override.js +7 -0
  131. package/target/annotation/property/Readonly.js +16 -0
  132. package/target/annotation/scan/ComponentInjection.js +21 -0
  133. package/target/annotation/scan/ComponentScan.js +9 -0
  134. package/target/annotation/scan/ComponentScanExclusion.js +25 -0
  135. package/target/annotation/scan/Hotter.js +8 -0
  136. package/target/annotation/stereotype/BeanName.js +11 -0
  137. package/target/annotation/stereotype/Component.js +8 -0
  138. package/target/annotation/stereotype/Configure.js +14 -0
  139. package/target/annotation/stereotype/Controller.js +9 -0
  140. package/target/annotation/stereotype/Injection.js +12 -0
  141. package/target/annotation/stereotype/Log.js +16 -0
  142. package/target/annotation/stereotype/Repository.js +9 -0
  143. package/target/annotation/stereotype/Service.js +9 -0
  144. package/target/annotation/valid/AddChildValid.js +56 -0
  145. package/target/annotation/valid/DefaultVal.js +10 -0
  146. package/target/annotation/valid/NotNull.js +8 -0
  147. package/target/annotation/valid/Rule.js +100 -0
  148. package/target/annotation/valid/Size.js +10 -0
  149. package/target/annotation/valid/Type.js +10 -0
  150. package/target/annotation/valid/ValidCustom.js +17 -0
  151. package/target/annotation/valid/ValidForm.js +131 -0
  152. package/target/annotation.js +101 -0
  153. package/target/config/ApplicationConfig.js +2 -0
  154. package/target/config/SysConfig.js +19 -0
  155. package/target/constant/AppStatusEnum.js +9 -0
  156. package/target/constant/BootPriority.js +11 -0
  157. package/target/constant/CommonConstant.js +16 -0
  158. package/target/constant/ComponentKind.js +11 -0
  159. package/target/constant/DataTypes.js +19 -0
  160. package/target/constant/FastCarMetaData.js +29 -0
  161. package/target/constant/LifeCycleModule.js +9 -0
  162. package/target/db.js +46 -0
  163. package/target/index.js +21 -0
  164. package/target/interface/ApplicationHook.js +2 -0
  165. package/target/interface/ApplicationRunnerService.js +2 -0
  166. package/target/interface/DataSourceManager.js +2 -0
  167. package/target/interface/Logger.js +5 -0
  168. package/target/model/BaseMapper.js +98 -0
  169. package/target/model/DataMap.js +87 -0
  170. package/target/model/FormRuleModel.js +2 -0
  171. package/target/model/ValidError.js +5 -0
  172. package/target/model/WinstonLogger.js +95 -0
  173. package/target/type/ComponentDesc.js +2 -0
  174. package/target/type/DesignMeta.js +15 -0
  175. package/target/type/FileHotterDesc.js +2 -0
  176. package/target/type/MapperType.js +2 -0
  177. package/target/type/ProcessType.js +2 -0
  178. package/target/type/SqlError.js +5 -0
  179. package/target/type/WinstonLoggerType.js +9 -0
  180. package/target/utils/ClassUtils.js +35 -0
  181. package/target/utils/CryptoUtil.js +84 -0
  182. package/target/utils/DataFormat.js +84 -0
  183. package/target/utils/DateUtil.js +71 -0
  184. package/target/utils/FileUtil.js +153 -0
  185. package/target/utils/FormatStr.js +14 -0
  186. package/target/utils/Mix.js +62 -0
  187. package/target/utils/ReflectUtil.js +22 -0
  188. package/target/utils/TypeUtil.js +47 -0
  189. package/target/utils/ValidationUtil.js +118 -0
  190. package/target/utils/classLoader.js +65 -0
  191. package/target/utils.js +23 -0
  192. package/test/example/logs/app.log +0 -0
  193. package/test/example/logs/logger.log +12 -0
  194. package/test/example/logs/logger1.log +12 -0
  195. package/test/example/logs/logger2.log +12 -0
  196. package/test/example/logs/logger3.log +12 -0
  197. package/test/example/logs/logger4.log +12 -0
  198. package/test/example/logs/logger5.log +6 -0
  199. package/test/example/logs/sys.log +7 -0
  200. package/test/example/logs/sys1.log +14 -0
  201. package/test/example/logs/sys10.log +12 -0
  202. package/test/example/logs/sys11.log +5 -0
  203. package/test/example/logs/sys13.log +5 -0
  204. package/test/example/logs/sys2.log +3 -0
  205. package/test/example/logs/sys4.log +15 -0
  206. package/test/example/logs/sys5.log +9 -0
  207. package/test/example/logs/sys6.log +10 -0
  208. package/test/example/logs/sys7.log +15 -0
  209. package/test/example/logs/sys8.log +11 -0
  210. package/test/example/logs/sys9.log +10 -0
  211. package/test/example/resource/application-test.yml +0 -0
  212. package/test/example/resource/application.yml +7 -0
  213. package/test/example/resource/evnconfig-test.yml +1 -0
  214. package/test/example/resource/hello.yml +1 -0
  215. package/test/example/simple/app.ts +99 -0
  216. package/test/example/simple/component/StartRunner.ts +10 -0
  217. package/test/example/simple/component/StopRunner.ts +14 -0
  218. package/test/example/simple/config/EnvConfig.ts +6 -0
  219. package/test/example/simple/config/HelloConfig.ts +8 -0
  220. package/test/example/simple/controller/AliasController.ts +6 -0
  221. package/test/example/simple/controller/HelloController.ts +39 -0
  222. package/test/example/simple/controller/NotFoundController.ts +20 -0
  223. package/test/example/simple/service/CallService.ts +11 -0
  224. package/test/example/simple/service/HelloService.ts +10 -0
  225. package/test/example/simple/service/LogService.ts +19 -0
  226. package/test/logs/logger.log +0 -0
  227. package/test/logs/sys.log +227 -0
  228. package/test/multi/app.ts +15 -0
  229. package/test/multi/service/aService.ts +16 -0
  230. package/test/multi/service/bService.ts +18 -0
  231. package/test/multi/service/cService.ts +21 -0
  232. package/test/unit/dataMap-test.ts +48 -0
  233. package/test/unit/decorators-test.ts +38 -0
  234. package/test/unit/ds-test.ts +33 -0
  235. package/test/unit/logs/sys.log +24 -0
  236. package/test/unit/reflectMetadata-test.ts +15 -0
  237. package/test/unit/valid-test.ts +65 -0
  238. package/test/unit/winston-test.ts +15 -0
  239. package/utils.d.ts +166 -0
@@ -0,0 +1,18 @@
1
+ export type WinstonLoggerType = {
2
+ consoleLevel: string;
3
+ fileLevel: string;
4
+ rootPath: string; //日志路径
5
+ maxsize: number; //字节
6
+ maxFiles: number; //最大文件数
7
+ printConsole: boolean; //是否在控制台模式
8
+ };
9
+
10
+ export const ColorLevelType = {
11
+ info: [32, 39], //green
12
+
13
+ debug: [36, 39], // cyan
14
+
15
+ warn: [33, 39], //yellow
16
+
17
+ error: [91, 39], //red
18
+ };
@@ -0,0 +1,72 @@
1
+ import TypeUtil from "./TypeUtil";
2
+ import FileUtil from "./FileUtil";
3
+ import * as fs from "fs";
4
+ import * as path from "path";
5
+
6
+ export default class ClassLoader {
7
+ /***
8
+ * @version 1.0 加载模块
9
+ * @version 1.1 新增是否强制重载模块
10
+ *
11
+ */
12
+ static loadModule(filePath: string, force: boolean = false): Map<string, any> | null {
13
+ //校验后缀名是否为js或者ts
14
+ if (!TypeUtil.isTSORJS(filePath)) {
15
+ return null;
16
+ }
17
+
18
+ //避免重复加载或者想要重新进行挂载
19
+ if (Reflect.has(require.cache, filePath)) {
20
+ if (force) {
21
+ Reflect.deleteProperty(require.cache, filePath);
22
+ }
23
+ }
24
+
25
+ //可能不止一个方法
26
+ const modulesMap = new Map<string, Object>();
27
+ //进行方法加载
28
+ let moduleClass = require(filePath);
29
+ let keys = Object.keys(moduleClass);
30
+ let fileName = FileUtil.getFileName(filePath);
31
+
32
+ keys.forEach((key) => {
33
+ let instance = moduleClass[key];
34
+
35
+ if (TypeUtil.isFunction(instance)) {
36
+ modulesMap.set(instance.name, instance);
37
+ return;
38
+ }
39
+
40
+ if (TypeUtil.isObject(instance)) {
41
+ modulesMap.set(fileName, instance);
42
+ }
43
+ });
44
+
45
+ return modulesMap;
46
+ }
47
+
48
+ /**
49
+ * @version 1.0 监听某个文件或者文件夹
50
+ */
51
+ static watchServices(fp: string, context: any, eventName: string = "reload"): boolean {
52
+ if (typeof context.emit != "function") {
53
+ return false;
54
+ }
55
+
56
+ const currStats = fs.statSync(fp);
57
+ let fileFlag = currStats.isFile();
58
+
59
+ //添加热更方法
60
+ fs.watch(fp, function (event, filename) {
61
+ if (event === "change") {
62
+ if (!fileFlag) {
63
+ context.emit(eventName, path.join(fp, filename));
64
+ } else {
65
+ context.emit(eventName, fp);
66
+ }
67
+ }
68
+ });
69
+
70
+ return true;
71
+ }
72
+ }
@@ -0,0 +1,38 @@
1
+ export default class ClassUtils {
2
+ //获取一个类所有的proto属性 采用递归的形式
3
+ static getProtoType(t: any): (string | symbol)[] {
4
+ if (!t?.prototype) {
5
+ return [];
6
+ }
7
+ let keys: (string | symbol)[] = Reflect.ownKeys(t?.prototype).map((item) => {
8
+ return item;
9
+ });
10
+
11
+ let parentObj = Reflect.getPrototypeOf(t);
12
+ if (!parentObj || !Reflect.has(parentObj, "prototype")) {
13
+ return keys;
14
+ }
15
+
16
+ let parentKeys = ClassUtils.getProtoType(parentObj);
17
+ let s = new Set([...keys, ...parentKeys]);
18
+
19
+ return [...s];
20
+ }
21
+
22
+ static getProtoDesc(t: any, key: string | symbol): PropertyDescriptor | null {
23
+ if (!t?.prototype) {
24
+ return null;
25
+ }
26
+ let desc = Object.getOwnPropertyDescriptor(t.prototype, key);
27
+ if (!!desc) {
28
+ return desc;
29
+ }
30
+
31
+ let parentObj = Reflect.getPrototypeOf(t);
32
+ if (!parentObj || !Reflect.has(parentObj, "prototype")) {
33
+ return null;
34
+ }
35
+
36
+ return ClassUtils.getProtoDesc(parentObj, key);
37
+ }
38
+ }
@@ -0,0 +1,106 @@
1
+ import * as crypto from "crypto";
2
+ import { BinaryToTextEncoding } from "crypto";
3
+
4
+ const enum serects {
5
+ aes = "aes-256-cbc",
6
+ sha256 = "sha256",
7
+ }
8
+
9
+ export default class CryptoUtil {
10
+ static aesDecode(cryptkey: string, iv: string, secretdata: string, aesType: string = serects.aes): string {
11
+ let decipher = crypto.createDecipheriv(aesType, cryptkey, iv);
12
+ let decoded = decipher.update(secretdata, "base64", "utf8");
13
+
14
+ decoded += decipher.final("utf8");
15
+ return decoded;
16
+ }
17
+
18
+ static aesEncode(cryptkey: string, iv: string, cleardata: string, aesType: string = serects.aes): string {
19
+ let encipher = crypto.createCipheriv(serects.aes, cryptkey, iv);
20
+ let encoded = encipher.update(cleardata, "utf8", "base64");
21
+
22
+ encoded += encipher.final("base64");
23
+ return encoded;
24
+ }
25
+
26
+ static shaEncode(cryptkey: string, data: string): string {
27
+ let hash = crypto.createHmac("sha256", cryptkey);
28
+ return hash.update(data).digest("base64");
29
+ }
30
+
31
+ static gcmEncrypt(password: string, msg: string): string | null {
32
+ try {
33
+ let pwd = Buffer.from(password, "hex");
34
+ let iv = crypto.randomBytes(12);
35
+ let cipher = crypto.createCipheriv("aes-128-gcm", pwd, iv);
36
+
37
+ //加密
38
+ let enc = cipher.update(msg, "utf8", "base64");
39
+ enc += cipher.final("base64");
40
+
41
+ //cipher.getAuthTag() 方法返回一个 Buffer,它包含已从给定数据计算后的认证标签。
42
+ //cipher.getAuthTag() 方法只能在使用 cipher.final() 之后调用 这里返回的是一个十六进制后的数组
43
+ let tags = cipher.getAuthTag();
44
+ let encStr = Buffer.from(enc, "base64");
45
+
46
+ //由于和java对应的AES/GCM/PKCS5Padding模式对应 所以采用这个拼接
47
+ let totalLength = iv.length + encStr.length + tags.length;
48
+ let bufferMsg = Buffer.concat([iv, encStr, tags], totalLength);
49
+
50
+ return bufferMsg.toString("base64");
51
+ } catch (e) {
52
+ console.log("Encrypt is error", e);
53
+ return null;
54
+ }
55
+ }
56
+
57
+ static gcmDecrypt(password: string, serect: string): string | null {
58
+ try {
59
+ let tmpSerect = Buffer.from(serect, "base64");
60
+ let pwd = Buffer.from(password, "hex");
61
+
62
+ //读取数组
63
+ let iv = tmpSerect.slice(0, 12);
64
+ let cipher = crypto.createDecipheriv("aes-128-gcm", pwd, iv);
65
+
66
+ //这边的数据为 去除头的iv12位和尾部的tags的16位
67
+ let msg = cipher.update(tmpSerect.slice(12, tmpSerect.length - 16));
68
+
69
+ return msg.toString("utf8");
70
+ } catch (e) {
71
+ console.log("Decrypt is error", e);
72
+ return null;
73
+ }
74
+ }
75
+
76
+ static sha256Encode(text: string, serect: string = crypto.randomBytes(32).toString("hex"), encoding: BinaryToTextEncoding = "base64"): { salt: string; msg: string } {
77
+ let msg = crypto
78
+ .createHmac("sha256", serect)
79
+ .update(text)
80
+ .digest(encoding);
81
+
82
+ return {
83
+ salt: serect,
84
+ msg: msg,
85
+ };
86
+ }
87
+
88
+ static sha256EncodeContent(str: string, encoding: BinaryToTextEncoding = "base64"): string {
89
+ let msg = crypto
90
+ .createHash("sha256")
91
+ .update(str)
92
+ .digest(encoding);
93
+
94
+ return msg;
95
+ }
96
+
97
+ static sha256Very(msg: string, serect: string, encodeMsg: string, encoding: BinaryToTextEncoding = "base64"): boolean {
98
+ let result = this.sha256Encode(msg, serect, encoding);
99
+
100
+ return result.msg === encodeMsg;
101
+ }
102
+
103
+ static getHashStr(num: number = 16): string {
104
+ return crypto.randomBytes(num).toString("hex");
105
+ }
106
+ }
@@ -0,0 +1,97 @@
1
+ /***
2
+ * @version 1.0 数据格式处理
3
+ */
4
+ class DataFormat {
5
+ static formatNumber(value: any, type: string): number | null {
6
+ if (type === "int") {
7
+ return parseInt(value);
8
+ }
9
+
10
+ if (type === "float" || type == "number") {
11
+ return parseFloat(value);
12
+ }
13
+
14
+ return null;
15
+ }
16
+
17
+ static formatString(value: any): string | null {
18
+ if (typeof value != "string") {
19
+ if (typeof value == "number") {
20
+ return value + "";
21
+ }
22
+ if (typeof value == "object") {
23
+ if (Reflect.has(value, "toString")) {
24
+ return value.toString();
25
+ }
26
+ }
27
+ return null;
28
+ }
29
+
30
+ return value;
31
+ }
32
+
33
+ static formatBoolean(value: any): boolean {
34
+ if (typeof value == "string") {
35
+ if (value == "false") {
36
+ return false;
37
+ }
38
+ }
39
+
40
+ return !!value;
41
+ }
42
+
43
+ static formatArray(value: any[], type: string): any[] {
44
+ if (type.startsWith("array")) {
45
+ if (typeof value === "string") {
46
+ value = JSON.parse(value);
47
+ }
48
+
49
+ if (Array.isArray(value)) {
50
+ let ntype = type.replace(/array/, "");
51
+ value = value.map((item) => {
52
+ return DataFormat.formatValue(item, ntype);
53
+ });
54
+
55
+ return value;
56
+ }
57
+ }
58
+
59
+ return [];
60
+ }
61
+
62
+ static formatDate(value: any): Date {
63
+ if (value instanceof Date) {
64
+ return value;
65
+ }
66
+
67
+ return new Date(value);
68
+ }
69
+
70
+ static formatValue(value: any, type: string): any {
71
+ if (type.startsWith("array")) {
72
+ return DataFormat.formatArray(value, type);
73
+ }
74
+
75
+ switch (type) {
76
+ case "string": {
77
+ return DataFormat.formatString(value);
78
+ }
79
+ case "boolean": {
80
+ return DataFormat.formatBoolean(value);
81
+ }
82
+ case "int":
83
+ case "float":
84
+ case "number": {
85
+ return DataFormat.formatNumber(value, type);
86
+ }
87
+ case "date": {
88
+ return DataFormat.formatDate(value);
89
+ }
90
+ default: {
91
+ return value;
92
+ }
93
+ }
94
+ }
95
+ }
96
+
97
+ export default DataFormat;
@@ -0,0 +1,85 @@
1
+ export default class DateUtil {
2
+ static twoDigits(num: number): string {
3
+ return num.toString().padStart(2, "0");
4
+ }
5
+
6
+ //返回年月日时分秒
7
+ static toDateDesc(datetime: number | string | Date = Date.now()) {
8
+ let dataC = new Date(datetime);
9
+ return {
10
+ YYYY: DateUtil.twoDigits(dataC.getFullYear()),
11
+ MM: DateUtil.twoDigits(dataC.getMonth() + 1),
12
+ DD: DateUtil.twoDigits(dataC.getDate()),
13
+ hh: DateUtil.twoDigits(dataC.getHours()),
14
+ mm: DateUtil.twoDigits(dataC.getMinutes()),
15
+ ss: DateUtil.twoDigits(dataC.getSeconds()),
16
+ ms: DateUtil.twoDigits(dataC.getMilliseconds()),
17
+ };
18
+ }
19
+
20
+ static toDay(datetime: number | string | Date = Date.now(), format: string = "YYYY-MM-DD") {
21
+ let desc = DateUtil.toDateDesc(datetime);
22
+ let ymd = format.replace(/YYYY/, desc.YYYY).replace(/MM/, desc.MM).replace(/DD/, desc.DD);
23
+ return ymd;
24
+ }
25
+
26
+ static toHms(datetime: number | string | Date = Date.now(), format: string = "hh:mm:ss") {
27
+ let desc = DateUtil.toDateDesc(datetime);
28
+ let hms = format.replace(/hh/, desc.hh).replace(/mm/, desc.mm).replace(/ss/, desc.ss);
29
+
30
+ return hms;
31
+ }
32
+
33
+ static toDateTime(datetime: number | string | Date = Date.now(), format: string = "YYYY-MM-DD hh:mm:ss"): string {
34
+ let desc = DateUtil.toDateDesc(datetime);
35
+ let str = format.replace(/YYYY/, desc.YYYY).replace(/MM/, desc.MM).replace(/DD/, desc.DD).replace(/hh/, desc.hh).replace(/mm/, desc.mm).replace(/ss/, desc.ss);
36
+
37
+ return str;
38
+ }
39
+
40
+ static toDateTimeMS(datetime: number | string | Date = Date.now(), format: string = "YYYY-MM-DD hh:mm:ss.sss"): string {
41
+ let desc = DateUtil.toDateDesc(datetime);
42
+ let str = format.replace(/YYYY/, desc.YYYY).replace(/MM/, desc.MM).replace(/DD/, desc.DD).replace(/hh/, desc.hh).replace(/mm/, desc.mm).replace(/ss/, desc.ss).replace(/sss/, desc.ms);
43
+
44
+ return str;
45
+ }
46
+
47
+ static toCutDown(datetime: number, format: string = "hh:mm:ss"): string {
48
+ let hours = Math.floor(datetime / 60 / 60);
49
+ let minutes = Math.floor((datetime - hours * 60 * 60) / 60);
50
+ let seconds = datetime - (hours * 60 + minutes) * 60;
51
+
52
+ let str = format.replace(/hh/, hours.toString()).replace(/mm/, minutes.toString()).replace(/ss/, seconds.toString());
53
+
54
+ return str;
55
+ }
56
+
57
+ static getTimeStr(datetime: number): string {
58
+ let days = datetime / (1000 * 60 * 60 * 24);
59
+ if (days >= 1) {
60
+ return `${days.toFixed(2)}d`;
61
+ }
62
+
63
+ let hours = datetime / (1000 * 60 * 60);
64
+ if (hours >= 1) {
65
+ return `${hours.toFixed(2)}h`;
66
+ }
67
+
68
+ let minutes = datetime / (1000 * 60);
69
+ if (minutes >= 1) {
70
+ return `${minutes.toFixed(2)}m`;
71
+ }
72
+
73
+ let seconds = datetime / 1000;
74
+ return `${Math.floor(seconds)}s`;
75
+ }
76
+
77
+ static getDateTime(datetimeStr?: string | number): number {
78
+ if (datetimeStr) {
79
+ let dataC = new Date(datetimeStr);
80
+ return dataC.getTime();
81
+ }
82
+
83
+ return Date.now();
84
+ }
85
+ }
@@ -0,0 +1,172 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+ import * as yaml from "yaml";
4
+ import { SYSConfig, SYSDefaultConfig } from "../config/SysConfig";
5
+ import { CommonConstant, FileResSuffix } from "../constant/CommonConstant";
6
+ import MixTool from "./Mix";
7
+
8
+ const fileResSuffix = ["json", "yml", "js"]; //文件资源后缀名
9
+ const bytesUnit = ["B", "K", "M", "G"];
10
+
11
+ export default class FileUtil {
12
+ /***
13
+ * @version 1.0 获取文件路径
14
+ *
15
+ */
16
+ static getFilePathList(filePath: string): string[] {
17
+ let pathList: string[] = Array.of();
18
+
19
+ let existFlag = fs.existsSync(filePath);
20
+ if (!existFlag) {
21
+ return pathList;
22
+ }
23
+
24
+ let currStats = fs.statSync(filePath);
25
+ let cfile = currStats.isFile();
26
+ let cdir = currStats.isDirectory();
27
+
28
+ if (!cdir && !cfile) {
29
+ return pathList;
30
+ }
31
+
32
+ if (cfile) {
33
+ pathList.push(filePath);
34
+ return pathList;
35
+ }
36
+
37
+ let childPaths = fs.readdirSync(filePath);
38
+ for (let c of childPaths) {
39
+ let fullPath = path.join(filePath, c);
40
+ let tmpStats = fs.statSync(fullPath);
41
+
42
+ if (tmpStats.isDirectory()) {
43
+ let childPaths = FileUtil.getFilePathList(fullPath);
44
+ if (childPaths.length > 0) {
45
+ pathList = [...pathList, ...childPaths];
46
+ }
47
+ } else if (tmpStats.isFile()) {
48
+ pathList.push(fullPath);
49
+ }
50
+ }
51
+
52
+ return pathList;
53
+ }
54
+
55
+ /***
56
+ * @version 1.0 获取后缀名
57
+ *
58
+ */
59
+ static getSuffix(filePath: string): string {
60
+ let lastIndex = filePath.lastIndexOf(".") + 1;
61
+ if (lastIndex <= 0) {
62
+ return "";
63
+ }
64
+ let suffix = filePath.substring(lastIndex);
65
+ return suffix;
66
+ }
67
+
68
+ /***
69
+ * @version 1.0 获取文件的文件名
70
+ */
71
+ static getFileName(filePath: string): string {
72
+ let pList = filePath.split(path.sep);
73
+ let lastPath = pList[pList.length - 1];
74
+ let lastName = lastPath.split(".");
75
+
76
+ return lastName[0];
77
+ }
78
+
79
+ //加载配置文件
80
+ static getResource(fp: string): object | null {
81
+ let currSuffix = FileUtil.getSuffix(fp);
82
+ if (!fileResSuffix.includes(currSuffix)) {
83
+ return null;
84
+ }
85
+
86
+ if (fs.existsSync(fp)) {
87
+ let currStats = fs.statSync(fp);
88
+ if (currStats.isFile()) {
89
+ //进行解析
90
+ let content = fs.readFileSync(fp, "utf-8");
91
+ switch (currSuffix) {
92
+ case "yml": {
93
+ return yaml.parse(content);
94
+ }
95
+ case "json": {
96
+ return JSON.parse(content);
97
+ }
98
+ case "js":
99
+ case "ts": {
100
+ if (Reflect.has(require.cache, fp)) {
101
+ Reflect.deleteProperty(require.cache, fp);
102
+ }
103
+
104
+ return require(fp);
105
+ }
106
+ default: {
107
+ return null;
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ return null;
114
+ }
115
+
116
+ //格式化字节大小
117
+ static formatBytes(n: number): string {
118
+ for (let i = 0; i < bytesUnit.length; i++) {
119
+ let nn = n / Math.pow(1024, i);
120
+ if (nn <= 1024) {
121
+ return `${nn.toFixed(2)}(${bytesUnit[i]})`;
122
+ }
123
+ }
124
+
125
+ let maxL = bytesUnit.length - 1;
126
+ return `${(n / Math.pow(1024, maxL)).toFixed(2)}(${bytesUnit[maxL]})`;
127
+ }
128
+
129
+ //获取加载配置项
130
+ static getApplicationConfig(resPath: string, configName: string, sysConfig: SYSConfig = SYSDefaultConfig): SYSConfig {
131
+ const replaceSetting = (property: string, fileContent: object) => {
132
+ let addConfig = Reflect.get(fileContent, property);
133
+ if (addConfig) {
134
+ let currConfig = Reflect.get(sysConfig, property);
135
+ Reflect.deleteProperty(fileContent, property);
136
+ if (CommonConstant.Settings == property) {
137
+ Object.keys(addConfig).forEach((key) => {
138
+ let afterConfig = addConfig[key];
139
+ let beforeConfig = sysConfig.settings.get(key);
140
+ if (beforeConfig) {
141
+ //对settings的属性进行覆盖
142
+ if (typeof beforeConfig == "object") {
143
+ afterConfig = Object.assign(beforeConfig, afterConfig);
144
+ }
145
+ }
146
+ sysConfig.settings.set(key, afterConfig);
147
+ });
148
+ } else {
149
+ Object.assign(currConfig, addConfig);
150
+ }
151
+ }
152
+ };
153
+
154
+ FileResSuffix.forEach((suffix) => {
155
+ let fileContent = FileUtil.getResource(path.join(resPath, `${configName}.${suffix}`));
156
+ if (fileContent) {
157
+ replaceSetting(CommonConstant.Settings, fileContent);
158
+
159
+ replaceSetting(CommonConstant.Application, fileContent);
160
+
161
+ //将application和sesstings进行删除
162
+ Reflect.deleteProperty(fileContent, CommonConstant.Application);
163
+ Reflect.deleteProperty(fileContent, CommonConstant.Settings);
164
+
165
+ //追加自定的属性
166
+ MixTool.copyProperties(sysConfig, fileContent);
167
+ }
168
+ });
169
+
170
+ return sysConfig;
171
+ }
172
+ }
@@ -0,0 +1,13 @@
1
+ export default class FormatStr {
2
+ static formatFirstToUp(str: string) {
3
+ return str.charAt(0).toUpperCase() + str.substring(1);
4
+ }
5
+
6
+ static formatFirstToLow(str: string) {
7
+ return str.charAt(0).toLowerCase() + str.substring(1);
8
+ }
9
+
10
+ static formatFirstToUpEnd(str: string) {
11
+ return str.charAt(0).toUpperCase() + str.substring(1).toLowerCase();
12
+ }
13
+ }
@@ -0,0 +1,69 @@
1
+ import ClassUtils from "./ClassUtils";
2
+
3
+ const BASETYPE = ["constructor", "prototype", "name"];
4
+
5
+ /***
6
+ * @version 1.0 混合多个类
7
+ *
8
+ */
9
+ export default class MixTool {
10
+ static mix(...mixins: any[]) {
11
+ class Mix {
12
+ constructor() {
13
+ for (let mixin of mixins) {
14
+ MixTool.copyProperties(this, new mixin()); // 拷贝实例属性
15
+ }
16
+ }
17
+ }
18
+
19
+ for (let mixin of mixins) {
20
+ MixTool.copyProperties(Mix, mixin); // 拷贝静态属性
21
+ MixTool.copyProperties(Mix.prototype, mixin.prototype); // 拷贝原型属性
22
+ }
23
+
24
+ return Mix;
25
+ }
26
+
27
+ static copyProperties(target: any, source: any) {
28
+ let keys = Reflect.ownKeys(source);
29
+
30
+ for (let key of keys) {
31
+ if (!BASETYPE.includes(key.toString())) {
32
+ let desc = Object.getOwnPropertyDescriptor(source, key);
33
+ if (desc) {
34
+ Object.defineProperty(target, key, desc);
35
+ }
36
+ }
37
+ }
38
+ }
39
+
40
+ //仅仅改变属性的值
41
+ static copPropertyValue(target: any, source: any) {
42
+ let keys = Reflect.ownKeys(source);
43
+
44
+ for (let key of keys) {
45
+ if (!BASETYPE.includes(key.toString())) {
46
+ let desc = Reflect.getOwnPropertyDescriptor(source, key);
47
+ if (!!desc) {
48
+ Reflect.defineProperty(target, key, desc);
49
+ }
50
+ }
51
+ }
52
+ }
53
+
54
+ //多个对象赋值
55
+ static assign(target: any, source: any) {
56
+ Object.assign(target, source);
57
+ let keys = ClassUtils.getProtoType(source);
58
+
59
+ keys.forEach((key) => {
60
+ let keyStr = key.toString();
61
+ if (!BASETYPE.includes(keyStr)) {
62
+ let desc = Object.getOwnPropertyDescriptor(source?.prototype, key);
63
+ if (desc) {
64
+ Reflect.defineProperty(target, key, desc);
65
+ }
66
+ }
67
+ });
68
+ }
69
+ }
@@ -0,0 +1,22 @@
1
+ import { FastCarMetaData } from "../constant/FastCarMetaData";
2
+ import FormatStr from "./FormatStr";
3
+
4
+ const SpecWords = ["Boolean", "Number", "String", "Object"];
5
+
6
+ export default class ReflectUtil {
7
+ static getNameByPropertyKey(target: any, propertyKey: string): string {
8
+ const designType = Reflect.getMetadata(FastCarMetaData.designType, target, propertyKey);
9
+ let key = "";
10
+ let name = "";
11
+ if (designType) {
12
+ name = designType.name;
13
+ key = Reflect.getMetadata(FastCarMetaData.InjectionUniqueKey, designType); //放入至原型中
14
+ }
15
+ //获取不到注入的值时默认为别名的值
16
+ if (!name || SpecWords.includes(name)) {
17
+ key = FormatStr.formatFirstToUp(propertyKey);
18
+ }
19
+
20
+ return key;
21
+ }
22
+ }