框架资源概览

12003

本文提供了江湖框架内置资源的全面概览,涵盖了内置表、页面、静态资源、接口、中间件和配置等关键部分。

内置表 (Table)

  • 内置表/View

    • 用户: _user, _user_session, _view01_user
    • 页面&接口: _page, _resource
    • 用户权限:
      • _group, _user_group_role,
      • _user_group_role_page, _user_group_role_resource
    • 工具表: constant_ui, constant, _record_history, _file, _cache
  • 说明:提供框架的基础功能,包括登陆、鉴权、数据管理等等。

内置页面资源 (Page)

资源 说明 使用
/template/
└── jhTemplateV4.html 页面模版 {% extends 'template/jhTemplateV4.html'%}
/page/
├── helpV4.html 页面.帮助 http://127.0.0.1:${port}/${appId}/page/help
├── loginV4.html 页面.登陆 http://127.0.0.1:${port}/${appId}/page/login
└── manual.html 页面.操作手册 http://127.0.0.1:${port}/${appId}/page/manual
/component/jianghuJs/
├── jhConfirmDialogV4.html 组件.Dialog <jh-confirm-dialog /> await window.confirmDialog({title: "新增", content: "确定新增吗?"})
├── jhMaskV4.html 组件.遮罩层 <jh-mask /> window.jhMask.show(); window.jhMask.hide();
├── jhMenuV4.html 组件.导航菜单 <jh-menu />
├── jhSideMenuV4.html 组件.导航菜单 <jh-menu-side />
├── jhSceneV4.html 组件.场景搜索 {% include 'component/jianghuJs/jhSceneV4.html' %} 👉场景搜索👈
└── jhToastV4.html 组件.消息提示 <jh-toast /> window.vtoast.success("成功"); window.vtoast.fail("失败"); window.vtoast.show({ message: '=_=', icon: 'mdi-help-circle' })
/common/jianghuJs/
├── fixedTableColV4.html 样式.固定table单元格宽度 自动识别所有table
├── fixedTableHeightV4.html 样式.固定table高度 {% include 'common/jianghuJs/fixedTableHeightV4.html' %} class="jh-fixed-table-height"
├── globalCSSFontV4.html 样式.字体样式
├── globalCSSJHV4.html 样式.江湖自定义
├── globalCSSMediaV4.html 样式.响应式
├── globalCSSVuetifyV4.html 样式.Vuetify样式
└── tableRowClickHighlight.html 样式.table行高亮样式 class="jh-fixed-table-height"
/utility/jianghuJs/
├── htmlErrorCollectionV4.html 工具.html异常收集 异常日志会记录到 /logs/${appId}.html.log
├── jianghuAxiosV4.html 工具.axios请求 window.jianghuAxios({...})
├── jianghuDisplayTextV4.html 工具.常量数据值转换文字 {{ getDisplayText({displayObj: constantObj.level, displayValue: item.level}) }}
├── pagePasscodeValidation.html 工具.页面二次认证 {% include 'utility/jianghuJs/pagePasscodeValidation.html' %} <page-passcode-validation validation-duration-hour="1"></page-passcode-validation>
├── prepareAppInfoV4.html 工具.应用信息 window.appInfo
├── prepareDeviceIdV4.html 工具.设备Id生成 {% include 'utility/jianghuJs/prepareDeviceIdV4.html' %} window.deviceId
├── prepareUserInfoV4.html 工具.用户信息 window.userInfo
└── uiActionV4.1.html 工具.多组件vueData共享 window.registerData({}) 👉更多registerData信息👈

内置后端工具 (common)

工具 说明 使用
commonUtil.js ---- ----
diffUtil.js ---- ----
fileUtil.js ---- ----
hyperDiff.js ---- ----
idGenerateUtil.js ---- ----
jianghuKnexUtil.js ---- ----
validateUtil.js ---- ----

内置静态资源 (Static)

内置接口 (Resource)

  • 示例:登录 (login), 登出 (logout), 用户信息查询 (userInfo) 等。
  • 说明:框架基础的接口, 登陆、用户、文件相关。
