@xujingquan/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 (90) hide show
  1. package/.browserslistrc +3 -0
  2. package/.eslintignore +4 -0
  3. package/.eslintrc +63 -0
  4. package/.husky/commit-msg +1 -0
  5. package/.husky/pre-commit +1 -0
  6. package/.prettierignore +16 -0
  7. package/.prettierrc +10 -0
  8. package/README.md +248 -0
  9. package/app/controller/base.js +41 -0
  10. package/app/controller/project.js +75 -0
  11. package/app/controller/view.js +28 -0
  12. package/app/extend/logger.js +39 -0
  13. package/app/middleware/api-params-verify.js +90 -0
  14. package/app/middleware/api-sign-veriyf.js +47 -0
  15. package/app/middleware/error-handler.js +33 -0
  16. package/app/middleware/project-handler.js +30 -0
  17. package/app/middleware.js +45 -0
  18. package/app/pages/asserts/custom.css +13 -0
  19. package/app/pages/boot.js +56 -0
  20. package/app/pages/common/api/business-api.js +19 -0
  21. package/app/pages/common/api/project-api.js +27 -0
  22. package/app/pages/common/request.js +119 -0
  23. package/app/pages/common/utils.js +2 -0
  24. package/app/pages/dashboard/complex-view/header-view/complex-view/sub-menu/sub-menu.vue +20 -0
  25. package/app/pages/dashboard/complex-view/header-view/header-view.vue +116 -0
  26. package/app/pages/dashboard/complex-view/iframe-view/iframe-view.vue +44 -0
  27. package/app/pages/dashboard/complex-view/schema-view/complex-view/search-panel/search-panel.vue +37 -0
  28. package/app/pages/dashboard/complex-view/schema-view/complex-view/table-panel/table-panel.vue +122 -0
  29. package/app/pages/dashboard/complex-view/schema-view/components/component-config.js +23 -0
  30. package/app/pages/dashboard/complex-view/schema-view/components/create-form/create-form.vue +86 -0
  31. package/app/pages/dashboard/complex-view/schema-view/components/detail-panel/detail-panel.vue +82 -0
  32. package/app/pages/dashboard/complex-view/schema-view/components/edit-form/edit-form.vue +115 -0
  33. package/app/pages/dashboard/complex-view/schema-view/hook/schema.js +135 -0
  34. package/app/pages/dashboard/complex-view/schema-view/schema-view.vue +93 -0
  35. package/app/pages/dashboard/complex-view/sider-view/complex-view/sub-menu/sub-menu.vue +21 -0
  36. package/app/pages/dashboard/complex-view/sider-view/sider-view.vue +115 -0
  37. package/app/pages/dashboard/dashboard.vue +93 -0
  38. package/app/pages/dashboard/entry.dashboard.js +45 -0
  39. package/app/pages/store/index.js +4 -0
  40. package/app/pages/store/menu.js +61 -0
  41. package/app/pages/store/project.js +17 -0
  42. package/app/pages/widgets/header-container/asserts/avatar.png +0 -0
  43. package/app/pages/widgets/header-container/asserts/logo.png +0 -0
  44. package/app/pages/widgets/header-container/header-container.vue +111 -0
  45. package/app/pages/widgets/schema-form/complex-view/input/input.vue +141 -0
  46. package/app/pages/widgets/schema-form/complex-view/input-number/input-number.vue +142 -0
  47. package/app/pages/widgets/schema-form/complex-view/select/select.vue +119 -0
  48. package/app/pages/widgets/schema-form/form-item-config.js +23 -0
  49. package/app/pages/widgets/schema-form/schema-form.vue +130 -0
  50. package/app/pages/widgets/schema-search-bar/complex-view/date-range/date-range.vue +50 -0
  51. package/app/pages/widgets/schema-search-bar/complex-view/dynamic-select/dynamic-select.vue +62 -0
  52. package/app/pages/widgets/schema-search-bar/complex-view/input/input.vue +40 -0
  53. package/app/pages/widgets/schema-search-bar/complex-view/select/select.vue +48 -0
  54. package/app/pages/widgets/schema-search-bar/schema-search-bar.vue +121 -0
  55. package/app/pages/widgets/schema-search-bar/search-item-config.js +27 -0
  56. package/app/pages/widgets/schema-table/schema-table.vue +243 -0
  57. package/app/pages/widgets/sider-container/sider-container.vue +26 -0
  58. package/app/public/static/logo.png +0 -0
  59. package/app/public/static/md5.js +950 -0
  60. package/app/public/static/normalize.css +267 -0
  61. package/app/router/project.js +11 -0
  62. package/app/router/view.js +13 -0
  63. package/app/router-schema/project.js +33 -0
  64. package/app/service/base.js +14 -0
  65. package/app/service/project.js +43 -0
  66. package/app/view/entry.tpl +27 -0
  67. package/app/webpack/config/utils.js +49 -0
  68. package/app/webpack/config/webpack-dev.js +55 -0
  69. package/app/webpack/config/webpack-prod.js +192 -0
  70. package/app/webpack/config/webpack.base.js +273 -0
  71. package/app/webpack/dev.js +60 -0
  72. package/app/webpack/libs/blank.js +1 -0
  73. package/app/webpack/prod.js +27 -0
  74. package/babel.config.js +15 -0
  75. package/commitlint.config.js +3 -0
  76. package/config/config.default.js +4 -0
  77. package/elpis-core/env.js +20 -0
  78. package/elpis-core/index.js +86 -0
  79. package/elpis-core/loader/config.js +54 -0
  80. package/elpis-core/loader/controller.js +69 -0
  81. package/elpis-core/loader/extend.js +57 -0
  82. package/elpis-core/loader/middleware.js +66 -0
  83. package/elpis-core/loader/router-schema.js +49 -0
  84. package/elpis-core/loader/router.js +50 -0
  85. package/elpis-core/loader/service.js +69 -0
  86. package/index.js +47 -0
  87. package/jsconfig.json +19 -0
  88. package/model/index.js +103 -0
  89. package/package.json +105 -0
  90. package/test/controller/project.test.js +200 -0
