# CodeGenerator 代码生成模块

CodeGenerator 模块是 DarkM 框架的效率工具模块,提供了可视化的代码生成功能。通过定义实体类和属性,自动生成完整的 CRUD 代码,包括后端 Controller、Service、Model 以及前端页面代码,大幅提升开发效率。


# 📋 模块概述

属性 说明
模块名称 CodeGenerator(代码生成器)
模块类型 工具模块
依赖关系 依赖 Admin 模块
必需性 可选(推荐安装)
Git 仓库 framework/darkm/darkm (opens new window)

# 🎯 核心功能

# 功能清单

功能 说明 重要性
模块管理 项目管理、模块配置 ⭐⭐⭐⭐⭐
实体管理 定义实体类、基类选择 ⭐⭐⭐⭐⭐
属性管理 配置实体属性、类型、验证规则 ⭐⭐⭐⭐⭐
枚举管理 枚举类型定义和管理 ⭐⭐⭐⭐
代码生成 一键生成前后端完整代码 ⭐⭐⭐⭐⭐
在线模块 已生成模块的管理 ⭐⭐⭐⭐
排序管理 实体和属性的排序配置 ⭐⭐⭐

# 📦 安装配置

# NuGet 包安装

Install-Package DarkM.Module.CodeGenerator.Web -Version 1.0.0
1

# NPM 包安装

npm i -S darkm-module-codegenerator
1

# 配置文件

