init.json指南

12003

快速开始

jianghu-init JSON以JSON格式高效地管理页面结构和API接口,从而优化开发流程和提升工作效率。本指南主要介绍jianghu-init JSON的详细教程。

安装

在开始之前,请确保您已经安装了jianghu-init工具。可以通过以下命令进行安装:

  1. npm uninstall -g @jianghujs/jianghu-init
  2. npm install -g @jianghujs/jianghu-init@latest

👉 jianghu-init 工具安装详解👈

在江湖项目中生成参考示例

  • 首先,进入已有的江湖JS项目目录

👉 创建jianghujs项目 👈

  • 使用以下命令生成参考示例:
  1. cd my-jh-project
  2. jianghu-init json --generateType=example

执行以上命令,当前项目数据库将添加 example_class 和 example_student 数据表、并在项目目录生成以下文件:

  • app\view\init-json\component\exampleStudentOfClass.js
  • app\view\init-json\page\exampleClass.js
  • app\view\component\example\exampleStudentOfClass.html
  • app\view\page\exampleClass.html

方便参考使用

步骤一:数据表生成配置文件

当有一个新的业务数据表之后,可以使用以下命令生成数据表的配置文件,例如,现在有一个名为class的数据表:

  1. CREATE TABLE `class` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `classId` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '班级ID',
  4. `className` varchar(255) DEFAULT NULL,
  5. `operation` varchar(255) COLLATE utf8mb4_bin DEFAULT 'insert' COMMENT '操作; insert, update, jhInsert, jhUpdate, jhDelete jhRestore',
  6. `operationByUserId` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '操作者userId',
  7. `operationByUser` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '操作者用户名',
  8. `operationAt` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '操作时间; E.g: 2021-05-28T10:24:54+08:00 ',
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

要为class数据表生成基础配置文件,可以使用以下命令:

  1. jianghu-init json --generateType=json --pageType=jh-page --table=class --pageId=classManagement

执行命令之后,会在项目的app/view/init-json/page/目录下生成一个名为classManagement.js的配置文件。

步骤二:配置文件生成页面

上一步生成的配置文件,需要通过以下命令生成页面:

  1. jianghu-init json --generateType=page --pageType=page --file=classManagement

执行以上命令,将会生成项目的app/view/page/目录下生成一个名为classManagement.html的页面文件,并自动配置页面对应的数据库表_page和_resouce的数据。

👉 开启dev模式,修改配置文件自动更新页面 👈

👉 配置字符串代码语法高亮 👈

配置协议

总览

  1. {
  2. pageType: "jh-page", // 页面类型
  3. pageId: "pageId", // 页面唯一标识
  4. pageName: "班级页面", // 页面标题
  5. resourceList: [ // 页面resource { actionId, resourceType, resourceData }
  6. {
  7. actionId: "selectItemList",
  8. resourceType: "sql",
  9. resourceHook: {},
  10. desc: "✅查询列表-exampleClass",
  11. resourceData: {
  12. table: "example_class",
  13. operation: "select"
  14. }
  15. }
  16. ],
  17. headContent: [], // 页面头部,常用于配置 标题、面包屑条、服务端搜索、场景搜索等
  18. pageContent: [], // 页面内容区域
  19. actionContent: [], // 触发内容集合,一般会放新增、修改等弹框
  20. includeList: [ // 页面引入资源 { type, path }
  21. { type: 'html', path: 'component/jianghuJs/jhSceneV4.html' }
  22. ],
  23. common: { // vue 原生变量 + uiAction {data, props, watch, methods, computed}
  24. data: {},
  25. computed: {},
  26. watch: {},
  27. methods: {},
  28. doUiAction: {}, // 自定义uiAction { [key]: [method1, method2, doUiAction1]}
  29. },
  30. style: '', // 自定义样式
  31. }

HTML标签

在 headContent、pageCotent、actionContent 中,可以使用使用指定的对象结构来配置一个标签,也支持直接使用HTML字符串。