desc pageId actionId resourceType resourceData
登陆 login passwordLogin service {"service": "user", "serviceFunction": "passwordLogin"}
登出 allPage logout service {"service": "user", "serviceFunction": "logout"}
获取用户信息 allPage userInfo service {"service": "user", "serviceFunction": "userInfo"}
修改用户密码 resetUserPassword resetPassword service {"service": "user", "serviceFunction": "resetPassword"}
查询常量 allPage getConstantUiList sql { "table": "_constant_ui", "operation": "select" }
文件分片下载-获取分片信息 allPage getChunkInfo service {"service": "file", "serviceFunction": "getChunkInfo"}
文件分片上传-所有分片上传完毕 allPage uploadFileDone service {"service": "file", "serviceFunction": "uploadFileDone"}
文件分片上传-http文件流 allPage uploadFileChunkByStream service {"service": "file", "serviceFunction": "uploadFileChunkByStream"}
文件分片上传-http base64 allPage uploadFileChunkByBase64 service {"service": "file", "serviceFunction": "uploadFileChunkByBase64"}
文件分片下载-http base64 allPage downloadFileChunkByBase64 service {"service": "file", "serviceFunction": "downloadFileChunkByBase64"}

内置中间件 (Middleware)

内置配置 (Config)

位置:/node_modules/@jianghujs/jianghu/config/config.default.js
说明:包括基础配置、页面路径、数据库连接、安全配置、跨域策略、性能优化和日志管理。

基础配置

  • appId: 应用ID。
  • authTokenKey: authToken存储的前缀
  • keys: cookie-session的安全密钥。
  • appTitle: 应用的名称。
  • appLogo: 应用的logo路径。
  • appType: 应用模式,single: 单应用; multiApp: 多应用。
  1. const appId = 'jianghujs-basic';
  2. const config = {
  3. appId: appId,
  4. authTokenKey: appId,
  5. keys: `${appId}_1638108566009`,
  6. appTitle: '第一个应用',
  7. appLogo: `${appId}/public/img/logo.svg`,
  8. appType: 'single', // single: 单应用; multiApp: 多应用
  9. }

页面配置

  • indexPage: 首页路径, 默认值/${appId}/page/manual
  • loginPage: 登陆页路径, 默认值/${appId}/page/loginPage
  • helpPage: 帮助页路径, 默认值/${appId}/page/helpPage
  • appDirectoryLink: 目录页路径。默认是跟路径'/'。
  • primaryColor: 主题色,默认值#1867c0
  • nunjucks: njk模板引擎,用于渲染视图。
    • cache: 模板缓存,默认不开启 。
    • tags: njk模板变量取值符号,默认为<$$>,用于包裹变量。(为了避免和vue的{{}} 起冲突)
  1. const appId = 'jianghujs-basic';
  2. const config = {
  3. indexPage: `/${appId}/page/manual`,
  4. loginPage: `/${appId}/page/login`,
  5. helpPage: `/${appId}/page/help`,
  6. appDirectoryLink: '/',
  7. primaryColor: '#1867c0',
  8. // nunjucks页面模版 @ref: https://github.com/eggjs/egg-view-nunjucks
  9. nunjucks: {
  10. cache: false,
  11. tags: {
  12. variableStart: '<$',
  13. variableEnd: '$>',
  14. },
  15. },
  16. }

数据库配置

  • knex: Mysql客户端。knex文档
  • jhIdConfig: 应用数据隔离(多个应用共用一个数据时)。
  1. const config = {
  2. // 数据库配置,使用knex插件 @ref: https://github.com/sunfuze/egg-knex
  3. knex: {
  4. client: {
  5. dialect: 'mysql', // 数据库类型
  6. connection: { // 连接设置
  7. host: '127.0.0.1', // 主机地址
  8. port: '3306', // 端口
  9. user: 'jianghu', // 用户名
  10. password: '123456', // 密码
  11. database: 'jianghu', // 数据库名
  12. },
  13. pool: { min: 0, max: 10 }, // 连接池大小
  14. acquireConnectionTimeout: 30000, // 连接超时时间(毫秒)
  15. },
  16. app: true, // 挂载到app
  17. },
  18. jianghuConfig: {
  19. jhIdConfig: { // 应用数据隔离
  20. enable: false, // 是否启用
  21. jhId: 'default', // 数据隔离时的Id
  22. careTableViewList: [ // 要隔离的表
  23. '_cache', '_constant', '_file', '_group',
  24. '_page', '_record_history', '_resource', '_resource_request_log',
  25. '_role', '_test_case', '_ui', '_user',
  26. '_user_group_role', '_user_group_role_page', '_user_group_role_resource',
  27. '_user_session', '_view01_user'
  28. ],
  29. },
  30. },
  31. }

