@siven-li/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 (78) hide show
  1. package/.eslintignore +3 -0
  2. package/.eslintrc +55 -0
  3. package/README.md +201 -0
  4. package/app/controller/base.js +37 -0
  5. package/app/controller/project.js +65 -0
  6. package/app/controller/view.js +20 -0
  7. package/app/extend/logger.js +35 -0
  8. package/app/middleware/api-params-verify.js +97 -0
  9. package/app/middleware/api-sign-verify.js +30 -0
  10. package/app/middleware/error-handler.js +34 -0
  11. package/app/middleware/project-handler.js +22 -0
  12. package/app/middleware.js +37 -0
  13. package/app/pages/assets/custom.css +16 -0
  14. package/app/pages/boot.js +53 -0
  15. package/app/pages/common/curl.js +74 -0
  16. package/app/pages/common/utils.js +2 -0
  17. package/app/pages/dashboard/complex-view/header-view/complex-view/sub-menu/sub-menu.vue +35 -0
  18. package/app/pages/dashboard/complex-view/header-view/header-view.vue +132 -0
  19. package/app/pages/dashboard/complex-view/iframe-view/iframe-view.vue +45 -0
  20. package/app/pages/dashboard/complex-view/schema-view/complex-view/search-panel/search-panel.vue +37 -0
  21. package/app/pages/dashboard/complex-view/schema-view/complex-view/table-panel/table-panel.vue +130 -0
  22. package/app/pages/dashboard/complex-view/schema-view/components/component-config.js +23 -0
  23. package/app/pages/dashboard/complex-view/schema-view/components/create-form/create-form.vue +90 -0
  24. package/app/pages/dashboard/complex-view/schema-view/components/detail-panel/detail-panel.vue +110 -0
  25. package/app/pages/dashboard/complex-view/schema-view/components/edit-form/edit-form.vue +133 -0
  26. package/app/pages/dashboard/complex-view/schema-view/hook/schema.js +125 -0
  27. package/app/pages/dashboard/complex-view/schema-view/schema-view.vue +97 -0
  28. package/app/pages/dashboard/complex-view/sider-view/complex-view/sub-menu/sub-menu.vue +35 -0
  29. package/app/pages/dashboard/complex-view/sider-view/sider-view.vue +127 -0
  30. package/app/pages/dashboard/dashboard.vue +94 -0
  31. package/app/pages/dashboard/entry.dashboard.js +43 -0
  32. package/app/pages/store/index.js +3 -0
  33. package/app/pages/store/menu.js +59 -0
  34. package/app/pages/store/project.js +17 -0
  35. package/app/pages/widgets/header-container/assets/avatar.png +0 -0
  36. package/app/pages/widgets/header-container/assets/logo.png +0 -0
  37. package/app/pages/widgets/header-container/header-container.vue +124 -0
  38. package/app/pages/widgets/schema-form/complex-view/input/input.vue +143 -0
  39. package/app/pages/widgets/schema-form/complex-view/input-number/input-number.vue +141 -0
  40. package/app/pages/widgets/schema-form/complex-view/select/select.vue +127 -0
  41. package/app/pages/widgets/schema-form/form-item-config.js +22 -0
  42. package/app/pages/widgets/schema-form/schema-form.vue +129 -0
  43. package/app/pages/widgets/schema-search-bar/complex-view/date-range/date-range.vue +49 -0
  44. package/app/pages/widgets/schema-search-bar/complex-view/dynamic-select/dynamic-select.vue +65 -0
  45. package/app/pages/widgets/schema-search-bar/complex-view/input/input.vue +43 -0
  46. package/app/pages/widgets/schema-search-bar/complex-view/select/select.vue +50 -0
  47. package/app/pages/widgets/schema-search-bar/schema-search-bar.vue +133 -0
  48. package/app/pages/widgets/schema-search-bar/search-item-config.js +26 -0
  49. package/app/pages/widgets/schema-table/schema-table.vue +234 -0
  50. package/app/pages/widgets/sider-container/sider-container.vue +32 -0
  51. package/app/public/static/logo.png +0 -0
  52. package/app/public/static/normalize.css +239 -0
  53. package/app/router/project.js +6 -0
  54. package/app/router/view.js +9 -0
  55. package/app/router-schema/project.js +30 -0
  56. package/app/service/base.js +12 -0
  57. package/app/service/project.js +44 -0
  58. package/app/view/entry.tpl +26 -0
  59. package/app/webpack/config/webpack.base.js +194 -0
  60. package/app/webpack/config/webpack.dev.js +58 -0
  61. package/app/webpack/config/webpack.prod.js +114 -0
  62. package/app/webpack/dev.js +48 -0
  63. package/app/webpack/libs/blank.js +1 -0
  64. package/app/webpack/prod.js +18 -0
  65. package/config/config.default.js +3 -0
  66. package/elpis-core/env.js +20 -0
  67. package/elpis-core/index.js +88 -0
  68. package/elpis-core/loader/config.js +55 -0
  69. package/elpis-core/loader/controller.js +56 -0
  70. package/elpis-core/loader/extend.js +45 -0
  71. package/elpis-core/loader/middleware.js +58 -0
  72. package/elpis-core/loader/router-schema.js +38 -0
  73. package/elpis-core/loader/router.js +40 -0
  74. package/elpis-core/loader/service.js +56 -0
  75. package/index.js +39 -0
  76. package/model/index.js +104 -0
  77. package/package.json +93 -0
  78. package/test/controll/project.test.js +203 -0
