@wanglindoc/elpis 1.0.0

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 (88) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc +52 -0
  3. package/.idea/codeStyles/Project.xml +14 -0
  4. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  5. package/.idea/elpis.iml +12 -0
  6. package/.idea/inspectionProfiles/Project_Default.xml +6 -0
  7. package/.idea/modules.xml +8 -0
  8. package/.idea/prettier.xml +7 -0
  9. package/.idea/vcs.xml +6 -0
  10. package/README.md +232 -0
  11. package/app/controller/base.js +42 -0
  12. package/app/controller/project.js +76 -0
  13. package/app/controller/view.js +19 -0
  14. package/app/extend/logger.js +42 -0
  15. package/app/middleware/api-params-verify.js +81 -0
  16. package/app/middleware/api-sign-verify.js +45 -0
  17. package/app/middleware/error-handler.js +41 -0
  18. package/app/middleware/project-handler.js +30 -0
  19. package/app/middleware.js +42 -0
  20. package/app/pages/assets/custom.css +14 -0
  21. package/app/pages/boot.js +51 -0
  22. package/app/pages/common/curl.js +91 -0
  23. package/app/pages/common/utils.js +7 -0
  24. package/app/pages/dashboard/complex-view/header-view/complex-view/sub-menu/sub-menu.vue +18 -0
  25. package/app/pages/dashboard/complex-view/header-view/header-view.vue +154 -0
  26. package/app/pages/dashboard/complex-view/iframe-view/iframe-view.vue +46 -0
  27. package/app/pages/dashboard/complex-view/schema-view/complex-view/search-panel/search-panel.vue +40 -0
  28. package/app/pages/dashboard/complex-view/schema-view/complex-view/table-panel/table-panel.vue +127 -0
  29. package/app/pages/dashboard/complex-view/schema-view/components/component-config.js +22 -0
  30. package/app/pages/dashboard/complex-view/schema-view/components/create-form/create-form.vue +95 -0
  31. package/app/pages/dashboard/complex-view/schema-view/components/detail-panel/detail-panel.vue +104 -0
  32. package/app/pages/dashboard/complex-view/schema-view/components/edit-form/edit-form.vue +129 -0
  33. package/app/pages/dashboard/complex-view/schema-view/hook/schema.js +137 -0
  34. package/app/pages/dashboard/complex-view/schema-view/schema-view.vue +102 -0
  35. package/app/pages/dashboard/complex-view/sider-view/complex-view/sub-menu.vue +21 -0
  36. package/app/pages/dashboard/complex-view/sider-view/sider-view.vue +141 -0
  37. package/app/pages/dashboard/dashboard.vue +96 -0
  38. package/app/pages/dashboard/entry.dashboard.js +53 -0
  39. package/app/pages/dashboard/todo/todo.vue +6 -0
  40. package/app/pages/store/index.js +5 -0
  41. package/app/pages/store/menu.js +73 -0
  42. package/app/pages/store/project.js +15 -0
  43. package/app/pages/widgets/header-container/asserts/avatar.png +0 -0
  44. package/app/pages/widgets/header-container/asserts/logo.png +0 -0
  45. package/app/pages/widgets/header-container/header-container.vue +106 -0
  46. package/app/pages/widgets/schema-form/complex-view/input/input.vue +137 -0
  47. package/app/pages/widgets/schema-form/complex-view/input-number/input-number.vue +135 -0
  48. package/app/pages/widgets/schema-form/complex-view/select/select.vue +119 -0
  49. package/app/pages/widgets/schema-form/form-item-config.js +20 -0
  50. package/app/pages/widgets/schema-form/schema-form.vue +145 -0
  51. package/app/pages/widgets/schema-search-bar/complex-view/date-range/date-range.vue +52 -0
  52. package/app/pages/widgets/schema-search-bar/complex-view/dynamic-select/dynamic-select.vue +65 -0
  53. package/app/pages/widgets/schema-search-bar/complex-view/input/input.vue +42 -0
  54. package/app/pages/widgets/schema-search-bar/complex-view/select/select.vue +49 -0
  55. package/app/pages/widgets/schema-search-bar/schema-search-bar.vue +129 -0
  56. package/app/pages/widgets/schema-search-bar/search-item-config.js +24 -0
  57. package/app/pages/widgets/schema-table/schema-table.vue +212 -0
  58. package/app/pages/widgets/sider-container/sider-container.vue +26 -0
  59. package/app/public/output/entry.page1.tpl +55 -0
  60. package/app/public/output/entry.page2.tpl +11 -0
  61. package/app/public/static/favicon.ico +0 -0
  62. package/app/public/static/normalize.css +267 -0
  63. package/app/router/project.js +14 -0
  64. package/app/router/view.js +9 -0
  65. package/app/router-schema/project.js +32 -0
  66. package/app/service/base.js +15 -0
  67. package/app/service/project.js +48 -0
  68. package/app/view/entry.tpl +22 -0
  69. package/app/webpack/build-dev.js +64 -0
  70. package/app/webpack/build-prod.js +29 -0
  71. package/app/webpack/config/webpack.base.js +352 -0
  72. package/app/webpack/config/webpack.dev.js +59 -0
  73. package/app/webpack/config/webpack.prod.js +145 -0
  74. package/app/webpack/libs/blank.js +1 -0
  75. package/config/config.default.js +4 -0
  76. package/elpis-core/env.js +20 -0
  77. package/elpis-core/index.js +106 -0
  78. package/elpis-core/loader/config.js +62 -0
  79. package/elpis-core/loader/controller.js +79 -0
  80. package/elpis-core/loader/extend.js +67 -0
  81. package/elpis-core/loader/middleware.js +77 -0
  82. package/elpis-core/loader/router-schema.js +57 -0
  83. package/elpis-core/loader/router.js +57 -0
  84. package/elpis-core/loader/service.js +76 -0
  85. package/index.js +39 -0
  86. package/model/index.js +128 -0
  87. package/package.json +90 -0
  88. package/test/controller/project.test.js +243 -0