{
  "Db": {
    "Modules": [
      {
        "Name": "CodeGenerator",
        "Database": "Nm_CodeGenerator",
        "Prefix": ""
      }
    ]
  },
  "CodeGenerator": {
    // 代码生成输出路径
    "OutputPath": "/output",
    // 是否启用在线模块管理
    "EnableOnlineModule": true
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

配置说明:

配置项 说明 默认值
OutputPath 代码生成输出路径 /output
EnableOnlineModule 是否启用在线模块管理 true

# 🗄️ 数据库表结构

# 核心表

表名 说明 主键
Module 模块/项目信息表 Id (Guid)
Class 实体类信息表 Id (Guid)
Property 实体属性表 Id (Guid)
Enum 枚举类型表 Id (string)
EnumItem 枚举项表 Id (Guid)
OnlineModule 在线模块表 Id (Guid)

# 关联表

表名 说明
ClassRelation 实体关联关系表

# 🔐 权限系统设计

# Controller 定义

using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using DarkM.Lib.Auth.Web.Attributes;
using DarkM.Lib.Utils.Core.Models;

namespace DarkM.Module.CodeGenerator.Web.Controllers
{
    [Description("模块管理")]
    public class ModuleController : Web.ModuleController
    {
        private readonly IModuleService _service;

        public ModuleController(IModuleService service)
        {
            _service = service;
        }

        [HttpGet]
        [Description("查询")]
        public Task<IResultModel> Query([FromQuery]ModuleQueryModel model)
        {
            return _service.Query(model);
        }

        [HttpPost]
        [Description("添加")]
        public Task<IResultModel> Add(ModuleAddModel model)
        {
            return _service.Add(model);
        }

        [HttpDelete]
        [Description("删除")]
        public Task<IResultModel> Delete([BindRequired]Guid id)
        {
            return _service.Delete(id);
        }

        [HttpGet]
        [Description("编辑")]
        public Task<IResultModel> Edit([BindRequired]Guid id)
        {
            return _service.Edit(id);
        }

        [HttpPost]
        [Description("修改")]
        public Task<IResultModel> Update(ModuleUpdateModel model)
        {
            return _service.Update(model);
        }

        [HttpGet]
        [Description("获取排序信息")]
        [Common(CommonPermissionType.All)]
        public Task<IResultModel> Sort()
        {
            return _service.QuerySortList();
        }

        [HttpPost]
        [Description("更新排序信息")]
        [Common(CommonPermissionType.All)]
        public Task<IResultModel> Sort(SortUpdateModel<Guid> model)
        {
            return _service.UpdateSortList(model);
        }

        [HttpPost]
        [Description("生成代码")]
        public async Task<IActionResult> BuildCode(ModuleBuildCodeModel model)
        {
            var result = await _service.BuildCode(model);
            return ExportFile(result.Data.ZipPath, result.Data.Name, "zip");
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

# 权限解析结果

权限编码 说明 访问级别
codegenerator_module_query_get 模块管理 - 查询 需授权
codegenerator_module_add_post 模块管理 - 添加 需授权
codegenerator_module_edit_get 模块管理 - 编辑 需授权
codegenerator_module_update_post 模块管理 - 修改 需授权
codegenerator_module_delete_delete 模块管理 - 删除 需授权
codegenerator_module_buildcode_post 模块管理 - 生成代码 需授权

# 🎨 核心功能详解

# 一、模块管理

# 1. 创建模块

public class ModuleAddModel
{
    /// <summary>
    /// 模块名称
    /// </summary>
    [Required]
    [Length(100)]
    public string Name { get; set; }

    /// <summary>
    /// 模块编码
    /// </summary>
    [Required]
    [Length(50)]
    public string Code { get; set; }

    /// <summary>
    /// 模块版本
    /// </summary>
    [Length(20)]
    public string ModuleVersion { get; set; }

    /// <summary>
    /// 图标
    /// </summary>
    [Nullable]
    public string Icon { get; set; }

    /// <summary>
    /// 版权声明
    /// </summary>
    [Nullable]
    [Length(200)]
    public string Copyright { get; set; }

    /// <summary>
    /// 公司单位
    /// </summary>
    [Nullable]
    [Length(100)]
    public string Company { get; set; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 2. 生成代码

public class ModuleBuildCodeModel
{
    /// <summary>
    /// 模块 ID
    /// </summary>
    [Required]
    public Guid ModuleId { get; set; }

    /// <summary>
    /// 生成的类列表(为空则生成所有类)
    /// </summary>
    public IList<Guid> ClassList { get; set; }

    /// <summary>
    /// 是否覆盖已存在的文件
    /// </summary>
    public bool Override { get; set; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

生成内容:

  • ✅ 后端 Controller 代码
  • ✅ 后端 Service 代码
  • ✅ Model 定义(Add/Update/Query)
  • ✅ 前端 Page.js 配置
  • ✅ 前端 Vue 组件
  • ✅ 数据库迁移脚本

# 二、实体管理

# 1. 实体类定义

public class ClassAddModel
{
    /// <summary>
    /// 实体名称
    /// </summary>
    [Required]
    [Length(100)]
    public string Name { get; set; }

    /// <summary>
    /// 实体编码
    /// </summary>
    [Required]
    [Length(50)]
    public string Code { get; set; }

    /// <summary>
    /// 所属模块 ID
    /// </summary>
    [Required]
    public Guid ModuleId { get; set; }

    /// <summary>
    /// 基类类型
    /// </summary>
    public BaseEntityType BaseEntityType { get; set; }

    /// <summary>
    /// 表名
    /// </summary>
    [Length(100)]
    public string TableName { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    [Length(500)]
    public string Description { get; set; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# 2. 基类类型

public enum BaseEntityType
{
    /// <summary>
    /// 无基类(自定义)
    /// </summary>
    None = 0,

    /// <summary>
    /// 基础实体(Id + 软删除)
    /// </summary>
    EntityBaseWithSoftDelete = 1,

    /// <summary>
    /// 标准实体(Id + 创建时间 + 创建人)
    /// </summary>
    EntityBase = 2,

    /// <summary>
    /// 完整实体(Id + 创建 + 修改 + 软删除)
    /// </summary>
    EntityBaseWithFullAudit = 3
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 3. 实体代码生成

// 生成的实体类示例
using DarkM.Lib.Data.Abstractions.Attributes;
using DarkM.Lib.Data.Core.Entities.Extend;

namespace DarkM.Module.Admin.Domain
{
    /// <summary>
    /// 配置项管理
    /// </summary>
    [Table("Config")]
    public partial class ConfigEntity : EntityBaseWithSoftDelete
    {
        /// <summary>
        /// 组别
        /// </summary>
        [Length(50)]
        public string Group { get; set; }

        /// <summary>
        /// 编码
        /// </summary>
        [Length(100)]
        public string Code { get; set; }

        /// <summary>
        /// 值
        /// </summary>
        [Length(500)]
        public string Value { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        [Length(500)]
        [Nullable]
        public string Description { get; set; }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

# 三、属性管理

# 1. 属性类型

public enum PropertyType
{
    /// <summary>
    /// 字符串
    /// </summary>
    string = 0,

    /// <summary>
    /// 整数
    /// </summary>
    int = 1,

    /// <summary>
    /// 长整数
    /// </summary>
    long = 2,

    /// <summary>
    /// 小数
    /// </summary>
    decimal = 3,

    /// <summary>
    /// 浮点数
    /// </summary>
    double = 4,

    /// <summary>
    /// 日期时间
    /// </summary>
    DateTime = 5,

    /// <summary>
    /// 布尔值
    /// </summary>
    bool = 6,

    /// <summary>
    /// Guid
    /// </summary>
    Guid = 7,

    /// <summary>
    /// 枚举
    /// </summary>
    Enum = 8
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

# 2. 属性配置

public class PropertyAddModel
{
    /// <summary>
    /// 属性名称
    /// </summary>
    [Required]
    [Length(100)]
    public string Name { get; set; }

    /// <summary>
    /// 属性编码
    /// </summary>
    [Required]
    [Length(50)]
    public string Code { get; set; }

    /// <summary>
    /// 所属实体 ID
    /// </summary>
    [Required]
    public Guid ClassId { get; set; }

    /// <summary>
    /// 属性类型
    /// </summary>
    [Required]
    public PropertyType PropertyType { get; set; }

    /// <summary>
    /// 长度(字符串类型)
    /// </summary>
    public int? Length { get; set; }

    /// <summary>
    /// 是否可空
    /// </summary>
    public bool Nullable { get; set; }

    /// <summary>
    /// 是否列表显示
    /// </summary>
    public bool ShowInList { get; set; }

    /// <summary>
    /// 是否编辑显示
    /// </summary>
    public bool ShowInEdit { get; set; }

    /// <summary>
    /// 是否查询条件
    /// </summary>
    public bool ShowInQuery { get; set; }

    /// <summary>
    /// UI 控件类型
    /// </summary>
    public string ControlType { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    [Length(500)]
    public string Description { get; set; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

# 3. 属性状态批量修改

// 修改可空状态
[HttpPost]
[Description("修改可空状态")]
public Task<IResultModel> UpdateNullable(PropertyUpdatePropertyEnableModel model)
{
    return _service.UpdateNullable(model);
}

// 修改列表显示状态
[HttpPost]
[Description("修改列表显示状态")]
public Task<IResultModel> UpdateShowInList(PropertyUpdatePropertyEnableModel model)
{
    return _service.UpdateShowInList(model);
}

// 修改编辑状态
[HttpPost]
[Description("修改属性是否可编辑")]
public Task<IResultModel> UpdateShowInEdit(PropertyUpdatePropertyEnableModel model)
{
    return _service.UpdateShowInEdit(model);
}

// 修改查询条件状态
[HttpPost]
[Description("修改列表页面查询条件状态")]
public Task<IResultModel> UpdateShowInQuery(PropertyUpdatePropertyEnableModel model)
{
    return _service.UpdateShowInQuery(model);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# 四、枚举管理

# 1. 枚举定义

public class EnumAddModel
{
    /// <summary>
    /// 枚举名称
    /// </summary>
    [Required]
    [Length(100)]
    public string Name { get; set; }

    /// <summary>
    /// 枚举编码
    /// </summary>
    [Required]
    [Length(50)]
    public string Code { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    [Length(500)]
    public string Description { get; set; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 2. 枚举项定义

public class EnumItemAddModel
{
    /// <summary>
    /// 枚举 ID
    /// </summary>
    [Required]
    public string EnumId { get; set; }

    /// <summary>
    /// 项名称
    /// </summary>
    [Required]
    [Length(100)]
    public string Name { get; set; }

    /// <summary>
    /// 项值
    /// </summary>
    [Required]
    public int Value { get; set; }

    /// <summary>
    /// 排序
    /// </summary>
    public int Sort { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    [Length(500)]
    public string Description { get; set; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 🖥️ 前端组件说明

# NPM 包信息

{
  "name": "darkm-module-codegenerator",
  "version": "1.5.0-beta-2.0",
  "code": "codeGenerator",
  "title": "代码生成",
  "dependencies": {
    "darkm-module-admin": "^1.5.0-beta-2.0",
    "darkm-ui": "^1.5.0-beta-2.0"
  }
}
1
2
3
4
5
6
7
8
9
10

# 组件目录结构

src/UI/module-codegenerator/
├── src/
│   ├── views/                      # 页面组件
│   │   ├── module/                 # 模块管理
│   │   ├── class/                  # 实体管理
│   │   ├── property/               # 属性管理
│   │   ├── enum/                   # 枚举管理
│   │   ├── enumItem/               # 枚举项管理
│   │   └── classRelation/          # 实体关联
│   └── components/                 # 通用组件
│       ├── class-select/           # 实体选择器
│       ├── property-select/        # 属性选择器
│       ├── classGrid-select/       # 表单栅格选择器
│       └── classGrid-tree-select/  # 表单栅格树选择器
└── package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 📦 通用组件详解

# 1. ClassSelect 实体选择器

组件路径: components/class-select

功能: 下拉选择实体类,支持按模块筛选

使用示例:

<template>
  <class-select 
    v-model="classId"
    :moduleId="moduleId"
    :classId="currentClassId"
  />
</template>

<script>
import { components } from 'darkm-module-codegenerator'
const { ClassSelect } = components

export default {
  components: { ClassSelect },
  data() {
    return {
      classId: '',
      moduleId: 'xxx',
      currentClassId: 'xxx'
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Props 参数:

参数 类型 必填 默认值 说明
value String - 选中的实体 ID(v-model)
moduleId String - 所属模块 ID
classId String - 自己的 ID(用于排除自己)
disabled Boolean false 是否禁用

Events 事件:

事件名 参数 说明
input (value: String) 选中值变化时触发(v-model)
change (value: String, data: Object) 选中值变化时触发,返回完整数据

特点:

  • ✅ 按模块筛选实体
  • ✅ 支持排除自己(用于关联选择)
  • ✅ 基于 mixins.select

# 2. PropertySelect 属性选择器

组件路径: components/property-select

功能: 下拉选择实体属性

使用示例:

<template>
  <property-select 
    v-model="propertyId"
    :classId="classId"
  />
</template>

<script>
import { components } from 'darkm-module-codegenerator'
const { PropertySelect } = components

export default {
  components: { PropertySelect },
  data() {
    return {
      propertyId: '',
      classId: 'xxx'
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Props 参数:

参数 类型 必填 默认值 说明
value String - 选中的属性 ID(v-model)
classId String - 实体 ID
disabled Boolean false 是否禁用

Events 事件:

事件名 参数 说明
input (value: String) 选中值变化时触发(v-model)
change (value: String, data: Object) 选中值变化时触发,返回完整数据

特点:

  • ✅ 按实体筛选属性
  • ✅ 基于 mixins.select

# 3. ClassGridSelect 表单栅格选择器

组件路径: components/classGrid-select

功能: 下拉选择表单栅格配置

使用示例:

<template>
  <classGrid-select v-model="gridId" />
</template>

<script>
import { components } from 'darkm-module-codegenerator'
const { ClassGridSelect } = components

export default {
  components: { ClassGridSelect },
  data() {
    return {
      gridId: ''
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Props 参数:

参数 类型 必填 默认值 说明
value String - 选中的栅格 ID(v-model)
disabled Boolean false 是否禁用

Events 事件:

事件名 参数 说明
input (value: String) 选中值变化时触发(v-model)
change (value: String, data: Object) 选中值变化时触发,返回完整数据

特点:

  • ✅ 简单下拉选择
  • ✅ 基于 mixins.select

# 4. ClassGridTreeSelect 表单栅格树选择器

组件路径: components/classGrid-tree-select

功能: 树形选择表单栅格,支持层级展开

使用示例:

<template>
  <classGrid-tree-select 
    v-model="gridId"
    :classId="classId"
    :collapseAll="false"
  />
</template>

<script>
import { components } from 'darkm-module-codegenerator'
const { ClassGridTreeSelect } = components

export default {
  components: { ClassGridTreeSelect },
  data() {
    return {
      gridId: '',
      classId: 'xxx'
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Props 参数:

参数 类型 必填 默认值 说明
value String - 选中的栅格 ID(v-model)
classId String - 实体 ID
refresh Boolean true 是否刷新树
title String '表单栅格选择' 弹窗标题
icon String 'sort-down' 图标
collapseAll Boolean true 是否默认折叠所有节点
disabled Boolean false 是否禁用

Events 事件:

事件名 参数 说明
input (value: String) 选中值变化时触发(v-model)
change (value: String, data: Object) 选中值变化时触发,返回完整数据

特点:

  • ✅ 树形结构展示
  • ✅ 支持层级展开/折叠
  • ✅ 基于 mixins.treeSelect

# 📋 组件使用注意事项

# 1. 组件引入方式

// 方式一:引入单个组件
import { components } from 'darkm-module-codegenerator'
const { ClassSelect, PropertySelect } = components

// 方式二:全局注册
import CodeGenerator from 'darkm-module-codegenerator'
Vue.use(CodeGenerator)
1
2
3
4
5
6
7

# 2. v-model 双向绑定

所有选择器组件都支持 v-model 双向绑定:

<!-- 推荐用法 -->
<class-select v-model="classId" :moduleId="moduleId" />

<!-- 等价于 -->
<class-select 
  :value="classId" 
  @input="classId = $event"
  :moduleId="moduleId"
/>
1
2
3
4
5
6
7
8
9

# 3. 级联选择

多个选择器可以级联使用:

<template>
  <div>
    <module-select v-model="moduleId" />
    <class-select v-model="classId" :moduleId="moduleId" />
    <property-select v-model="propertyId" :classId="classId" />
  </div>
</template>
1
2
3
4
5
6
7

# 4. 数据格式

所有组件返回的数据格式:

  • 单选: 返回 ID(String)
  • change 事件: 返回 (value, data),data 为完整对象

# 5. 组件依赖

CodeGenerator 模块依赖以下模块:

  • darkm-module-admin - 权限管理
  • darkm-ui - UI 组件库

确保先安装依赖:

npm install darkm-module-admin --registry http://npm.woowis.com
npm install darkm-ui --registry http://npm.woowis.com
1
2

# 🔧 核心服务接口

# IModuleService(模块服务)

public interface IModuleService
{
    /// <summary>
    /// 查询模块列表
    /// </summary>
    Task<IResultModel> Query(ModuleQueryModel model);

    /// <summary>
    /// 添加模块
    /// </summary>
    Task<IResultModel> Add(ModuleAddModel model);

    /// <summary>
    /// 删除模块
    /// </summary>
    Task<IResultModel> Delete(Guid id);

    /// <summary>
    /// 编辑模块
    /// </summary>
    Task<IResultModel> Edit(Guid id);

    /// <summary>
    /// 修改模块
    /// </summary>
    Task<IResultModel> Update(ModuleUpdateModel model);

    /// <summary>
    /// 生成代码
    /// </summary>
    Task<IResultModel<ModuleBuildCodeResultModel>> BuildCode(ModuleBuildCodeModel model);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# IClassService(实体服务)

public interface IClassService
{
    /// <summary>
    /// 查询实体列表
    /// </summary>
    Task<IResultModel> Query(ClassQueryModel model);

    /// <summary>
    /// 添加实体
    /// </summary>
    Task<IResultModel> Add(ClassAddModel model);

    /// <summary>
    /// 删除实体
    /// </summary>
    Task<IResultModel> Delete(Guid id);

    /// <summary>
    /// 编辑实体
    /// </summary>
    Task<IResultModel> Edit(Guid id);

    /// <summary>
    /// 修改实体
    /// </summary>
    Task<IResultModel> Update(ClassUpdateModel model);

    /// <summary>
    /// 生成实体代码
    /// </summary>
    Task<IResultModel<ModuleBuildCodeResultModel>> BuildCode(Guid id);

    /// <summary>
    /// 复制实体
    /// </summary>
    Task<IResultModel> Copy(Guid classId);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# IPropertyService(属性服务)

public interface IPropertyService
{
    /// <summary>
    /// 查询属性列表
    /// </summary>
    Task<IResultModel> Query(PropertyQueryModel model);

    /// <summary>
    /// 添加属性
    /// </summary>
    Task<IResultModel> Add(PropertyAddModel model);

    /// <summary>
    /// 删除属性
    /// </summary>
    Task<IResultModel> Delete(Guid id);

    /// <summary>
    /// 批量删除属性
    /// </summary>
    Task<IResultModel> BatchDelete(IList<Guid> ids);

    /// <summary>
    /// 修改属性
    /// </summary>
    Task<IResultModel> Update(PropertyUpdateModel model);

    /// <summary>
    /// 修改可空状态
    /// </summary>
    Task<IResultModel> UpdateNullable(PropertyUpdatePropertyEnableModel model);

    /// <summary>
    /// 修改列表显示状态
    /// </summary>
    Task<IResultModel> UpdateShowInList(PropertyUpdatePropertyEnableModel model);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 📊 使用流程

# 完整开发流程

1. 创建模块(Module)
   ↓
2. 创建实体(Class)
   ↓
3. 添加属性(Property)
   ↓
4. 配置枚举(Enum,如需要)
   ↓
5. 生成代码(BuildCode)
   ↓
6. 下载代码包
   ↓
7. 解压到项目
   ↓
8. 数据库迁移
   ↓
9. 配置权限
   ↓
10. 完成✅
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 示例:创建客户管理模块

# 步骤 1:创建模块

模块名称:客户管理
模块编码:Customer
模块版本:1.0.0
图标:customer
公司:Woowis
1
2
3
4
5

# 步骤 2:创建实体

实体名称:客户信息
实体编码:CustomerInfo
基类类型:EntityBaseWithFullAudit
表名:Customer_Info
描述:客户基本信息表
1
2
3
4
5

# 步骤 3:添加属性

属性名称 编码 类型 长度 列表 编辑 查询 控件
客户名称 Name string 100 输入框
联系电话 Phone string 20 手机号
邮箱 Email string 100 × 邮箱
客户类型 Type Enum - 下拉框
地址 Address string 500 × 文本域
状态 Status int - 开关

# 步骤 4:生成代码

点击"生成代码"按钮,下载 ZIP 包。

# 步骤 5:解压代码包

Customer.zip
├── DarkM.Module.Customer.Web/
│   ├── Controllers/
│   │   └── CustomerInfoController.cs
│   ├── Application/
│   │   └── CustomerInfoService/
│   │       ├── CustomerInfoService.cs
│   │       ├── ICustomerInfoService.cs
│   │       └── ViewModels/
│   └── Domain/
│       └── CustomerInfo/
│           └── CustomerInfoEntity.cs
├── UI/
│   └── customer/
│       └── customerinfo/
│           ├── index.vue
│           └── page.js
└── Database/
    └── Migration.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 步骤 6:数据库迁移

-- 执行迁移脚本
CREATE TABLE [Customer_Info] (
    [Id] UNIQUEIDENTIFIER NOT NULL,
    [Name] NVARCHAR(100) NOT NULL,
    [Phone] NVARCHAR(20) NOT NULL,
    [Email] NVARCHAR(100) NULL,
    [Type] INT NOT NULL,
    [Address] NVARCHAR(500) NULL,
    [Status] INT NOT NULL,
    [CreateTime] DATETIME NOT NULL,
    [CreateBy] UNIQUEIDENTIFIER NOT NULL,
    [ModifyTime] DATETIME NULL,
    [ModifyBy] UNIQUEIDENTIFIER NULL,
    [IsDeleted] BIT NOT NULL DEFAULT 0,
    CONSTRAINT [PK_Customer_Info] PRIMARY KEY ([Id])
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 步骤 7:配置权限

在 Admin 模块中配置权限:

  • 添加菜单:客户管理
  • 分配权限:查询、添加、编辑、删除
  • 分配角色:管理员、客服

# 步骤 8:完成

访问 /customer/customerinfo 页面,客户管理功能已就绪!✅


# 🔍 常见问题

# 1. 生成的代码无法编译

问题: 编译时报错,缺少引用

解决:

# 检查是否安装了 Admin 模块
# CodeGenerator 依赖 Admin 模块

# 检查 NuGet 包版本是否一致
cat *.csproj | grep DarkM

# 重新恢复 NuGet 包
dotnet restore
1
2
3
4
5
6
7
8

# 2. 生成的前端页面空白

问题: 页面加载后显示空白

解决:

  1. 检查前端代码是否正确解压
  2. 检查路由配置是否正确
  3. 清除浏览器缓存
  4. 检查控制台错误信息

# 3. 权限验证失败

问题: 提示无权限访问

解决:

  1. 检查是否已安装 Admin 模块
  2. 检查权限是否已配置
  3. 检查角色是否已分配权限
  4. 重新登录后重试

# 4. 代码生成失败

问题: 点击生成代码报错

解决:

# 检查输出目录是否有写权限
ls -la /output

# 检查模块配置是否完整
# 确保至少有一个实体和属性

# 查看详细错误日志
tail -f logs/codegenerator.log
1
2
3
4
5
6
7
8

# 📚 相关文档


# 🔗 参考链接


最后更新: 2026-03-20