安全配置

  • cors: 跨域配置。egg-cors文档
  • jianghuConfig
    • enableUploadStaticFileAuthorization: 上传文件鉴权。
    • enableRateLimiter: 限流是否开启。默认关闭
    • rateLimiterDuration: 限流持续时间(毫秒)。
    • rateLimiterMax: 限流最大请求数。
    • rateLimiterIgnorePathPrefix: 限流忽略的路径前缀列表。
    • rateLimiterWhitelist: 限流白名单。
    • enableIpBlock: IP黑名单是否开启。默认关闭
    • ipBlocklist: IP黑名单列表。
    • ipBlocklistFilePath: 黑名单文件路径。
  1. const appId = 'jianghujs-basic';
  2. const config = {
  3. // 跨域支持, @ref: https://github.com/eggjs/egg-cors
  4. cors: {
  5. origin: '*',
  6. allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
  7. },
  8. jianghuConfig: {
  9. // 上传文件鉴权 @ref: /middleware/downloadUserInfo.js
  10. enableUploadStaticFileAuthorization: true,
  11. // WAF: 限流
  12. enableRateLimiter: false,
  13. rateLimiterDuration: 60000,
  14. rateLimiterMax: 100,
  15. rateLimiterIgnorePathPrefix: [ '/${appId}/resource', '/${appId}/page', '/${appId}/public', '${appId}/upload' ],
  16. rateLimiterWhitelist: [],
  17. // WAF: IP 黑名单
  18. enableIpBlock: false,
  19. ipBlocklist: [],
  20. ipBlocklistFilePath: '',
  21. },
  22. }

性能与资源

  • enableUserInfoCache: 用户信息缓存开启,默认关闭
  • enableUploadStaticFileAuthorization: 下载Upload文件鉴权开启,默认关闭
  • enableUploadStaticFileCache: Upload文件缓存开启,默认关闭
  • uploadFileMaxAge: upload文件缓存时间
  • multipart: 文件上传的配置,包括模式和文件大小限制。multipart文档
  • bodyParser bodyParser文档
    • formLimit: 表单数据的最大大小限制。
    • jsonLimit: JSON 数据的最大大小限制。
    • textLimit: 文本数据的最大大小限制。
  1. const appId = 'jianghujs-basic';
  2. const config = {
  3. jianghuConfig: {
  4. // 开启用户信息缓存, @ref: /schedule/syncDataToCache.js
  5. enableUserInfoCache: false,
  6. userInfoCacheRefreshInterval: '10s',
  7. // 上传文件鉴权 @ref: /middleware/downloadUserInfo.js
  8. enableUploadStaticFileAuthorization: false,
  9. // 缓存配置
  10. enableUploadStaticFileCache: true,
  11. uploadFileMaxAge: 30 * 24 * 60 * 60 * 1000, // 30d
  12. },
  13. // 文件上传限制 @ref: https://eggjs.org/zh-cn/plugins/multipart.html
  14. multipart: {
  15. mode: 'file',
  16. fileSize: '100mb',
  17. allowArrayField: false,
  18. // 允许所有格式的文件上传
  19. whitelist: () => true,
  20. tmpdir: path.join(appInfo.baseDir, 'multipartTmp'),
  21. cleanSchedule: {
  22. // run tmpdir clean job on every day 04:30 am
  23. // cron style see https://github.com/eggjs/egg-schedule#cron-style-scheduling
  24. cron: '0 30 4 * * *',
  25. disable: false,
  26. },
  27. },
  28. // 表单限制 @ref: https://github.com/eggjs/egg/blob/master/app/middleware/body_parser.js
  29. bodyParser: {
  30. formLimit: '100mb',
  31. jsonLimit: '100mb',
  32. textLimit: '100mb',
  33. },
  34. }

