@gingkoo/base-server 0.0.1-alpha.0 → 0.0.1-alpha.2

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 (122) hide show
  1. package/README.md +7 -0
  2. package/app.js +36 -40
  3. package/backend/common/entity.js +55 -0
  4. package/backend/common/enum.js +247 -0
  5. package/backend/common/fss/index.js +18 -0
  6. package/backend/common/fss/routers/attachment.js +45 -0
  7. package/backend/common/fss/routers/download.js +43 -0
  8. package/backend/common/fss/routers/import_export.js +113 -0
  9. package/backend/common/fss/routers/resources.js +149 -0
  10. package/backend/common/fss/routers/upload.js +81 -0
  11. package/backend/common/fss/services/import_export.js +159 -0
  12. package/backend/common/fss/services/index.js +92 -0
  13. package/backend/common/fss/utils.js +39 -0
  14. package/backend/common/ginfo/config.js +5 -0
  15. package/backend/common/ginfo/index.js +62 -0
  16. package/backend/common/index_template.html +28 -0
  17. package/backend/common/logger/index.js +96 -0
  18. package/backend/common/mapping.js +98 -0
  19. package/backend/common/middleware/auth.js +97 -0
  20. package/backend/common/middleware/cors.js +13 -0
  21. package/backend/common/middleware/response.js +25 -0
  22. package/backend/common/page_engine.js +487 -0
  23. package/backend/common/schedule.js +45 -0
  24. package/backend/common/services/dataCombine.js +67 -0
  25. package/backend/common/services/dept.js +37 -0
  26. package/backend/common/services/dict.js +487 -0
  27. package/backend/common/services/email.js +49 -0
  28. package/backend/common/services/generalConfig.js +137 -0
  29. package/backend/common/services/login.js +18 -0
  30. package/backend/common/services/notice.js +260 -0
  31. package/backend/common/services/permission.js +500 -0
  32. package/backend/common/services/roles.js +57 -0
  33. package/backend/common/services/send-notice.js +86 -0
  34. package/backend/common/services/task.js +259 -0
  35. package/backend/common/services/user.js +673 -0
  36. package/backend/common/socket.js +18 -0
  37. package/backend/common/sse/index.js +81 -0
  38. package/backend/common/sse/router.js +30 -0
  39. package/backend/common/task.js +75 -0
  40. package/backend/common/wechat/index.js +9 -0
  41. package/backend/common/wechat/routers/auth.js +238 -0
  42. package/{modules/user/frontend → backend/common/wechat/routers}/index.html +14 -7
  43. package/backend/common/wechat/services/auth.js +209 -0
  44. package/backend/common/wechat/services/notice.js +171 -0
  45. package/backend/config/index.js +63 -0
  46. package/backend/config/path.js +3 -0
  47. package/backend/router.js +96 -0
  48. package/backend/routers/app.js +222 -0
  49. package/backend/routers/automate.js +276 -0
  50. package/backend/routers/dict.js +370 -0
  51. package/backend/routers/email.js +85 -0
  52. package/backend/routers/generalConfig.js +276 -0
  53. package/backend/routers/idm.js +245 -0
  54. package/backend/routers/module.js +357 -0
  55. package/backend/routers/notice.js +138 -0
  56. package/backend/routers/pages.js +46 -0
  57. package/backend/routers/permission.js +985 -0
  58. package/backend/routers/setting.js +184 -0
  59. package/backend/routers/team/index.js +22 -0
  60. package/backend/routers/team/routers/mapping.js +29 -0
  61. package/backend/routers/team/routers/member.js +72 -0
  62. package/backend/routers/team/routers/membermanage.js +289 -0
  63. package/backend/routers/team/routers/pages.js +47 -0
  64. package/backend/routers/team/routers/roles.js +92 -0
  65. package/backend/routers/team/routers/teaminfo.js +27 -0
  66. package/backend/routers/team/routers/usergroup.js +213 -0
  67. package/backend/routers/team/services/mapping.js +101 -0
  68. package/backend/routers/team/services/member.js +206 -0
  69. package/backend/routers/team/services/roles.js +71 -0
  70. package/backend/routers/team/services/teaminfo.js +20 -0
  71. package/backend/routers/team/services/usergroup.js +128 -0
  72. package/backend/routers/user.js +436 -0
  73. package/backend/services/automate.js +60 -0
  74. package/backend/services/config.js +14 -0
  75. package/backend/services/module.js +298 -0
  76. package/backend/services/permission.js +192 -0
  77. package/backend/services/services.js +115 -0
  78. package/backend/services/setting.js +190 -0
  79. package/backend/services/token.js +42 -0
  80. package/backend/space.js +52 -0
  81. package/backend/space_mapping.js +15 -0
  82. package/backend/utils/avatar.js +48 -0
  83. package/backend/utils/color_gen_helper.js +20 -0
  84. package/backend/utils/date.js +66 -0
  85. package/backend/utils/excel.js +446 -0
  86. package/backend/utils/fs/doc.md +64 -0
  87. package/backend/utils/fs/index.js +127 -0
  88. package/backend/utils/jwt.js +54 -0
  89. package/backend/utils/modules/sequence.js +93 -0
  90. package/backend/utils/object.js +31 -0
  91. package/backend/utils/page-query-helper/index.js +61 -0
  92. package/backend/utils/path.js +123 -0
  93. package/backend/utils/run.js +25 -0
  94. package/backend/utils/tokenize.js +82 -0
  95. package/backend/utils/typeof.js +5 -0
  96. package/backend/utils/util.js +153 -0
  97. package/backend/views/api/index.js +32 -0
  98. package/backend/views/api/index.xml +49 -0
  99. package/backend/views/dict/index.js +80 -0
  100. package/backend/views/dict/index.xml +52 -0
  101. package/backend/views/index.js +32 -0
  102. package/backend/views/members_manage/index.js +68 -0
  103. package/backend/views/members_manage/index.xml +68 -0
  104. package/backend/views/roles/index.js +17 -0
  105. package/backend/views/roles/index.xml +47 -0
  106. package/backend/views/usergroup/index.js +68 -0
  107. package/backend/views/usergroup/index.xml +65 -0
  108. package/dist/common-assets/css/index-05a08720.css +3 -0
  109. package/dist/common-assets/css/index-05a08720.css.gz +0 -0
  110. package/dist/common-assets/js/index-2e8c7128.js +762 -0
  111. package/dist/common-assets/js/index-2e8c7128.js.gz +0 -0
  112. package/dist/common-assets/js/react-cropper.es-19c27537.js +10 -0
  113. package/dist/common-assets/js/react-cropper.es-19c27537.js.gz +0 -0
  114. package/dist/common-assets/png/u9-2348c304.png +0 -0
  115. package/dist/common-assets/woff2/materialicons-83be7b2f.woff2 +0 -0
  116. package/dist/index.html +162 -0
  117. package/package.json +61 -5
  118. package/common/router.js +0 -42
  119. package/modules/user/backend/index.js +0 -19
  120. package/modules/user/backend/routers/user.js +0 -11
  121. package/modules/user/frontend/index.js +0 -1
  122. package/serve.js +0 -9