对象结构

  1. pageContent: [
  2. {
  3. tag: 'v-row',
  4. attrs: {
  5. align: "center"
  6. "@click": "doUiAction('updateItem')"
  7. },
  8. quickAttrs: ['no-gutters'],
  9. value: [
  10. { tag: 'v-col', attrs: { cols: 12 }, value: '' },
  11. `<v-col cols="12"></v-col>`
  12. ]
  13. }
  14. ]
属性 类型 必填 描述
tag string 标签名称
attr object 标签属性,如class、@click等
quickAttrs array 标签简洁属性,如 v-else small disabled等
value object 标签内的HTML内容

HTML字符串

  1. pageContent: [
  2. '<v-row align="center"><v-col cols="12"></v-col></v-row>'
  3. ]

接口配置

配置江湖resource,定义的resouce将自动更新到数据库中,供前端调用。

  1. resourceList: [
  2. {
  3. resourceHook: { "before": [], "after": [] },
  4. actionId: 'balance-updateItem',
  5. desc: '修改班费余额',
  6. resourceType: 'sql',
  7. resourceData: { "table": "class", "operation": "update" }
  8. }
  9. ]

👉 resouce详细配置文档 👈

资源引入

当页面需要引入组件、样式、脚本等资源时,可以通过includeList配置。支持对象结构和HTML字符串。

  1. includeList: [
  2. { type: 'css', path: "/<$ ctx.app.config.appId $>/public/lib/echarts.min.css" },
  3. { type: 'script', path: "/<$ ctx.app.config.appId $>/public/lib/echarts.min.js" },
  4. { type: 'html', path: "component/exampleChart/lineChart.html" },
  5. { type: 'html', path: 'component/courseBatchDrawer.html', includeType: 'auto', attrs: { id: 'courseBatchDrawer' } },
  6. { type: 'vueComponent', name: 'v-chart', component: 'VueCharts' },
  7. `<script src="/<$ ctx.app.config.appId $>/public/lib/echarts.min.js"></script>`
  8. ]
属性 类型 必填 描述
type string 引入类型,可选 css、js、html、vueComponent
path string 引入路径,type为css、js、html时可用
includeType string 引入类型,可选 auto,当值为auto时将自动添加标签内容添加到页面末端。type为css、js、html时可用
attrs object 自定引入标签属性。type为html、includeType为auto时可用
name string 组件名,type为vueComponent时可用
component string 引入组件,type为vueComponent时可用

配置doUiAction

提供补充doUiAction方法的配置入口

  1. pageContent: [
  2. {
  3. tag: 'v-btn',
  4. value: '测试doUiAction1',
  5. attrs: { '@click': 'doUiAction("doSomething")' },
  6. quickAttrs: ['small']
  7. },
  8. `<v-btn @click="doUiAction('doSomething')">测试doUiAction2</v-btn>`
  9. ],
  10. common: {
  11. doUiAction: {
  12. startUpdataBalance: ['method1', 'method2'],
  13. doSomething: ['method1(134)', 'doUiAction.startUpdataBalance']
  14. },
  15. methods: {
  16. method1: function (id) {
  17. console.log('method1', id)
  18. },
  19. method2: function () {
  20. console.log('method2')
  21. }
  22. }
  23. }

调用链中支持配置方法名、方法名+参数、doUiAction方法名。

配置示例

标题-只有页面标题

  1. headContent: [
  2. { tag: 'jh-page-title', value: "classManagement", attrs: { cols: 12, sm: 6, md:4 }, helpBtn: true, slot: [] }
  3. ]

标题-服务端搜索

  1. headContent: [
  2. { tag: 'jh-page-title', value: "classManagement", attrs: { cols: 12, sm: 6, md:4 }, helpBtn: true, slot: [] },
  3. { tag: 'v-spacer' },
  4. {
  5. tag: 'jh-search',
  6. attrs: { cols: 12, sm: 6, md:8 },
  7. value: [
  8. { tag: "v-text-field", model: "serverSearchWhereLike.className", attrs: {prefix: '前缀'} },
  9. ],
  10. searchBtn: true
  11. }
  12. ]

