@gingkoo/base-server 0.0.1-alpha.8 → 0.0.1

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/app.js CHANGED
@@ -15,6 +15,7 @@ const router = require('./backend/router');
15
15
  class App {
16
16
  /**
17
17
  * @param {string} optinos.timeout 超时时间 默认 30s
18
+ * @param {(app: any, express: any) => void} optinos.middleware 注册中间件
18
19
  * @param {(app: any, express: any) => void} optinos.beforeRoutes 在前面注册路由
19
20
  * @param {(app: any, express: any) => void} optinos.afterRoutes 在后面注册路由
20
21
  */
@@ -63,6 +64,10 @@ class App {
63
64
  this.app.use((req, res, next) => {
64
65
  if (!req.timedout) next();
65
66
  });
67
+
68
+ if (this.options.middleware) {
69
+ this.options.middleware(this.app, express);
70
+ }
66
71
  }
67
72
 
68
73
  routes() {
@@ -21,19 +21,31 @@ class MappingFactory {
21
21
 
22
22
  async getMapping(module, dict_ids, extra) {
23
23
  let mapping = {};
24
+ let list = {};
24
25
 
25
- if (!Array.isArray(dict_ids)) return mapping;
26
- if (!dict_ids.length) return mapping;
26
+ if (!Array.isArray(dict_ids) || !dict_ids?.length) {
27
+ return {
28
+ dict: mapping,
29
+ list: list,
30
+ };
31
+ }
27
32
 
28
33
  let modulesMap = {
29
34
  async USER_LIST() {
30
35
  let res = await userService.getUsers();
31
- return res.reduce((mo, item) => {
36
+ let arr = [];
37
+ let dict = res.reduce((mo, item) => {
32
38
  if (item.USER_ID) {
39
+ arr.push({ value: item.USER_ID, label: item.USER_NAME });
33
40
  mo[item.USER_ID] = item.USER_NAME;
34
41
  }
35
42
  return mo;
36
43
  }, {});
44
+
45
+ return {
46
+ dict,
47
+ list: arr,
48
+ };
37
49
  },
38
50
  };
39
51
 
@@ -68,6 +80,12 @@ class MappingFactory {
68
80
  mo[value] = rest;
69
81
  return mo;
70
82
  }, {});
83
+ list[key] = value.map((item) => {
84
+ return {
85
+ label: item.label,
86
+ value: item.value,
87
+ };
88
+ });
71
89
  }
72
90
  });
73
91
  }
@@ -76,18 +94,30 @@ class MappingFactory {
76
94
  if (module_dict.length) {
77
95
  await module_dict.myforeach(async (dict_id) => {
78
96
  let fn = modulesMap[dict_id];
79
-
97
+ let result = {};
80
98
  if (isAsync(fn)) {
81
- mapping[dict_id] = await fn(extra);
99
+ result = await fn(extra);
82
100
  } else if (typeof fn === 'function') {
83
- mapping[dict_id] = fn(extra);
84
- } else if (isPlainObject(fn)) {
85
- mapping[dict_id] = fn;
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
+ }
86
113
  }
87
114
  });
88
115
  }
89
116
 
90
- return mapping;
117
+ return {
118
+ dict: mapping,
119
+ list: list,
120
+ };
91
121
  }
92
122
  }
93
123
 
package/backend/router.js CHANGED
@@ -2,6 +2,7 @@
2
2
  const path = require('path');
3
3
  const express = require('express');
4
4
  const { createProxyMiddleware } = require('http-proxy-middleware');
5
+ const { applog } = require('./common/logger');
5
6
  const jwt = require('./utils/jwt');
6
7
  const config = require('./config/index');
7
8
  const mapping = require('./common/mapping');
@@ -17,10 +18,15 @@ var router = express.Router();
17
18
  // 后端
18
19
  router.get('/(:module/)?mapping', async (req, res) => {
19
20
  let { module } = req.params;
20
- let { list = '', ...extra } = req.query;
21
- res.sendOk({
22
- dict: await mapping.getMapping(module, list.split('|'), extra),
23
- });
21
+ let { list = '', type = 'dict', ...extra } = req.query;
22
+ let data = await mapping.getMapping(module, list.split('|'), extra);
23
+ applog.info(`🌟 ~ mapping:`, type, data);
24
+ if (type === 'dict') {
25
+ Reflect.deleteProperty(data, 'list');
26
+ } else if (type === 'list') {
27
+ Reflect.deleteProperty(data, 'dict');
28
+ }
29
+ res.sendOk(data);
24
30
  });
25
31
 
26
32
  router.use('/fss', fss);
@@ -5,11 +5,18 @@ const deptService = require('./common/services/dept');
5
5
  mapping.register('space', {
6
6
  async DEPT_LIST() {
7
7
  let res = await deptService.getDepts();
8
- return res.reduce((mo, item) => {
8
+ let list = [];
9
+ let dict = res.reduce((mo, item) => {
9
10
  if (item.DEPT_NO) {
10
11
  mo[item.DEPT_NO] = item.DEPT_NAME;
12
+ list.push({ value: item.DEPT_NO, label: item.DEPT_NAME });
11
13
  }
12
14
  return mo;
13
15
  }, {});
16
+
17
+ return {
18
+ dict,
19
+ list,
20
+ };
14
21
  },
15
22
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gingkoo/base-server",
3
- "version": "0.0.1-alpha.8",
3
+ "version": "0.0.1",
4
4
  "description": "",
5
5
  "main": "app.js",
6
6
  "scripts": {