@@ -0,0 +1,96 @@
1
+ const log4js = require('log4js');
2
+ const path = require('path');
3
+ const config = require('../../config');
4
+
5
+ const appenders = {
6
+ console: {
7
+ type: 'console',
8
+ },
9
+ applog: {
10
+ type: 'dateFile',
11
+ filename: path.join(config.app.home, 'logs', 'app.log'),
12
+ pattern: 'yyyy-MM-dd.log',
13
+ // alwaysIncludePattern: true,
14
+ // maxLogSize: 10, // 无效
15
+ // backups: 5, // 无效
16
+ compress: false,
17
+ numBackups: 7,
18
+ },
19
+ wslog: {
20
+ type: 'dateFile',
21
+ filename: path.join(config.app.home, 'logs', 'ws.log'),
22
+ pattern: 'yyyy-MM-dd.log',
23
+ compress: false,
24
+ numBackups: 7,
25
+ },
26
+
27
+ infolog: {
28
+ type: 'dateFile',
29
+ filename: path.join(config.app.home, 'logs', 'info.log'),
30
+ pattern: 'yyyy-MM-dd.log',
31
+ compress: true,
32
+ numBackups: 7,
33
+ },
34
+ scheduleLog: {
35
+ type: 'dateFile',
36
+ filename: path.join(config.app.home, 'logs', 'schedule.log'),
37
+ pattern: 'yyyy-MM-dd.log',
38
+ compress: false,
39
+ numBackups: 7,
40
+ },
41
+ ...config.logger.appenders,
42
+ };
43
+
44
+ const categories = {
45
+ default: {
46
+ appenders: ['console'],
47
+ level: 'debug',
48
+ },
49
+ applog: {
50
+ // 指定为上面定义的appender,如果不指定,无法写入
51
+ appenders: ['console', 'applog'],
52
+ level: 'debug', // 指定等级
53
+ },
54
+ infolog: {
55
+ appenders: ['console', 'infolog'],
56
+ level: 'info',
57
+ },
58
+ wslog: {
59
+ appenders: ['console', 'wslog'],
60
+ level: 'debug',
61
+ },
62
+
63
+ schedulelog: {
64
+ appenders: ['console', 'scheduleLog'],
65
+ level: 'debug', // 指定等级
66
+ },
67
+ ...config.logger.categories,
68
+ };
69
+
70
+ log4js.configure({
71
+ appenders,
72
+ categories,
73
+ // for pm2...
74
+ pm2: true,
75
+ disableClustering: true, // not sure...
76
+ });
77
+
78
+ function getLogger(type) {
79
+ return log4js.getLogger(type);
80
+ }
81
+
82
+ module.exports = new Proxy(appenders, {
83
+ get(target, prop) {
84
+ if (prop === 'getLogger') {
85
+ return getLogger;
86
+ }
87
+ if (prop === 'logger') {
88
+ return log4js.getLogger('applog');
89
+ }
90
+ if (prop in target) {
91
+ return log4js.getLogger(prop);
92
+ }
93
+
94
+ return null;
95
+ },
96
+ });
@@ -0,0 +1,98 @@
1
+ const isPlainObject = require('lodash/isPlainObject');
2
+ const dictService = require('./services/dict');
3
+
4
+ const userService = require('./services/user');
5
+
6
+ class MappingFactory {
7
+ /**
8
+ * interface DictMap {
9
+ * [module: string]: {
10
+ * [DICT_ID: string]: any
11
+ * }
12
+ * }
13
+ */
14
+ dictMap = {};
15
+
16
+ register(module, moduleMapping) {
17
+ if (typeof module !== 'string') return console.log('MappingFactory register 缺少参数 module');
18
+
19
+ this.dictMap[module] = moduleMapping ?? {};
20
+ }
21
+
22
+ async getMapping(module, dict_ids, extra) {
23
+ let mapping = {};
24
+
25
+ if (!Array.isArray(dict_ids)) return mapping;
26
+ if (!dict_ids.length) return mapping;
27
+
28
+ let modulesMap = {
29
+ async USER_LIST() {
30
+ let res = await userService.getUsers();
31
+ return res.reduce((mo, item) => {
32
+ if (item.USER_ID) {
33
+ mo[item.USER_ID] = item.USER_NAME;
34
+ }
35
+ return mo;
36
+ }, {});
37
+ },
38
+ };
39
+
40
+ if (module in this.dictMap) {
41
+ modulesMap = {
42
+ ...modulesMap,
43
+ ...this.dictMap[module],
44
+ };
45
+ }
46
+
47
+ // 字典表字典
48
+ let base_dict = [];
49
+ // 模块字典
50
+ let module_dict = [];
51
+
52
+ dict_ids.forEach((id) => {
53
+ if (modulesMap[id]) {
54
+ module_dict.push(id);
55
+ } else {
56
+ base_dict.push(id);
57
+ }
58
+ });
59
+
60
+ if (base_dict.length) {
61
+ let dicts = await dictService.getDicts(base_dict);
62
+ if (isPlainObject(dicts)) {
63
+ Object.entries(dicts).forEach((dict) => {
64
+ let [key, value] = dict;
65
+ if (Array.isArray(value) && value.length) {
66
+ mapping[key] = value.reduce((mo, item) => {
67
+ let { value, ...rest } = item;
68
+ mo[value] = rest;
69
+ return mo;
70
+ }, {});
71
+ }
72
+ });
73
+ }
74
+ }
75
+
76
+ if (module_dict.length) {
77
+ await module_dict.myforeach(async (dict_id) => {
78
+ let fn = modulesMap[dict_id];
79
+
80
+ if (isAsync(fn)) {
81
+ mapping[dict_id] = await fn(extra);
82
+ } else if (typeof fn === 'function') {
83
+ mapping[dict_id] = fn(extra);
84
+ } else if (isPlainObject(fn)) {
85
+ mapping[dict_id] = fn;
86
+ }
87
+ });
88
+ }
89
+
90
+ return mapping;
91
+ }
92
+ }
93
+
94
+ function isAsync(func) {
95
+ return Object.prototype.toString.call(func) === '[object AsyncFunction]';
96
+ }
97
+
98
+ module.exports = new MappingFactory();
@@ -0,0 +1,97 @@
1
+ 'use strict';
2
+ const jwt = require('../../utils/jwt');
3
+ const config = require('../../config/index');
4
+ const { loginType, getLoginMethod } = require('../services/login');
5
+
6
+ // 后端登录
7
+ async function authCheck(req, res, next) {
8
+ if (req.login) return next();
9
+ let token = req.cookies['X-Token'] || req.get('X-Token');
10
+
11
+ let data = jwt.token2data(token);
12
+ if (data) {
13
+ let { userid, exp } = data;
14
+ req.headers['x-userid'] = userid;
15
+ let expires = new Date();
16
+ expires.setDate(expires.getDate() + 3);
17
+ if (exp + 30 * 60 > Math.floor(Date.now() / 1000)) {
18
+ let token = jwt.generateToken(tokenData);
19
+ res.cookie('X-Token', token, { expires });
20
+ }
21
+ return next();
22
+ }
23
+
24
+ if ((await getLoginMethod()) === loginType.wechat) {
25
+ res.redirect('/wechat/auth');
26
+ } else {
27
+ let referer = req.headers.referer;
28
+ let pandoraPort = '3002';
29
+ if (referer && ~referer.indexOf(pandoraPort)) {
30
+ req.headers['x-userid'] = config['testUser'];
31
+ return next();
32
+ } else {
33
+ res.redirect('/#/login');
34
+ }
35
+ }
36
+ }
37
+
38
+ // 前端登录
39
+ function checkLogin(req, res, next) {
40
+ let token = req.cookies['X-Token'] || req.get('X-Token');
41
+ const ip = req.ip;
42
+
43
+ let data = jwt.token2data(token);
44
+ if (data) {
45
+ let { userid, exp } = data;
46
+ req.headers['x-userid'] = userid;
47
+ let expires = new Date();
48
+ expires.setDate(expires.getDate() + 3);
49
+ if (exp + 30 * 60 > Math.floor(Date.now() / 1000)) {
50
+ let token = jwt.generateToken(tokenData);
51
+ res.cookie('X-Token', token, { expires });
52
+ }
53
+ return next();
54
+ }
55
+
56
+ if (config.allowedIPs.some((it) => ip.startsWith(it))) {
57
+ req.headers['x-userid'] = req.query.userid || config.testUser || '';
58
+ return next();
59
+ }
60
+
61
+ res.sendStatus(401);
62
+ }
63
+
64
+ // 机器人登录校验
65
+ function appAuthCheck(req, res, next) {
66
+ const authorization = req.headers.authorization;
67
+ if (authorization) {
68
+ const appid = jwt.token2data(authorization.split('Bearer ')?.[1], 'appid');
69
+ if (appid) {
70
+ req.headers.appid = appid;
71
+ }
72
+ }
73
+ return next();
74
+ }
75
+
76
+ // 用户授权的第三方应用 token校验,
77
+ function userAppAuthCheck(req, res, next) {
78
+ const authorization = req.headers.authorization;
79
+ if (authorization) {
80
+ const token = authorization.split('Bearer ')?.[1];
81
+ const cert = req.headers.cert;
82
+ if ((token, cert)) {
83
+ const userid = jwt.token2data(token, 'userid', cert);
84
+ if (userid) {
85
+ req.headers['x-userid'] = userid || '';
86
+ }
87
+ }
88
+ }
89
+ return next();
90
+ }
91
+
92
+ module.exports = {
93
+ authCheck,
94
+ checkLogin,
95
+ appAuthCheck,
96
+ userAppAuthCheck,
97
+ };
@@ -0,0 +1,13 @@
1
+ module.exports = function allowCors(req, res, next) {
2
+ res.header('Access-Control-Allow-Origin', req.headers.origin);
3
+ res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
4
+ res.header('Access-Control-Allow-Headers', 'Content-Type,X-Token');
5
+ res.header('Access-Control-Allow-Credentials', 'true');
6
+
7
+ if (req.method === 'OPTIONS') {
8
+ res.sendStatus(200);
9
+ } else {
10
+ // 处理实际请求
11
+ next();
12
+ }
13
+ };
@@ -0,0 +1,25 @@
1
+ const errorCode = {};
2
+
3
+ const response = (req, res, next) => {
4
+ res.sendOk = (data = null) => {
5
+ res.json({
6
+ code: 'OK',
7
+ message: 'success',
8
+ data,
9
+ });
10
+ };
11
+
12
+ res.sendErr = (message, code = '-1', data) => {
13
+ let content = {
14
+ code,
15
+ message: errorCode[code] || message,
16
+ };
17
+ if (data) {
18
+ content.data = data;
19
+ }
20
+ res.json(content);
21
+ };
22
+ next();
23
+ };
24
+
25
+ module.exports = response;