标题-场景搜索

  1. headContent: [
  2. { tag: 'jh-page-title', value: "classManagement", attrs: { cols: 12, sm: 6, md:4 }, helpBtn: true, slot: [] },
  3. { tag: 'v-spacer' },
  4. {
  5. tag: 'jh-scene',
  6. attrs: {},
  7. value: [],
  8. formItemList: [
  9. { label: '年级', tag: 'v-select', model: 'form.grade', attrs: {":items": 'constantObj.grade'} },
  10. ],
  11. },
  12. ],
  13. common: {
  14. data: {
  15. constantObj: {
  16. grade: ['初中', '高中', '大学'],
  17. },
  18. defaultSceneList: [
  19. { form: { }, name: "全部", default: true, id: "scene-custom-all", 'system': true },
  20. { form: { grade: "高中" }, name: "高中", id: "scene-custom-1", 'system': true },
  21. ],
  22. serverSearchWhere: {},
  23. }
  24. }

内容-数据表格

  1. pageContent: [
  2. {
  3. tag: 'jh-table',
  4. attrs: {},
  5. colAttrs: { clos: 12 },
  6. cardAttrs: { class: 'rounded-lg elevation-0' },
  7. headActionList: [
  8. { tag: 'v-btn', value: '新增', attrs: { color: 'success', class: 'mr-2', '@click': 'doUiAction("startCreateItem")', small: true } },
  9. { tag: 'v-spacer' },
  10. // 默认筛选
  11. {
  12. tag: 'v-col',
  13. attrs: { cols: '12', sm: '6', md: '4', class: 'pa-0' },
  14. value: [
  15. { tag: 'v-text-field', attrs: {prefix: '筛选', 'v-model': 'searchInput', class: 'jh-v-input', ':dense': true, ':filled': true, ':single-line': true} },
  16. ],
  17. }
  18. ],
  19. headers: [
  20. { text: "id", value: "id", width: 80, sortable: true, class: "fixed", cellClass: "fixed" },
  21. { text: "班级ID", value: "classId", width: 80, sortable: true },
  22. { text: "className", value: "className", width: 80, sortable: true },
  23. { text: "操作", value: "operation", width: 80, sortable: true },
  24. { text: "操作者userId", value: "operationByUserId", width: 80, sortable: true },
  25. { text: "操作者用户名", value: "operationByUser", width: 80, sortable: true },
  26. { text: "操作时间", value: "operationAt", width: 80, sortable: true },
  27. { text: "操作", value: "action", type: "action", width: 'window.innerWidth < 500 ? 70 : 120', align: "center", class: "fixed", cellClass: "fixed" },
  28. ],
  29. value: [
  30. // v-table插槽
  31. // { tag: 'template', attrs: {'slot': 'body', 'slot-scope': "item"}, value: "<div>测试插槽</div>" },
  32. // { tag: 'template', attrs: {'slot': 'item.className', 'slot-scope': "{item, index}"}, value: "<div>{{item.className}}</div>" },
  33. ],
  34. rowActionList: [
  35. { text: '编辑', icon: 'mdi-note-edit-outline', color: 'success', click: 'doUiAction("startUpdateItem", item)' }, // 简写支持 pc 和 移动端折叠
  36. { text: '删除', icon: 'mdi-trash-can-outline', color: 'error', click: 'doUiAction("deleteItem", item)' } // 简写支持 pc 和 移动端折叠
  37. ],
  38. }
  39. ]

内容-数据表格(启用列设置)

常用

  1. pageContent: [
  2. {
  3. tag: 'jh-table',
  4. attrs: {},
  5. headers: [],
  6. value: [],
  7. showTableColumnSettingBtn: true,
  8. headActionList: [],
  9. rowActionList: [],
  10. }
  11. ],