@@ -0,0 +1,3 @@
1
+ > 0.01%
2
+ last 2 version
3
+ not dead
package/.eslintignore ADDED
@@ -0,0 +1,4 @@
1
+ **/node_modules/*
2
+ **/public/*
3
+ **/view/*
4
+ **/docs/*
package/.eslintrc ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "root": true,
3
+ "extends": ["plugin:vue/base", "plugin:vue/recommended"],
4
+ "plugins": ["vue"],
5
+ "env": {
6
+ "browser": true,
7
+ "node": true
8
+ },
9
+ "parser": "vue-eslint-parser",
10
+ "parserOptions": {
11
+ "parser": "babel-eslint",
12
+ "ecmaVersion": 2017,
13
+ "sourceType": "module"
14
+ },
15
+ "rules": {
16
+ "no-unused-vars": [2, { "args": "none" }],
17
+ "strict": "off",
18
+ "valid-jsdoc": "off",
19
+ "jsdoc/require-param-description": "off",
20
+ "jsdoc/require-param-type": "off",
21
+ "jsdoc/check-param-names": "off",
22
+ "jsdoc/require-param": "off",
23
+ "jsdoc/check-tag-names": "off",
24
+ "linebreak-style": "off",
25
+ "array-bracket-spacing": "off",
26
+ "prefer-promise-reject-errors": "off",
27
+ "comma-dangle": "off",
28
+ "newline-per-chained-call": "off",
29
+ "no-loop-func": "off",
30
+ "no-empty": "off",
31
+ "no-else-return": "off",
32
+ "no-unneeded-ternary": "off",
33
+ "no-eval": "off",
34
+ "prefer-destructuring": "off",
35
+ "no-param-reassign": "off",
36
+ "max-len": "off",
37
+ "no-restricted-syntax": "off",
38
+ "no-plusplus": "off",
39
+ "no-useless-escape": "off",
40
+ "no-nested-ternary": "off",
41
+ "radix": "off",
42
+ "arrow-body-style": "off",
43
+ "arrow-parens": "off",
44
+ "vue/multi-word-component-names": "off",
45
+ "vue/valid-v-for": "off",
46
+ "vue/no-multiple-template-root": "off",
47
+ "vue/html-self-closing": "off",
48
+ "vue/html-closing-bracket-newline": "off",
49
+ "vue/html-closing-bracket-spacing": "off",
50
+ "vue/max-attributes-per-line": "off",
51
+ "vue/script-setup-uses-vars": "error",
52
+ "vue/singleline-html-element-content-newline": "off",
53
+ "vue/require-default-prop": "off",
54
+ "vue/multiline-html-element-content-newline": "off",
55
+ "vue/attribute-hyphenation": "off",
56
+ "vue/html-indent": "off"
57
+ },
58
+ "globals": {
59
+ "$": true,
60
+ "axios": true,
61
+ "Vue": true
62
+ }
63
+ }
@@ -0,0 +1 @@
1
+ npx --no-install commitlint --edit "$1"
@@ -0,0 +1 @@
1
+ npx lint-staged
@@ -0,0 +1,16 @@
1
+ package-lock.json
2
+ README.md
3
+ **/.next/*
4
+ **/node_modules/*
5
+ **/out/*
6
+ **/.vscode/*
7
+ **/*.yml
8
+ **/*.svg
9
+ **/*.png
10
+ **/*.jpg
11
+ **/*.gif
12
+ **/*.md
13
+ **/*.ejs
14
+ **/*.min.js
15
+ **/*.min.css
16
+ **/docs/*
package/.prettierrc ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "semi": true,
3
+ "singleQuote": true,
4
+ "tabWidth": 2,
5
+ "trailingComma": "es5",
6
+ "printWidth": 100,
7
+ "bracketSpacing": true,
8
+ "arrowParens": "always",
9
+ "endOfLine": "lf"
10
+ }
package/README.md ADDED
@@ -0,0 +1,248 @@
1
+ # elips
2
+ ## 一个企业级全栈应用框架
3
+
4
+ ### model 配置
5
+ ```js
6
+ {
7
+ mode: 'dashboard', // 模版类型,不同模版类型对应不一样的模版数据结构
8
+ name: '', // 名称
9
+ desc: '', // 描述
10
+ icon: '', // icon
11
+ homePage: '', // 首页(项目配置)
12
+ // 头部菜单
13
+ menu: [
14
+ {
15
+ key: '', // 菜单唯一描述
16
+ name: '', // 菜单名称
17
+ menuType: '', // 枚举值 group / module
18
+ // 当 menuType === group 时,可填
19
+ subMenu: [
20
+ {
21
+ // 可递归 menuItem
22
+ },
23
+ ...,
24
+ ],
25
+ // 当 menuType === module 时,可填
26
+ moduleType: '', // 枚举 iframe(第三方页面)/custom(自定义页面)/schema(架构系统页面)
27
+ // 当 moduleType === sider 时
28
+ // 侧边栏
29
+ siderConfig: {
30
+ menu: [
31
+ {
32
+ // 可递归 menuItem (除 moduleType === sider)
33
+ },
34
+ ...,
35
+ ],
36
+ },
37
+ // 当 menuType === iframe 时
38
+ iframeConfig: {
39
+ path: '', // iframe 路径
40
+ },
41
+ // 当 menuType === custom 时
42
+ customConfig: {
43
+ path: '', // 自定义路由路径
44
+ },
45
+ // 当 menuType === schema 时
46
+ schemaConfig: {
47
+ api: '', // 数据源API(遵循 RESTFUL 规范)
48
+ schema: {
49
+ // 板块数据结构
50
+ type: 'object',
51
+ properties: {
52
+ key: {
53
+ ...schema, // 标准 schema 配置
54
+ type: '', //字段类型
55
+ label: '', // 字段的中文名
56
+ // 字段在 table 中的相关配置
57
+ tableOption:{
58
+ ...elTableColumnConfig, // 标准 el-table-column
59
+ toFixed: 0, // 保留小数点后几位
60
+ visible: true, // 默认为 true(false 表示不在表单中显示)
61
+ },
62
+ // 字段在 search-bar 中的相关配置
63
+ searchOption: {
64
+ ...eleComponentConfig, // 标准 el-component-column 配置
65
+ comType: '', // 配置控件类型 input/select/...
66
+ default: '', // 默认值
67
+
68
+ // comType === 'select'
69
+ enumList: [], // 下拉框可选项
70
+
71
+ // comType === 'dynamicSelect',
72
+ api:''
73
+ },
74
+ // 字段在不同动态 component 中的相关配置,前缀对应 componentConfig 中的键值
75
+ // 如 componentConfig.createForm 这里对应 createFormOption
76
+ createFormOption: {
77
+ ...eleComponentConfig, // 标准 el-component-column 配置
78
+ comType: '', // 控件类型 如 input/select/input-number
79
+ visible: true, // 是否展示 true/false 默认 true
80
+ disabled: false, // 是否禁用 true/false 默认 fasle
81
+ default: '', // 默认值
82
+
83
+ // 当 comType === 'select' 时生效
84
+ enumList: [] // 枚举列表
85
+ },
86
+ // 字段在 editForm 中的相关配置
87
+ editFormOption: {
88
+ ...eleComponentConfig, // 标准 el-component-column 配置
89
+ comType: '', // 控件类型 如 input/select/input-number
90
+ visible: true, // 是否展示 true/false 默认 true
91
+ disabled: false, // 是否禁用 true/false 默认 fasle
92
+ default: '', // 默认值
93
+ // 当 comType === 'select' 时生效
94
+ enumList: [] // 枚举列表
95
+ },
96
+ // 字段在 detailPanel 中的相关配置
97
+ detailPanelOption: {
98
+ ...eleComponentConfig, // 标准 el-component-column 配置
99
+ comType: '', // 控件类型 如 input/select/
100
+ // 当 comType === 'select' 时生效
101
+
102
+ enumList: [] // 枚举列表
103
+ }
104
+ },
105
+ ...,
106
+ },
107
+ },
108
+ required: [], // 标记那些字段是必填项
109
+ // tabel 相关配置
110
+ tableConfig: {
111
+ headerButtons:[{
112
+ label: '', // 按钮中文名
113
+ eventKey: '', // 按钮事件名
114
+ // 按钮事件具体配置
115
+ eventOption: {
116
+ // 当 eventKey === 'showComponent'
117
+ comName: '', // 组件名称
118
+ },
119
+ ...elButtonConfig // 标准的 el-botton 配置
120
+ }, ...],
121
+ rowButtons:[{
122
+ label: '', // 按钮中文名
123
+ eventKey: '', // 按钮事件名
124
+ eventOption: {
125
+ // 当 eventKey === 'showComponent'
126
+ comName: '', // 组件名称
127
+
128
+ // 当 eventKey === 'remove 或者其他的时候'
129
+ params:{
130
+ // paramKey = 参数的键值
131
+ // rowValueKey = 参数值(当格式为 schema::tableKey 的时候,到table 中找到相应的字段)
132
+ // 例如 user_id: schema::user_id
133
+ paramKey: rowValueKey
134
+
135
+ }
136
+ }, // 按钮事件具体配置
137
+ ...elButtonConfig // 标准的 el-botton 配置
138
+ },...]
139
+ },
140
+ // search-bar 相关配置
141
+ searchConfig: {},
142
+ // 动态组件,相关配置
143
+ componentConfig: {
144
+ // create-form 表单相关配置
145
+ createForm: {
146
+ title: '', // 表单标题
147
+ saveBtnText: '' , // 保存按钮
148
+ },
149
+ // edit-form 表单相关配置
150
+ editForm: {
151
+ mainKey: '', // 表单主键,用于唯一标识要修改的数据对象
152
+ title: '', // 表单标题
153
+ saveBtnText: '' , // 保存按钮
154
+ },
155
+ // detail-panel 相关配置
156
+ detailPanel:{
157
+ mainKey: '', // 表单主键,用于唯一标识要修改的数据对象
158
+ title: '', // 表单标题
159
+ saveBtnText: '' , // 保存按钮
160
+ }
161
+ // ...支持用户动态扩展
162
+ },
163
+ },
164
+ },
165
+ ...,
166
+ ],
167
+ }
168
+ ```
169
+
170
+ ### 服务端启动
171
+ ```js
172
+ const { serverStart } = require('@xujingquan/elpis');
173
+
174
+ // 服务端启动
175
+ const app = serverStart({
176
+ name: '', // 项目名称
177
+ homePage: '', // 项目首页
178
+ });
179
+ ```
180
+
181
+ ### 自定义服务端
182
+ ├── app/
183
+ │ ├── router-schema
184
+ │ ├── router
185
+ │ ├── controller
186
+ │ ├── service
187
+ │ ├── extend
188
+ ├── config/
189
+
190
+ ### 前端构建
191
+ ```js
192
+ const { frontendBuild } = require('@xujingquan/elpis');
193
+
194
+ // 编译构建前端工程
195
+ frontendBuild(process.env._ENV);
196
+ ```
197
+
198
+ ### 自定义页面扩展
199
+ * 在 `app/pages/` 目录写一个entry.xxx.js
200
+
201
+ ### 在 dashboard / custom-view 自定义页面扩展
202
+ * 在 `app/pages/dashboard/xxx`
203
+
204
+ ### dashboard / schema-view / components 动态组件扩展
205
+ - 1.在 `app/pages/dashboard/complex-view/schema-view/components` 下写组件
206
+ - 2.配置到 `app/pages/dashboard/complex-view/schema-view/components/component-config.js`
207
+ ```js
208
+ import demoComponent from './components/demo-component.vue';
209
+
210
+ const ComponentConfig = {
211
+ demoComponent: {
212
+ component: demoComponent,
213
+ },
214
+ };
215
+
216
+ export default ComponentConfig;
217
+ ```
218
+
219
+ ### schema-form 控件控制
220
+ - 1.在 `app/pages/widgets/schema-form/complex-view` 下写组件
221
+ - 2.配置到 `app/pages/widgets/schema-form/form-item-config.js`
222
+ ``` javascript
223
+ import demoFormItem from './complex-view/demo-form-item/demo-form-item.vue';
224
+
225
+ const FormItemConfig = {
226
+ demoFormItem: {
227
+ component: demoFormItem,
228
+ },
229
+ };
230
+
231
+ export default FormItemConfig;
232
+ ```
233
+
234
+ ### schema-search-bar 控件控制
235
+ - 1.在 `app/pages/widgets/schema-search-bar/complex-view` 下写组件
236
+ - 2.配置到 `app/pages/widgets/schema-search-bar/search-item-config.js`
237
+
238
+ ```js
239
+ import demoSearchItem from './complex-view/demo-search-item/demo-search-item.vue';
240
+
241
+ const SearchItemConfig = {
242
+ demoSearchItem: {
243
+ component: demoSearchItem,
244
+ },
245
+ };
246
+
247
+ export default SearchItemConfig;
248
+ ```
@@ -0,0 +1,41 @@
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
+ }
11
+
12
+ /**
13
+ * API 处理成功时统一返回结构
14
+ * @params {object} ctx 上下文
15
+ * @params {object} data 核心数据
16
+ * @params {object} metadata 附加数据
17
+ */
18
+ success(ctx, data = {}, metadata = {}) {
19
+ ctx.status = 200;
20
+ ctx.body = {
21
+ success: true,
22
+ data,
23
+ metadata
24
+ };
25
+ }
26
+
27
+ /**
28
+ * API 处理失败时统一返回结构
29
+ * @params {object} ctx 上下文
30
+ * @params {object} message 错误信息
31
+ * @params {number} code 附加数据
32
+ */
33
+ fail(ctx, message, code) {
34
+ ctx.body = {
35
+ success: false,
36
+ message,
37
+ code
38
+ };
39
+ }
40
+ };
41
+ };
@@ -0,0 +1,75 @@
1
+ /**
2
+ * 控制层
3
+ * */
4
+ module.exports = (app) => {
5
+ const BaseController = require('./base')(app);
6
+ // 后续在 controller loader 中会使用 new XXX()
7
+ return class ProjectController extends BaseController {
8
+ /**
9
+ * 根据 proj_key 获取项目配置
10
+ * */
11
+ get(ctx) {
12
+ const { proj_key: projKey } = ctx.request.query;
13
+
14
+ const { project: projectService } = app.service;
15
+ const projConfig = projectService.get(projKey);
16
+
17
+ if (!projConfig) {
18
+ this.fail(ctx, '获取项目异常', 50000);
19
+ return;
20
+ }
21
+
22
+ this.success(ctx, projConfig);
23
+ }
24
+ /**
25
+ * 获取当前 projectKey 对应模型下的项目列表(如果无 projectKey 全量获取)
26
+ * */
27
+ async getList(ctx) {
28
+ const { proj_key: projKey } = ctx.request.query;
29
+
30
+ const { project: projectService } = app.service;
31
+ const projectList = await projectService.getList({ projKey });
32
+
33
+ // 构造关键数据 list
34
+ const dtoProjectList = projectList.map((item) => {
35
+ const { modelKey, key, name, desc, homePage } = item;
36
+ return { modelKey, key, name, desc, homePage };
37
+ });
38
+
39
+ this.success(ctx, dtoProjectList);
40
+ }
41
+ /**
42
+ * 获取所有模型与项目的结构化数据
43
+ */
44
+ async getModelList(ctx) {
45
+ const { project: projectService } = app.service;
46
+ const modelList = await projectService.getModelList();
47
+
48
+ // 构造返回结构,只返回关键数据
49
+ const dtoModelList = modelList.reduce((preList, item) => {
50
+ const { model, project } = item;
51
+
52
+ // 构造 model 关键数据
53
+ const { key, name, desc } = model;
54
+ const dtoModel = { key, name, desc };
55
+
56
+ // 构造 project 构建数据
57
+ const dtoProject = Object.keys(project).reduce((preObj, projKey) => {
58
+ const { key, name, desc, homePage } = project[projKey];
59
+ preObj[projKey] = { key, name, desc, homePage };
60
+ return preObj;
61
+ }, {});
62
+
63
+ preList.push({
64
+ model: dtoModel,
65
+ project: dtoProject,
66
+ });
67
+
68
+ return preList;
69
+ }, []);
70
+
71
+ // success 继承 BaseController 方法
72
+ this.success(ctx, dtoModelList);
73
+ }
74
+ };
75
+ };
@@ -0,0 +1,28 @@
1
+ /**
2
+ * 控制层
3
+ * */
4
+ module.exports = (app) => {
5
+ // 后续在 controller loader 中会使用 new XXX()
6
+ return class ViewController {
7
+ /**
8
+ * 渲染页面
9
+ * @param {object} ctx 上下文
10
+ */
11
+ async renderPage(ctx) {
12
+ const { query, params } = ctx.request;
13
+
14
+ app.logger.info(`[ViewController] query:${JSON.stringify(query)}`);
15
+ app.logger.info(`[ViewController] params:${JSON.stringify(params)}`);
16
+
17
+ // 这个是 koa-nunjucks-2 渲染引擎中间件 使用后挂载在上下文对象 ctx 中的 render方法
18
+ // render 第二个参数可以把模版预留的变量注入渲染模版
19
+ await ctx.render(`dist/entry.${ctx.params.page}`, {
20
+ // options、env 启动服务挂载在 app 上的对象
21
+ name: app.options?.name,
22
+ env: app.env.get(),
23
+ options: JSON.stringify(app.options),
24
+ projKey: ctx.query?.proj_key,
25
+ });
26
+ }
27
+ };
28
+ };
@@ -0,0 +1,39 @@
1
+ const log4js = require('log4js'); // 日志
2
+
3
+ /**
4
+ * 日志工具
5
+ * 外部调用 app.logger.log app.logger.error
6
+ */
7
+
8
+ module.exports = app => {
9
+ let logger;
10
+ // 本地环境使用 console 其他环境使用 log4js
11
+ if (app.env.isLocal()) {
12
+ // 打印在控制台即可
13
+ logger = console;
14
+ } else {
15
+ // 把日志输出并落地带磁盘(日志磁盘)
16
+ log4js.configure({
17
+ appenders: {
18
+ console: {
19
+ type: 'console'
20
+ },
21
+ // 日志文件切分
22
+ dateFile: {
23
+ type: 'dateFile',
24
+ filename: './logs/application.log',
25
+ pattern: '.yyyy-MM-dd'
26
+ }
27
+ },
28
+ categories: {
29
+ default: {
30
+ appenders: ['console', 'dateFile'],
31
+ level: 'trace'
32
+ }
33
+ }
34
+ });
35
+ logger = log4js.getLogger();
36
+ }
37
+
38
+ return logger;
39
+ };
@@ -0,0 +1,90 @@
1
+ const Ajv = require('ajv');
2
+ const ajv = new Ajv();
3
+ /**
4
+ * API 参数校验
5
+ */
6
+ module.exports = (app) => {
7
+ // 校验版本
8
+ const $schema = 'http://json-schema.org/draft-07/schema#';
9
+ return async (ctx, next) => {
10
+ // 只对API请求做参数校验
11
+ if (ctx.path.indexOf('/api/') < 0) {
12
+ // 不是API不做校验
13
+ return await next();
14
+ }
15
+
16
+ const { request, params, path, method } = ctx;
17
+ // 获取请求参数
18
+ const { body, query, headers } = request;
19
+
20
+ // 日志输出方便问题排查
21
+ app.logger.info(`[${method} ${path}] body:${JSON.stringify(body)}`);
22
+ app.logger.info(`[${method} ${path}] query:${JSON.stringify(query)}`);
23
+ app.logger.info(`[${method} ${path}] params:${JSON.stringify(params)}`);
24
+ app.logger.info(`[${method} ${path}] headers:${JSON.stringify(headers)}`);
25
+
26
+ const sheama = app.routerSchema[path]?.[method.toLowerCase()];
27
+
28
+ if (!sheama) {
29
+ // 没有校验规则直接通过
30
+ return await next();
31
+ }
32
+
33
+ let valid = true;
34
+
35
+ // ajv 校验器
36
+ let validate;
37
+
38
+ // 检验 headers
39
+ if (valid && headers && sheama.headers) {
40
+ // 添加 $schema 版本
41
+ sheama.headers.$schema = $schema;
42
+ // ajv.compile 解析 sheama.headers 返回校验器
43
+ validate = ajv.compile(sheama.headers);
44
+ // 校验器去解析参数是否满足,返回校验结构
45
+ valid = validate(headers);
46
+ }
47
+
48
+ // 校验body
49
+ if (valid && body && sheama.body) {
50
+ // 添加 $schema 版本
51
+ sheama.body.$schema = $schema;
52
+ // ajv.compile 解析 sheama.body 返回校验器
53
+ validate = ajv.compile(sheama.body);
54
+ // 校验器去解析参数是否满足,返回校验结构
55
+ valid = validate(body);
56
+ }
57
+
58
+ // 校验query
59
+ if (valid && query && sheama.query) {
60
+ // 添加 $schema 版本
61
+ sheama.query.$schema = $schema;
62
+ // ajv.compile 解析 sheama.query 返回校验器
63
+ validate = ajv.compile(sheama.query);
64
+ // 校验器去解析参数是否满足,返回校验结构
65
+ valid = validate(query);
66
+ }
67
+
68
+ // 校验params
69
+ if (valid && params && sheama.params) {
70
+ // 添加 $schema 版本
71
+ sheama.params.$schema = $schema;
72
+ // ajv.compile 解析 sheama.params 返回校验器
73
+ validate = ajv.compile(sheama.params);
74
+ // 校验器去解析参数是否满足,返回校验结构
75
+ valid = validate(params);
76
+ }
77
+
78
+ if (valid === false) {
79
+ ctx.status = 200;
80
+ ctx.body = {
81
+ success: false,
82
+ message: `request validate fail: ${ajv.errorsText(validate.errors)}`,
83
+ code: 442,
84
+ };
85
+ return;
86
+ }
87
+
88
+ await next();
89
+ };
90
+ };
@@ -0,0 +1,47 @@
1
+ const md5 = require('md5');
2
+ /**
3
+ * API 签名合法校验
4
+ * */
5
+ module.exports = app => {
6
+ return async (ctx, next) => {
7
+ const { path, method, request } = ctx;
8
+ // 只对API请求做签名校验
9
+ if (path.indexOf('/api') < 0) {
10
+ // 不是API不做校验
11
+ return await next();
12
+ }
13
+
14
+ const { headers } = request;
15
+ /**
16
+ * s_sign 签名
17
+ * s_t 时间
18
+ */
19
+ const { s_sign: sSign, s_t: st } = headers;
20
+
21
+ const signKey = 'da98fc11-295c-a64f-5b31-d6dc4055dd06'; // UUID
22
+ const signature = md5(`${signKey}_${st}`); // 签名
23
+ app.logger.info(`[${method} ${path}] signature:${signature}`);
24
+
25
+ /**
26
+ * 用户没有传递签名
27
+ * 用户没有传递时间
28
+ * 签名不正确
29
+ * 时间超过 600s(超出时效)
30
+ */
31
+ if (
32
+ !sSign ||
33
+ !st ||
34
+ signature !== sSign.toLowerCase() ||
35
+ Date.now() - st > 1000 * 600
36
+ ) {
37
+ ctx.status = 200;
38
+ ctx.body = {
39
+ success: false,
40
+ message: 'signature not correct or api timerout',
41
+ code: 445
42
+ };
43
+ return;
44
+ }
45
+ await next();
46
+ };
47
+ };