package/.eslintignore ADDED
@@ -0,0 +1,2 @@
1
+ node_modules/
2
+ public/
package/.eslintrc ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "extends": ["plugin:vue/base", "plugin:vue/recommended"],
3
+ "plugins": ["vue"],
4
+ "env": {
5
+ "browser": true,
6
+ "node": true
7
+ },
8
+ "parser": "vue-eslint-parser",
9
+ "parserOptions": {
10
+ "parser": "babel-eslint",
11
+ "ecmaVersion": 2017,
12
+ "sourceType": "module"
13
+ },
14
+ "rules": {
15
+ "no-unused-vars": [2, { "args": "none" }],
16
+ "strict": "off",
17
+ "valid-jsdoc": "off",
18
+ "jsdoc/require-param-description": "off",
19
+ "jsdoc/require-param-type": "off",
20
+ "jsdoc/check-param-names": "off",
21
+ "jsdoc/require-param": "off",
22
+ "jsdoc/check-tag-names": "off",
23
+ "linebreak-style": "off",
24
+ "array-bracket-spacing": "off",
25
+ "prefer-promise-reject-errors": "off",
26
+ "comma-dangle": "off",
27
+ "newline-per-chained-call": "off",
28
+ "no-loop-func": "off",
29
+ "no-empty": "off",
30
+ "no-else-return": "off",
31
+ "no-unneeded-ternary": "off",
32
+ "no-eval": "off",
33
+ "prefer-destructuring": "off",
34
+ "no-param-reassign": "off",
35
+ "max-len": "off",
36
+ "no-restricted-syntax": "off",
37
+ "no-plusplus": "off",
38
+ "no-useless-escape": "off",
39
+ "no-nested-ternary": "off",
40
+ "radix": "off",
41
+ "arrow-body-style": "off",
42
+ "arrow-parens": "off",
43
+ "vue/multi-word-component-names": "off",
44
+ "vue/valid-v-for": "off",
45
+ "vue/no-multiple-template-root": "off"
46
+ },
47
+ "globals": {
48
+ "$": true,
49
+ "axios": true,
50
+ "Vue": true
51
+ }
52
+ }
@@ -0,0 +1,14 @@
1
+ <component name="ProjectCodeStyleConfiguration">
2
+ <code_scheme name="Project" version="173">
3
+ <HTMLCodeStyleSettings>
4
+ <option name="HTML_ENFORCE_QUOTES" value="true" />
5
+ <option name="HTML_NEWLINE_AFTER_LAST_ATTRIBUTE" value="When multiline" />
6
+ </HTMLCodeStyleSettings>
7
+ <codeStyleSettings language="HTML">
8
+ <indentOptions>
9
+ <option name="INDENT_SIZE" value="2" />
10
+ <option name="CONTINUATION_INDENT_SIZE" value="2" />
11
+ </indentOptions>
12
+ </codeStyleSettings>
13
+ </code_scheme>
14
+ </component>
@@ -0,0 +1,5 @@
1
+ <component name="ProjectCodeStyleConfiguration">
2
+ <state>
3
+ <option name="USE_PER_PROJECT_SETTINGS" value="true" />
4
+ </state>
5
+ </component>
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
+ <excludeFolder url="file://$MODULE_DIR$/temp" />
7
+ <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
+ </content>
9
+ <orderEntry type="inheritedJdk" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ </component>
12
+ </module>
@@ -0,0 +1,6 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
5
+ </profile>
6
+ </component>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/elpis.iml" filepath="$PROJECT_DIR$/.idea/elpis.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="PrettierConfiguration">
4
+ <option name="myConfigurationMode" value="AUTOMATIC" />
5
+ <option name="myRunOnSave" value="true" />
6
+ </component>
7
+ </project>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
package/README.md ADDED
@@ -0,0 +1,232 @@
1
+ # elpis
2
+
3
+ ## 企业级应用框架
4
+
5
+ ### model配置:
6
+
7
+ ```javascript
8
+ export const obj = {
9
+ mode: "dashboard", // 模版类型,不同模板类型对应不一样的模板数据结构
10
+ name: "", // 名称
11
+ desc: "", // 描述
12
+ icon: "", // icon
13
+ homePage: "", // 首页 (项目配置)
14
+ // 头部菜单
15
+ menu: [
16
+ {
17
+ key: "", // 菜单唯一描述
18
+ name: "", // 菜单名称
19
+ menuType: "", // 枚举值,group/module
20
+
21
+ // 当 menuType 为 group 时可填
22
+ subMenu: [
23
+ {
24
+ // 可递归 menuItem
25
+ },
26
+ // ...
27
+ ],
28
+
29
+ // 当 menuType 为 module 时可填
30
+ moduleType: "", // sider/iframe/custom/schema
31
+
32
+ // 当 moduleType 为 sider 时可填
33
+ siderConfig: {
34
+ menu: [
35
+ {
36
+ // 可递归 menuItem (除 moduleType === sider)
37
+ },
38
+ ],
39
+ },
40
+
41
+ // 当 moduleType 为 iframe 时可填
42
+ iframeConfig: {
43
+ path: "", // iframe 路径
44
+ },
45
+
46
+ // 当 moduleType 为 custom 时可填
47
+ customCofig: {
48
+ path: "", // 自定义路由
49
+ },
50
+
51
+ // 当 moduleType 为 schema 时可填
52
+ schemaConfig: {
53
+ api: "", // 数据源 API (遵循 RESTFUL 规范)
54
+ schema: {
55
+ // 板块数据结构
56
+ type: "object",
57
+ properties: {
58
+ key: {
59
+ ...schema, // 标准 schema 配置
60
+ type: "", // 字段类型
61
+ label: "", // 字段中文名
62
+
63
+ // 字段在 table 中的相关配置
64
+ tableOption: {
65
+ ...elTableColumnConfig, // 标准 el-table-column 配置
66
+ visible: true, // 默认为 true(false 或不配置时,表示不在表格中显示)
67
+ },
68
+
69
+ // 字段在 search-bar 中的相关配置
70
+ searchOption: {
71
+ ...eleComponentConfig, // 标准 el-component-column 配置
72
+ comType: "", // 配置组件类型 input/select…
73
+ default: "", // 默认值
74
+
75
+ // 当 comType === ‘select‘ 时
76
+ enumList: [], // 下拉框可选项
77
+
78
+ // 当 comType === 'dynamicSelect' 时
79
+ api: "",
80
+ },
81
+
82
+ // 字段在不同动态 component 中的相关配置,前缀对应 componentConfig 中的键值
83
+ // 如:componentConfig.createForm,这里对应 createFormOption
84
+ // 字段在 createForm 中相关配置
85
+ createFormOption: {
86
+ ...eleComponentConfig, // 标准 el-component 配置
87
+ comType: "", // 控件类型 input/select/input-number
88
+ visible: true, // 是否展示(true/false)默认为 true
89
+ disabled: false, // 是否禁用(true/false)默认为 false
90
+ default: "", // 默认值
91
+
92
+ // comType === 'select' 时生效
93
+ enumList: [], // 枚举列表
94
+ },
95
+
96
+ // 字段在 edit-form 表单中的相关配置
97
+ editFormOption: {
98
+ ...eleComponentConfig, // 标准 el-component 配置
99
+ comType: "", // 控件类型 input/select/input-number
100
+ visible: true, // 是否展示(true/false)默认为 true
101
+ disabled: false, // 是否禁用(true/false)默认为 false
102
+ default: "", // 默认值
103
+
104
+ // comType === 'select' 时生效
105
+ enumList: [], // 枚举列表
106
+ },
107
+
108
+ detailPanelOption: {
109
+ ...eleComponentConfig, // 标准 el-component 配置
110
+ },
111
+ },
112
+ // ...
113
+ },
114
+
115
+ // 必填 properties
116
+ required: [],
117
+ },
118
+
119
+ // table 相关配置
120
+ tableConfig: {
121
+ headerButtons: [
122
+ {
123
+ label: "", // 按钮中文名
124
+ eventKey: "", // 按钮事件名
125
+ // 按钮配置
126
+ eventOption: {
127
+ // 当 eventKey === 'showComponent' 时
128
+ comName: "", // 组件名称(showComponent 调起的组件)
129
+ },
130
+ ...elButtonConfig,
131
+ },
132
+ // ...
133
+ ],
134
+ rowButtons: [
135
+ {
136
+ label: "", // 按钮中文名
137
+ eventKey: "", // 按钮事件名
138
+ // 按钮事件具体配置
139
+ eventOption: {
140
+ // 当 eventKey === 'showComponent' 时
141
+ comName: "", // 组件名称(showComponent 调起的组件)
142
+
143
+ // 当 eventKey === 'remove' 时
144
+ params: {
145
+ // paramKey=接口参数名
146
+ // rowValueKey= 参数值 (格式为 schema::tableKey,到 table 中找相应的字段)
147
+ paramKey: rowValueKey,
148
+ },
149
+ }, // 按钮配置
150
+ ...elButtonConfig,
151
+ },
152
+ // ...
153
+ ],
154
+ }, // table 相关配置
155
+
156
+ // search-bar 相关配置
157
+ searchConfig: {},
158
+
159
+ // 动态组件 相关配置
160
+ componentConfig: {
161
+ // create-form 表单相关配置
162
+ createForm: {
163
+ title: "", // 表单标题
164
+ saveBtnText: "", // 保存按钮文案
165
+ },
166
+ // edit-form 表单相关配置
167
+ editForm: {
168
+ title: "", // 表单标题
169
+ saveBtnText: "", // 保存按钮文案
170
+ mainKey: "", // 表单主键,用于唯一表示要修改的数据对象
171
+ },
172
+
173
+ // 详情面板的相关配置
174
+ detailPanel: {
175
+ title: "", // 表单标题
176
+ mainKey: "", // 表单主键,用于唯一表示要修改的数据对象
177
+ },
178
+ },
179
+ },
180
+ },
181
+ // ...
182
+ ],
183
+ };
184
+ ```
185
+
186
+ ### 服务端启动:
187
+
188
+ ```javascript
189
+ const { serverStart } = require("@wanglindoc/elpis");
190
+
191
+ const app = serverStart({});
192
+ ```
193
+
194
+ ### 自定义服务端:
195
+
196
+ - router-schema
197
+ - router
198
+ - controller
199
+ - service
200
+ - config
201
+ - extend
202
+
203
+ ### 前端构建:
204
+
205
+ ```javascript
206
+ const { frontendBuild } = require("@wanglindoc/elpis");
207
+
208
+ frontendBuild(process.env._ENV);
209
+ ```
210
+
211
+ ### 自定义页面拓展:
212
+
213
+ - 在 `app/pages/` 目录下写入口 entry.xxx.js
214
+
215
+ ### dashboard / custom-view 自定义页面拓展:
216
+
217
+ - 在 `app/pages/dashboard/xxxx` 下写页面
218
+
219
+ ### dashboard / schema-view / components 动态组件拓展:
220
+
221
+ 1. 在 `app/pages/dashboard/complex-view/schema-view/components` 下写组件
222
+ 2. 配置到 `app/pages/dashboard/complex-view/schema-view/components/component-config.js`
223
+
224
+ ### schema-form 控件拓展
225
+
226
+ 1. 在 `app/widgets/schema-form/complex-view` 写组件
227
+ 2. 配置到 `app/widgets/schema-form/form-item-config.js`
228
+
229
+ ### schema-search-bar 控件拓展
230
+
231
+ 1. 在 `app/widgets/schema-search-bar/complex-view` 下写控件
232
+ 2. 配置到 `app/widgets/schema-search-bar/search-item-config.js`
@@ -0,0 +1,42 @@
1
+ module.exports = (app) => {
2
+ return class BaseController {
3
+ /**
4
+ * controller 基类
5
+ * 统一收拢 controller 的公共方法
6
+ */
7
+ constructor() {
8
+ this.app = app;
9
+ this.config = app.config;
10
+ this.service = app.service;
11
+ }
12
+
13
+ /**
14
+ * API 处理成功时的统一返回结构
15
+ * @param {*} ctx ctx 上下文
16
+ * @param {*} data data核心数据
17
+ * @param {*} metadata metadata附加数据
18
+ */
19
+ success(ctx, data = {}, metadata = {}) {
20
+ ctx.status = 200;
21
+ ctx.body = {
22
+ success: true,
23
+ data,
24
+ metadata,
25
+ };
26
+ }
27
+
28
+ /**
29
+ * API 处理失败时的统一返回结构
30
+ * @param {*} ctx ctx 上下文
31
+ * @param {*} message 错误信息
32
+ * @param {*} code 错误码
33
+ */
34
+ fail(ctx, message, code) {
35
+ ctx.body = {
36
+ success: false,
37
+ message,
38
+ code,
39
+ };
40
+ }
41
+ };
42
+ };
@@ -0,0 +1,76 @@
1
+ module.exports = (app) => {
2
+ const BaseController = require("./base")(app);
3
+ return class ProjectController extends BaseController {
4
+ /**
5
+ * 获取所有模型与项目的结构化数据
6
+ * @param {*} ctx
7
+ */
8
+ async getModelList(ctx) {
9
+ const { project: projectService } = app.services;
10
+
11
+ const modelList = await projectService.getModelList();
12
+
13
+ // 构造返回结果,只返回关键数据
14
+ const dtoModelList = modelList.reduce((preList, item) => {
15
+ const { model, project } = item;
16
+
17
+ // 构造model关键数据
18
+ const { key, name, desc } = model;
19
+ const dtoModel = { key, name, desc };
20
+
21
+ // 构造project关键数据
22
+ const dtoProject = Object.keys(project).reduce((preObj, projKey) => {
23
+ const { key, name, desc, homePage } = project[projKey];
24
+ preObj[projKey] = { key, name, desc, homePage };
25
+
26
+ return preObj;
27
+ }, {});
28
+
29
+ // 整合返回结果
30
+ preList.push({
31
+ model: dtoModel,
32
+ project: dtoProject,
33
+ });
34
+
35
+ return preList;
36
+ }, []);
37
+
38
+ this.success(ctx, dtoModelList);
39
+ }
40
+
41
+ /**
42
+ * 获取当前 projectKey 对应模型下的项目列表(如果无projectKey,全量获取)
43
+ */
44
+ getList(ctx) {
45
+ const { proj_key: projKey } = ctx.request.query;
46
+
47
+ const { project: projectService } = app.services;
48
+ const projectList = projectService.getList({ projKey });
49
+
50
+ // 构造关键数据 list
51
+ const dtoProjectList = projectList.map((item) => {
52
+ const { modelKey, key, name, desc, homePage } = item;
53
+ return { modelKey, key, name, desc, homePage };
54
+ });
55
+
56
+ this.success(ctx, dtoProjectList);
57
+ }
58
+
59
+ /**
60
+ * 根据 proj_key 获取项目配置
61
+ * @param {*} ctx
62
+ */
63
+ get(ctx) {
64
+ const { proj_key: projKey } = ctx.request.query;
65
+ const { project: projectService } = app.services;
66
+ const projConfig = projectService.get({ projKey });
67
+
68
+ if (!projConfig) {
69
+ this.fail(ctx, "获取项目异常", 50000);
70
+ return;
71
+ }
72
+
73
+ this.success(ctx, projConfig);
74
+ }
75
+ };
76
+ };
@@ -0,0 +1,19 @@
1
+ module.exports = (app) => {
2
+ return class ViewController {
3
+ /**
4
+ * 渲染页面
5
+ * @param {*} ctx 上下文
6
+ */
7
+ async renderPage(ctx) {
8
+ const { query, params } = ctx.request;
9
+ app.logger.info(`[ViewController] query: ${JSON.stringify(query)}`);
10
+ app.logger.info(`[ViewController] params: ${JSON.stringify(params)}`);
11
+
12
+ await ctx.render(`dist/entry.${ctx.params.page}`, {
13
+ projKey: query?.proj_key,
14
+ name: app.options?.name,
15
+ env: app.env.get(),
16
+ });
17
+ }
18
+ };
19
+ };
@@ -0,0 +1,42 @@
1
+ const log4js = require("log4js");
2
+
3
+ /**
4
+ * 日志工具
5
+ * 外部调用 app.logger.info app.logger.error
6
+ * @param {*} app
7
+ * @returns
8
+ */
9
+ module.exports = (app) => {
10
+ let logger;
11
+
12
+ if (app.env.isLocal()) {
13
+ // 打印至控制台即可
14
+ logger = console;
15
+ } else {
16
+ // 日志输出并落地磁盘(日志落盘)
17
+ log4js.configure({
18
+ appenders: {
19
+ console: {
20
+ type: "console",
21
+ },
22
+ // 日志文件切分
23
+ dateFile: {
24
+ type: "dateFile",
25
+ filename: "./logs/application.log",
26
+ pattern: ".yyyy-MM-dd",
27
+ },
28
+ },
29
+
30
+ categories: {
31
+ default: {
32
+ appenders: ["console", "dateFile"],
33
+ level: "trace",
34
+ },
35
+ },
36
+ });
37
+
38
+ logger = log4js.getLogger();
39
+ }
40
+
41
+ return logger;
42
+ };
@@ -0,0 +1,81 @@
1
+ const Ajv = require("ajv");
2
+ const ajv = new Ajv();
3
+
4
+ /**
5
+ * API 参数校验
6
+ * @param {*} app
7
+ * @returns
8
+ */
9
+ module.exports = (app) => {
10
+ // json-schema标准的版本
11
+ const $schema = "http://json-schema.org/draft-07/schema#";
12
+
13
+ return async (ctx, next) => {
14
+ // 只对API请求做参数校验
15
+ if (ctx.path.indexOf("/api/") < 0) {
16
+ return await next();
17
+ }
18
+
19
+ // 获取参数
20
+ const { params, path, method } = ctx;
21
+ const { body, query, headers } = ctx.request;
22
+
23
+ app.logger.info(`[${method} ${path}] body: ${JSON.stringify(body)}`);
24
+ app.logger.info(`[${method} ${path}] query: ${JSON.stringify(query)}`);
25
+ app.logger.info(`[${method} ${path}] params: ${JSON.stringify(params)}`);
26
+ app.logger.info(`[${method} ${path}] headers: ${JSON.stringify(headers)}`);
27
+
28
+ // 获取routerSchema
29
+ const schema = app.routerSchema[path]?.[method.toLowerCase()];
30
+
31
+ if (!schema) {
32
+ return await next();
33
+ }
34
+
35
+ let valid = true;
36
+
37
+ // ajv 校验器
38
+ let validate;
39
+
40
+ // 校验header
41
+ if (valid && headers && schema.headers) {
42
+ schema.headers.$schema = $schema;
43
+ validate = ajv.compile(schema.headers);
44
+ valid = validate(headers);
45
+ }
46
+
47
+ // 校验body 请求体 {}
48
+ if (valid && body && schema.body) {
49
+ schema.body.$schema = $schema;
50
+ validate = ajv.compile(schema.body);
51
+ valid = validate(body);
52
+ }
53
+
54
+ // 校验query ?a=''&b=''
55
+ if (valid && query && schema.query) {
56
+ schema.query.$schema = $schema;
57
+ validate = ajv.compile(schema.query);
58
+ valid = validate(query);
59
+ }
60
+
61
+ // 校验params /api/project/detail/:id => /api/project/detail/1001
62
+ if (valid && params && schema.params) {
63
+ schema.params.$schema = $schema;
64
+ validate = ajv.compile(schema.params);
65
+ valid = validate(params);
66
+ }
67
+
68
+ if (!valid) {
69
+ ctx.status = 200;
70
+ ctx.body = {
71
+ success: false,
72
+ message: `request validate fial: ${ajv.errorsText(validate.errors)}`,
73
+ code: 442,
74
+ };
75
+
76
+ return;
77
+ }
78
+
79
+ await next();
80
+ };
81
+ };
@@ -0,0 +1,45 @@
1
+ const md5 = require("md5");
2
+
3
+ /**
4
+ * API 签名合法性校验
5
+ */
6
+ module.exports = (app) => {
7
+ return async (ctx, next) => {
8
+ // 只对API请求做签名校验
9
+ if (ctx.path.indexOf("/api") < 0) {
10
+ return await next();
11
+ }
12
+
13
+ const { path, method } = ctx;
14
+ const { headers } = ctx.request;
15
+ const { s_sign: sSign, s_t: st } = headers;
16
+
17
+ // key可以是任意自定义的内容,也可以是生成的 uuid
18
+ const signKey = "ajflskjflaskdjfqoiwefjlaskdjflka";
19
+ const signature = md5(`${signKey}_${st}`);
20
+
21
+ app.logger.info(`[${method} ${path}] signature: ${signature}`);
22
+
23
+ // 如果用户没有传递签名 sSign
24
+ // 或者用户没有传递时间 st
25
+ // 或者[用户传递的签名(sSign)]与[signKey和用户传递的时间(st)生成的签名]不匹配
26
+ // 或者当前时间比用户传递的时间大于一定时间
27
+ if (
28
+ !sSign ||
29
+ !st ||
30
+ signature !== sSign.toLowerCase() ||
31
+ Date.now() - st > 600000
32
+ ) {
33
+ ctx.status = 200;
34
+ ctx.body = {
35
+ success: false,
36
+ message: "signature no correct or time out",
37
+ code: 445, // 445 签名错误
38
+ };
39
+
40
+ return;
41
+ }
42
+
43
+ await next();
44
+ };
45
+ };