模式设定

  1. pageContent: [
  2. {
  3. tag: 'jh-table',
  4. attrs: {},
  5. value: [],
  6. showTableColumnSettingBtn: true,
  7. headActionList: [],
  8. rowActionList: [],
  9. }
  10. ],
  11. common: {
  12. data: {
  13. columnSettingGroup: {
  14. '01模式': [
  15. "netName",
  16. "stage",
  17. "followUpDate",
  18. "district",
  19. "memberId",
  20. "followUpBy",
  21. "currentResidence",
  22. "openVisitationStatus",
  23. "totalVisitationCount",
  24. "visitorAssignInfo",
  25. "operationAt",
  26. "action"
  27. ],
  28. '02模式': [
  29. "netName",
  30. "stage",
  31. "duoxingRoomName",
  32. "followUpDate",
  33. "tag",
  34. "projectList",
  35. "followUpBy",
  36. "district",
  37. "memberId",
  38. "currentResidence",
  39. "operationAt",
  40. "action"
  41. ],
  42. '03模式': [
  43. "netName",
  44. "orgId",
  45. "orgName",
  46. "roleId",
  47. "isDefaultOrg"
  48. ],
  49. },
  50. }
  51. }
属性 类型 必填 描述
showTableColumnSettingBtn boolean 是否启用列设置,默认false

弹框-新增编辑表单

  1. actionContent: [
  2. {
  3. tag: 'jh-create-drawer',
  4. key: "create",
  5. attrs: {},
  6. title: '新增',
  7. headSlot: [
  8. { tag: 'v-spacer'}
  9. ],
  10. contentList: [
  11. {
  12. label: "新增",
  13. type: "form",
  14. formItemList: [
  15. { label: "id", model: "id", tag: "v-text-field", rules: "validationRules.requireRules", },
  16. { label: "班级ID", model: "classId", tag: "v-text-field", rules: "validationRules.requireRules", },
  17. { label: "className", model: "className", tag: "v-text-field", rules: "validationRules.requireRules", },
  18. { label: "操作", model: "operation", tag: "v-text-field", rules: "validationRules.requireRules", },
  19. { label: "操作者userId", model: "operationByUserId", tag: "v-text-field", rules: "validationRules.requireRules", },
  20. { label: "操作者用户名", model: "operationByUser", tag: "v-text-field", rules: "validationRules.requireRules", },
  21. { label: "操作时间", model: "operationAt", tag: "v-text-field", rules: "validationRules.requireRules", },
  22. ],
  23. action: [{
  24. tag: "v-btn",
  25. value: "新增",
  26. attrs: {
  27. color: "success",
  28. ':small': true,
  29. '@click': "doUiAction('createItem')"
  30. }
  31. }],
  32. },
  33. ]
  34. },
  35. {
  36. tag: 'jh-update-drawer',
  37. key: "update",
  38. attrs: {},
  39. title: '编辑',
  40. headSlot: [
  41. { tag: 'v-spacer'}
  42. ],
  43. contentList: [
  44. {
  45. label: "编辑",
  46. type: "form",
  47. formItemList: [
  48. { label: "id", model: "id", tag: "v-text-field", rules: "validationRules.requireRules", },
  49. { label: "班级ID", model: "classId", tag: "v-text-field", rules: "validationRules.requireRules", },
  50. { label: "className", model: "className", tag: "v-text-field", rules: "validationRules.requireRules", },
  51. { label: "操作", model: "operation", tag: "v-text-field", rules: "validationRules.requireRules", },
  52. { label: "操作者userId", model: "operationByUserId", tag: "v-text-field", rules: "validationRules.requireRules", },
  53. { label: "操作者用户名", model: "operationByUser", tag: "v-text-field", rules: "validationRules.requireRules", },
  54. { label: "操作时间", model: "operationAt", tag: "v-text-field", rules: "validationRules.requireRules", },
  55. ],
  56. action: [{
  57. tag: "v-btn",
  58. value: "编辑",
  59. attrs: {
  60. color: "success",
  61. ':small': true,
  62. '@click': "doUiAction('updateItem')"
  63. }
  64. }],
  65. },
  66. { label: "操作记录", type: "component", componentPath: "recordHistory", attrs: { table: 'class', pageId: 'classManagement', ':id': 'updateItem.id' } },
  67. ]
  68. },
  69. ]

弹框-多标签

  1. pageContent: [
  2. `<v-btn @click="openTestMultiTabDrawer">打开测试多标签弹框</v-btn>`,
  3. ],
  4. actionContent: [
  5. {
  6. tag: 'jh-drawer',
  7. key: "testMultiTab",
  8. attrs: {},
  9. title: '测试多标签',
  10. contentList: [
  11. { label: "操作记录", type: "component", componentPath: "recordHistory" },
  12. { label: "学生列表", type: "component", componentPath: "example/studentOfClass", bind: ['classId'] },
  13. ]
  14. }
  15. ]