package/.eslintignore ADDED
@@ -0,0 +1,3 @@
1
+ node_modules/
2
+ public/
3
+ docs/
package/.eslintrc ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "extends": [
3
+ "plugin:vue/base",
4
+ "plugin:vue/recommended",
5
+ ],
6
+ "plugins": ["vue"],
7
+ "env": {
8
+ "browser": true,
9
+ "node": true
10
+ },
11
+ "parser": "vue-eslint-parser",
12
+ "parserOptions": {
13
+ "parser": "babel-eslint",
14
+ "ecmaVersion": 2017,
15
+ "sourceType": "module"
16
+ },
17
+ "rules": {
18
+ "no-unused-vars": [2, {"args": "none"}],
19
+ "strict": "off",
20
+ "valid-jsdoc": "off",
21
+ "jsdoc/require-param-description": "off",
22
+ "jsdoc/require-param-type": "off",
23
+ "jsdoc/check-param-names": "off",
24
+ "jsdoc/require-param": "off",
25
+ "jsdoc/check-tag-names": "off",
26
+ "linebreak-style": "off",
27
+ "array-bracket-spacing": "off",
28
+ "prefer-promise-reject-errors": "off",
29
+ "comma-dangle": "off",
30
+ "newline-per-chained-call": "off",
31
+ "no-loop-func": "off",
32
+ "no-empty": "off",
33
+ "no-else-return": "off",
34
+ "no-unneeded-ternary": "off",
35
+ "no-eval": "off",
36
+ "prefer-destructuring": "off",
37
+ "no-param-reassign": "off",
38
+ "max-len": "off",
39
+ "no-restricted-syntax": "off",
40
+ "no-plusplus": "off",
41
+ "no-useless-escape": "off",
42
+ "no-nested-ternary": "off",
43
+ "radix": "off",
44
+ "arrow-body-style": "off",
45
+ "arrow-parens": "off",
46
+ "vue/multi-word-component-names": "off",
47
+ "vue/valid-v-for": "off",
48
+ "vue/no-multiple-template-root": "off"
49
+ },
50
+ "globals": {
51
+ "$": true,
52
+ "axios": true,
53
+ "Vue": true
54
+ }
55
+ }
package/README.md ADDED
@@ -0,0 +1,201 @@
1
+ # Elips
2
+ ## 企业级全栈应用框架
3
+
4
+ ### model配置
5
+ ```javascript
6
+ {
7
+ mode: 'dashboard', // 模板类型,不同模板类型对应不一样的模板数据结构
8
+ name: '', // 名称
9
+ desc: '', // 描述
10
+ icon: '', // icon
11
+ homePage: '', // 首页(项目配置)
12
+ // 头部菜单
13
+ menu: [{
14
+ key: '', // 菜单唯一描述
15
+ name: '', // 菜单名称
16
+ menuType: '', // 枚举值: group / module
17
+
18
+ // 当 menuType === group 时,可填
19
+ subMenu: [{
20
+ // 可递归 menuItem
21
+ }, ...],
22
+
23
+ // 当 menuType === module 时,可填
24
+ moduleType: '', // 枚举值: sider / iframe / custom / schema
25
+
26
+ // 当 moduleType === sider 时
27
+ siderConfig: {
28
+ menu: [{
29
+ // 可递归 menuItem(除 moduleType === sider)
30
+ }, ...]
31
+ },
32
+
33
+ // 当 moduleType === iframe 时
34
+ iframeConfig: {
35
+ path: '' // iframe 路径
36
+ },
37
+
38
+ // 当 moduleType === custom 时
39
+ customConfig: {
40
+ path: '' // 自定路由路径
41
+ },
42
+
43
+ // 当 moduleType === schema 时
44
+ schemaConfig: {
45
+ api: '', // 数据源API(遵循 RESTFUL 规范)
46
+ schema: { // 板块数据结构
47
+ type: 'object',
48
+ properties: {
49
+ key: {
50
+ ...schema, // 标准 schema 配置
51
+ type: '', // 字段类型
52
+ label: '', // 字段中文名
53
+ // 字段在 table 中的相关配置
54
+ tableOption: {
55
+ ...elTableColumnConfig, // 标准 el-table-column 配置
56
+ toFixed: 0, // 保留小数点第几位
57
+ visible: true, // 是否显示在 table 中(只有配置为 false 时才不展示)
58
+ },
59
+ // 字段在 search 中的相关配置
60
+ searchOption: {
61
+ ...elComponentConfig, // 标准 el-component 配置
62
+ comType: '', // 组件类型 input / select / date-picker / cascader / ...
63
+ default: '', // 搜索默认值
64
+ // comType 为 select / cascader 时,可填
65
+ enumList: [], // 下拉选项列表
66
+ // comType 为 dynamicSelect 时
67
+ api: '',
68
+ },
69
+ // 字段在不同 component 中的相关配置,前缀对应 componentConfig 中的键值
70
+ // 如:componentConfig.createForm,这里对应 createFormOption
71
+ // 字段在 createForm 中相关配置
72
+ createFormOption: {
73
+ ...elFormItemConfig, // 标准 el-form-item 配置
74
+ comType: '', // 空间类型 input / select / input-number / ...
75
+ visible: true, // 是否展示 (true/false),默认为 true
76
+ disabled: false, // 是否禁用 (true/false),默认为 false
77
+ default: '', // 默认值
78
+
79
+ // comType === 'select'时生效
80
+ enumList: [], // 下拉选项列表
81
+ },
82
+ editFormOption: {
83
+ ...elFormItemConfig, // 标准 el-form-item 配置
84
+ comType: '', // 空间类型 input / select / input-number / ...
85
+ visible: true, // 是否展示 (true/false),默认为 true
86
+ disabled: false, // 是否禁用 (true/false),默认为 false
87
+ default: '', // 默认值
88
+
89
+ // comType === 'select'时生效
90
+ enumList: [], // 下拉选项列表
91
+ },
92
+ detailPanelOption: {
93
+ ...elFormItemConfig, // 标准 el-form-item 配置
94
+ }
95
+ },
96
+ ...
97
+ },
98
+ required: [], // 标记哪些字段是必填项
99
+ },
100
+ // table 相关配置
101
+ tableConfig: {
102
+ headerButtons: [{
103
+ label: '', // 按钮名称
104
+ eventKey: '', // 按钮事件名称
105
+ // 按钮事件的具体配置
106
+ eventOption: {
107
+ // 当 eventKey === 'showComponent'
108
+ comName: '', // 组件名称
109
+ },
110
+ ...elButtonConfig // 标准 el-button 配置
111
+ }, ...],
112
+
113
+ rowButtons: [{
114
+ label: '', // 按钮名称
115
+ eventKey: '', // 按钮事件名称
116
+
117
+ // 按钮事件的具体配置
118
+ eventOption: {
119
+ // 当 eventKey === 'showComponent'
120
+ comName: '', // 组件名称
121
+
122
+ // 当 eventKey === 'remove'
123
+ params: {
124
+ // paramKey = 参数的键值
125
+ // rowValueKey = 参数值,格式为 schema::tableKey,到 table中找相应的字段
126
+ paramKey: rowValueKey
127
+ }
128
+ },
129
+ ...elButtonConfig // 标准 el-button 配置
130
+ }, ...]
131
+ },
132
+ searchConfig: {}, // search-bar 相关配置
133
+ // 动态组件相关配置
134
+ componentConfig: {
135
+ // create-form 表单相关配置
136
+ createForm: {
137
+ title: '', // 表单标题
138
+ saveBtnText: '', // 保存按钮文本
139
+ },
140
+ // edit-form 表单相关配置
141
+ editForm: {
142
+ mainKey: '', // 表单主键
143
+ title: '', // 表单标题
144
+ saveBtnText: '', // 保存按钮文本
145
+ },
146
+ // detail-panel 详情面板相关配置
147
+ detailPanel: {
148
+ mainKey: '', // 表单主键
149
+ title: ''
150
+ }
151
+ // ...支持用户动态扩展
152
+ }
153
+ }
154
+ }, ...]
155
+ }
156
+ ```
157
+
158
+ ### 服务端启动
159
+ ```javascript
160
+ const {
161
+ serverStart
162
+ } = require('@siven-li/elpis');
163
+
164
+ // 启动 elpis 服务
165
+ const app = serverStart({});
166
+ ```
167
+
168
+ ### 自定义服务端
169
+ - router-schema
170
+ - router
171
+ - controller
172
+ - service
173
+ - extend
174
+ - config
175
+
176
+ ### 前端构建
177
+ ```javascript
178
+ const { frontendBuild } = require('@siven-li/elpis');
179
+
180
+ // 编译构建前端工程
181
+ frontendBuild(process.env._ENV);
182
+ ```
183
+
184
+ ### 自定义页面扩展
185
+ * 在 `app/pages/` 目录下写入口 entry.xxx.js
186
+
187
+ ### dashboard / custom-view 自定义页面扩展
188
+ * 在 `app/pages/dashboard/xxxx` 下写页面
189
+
190
+ ### dashboard / schema-view /components 动态组件扩展
191
+ 1. 在 `app/pages/dashboard/complex-view/schema-view/components` 下写组件
192
+ 2. 配置到 `app/pages/dashboard/complex-view/schema-view/components/component-config.js`
193
+
194
+ ### schema-form 控件扩展
195
+ 1. 在 `app/widgets/schema-form/complex-view` 下写组件
196
+ 2. 配置到 `app/widgets/schema-form/form-item-config.js`
197
+
198
+ ### schema-search-bar 控件扩展
199
+ 1. 在 `app/widgets/schema-search-bar/complex-view` 下写组件
200
+ 2. 配置到 `app/widgets/schema-search-bar/search-item-config.js`
201
+
@@ -0,0 +1,37 @@
1
+ module.exports = (app) => class BaseController {
2
+ /**
3
+ * controller 基类
4
+ * 统一收拢 controller 相关的公共方法
5
+ */
6
+ constructor() {
7
+ this.app = app;
8
+ this.config = app.config;
9
+ }
10
+ /**
11
+ * API成功时统一返回结构
12
+ * @params {object} ctx 上下文
13
+ * @params {object} data 核心数据
14
+ * @params {object} metadata 附加数据
15
+ */
16
+ success(ctx, data = {}, metadata = {}) {
17
+ ctx.status = 200;
18
+ ctx.body = {
19
+ success: true,
20
+ data,
21
+ metadata
22
+ }
23
+ }
24
+ /**
25
+ * API失败时统一返回结构
26
+ * @params {object} ctx 上下文
27
+ * @params {object} message 错误信息
28
+ * @params {object} code 错误码
29
+ */
30
+ fail(ctx, message, code) {
31
+ ctx.body = {
32
+ success: false,
33
+ message,
34
+ code
35
+ }
36
+ }
37
+ }
@@ -0,0 +1,65 @@
1
+ module.exports = (app) => {
2
+ const BaseController = require('./base')(app);
3
+ return class ProjectController extends BaseController {
4
+ /**
5
+ * 根绝 proj_key 获取项目配置
6
+ */
7
+ get(ctx) {
8
+ const { proj_key: projKey } = ctx.request.query;
9
+ const { project: projectService } = app.service;
10
+ const projConfig = projectService.get({ projKey });
11
+
12
+ if (!projConfig) return this.fail(ctx, '项目配置不存在', 50000);
13
+ this.success(ctx, projConfig);
14
+ }
15
+ /**
16
+ * 获取当前 projectKey 对应模型下的项目列表(如果无 projectKey,全量获取)
17
+ */
18
+ getList(ctx) {
19
+ const { proj_key: projKey } = ctx.request.query;
20
+ const { project: projectService } = app.service;
21
+ const projectList = projectService.getList({ projKey });
22
+
23
+ // 构造关键数据 list
24
+ const dtoProjectList = projectList.map(item => {
25
+ const { modelKey, key, name, desc, homePage } = item;
26
+ return { modelKey, key, name, desc, homePage };
27
+ });
28
+
29
+ this.success(ctx, dtoProjectList);
30
+ }
31
+ /**
32
+ * 获取所有模型与项目的结构化数据
33
+ * @param {*} ctx
34
+ */
35
+ async getModelList(ctx) {
36
+ const { project: projectService } = app.service;
37
+ const modelList = await projectService.getModelList();
38
+
39
+ // 构造返回结果,只返回关键数据
40
+ const dtoModelList = modelList.reduce((preList, item) => {
41
+ const { model, project } = item;
42
+
43
+ // 构造 model 关键数据
44
+ const { key, name, desc } = model;
45
+ const dtoModel = { key, name, desc };
46
+
47
+ // 构造 project 关键数据
48
+ const dtoProject = Object.keys(project).reduce((preObj, projKey) => {
49
+ const { key, name, desc, homePage } = project[projKey];
50
+ preObj[projKey] = { key, name, desc, homePage };
51
+ return preObj;
52
+ }, {});
53
+
54
+ preList.push({
55
+ model: dtoModel,
56
+ project: dtoProject
57
+ })
58
+
59
+ return preList;
60
+ }, [])
61
+
62
+ this.success(ctx, dtoModelList);
63
+ }
64
+ }
65
+ }
@@ -0,0 +1,20 @@
1
+ module.exports = (app) => {
2
+ return class ViewController {
3
+ /**
4
+ * 渲染页面
5
+ * @param {Object} ctx 上下文对象
6
+ */
7
+ async renderPage(ctx) {
8
+ // 注册了koa-nunjucks-2中间件,所以ctx上会有render方法,这里的本质就是服务端渲染(SSR - server side rendering)
9
+ app.logger.info(`[ViewController] query: ${JSON.stringify(ctx.query)}`);
10
+ app.logger.info(`[ViewController] params: ${JSON.stringify(ctx.params)}`)
11
+
12
+ await ctx.render(`dist/entry.${ctx.params.page}`, {
13
+ projKey: ctx.query?.proj_key,
14
+ name: app.options.name,
15
+ env: app.env.get(),
16
+ options: JSON.stringify(app.options)
17
+ })
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,35 @@
1
+ const log4js = require('log4js');
2
+
3
+ /**
4
+ * 日志工具
5
+ * 外部调用 app.logger.info app.logger.error
6
+ */
7
+ module.exports = (app) => {
8
+ let logger;
9
+ if (app.env.isLocal()) {
10
+ // 本地环境打印在控制台
11
+ logger = console;
12
+ } else {
13
+ // 把日志输出并落入磁盘(日志落盘)
14
+ log4js.configure({
15
+ appenders: {
16
+ console: {
17
+ type: 'console'
18
+ },
19
+ dateFile: {
20
+ type: 'dateFile',
21
+ filename: './logs/application.log',
22
+ pattern: '.yyyy-MM-dd'
23
+ }
24
+ },
25
+ categories: {
26
+ default: {
27
+ appenders: ['console', 'dateFile'],
28
+ level: 'trace'
29
+ }
30
+ }
31
+ });
32
+ logger = log4js.getLogger();
33
+ }
34
+ return logger;
35
+ }
@@ -0,0 +1,97 @@
1
+ const Ajv = require('ajv');
2
+ const ajv = new Ajv();
3
+
4
+ /**
5
+ * API 参数校验
6
+ */
7
+ module.exports = (app) => {
8
+ const $schema = 'http://json-schema.org/draft-07/schema#';
9
+
10
+ return async (ctx, next) => {
11
+ // 只对 API 请求做签名校验
12
+ if (ctx.path.indexOf('/api/') < 0) {
13
+ return await next();
14
+ };
15
+
16
+ // 此时ctx.params还未被添加到路由上,先执行下一个中间件(包括路由中间件)
17
+ await next();
18
+
19
+ // 获取请求参数
20
+ const { body, query, headers } = ctx.request;
21
+ const { params, path, method } = ctx;
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}] headers: ${JSON.stringify(headers)}`);
26
+ app.logger.info(`[${method} ${path}] params: ${JSON.stringify(params)}`);
27
+
28
+ // 查找匹配的 schema(支持动态路由)
29
+ let schema = null;
30
+ const methodLower = method.toLowerCase();
31
+
32
+ // 遍历所有路由 schema,查找匹配的动态路由
33
+ Object.keys(app.routerSchema).forEach(schemaPath => {
34
+ // 如果已找到匹配的 schema,跳过
35
+ if (schema) return;
36
+
37
+ // 检查是否为动态路由(包含 :param)
38
+ if (schemaPath.includes(':')) {
39
+ // 构建正则表达式
40
+ const regexPath = schemaPath.replace(/:\w+/g, '([^/]+)');
41
+ const regex = new RegExp(`^${regexPath}$`);
42
+
43
+ // 匹配请求路径
44
+ if (regex.test(path)) {
45
+ schema = app.routerSchema[schemaPath][methodLower];
46
+ }
47
+ } else {
48
+ // 静态路由直接匹配
49
+ if (schemaPath === path) {
50
+ schema = app.routerSchema[schemaPath][methodLower];
51
+ }
52
+ }
53
+ });
54
+
55
+
56
+ if (!schema) {
57
+ return;
58
+ }
59
+
60
+ let valid = true;
61
+ // ajv 校验器
62
+ let validate;
63
+ // 校验 headers
64
+ if (valid && headers && schema.headers) {
65
+ schema.headers.$schema = $schema;
66
+ validate = ajv.compile(schema.headers);
67
+ valid = validate(headers);
68
+ }
69
+ // 校验 body
70
+ if (valid && body && schema.body) {
71
+ schema.body.$schema = $schema;
72
+ validate = ajv.compile(schema.body);
73
+ valid = validate(body);
74
+ }
75
+ // 校验 query
76
+ if (valid && query && schema.query) {
77
+ schema.query.$schema = $schema;
78
+ validate = ajv.compile(schema.query);
79
+ valid = validate(query);
80
+ }
81
+ // 校验 params
82
+ if (valid && params && schema.params) {
83
+ schema.params.$schema = $schema;
84
+ validate = ajv.compile(schema.params);
85
+ valid = validate(params);
86
+ }
87
+
88
+ if (!valid) {
89
+ ctx.status = 200;
90
+ ctx.body = {
91
+ success: false,
92
+ message: `request validate fail: ${ajv.errorsText(validate.errors)}`,
93
+ code: 442
94
+ }
95
+ }
96
+ }
97
+ }
@@ -0,0 +1,30 @@
1
+ const md5 = require('md5');
2
+ /**
3
+ * API 签名合法性校验
4
+ */
5
+ module.exports = (app) => {
6
+ return async (ctx, next) => {
7
+ // 只对 API 请求做签名校验
8
+ if (ctx.path.indexOf('/api/') < 0) {
9
+ return await next();
10
+ };
11
+ const { path, method } = ctx;
12
+ const { headers } = ctx.request;
13
+ const { s_sign: sSign, s_t: st } = headers; // s_t-请求时间戳,s_sign-请求端提供的签名
14
+
15
+ const signKey = 'li11shi08z2cx3shi0wen920';
16
+ const signature = md5(`${signKey}_${st}`); // 自己生成的md5签名
17
+ app.logger.info(`[${method} ${path}] signature: ${signature}`);
18
+
19
+ if (!sSign || !st || signature !== sSign.toLowerCase() || Date.now() - st > 600000) {
20
+ ctx.status = 200;
21
+ ctx.body = {
22
+ success: false,
23
+ message: 'signature not correct or api timeout!!',
24
+ code: 445
25
+ }
26
+ return;
27
+ }
28
+ await next();
29
+ }
30
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * 运行时异常错误处理,兜底所有异常
3
+ * @param {[Object]} app koa 实例
4
+ */
5
+ module.exports = (app) => {
6
+ return async (ctx, next) => {
7
+ try {
8
+ await next()
9
+ } catch(err) {
10
+ // 异常处理
11
+ const { status, message, detail } = err;
12
+
13
+ app.logger.info(JSON.stringify(err));
14
+ app.logger.error('[-- excption --]:', err);
15
+ app.logger.error('[-- excption --]:', status, message, detail);
16
+
17
+ if (message && message.indexOf('template not found') > -1) {
18
+ // 页面重定向
19
+ ctx.status = 302;
20
+ ctx.redirect(`${app.options?.homePage}`);
21
+ return;
22
+ }
23
+
24
+ const resBody = {
25
+ success: false,
26
+ code: 50000,
27
+ message: '网络异常,请稍后重试'
28
+ };
29
+
30
+ ctx.status = 200;
31
+ ctx.body = resBody;
32
+ }
33
+ }
34
+ }
@@ -0,0 +1,22 @@
1
+ module.exports = (app) => {
2
+ return async (ctx, next) => {
3
+ // 只对业务 api 进行 proj_key 处理
4
+ if (ctx.path.indexOf('/api/proj/') < 0) return await next();
5
+
6
+ // 获取 projKey
7
+ const { proj_key: projKey } = ctx.request.headers;
8
+
9
+ if (!projKey) {
10
+ ctx.status = 200;
11
+ ctx.body = {
12
+ success: false,
13
+ message: 'proj_key not found',
14
+ code: 446
15
+ }
16
+ return;
17
+ };
18
+
19
+ ctx.projKey = projKey;
20
+ await next();
21
+ }
22
+ }
@@ -0,0 +1,37 @@
1
+ const path = require('path');
2
+
3
+ module.exports = (app) => {
4
+ // 配置静态文件根目录
5
+ const koaStatic = require('koa-static');
6
+ app.use(koaStatic(path.resolve(process.cwd(), './app/public')));
7
+
8
+ // 模板渲染引擎
9
+ const koaNunjucks = require('koa-nunjucks-2');
10
+ app.use(koaNunjucks({
11
+ ext: 'tpl',
12
+ path: path.resolve(process.cwd(), './app/public'),
13
+ nunjucksConfig: {
14
+ trimBlocks: true,
15
+ noCache: true
16
+ }
17
+ }));
18
+
19
+ // 引入 ctx.body 解析中间件
20
+ const bodyParser = require('koa-bodyparser');
21
+ app.use(bodyParser({
22
+ formList: '1000mb',
23
+ enableTypes: [ 'form', 'json', 'text' ]
24
+ }));
25
+
26
+ // 引入异常捕获中间件
27
+ app.use(app.middlewares.errorHandler);
28
+
29
+ // 引入签名合法性校验
30
+ app.use(app.middlewares.apiSignVerify);
31
+
32
+ // 引入 API 参数校验
33
+ app.use(app.middlewares.apiParamsVerify);
34
+
35
+ // 引入项目相关处理中间件
36
+ app.use(app.middlewares.projectHandler);
37
+ }
@@ -0,0 +1,16 @@
1
+ html, body {
2
+ height: 100%;
3
+ }
4
+
5
+ #root {
6
+ height: 100%;
7
+ }
8
+
9
+ input:-webkit-autofill {
10
+ /* 覆盖自动填充背景颜色 */
11
+ -webkit-box-shadow: 0 0 0 1000px #121212 inset;
12
+ }
13
+
14
+ li {
15
+ list-style: none;
16
+ }