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

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 +4 -0
  2. package/app.js +41 -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 +128 -0
  19. package/backend/common/middleware/auth.js +109 -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 +488 -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 +61 -0
  46. package/backend/config/path.js +3 -0
  47. package/backend/router.js +100 -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 +22 -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/base-assets/css/index-ffdb55a5.css +3 -0
  109. package/dist/base-assets/css/index-ffdb55a5.css.gz +0 -0
  110. package/dist/base-assets/js/index-b3998a47.js +762 -0
  111. package/dist/base-assets/js/index-b3998a47.js.gz +0 -0
  112. package/dist/base-assets/js/react-cropper.es-d5f06996.js +10 -0
  113. package/dist/base-assets/js/react-cropper.es-d5f06996.js.gz +0 -0
  114. package/dist/base-assets/png/u9-2348c304.png +0 -0
  115. package/dist/base-assets/woff2/materialicons-83be7b2f.woff2 +0 -0
  116. package/dist/index.html +162 -0
  117. package/package.json +62 -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,128 @@
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
+ let list = {};
25
+
26
+ if (!Array.isArray(dict_ids) || !dict_ids?.length) {
27
+ return {
28
+ dict: mapping,
29
+ list: list,
30
+ };
31
+ }
32
+
33
+ let modulesMap = {
34
+ async USER_LIST() {
35
+ let res = await userService.getUsers();
36
+ let arr = [];
37
+ let dict = res.reduce((mo, item) => {
38
+ if (item.USER_ID) {
39
+ arr.push({ value: item.USER_ID, label: item.USER_NAME });
40
+ mo[item.USER_ID] = item.USER_NAME;
41
+ }
42
+ return mo;
43
+ }, {});
44
+
45
+ return {
46
+ dict,
47
+ list: arr,
48
+ };
49
+ },
50
+ };
51
+
52
+ if (module in this.dictMap) {
53
+ modulesMap = {
54
+ ...modulesMap,
55
+ ...this.dictMap[module],
56
+ };
57
+ }
58
+
59
+ // 字典表字典
60
+ let base_dict = [];
61
+ // 模块字典
62
+ let module_dict = [];
63
+
64
+ dict_ids.forEach((id) => {
65
+ if (modulesMap[id]) {
66
+ module_dict.push(id);
67
+ } else {
68
+ base_dict.push(id);
69
+ }
70
+ });
71
+
72
+ if (base_dict.length) {
73
+ let dicts = await dictService.getDicts(base_dict);
74
+ if (isPlainObject(dicts)) {
75
+ Object.entries(dicts).forEach((dict) => {
76
+ let [key, value] = dict;
77
+ if (Array.isArray(value) && value.length) {
78
+ mapping[key] = value.reduce((mo, item) => {
79
+ let { value, ...rest } = item;
80
+ mo[value] = rest;
81
+ return mo;
82
+ }, {});
83
+ list[key] = value.map((item) => {
84
+ return {
85
+ label: item.label,
86
+ value: item.value,
87
+ };
88
+ });
89
+ }
90
+ });
91
+ }
92
+ }
93
+
94
+ if (module_dict.length) {
95
+ await module_dict.myforeach(async (dict_id) => {
96
+ let fn = modulesMap[dict_id];
97
+ let result = {};
98
+ if (isAsync(fn)) {
99
+ result = await fn(extra);
100
+ } else if (typeof fn === 'function') {
101
+ result = fn(extra);
102
+ } else {
103
+ result = fn;
104
+ }
105
+
106
+ if (isPlainObject(result)) {
107
+ if (result['dict']) {
108
+ mapping[dict_id] = result['dict'];
109
+ }
110
+ if (result['list']) {
111
+ list[dict_id] = result['list'];
112
+ }
113
+ }
114
+ });
115
+ }
116
+
117
+ return {
118
+ dict: mapping,
119
+ list: list,
120
+ };
121
+ }
122
+ }
123
+
124
+ function isAsync(func) {
125
+ return Object.prototype.toString.call(func) === '[object AsyncFunction]';
126
+ }
127
+
128
+ module.exports = new MappingFactory();
@@ -0,0 +1,109 @@
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
+ const authorization = req.headers.authorization;
11
+ if (!token && authorization) {
12
+ token = authorization?.split?.('Bearer ')?.[1] || '';
13
+ }
14
+
15
+ let data = jwt.token2data(token);
16
+ if (data) {
17
+ let { userid, exp } = data;
18
+ if (userid) {
19
+ req.headers['x-userid'] = userid;
20
+ let expires = new Date();
21
+ expires.setDate(expires.getDate() + 3);
22
+ if (exp + 30 * 60 > Math.floor(Date.now() / 1000)) {
23
+ let token = jwt.generateToken(tokenData);
24
+ res.cookie('X-Token', token, { expires });
25
+ }
26
+ }
27
+ return next();
28
+ }
29
+
30
+ if ((await getLoginMethod()) === loginType.wechat) {
31
+ res.redirect('/wechat/auth');
32
+ } else {
33
+ let referer = req.headers.referer;
34
+ let pandoraPort = '3002';
35
+ if (referer && ~referer.indexOf(pandoraPort)) {
36
+ req.headers['x-userid'] = config['testUser'];
37
+ return next();
38
+ } else {
39
+ res.redirect('/#/login');
40
+ }
41
+ }
42
+ }
43
+
44
+ // 前端登录
45
+ function checkLogin(req, res, next) {
46
+ let token = req.cookies['X-Token'] || req.get('X-Token');
47
+ const ip = req.ip;
48
+ const authorization = req.headers.authorization;
49
+ if (!token && authorization) {
50
+ token = authorization?.split?.('Bearer ')?.[1] || '';
51
+ }
52
+
53
+ let data = jwt.token2data(token);
54
+ if (data) {
55
+ let { userid, exp } = data;
56
+ if (userid) {
57
+ req.headers['x-userid'] = userid;
58
+ let expires = new Date();
59
+ expires.setDate(expires.getDate() + 3);
60
+ if (exp + 30 * 60 > Math.floor(Date.now() / 1000)) {
61
+ let token = jwt.generateToken(tokenData);
62
+ res.cookie('X-Token', token, { expires });
63
+ }
64
+ }
65
+ return next();
66
+ }
67
+
68
+ if (config.allowedIPs.some((it) => ip.startsWith(it))) {
69
+ req.headers['x-userid'] = req.query.userid || config.testUser || '';
70
+ return next();
71
+ }
72
+
73
+ res.sendStatus(401);
74
+ }
75
+
76
+ // 机器人登录校验
77
+ function appAuthCheck(req, res, next) {
78
+ const authorization = req.headers.authorization;
79
+ if (authorization) {
80
+ const appid = jwt.token2data(authorization.split('Bearer ')?.[1], 'appid');
81
+ if (appid) {
82
+ req.headers.appid = appid;
83
+ }
84
+ }
85
+ return next();
86
+ }
87
+
88
+ // 用户授权的第三方应用 token校验,
89
+ function userAppAuthCheck(req, res, next) {
90
+ const authorization = req.headers.authorization;
91
+ if (authorization) {
92
+ const token = authorization.split('Bearer ')?.[1];
93
+ const cert = req.headers.cert;
94
+ if ((token, cert)) {
95
+ const userid = jwt.token2data(token, 'userid', cert);
96
+ if (userid) {
97
+ req.headers['x-userid'] = userid || '';
98
+ }
99
+ }
100
+ }
101
+ return next();
102
+ }
103
+
104
+ module.exports = {
105
+ authCheck,
106
+ checkLogin,
107
+ appAuthCheck,
108
+ userAppAuthCheck,
109
+ };
@@ -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;