弹框-自定义按钮

  1. pageContent: [
  2. `<v-btn @click="openTestCustomBtnDrawer">打开测试自定义按钮弹框</v-btn>`
  3. ],
  4. actionContent: [
  5. {
  6. tag: 'jh-create-drawer',
  7. key: "create",
  8. attrs: {},
  9. title: '测试自定义按钮',
  10. headSlot: [
  11. { tag: 'v-spacer' },
  12. { tag: 'v-btn', value: '自定义按钮', attrs: { color: 'success', class: 'mr-2', '@click': 'doUiAction("123")', small: true } },
  13. ],
  14. contentList: [ /* 内容列表 */ ]
  15. }
  16. ]

弹框-关闭弹框检查

  1. actionContent: [
  2. {
  3. tag: 'jh-create-drawer',
  4. key: "create",
  5. attrs: {},
  6. title: '新增',
  7. isCheckFormBeforeClose: true,
  8. onCheckFormConfirm: 'doUiAction("createItem", { notConfirmed: true })',
  9. headSlot: [
  10. { tag: 'v-spacer'}
  11. ],
  12. contentList: [
  13. {
  14. label: "新增",
  15. type: "form",
  16. formItemList: [
  17. { label: "id", model: "id", tag: "v-text-field", rules: "validationRules.requireRules", },
  18. { label: "班级ID", model: "classId", tag: "v-text-field", rules: "validationRules.requireRules", },
  19. { label: "className", model: "className", tag: "v-text-field", rules: "validationRules.requireRules", },
  20. { label: "操作", model: "operation", tag: "v-text-field", rules: "validationRules.requireRules", },
  21. { label: "操作者userId", model: "operationByUserId", tag: "v-text-field", rules: "validationRules.requireRules", },
  22. { label: "操作者用户名", model: "operationByUser", tag: "v-text-field", rules: "validationRules.requireRules", },
  23. { label: "操作时间", model: "operationAt", tag: "v-text-field", rules: "validationRules.requireRules", },
  24. ],
  25. action: [{
  26. tag: "v-btn",
  27. value: "新增",
  28. attrs: {
  29. color: "success",
  30. ':small': true,
  31. '@click': "doUiAction('createItem')"
  32. }
  33. }],
  34. },
  35. ]
  36. }
  37. ]
属性 类型 必填 描述
isCheckFormBeforeClose boolean 是否打开关闭弹框检查,默认false,注意检查的表单对象与弹框的key需要保持匹配,如 表单对象:createItem,弹框key:create
onCheckFormConfirm string 保存内容方法名称

命令详解

生成样例文件页面

默认生成 example_classexample_student 数据表、配置文件和页面,方便参考使用

  1. jianghu-init json --generateType=example

生成页面配置文件

根据数据表生成页面配置文件

  1. jianghu-init json --generateType=json --pageType=page --table=class --pageId=classManagement

配置渲染html

根据配置文件生成页面

  1. jianghu-init json --generateType=page --pageType=page --file=classManagement

开启Dev模式

开启dev模式,修改配置文件将自动更新页面

  1. jianghu-init json dev

参数说明

参数 描述
generateType 生成类型,可选 example(生成示例文件)、json(数据表生成配置文件)、page(配置文件生成页面)
pageType 页面类型,可选 page、component
table 数据表名,当generateType为json可用
dev 启用开发模式,执行后会自动检测当前项目的json文件更新并自动更新html文件

其他

字符串代码语法高亮

在VSCode中,可以安装以下插件来实现字符串代码的语法高亮,以提高代码的可读性:

安装以上插件之后,在字符串代码前指定语法名称,即可显示语法高亮效果。

  1. pageContent: [
  2. /*html*/`<v-btn @click="openTestMultiTabDrawer">打开测试多标签弹框</v-btn>`,
  3. ],
  4. style: /*css*/`
  5. .jh-v-input {
  6. width: 100%;
  7. }
  8. `