异常处理

  • onerror: 异常处理。
    1. const config = {
    2. onerror: {
    3. async json(err, ctx) {
    4. ctx.status = 200;
    5. const { packageId } = ctx.request.body;
    6. // TODO: 如何将系统异常 & 数据库异常 & Biz异常 优雅的返回
    7. const errorCode =
    8. err.errorCode || err.code || errorInfoEnum.server_error.errorCode;
    9. const errorReason =
    10. err.errorReason ||
    11. err.sqlMessage ||
    12. err.message ||
    13. errorInfoEnum.server_error.errorReason;
    14. // 清除cookie;
    15. if (
    16. errorCode === 'request_token_invalid' ||
    17. errorCode === 'request_user_not_exist' ||
    18. errorCode === 'request_token_expired' ||
    19. errorCode === 'request_app_forbidden' ||
    20. errorCode === 'user_banned'
    21. ) {
    22. ctx.cookies.set(`${ctx.app.config.authTokenKey}_authToken`, null);
    23. }
    24. const errorReasonSupplement = err.errorReasonSupplement || null;
    25. ctx.body = httpResponse.fail({
    26. packageId,
    27. appData: { errorCode, errorReason, errorReasonSupplement },
    28. });
    29. },
    30. }
    31. }

日志配置

  • enableHtmlErrorLogRecord: 页面错误日志记录,默认关闭
  • enableResourceLogRecord: 接口日志记录,默认关闭
  • autoClearOldLogFile: 自动清理日志,默认关闭
  • logger: 日志输出配置。
  • logrotator: 日志切割配置。
  • customLogger: 定制分类日志记录。
  1. const appId = 'jianghujs-basic';
  2. const config = {
  3. jianghuConfig: {
  4. // 页面日志收集
  5. enableHtmlErrorLogRecord: false,
  6. htmlErrorLogRecordInterval: 60000,
  7. // resource 日志收集
  8. enableResourceLogRecord: true,
  9. ignoreListOfResourceLogRecord: [ 'user.passwordLogin', 'allPage.getConstantList', 'allPage.httpUploadByStream', 'allPage.httpUploadByBase64', 'allPage.httpDownloadByBase64' ],
  10. // 自动清理旧日志文件,保留 N 天内的日志
  11. autoClearOldLogFile: false,
  12. autoClearOldLogBeforeDays: 7,
  13. autoClearOldLogFilePrefixList: [
  14. 'common-error', 'egg-web', 'egg-schedule', 'egg-knex', 'egg-agent', '_resource_request_log',
  15. `${appId}.html`, `${appId}.resource`, `${appId}-web`, `${appId}.html`,
  16. ],
  17. },
  18. // 日志配置
  19. logger: {
  20. outputJSON: true,
  21. level: 'INFO',
  22. dir: path.join(appInfo.baseDir, 'logs'),
  23. contextFormatter(meta) {
  24. return `[${meta.date}] [${meta.level}] [${meta.ctx.method} ${meta.ctx.url}] ${meta.message}`;
  25. },
  26. },
  27. // 日志切割
  28. logrotator: {
  29. filesRotateBySize: [
  30. path.join(appInfo.baseDir, `logs/${appId}.page.log`),
  31. path.join(appInfo.baseDir, `logs/${appId}.page.json.log`),
  32. path.join(appInfo.baseDir, `logs/${appId}.html.log`),
  33. path.join(appInfo.baseDir, `logs/${appId}.html.json.log`),
  34. ],
  35. maxFileSize: 10 * 1024 * 1024, // 10M
  36. maxFiles: 20, // 最大文件个数
  37. maxDays: 20, // 最大天数
  38. },
  39. // 定制的分类日志
  40. customLogger: {
  41. knex: { consoleLevel: 'WARN' },
  42. // https://www.eggjs.org/zh-CN/core/logger
  43. htmlLogger: {
  44. file: path.join(appInfo.baseDir, `logs/${appId}.html.log`),
  45. contextFormatter(meta) {
  46. return `[${meta.date}] [${meta.level}] [${meta.ctx.method} ${meta.ctx.url}] ${meta.message}`;
  47. },
  48. },
  49. resourceLogger: {
  50. file: path.join(appInfo.baseDir, `logs/${appId}.resource.log`),
  51. contextFormatter(meta) {
  52. return `[${meta.date}] [${meta.level}] [${meta.ctx.method} ${meta.ctx.url}] ${meta.message}`;
  53. },
  54. },
  55. pageLogger: {
  56. file: path.join(appInfo.baseDir, `logs/${appId}.page.log`),
  57. contextFormatter(meta) {
  58. return `[${meta.date}] [${meta.level}] [${meta.ctx.method} ${meta.ctx.url}] ${meta.message}`;
  59. },
  60. },
  61. },
  62. }