index.vue 14.6 KB
Newer Older
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
<template>
  <div class="app-container">
    <Breadcrumb :items="['menu.tool', 'menu.tool.generator.list']" />
    <a-card class="general-card" :title="$t('menu.tool.generator.list')">
      <!-- 头部区域 -->
      <div class="header">
        <!-- 搜索栏 -->
        <div v-if="showQuery" class="header-query">
          <a-form ref="queryRef" :model="queryParams" layout="inline">
            <a-form-item field="tableName" hide-label>
              <a-input
                v-model="queryParams.tableName"
                placeholder="输入表名称搜索"
                allow-clear
                style="width: 150px"
                @press-enter="handleQuery"
              />
            </a-form-item>
            <a-form-item hide-label>
              <a-space>
                <a-button type="primary" @click="handleQuery">
                  <template #icon><icon-search /></template>查询
                </a-button>
                <a-button @click="resetQuery">
                  <template #icon><icon-refresh /></template>重置
                </a-button>
              </a-space>
            </a-form-item>
          </a-form>
        </div>
      </div>

      <!-- 列表区域 -->
      <a-table
        ref="tableRef"
36
        row-key="tableName"
37
        :data="tableList"
38
        :loading="loading"
39 40 41 42 43 44 45
        :pagination="{
          showTotal: true,
          showPageSize: true,
          total: total,
          current: queryParams.page,
        }"
        :bordered="false"
46 47
        column-resizable
        stripe
48 49 50 51 52 53 54 55 56 57
        size="large"
        @page-change="handlePageChange"
        @page-size-change="handlePageSizeChange"
      >
        <template #columns>
          <a-table-column title="序号">
            <template #cell="{ rowIndex }">
              {{ rowIndex + 1 + (queryParams.page - 1) * queryParams.size }}
            </template>
          </a-table-column>
58
          <a-table-column title="表名称" data-index="tableName" :width="225" />
59
          <a-table-column title="描述" data-index="comment" tooltip />
60 61 62 63 64
          <a-table-column title="存储引擎" data-index="engine" align="center" />
          <a-table-column title="字符集" data-index="charset" />
          <a-table-column title="创建时间" data-index="createTime" />
          <a-table-column title="操作" align="center">
            <template #cell="{ record }">
65 66 67 68 69 70 71 72
              <a-button
                type="text"
                size="small"
                title="配置"
                @click="toConfig(record.tableName)"
              >
                <template #icon><icon-settings /></template>配置
              </a-button>
73 74 75
              <a-button
                type="text"
                size="small"
76 77
                :title="record.isConfiged ? '生成' : '请先进行生成配置'"
                :disabled="!record.isConfiged"
78
                @click="handleGenerate(record.tableName)"
79 80 81 82 83 84
              >
                <template #icon><icon-robot-add /></template>生成
              </a-button>
            </template>
          </a-table-column>
        </template>
85 86 87 88 89 90 91
        <template #pagination-left>
          <a-tooltip content="刷新">
            <div class="action-icon" @click="handleQuery">
              <icon-refresh size="18" />
            </div>
          </a-tooltip>
        </template>
92
      </a-table>
93 94 95 96 97 98 99 100 101 102 103 104 105 106

      <!-- 表单区域 -->
      <a-drawer
        :title="title"
        :visible="visible"
        :width="1000"
        :mask-closable="false"
        :esc-to-close="false"
        unmount-on-close
        render-to-body
        @ok="handleOk"
        @cancel="handleCancel"
      >
        <a-card title="字段配置" class="field-config">
107
          <template #extra>
108 109 110 111 112 113 114 115 116 117 118 119
            <a-popconfirm
              content="确定要同步最新数据表结构吗?同步后只要不点击确定保存,则不影响原有配置数据。"
              type="warning"
              @ok="handleRefresh(form.tableName)"
            >
              <a-tooltip content="同步最新数据表结构">
                <a-button
                  type="primary"
                  status="success"
                  size="small"
                  title="同步"
                  :disabled="
120 121
                    fieldConfigList.length !== 0 &&
                    fieldConfigList[0].createTime === null
122 123 124 125 126 127
                  "
                >
                  <template #icon><icon-sync /></template>同步
                </a-button>
              </a-tooltip>
            </a-popconfirm>
128
          </template>
129
          <a-table
130 131 132
            ref="fieldConfigRef"
            :data="fieldConfigList"
            :loading="fieldConfigLoading"
133 134 135 136 137 138 139 140 141 142 143
            :pagination="false"
            :bordered="false"
            size="large"
            :scroll="{
              y: 400,
            }"
          >
            <template #columns>
              <a-table-column
                title="名称"
                data-index="fieldName"
144
                :width="125"
145 146 147 148 149 150
                ellipsis
                tooltip
              />
              <a-table-column
                title="类型"
                data-index="fieldType"
151
                :width="95"
152 153 154 155 156 157 158 159 160
                ellipsis
                tooltip
              />
              <a-table-column title="描述" data-index="comment" :width="170">
                <template #cell="{ record }">
                  <a-input v-model="record.comment" />
                </template>
              </a-table-column>
              <a-table-column
161 162
                title="列表"
                data-index="showInList"
163 164 165 166
                :width="60"
                align="center"
              >
                <template #cell="{ record }">
167
                  <a-checkbox v-model="record.showInList" value="true" />
168 169 170
                </template>
              </a-table-column>
              <a-table-column
171 172
                title="表单"
                data-index="showInForm"
173 174 175 176
                :width="60"
                align="center"
              >
                <template #cell="{ record }">
177
                  <a-checkbox v-model="record.showInForm" value="true" />
178 179 180
                </template>
              </a-table-column>
              <a-table-column
181 182
                title="必填"
                data-index="isRequired"
183 184 185 186
                :width="60"
                align="center"
              >
                <template #cell="{ record }">
187 188 189 190 191
                  <a-checkbox
                    v-if="record.showInForm"
                    v-model="record.isRequired"
                    value="true"
                  />
192
                  <a-checkbox v-else disabled />
193 194 195
                </template>
              </a-table-column>
              <a-table-column
196 197
                title="查询"
                data-index="showInQuery"
198 199 200 201
                :width="60"
                align="center"
              >
                <template #cell="{ record }">
202
                  <a-checkbox v-model="record.showInQuery" value="true" />
203 204 205 206 207 208 209 210 211
                </template>
              </a-table-column>
              <a-table-column
                title="表单类型"
                data-index="formType"
                :width="150"
              >
                <template #cell="{ record }">
                  <a-select
212
                    v-if="record.showInForm || record.showInQuery"
213 214 215 216
                    v-model="record.formType"
                    :options="FormTypeEnum"
                    placeholder="请选择表单类型"
                  />
217
                  <span v-else>无需设置</span>
218 219 220 221 222
                </template>
              </a-table-column>
              <a-table-column title="查询方式" data-index="queryType">
                <template #cell="{ record }">
                  <a-select
223
                    v-if="record.showInQuery"
224 225 226 227
                    v-model="record.queryType"
                    :options="QueryTypeEnum"
                    placeholder="请选择查询方式"
                  />
228
                  <span v-else>无需设置</span>
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
                </template>
              </a-table-column>
            </template>
          </a-table>
        </a-card>
        <a-card title="生成配置" style="margin-top: 10px">
          <a-form
            ref="formRef"
            :model="form"
            :rules="rules"
            class="gen-config"
            size="large"
          >
            <a-form-item label="作者名称" field="author">
              <a-input v-model="form.author" placeholder="请输入作者名称" />
            </a-form-item>
            <a-form-item label="业务名称" field="businessName">
              <a-input
                v-model="form.businessName"
                placeholder="自定义业务名称,例如:用户"
              />
            </a-form-item>
            <a-form-item label="所属模块" field="moduleName">
              <a-input
                v-model="form.moduleName"
                placeholder="项目模块名称,例如:continew-admin-system"
              />
            </a-form-item>
            <a-form-item label="模块包名" field="packageName">
              <a-input
                v-model="form.packageName"
                placeholder="项目模块包名,例如:top.charles7c.cnadmin.system"
              />
            </a-form-item>
            <a-form-item label="前端路径" field="frontendPath">
              <a-input
                v-model="form.frontendPath"
                placeholder="前端项目 views 目录地址"
              />
            </a-form-item>
            <a-form-item label="去表前缀" field="tablePrefix">
              <a-input
                v-model="form.tablePrefix"
                placeholder="数据库表前缀,例如:sys_"
              />
            </a-form-item>
            <a-form-item label="是否覆盖" field="isOverride">
              <a-radio-group v-model="form.isOverride" type="button">
                <a-radio :value="true"></a-radio>
                <a-radio :value="false"></a-radio>
              </a-radio-group>
            </a-form-item>
          </a-form>
        </a-card>
      </a-drawer>
284 285 286 287 288 289
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { getCurrentInstance, ref, toRefs, reactive } from 'vue';
290 291 292
  import {
    TableRecord,
    TableParam,
293
    FieldConfigRecord,
294 295
    GenConfigRecord,
    listTable,
296
    listFieldConfig,
297
    getGenConfig,
298 299
    GeneratorConfigRecord,
    saveConfig,
300
    generate,
301
  } from '@/api/tool/generator';
302 303

  const { proxy } = getCurrentInstance() as any;
304 305 306 307
  const { FormTypeEnum, QueryTypeEnum } = proxy.useDict(
    'FormTypeEnum',
    'QueryTypeEnum'
  );
308 309

  const tableList = ref<TableRecord[]>([]);
310
  const fieldConfigList = ref<FieldConfigRecord[]>([]);
311
  const total = ref(0);
312
  const title = ref('');
313 314
  const showQuery = ref(true);
  const loading = ref(false);
315
  const visible = ref(false);
316
  const fieldConfigLoading = ref(false);
317 318 319 320 321 322 323 324

  const data = reactive({
    // 查询参数
    queryParams: {
      tableName: undefined,
      page: 1,
      size: 10,
    },
325 326
    // 表单数据
    form: {} as GenConfigRecord,
327
    // 代码生成配置数据
328
    config: {} as GeneratorConfigRecord,
329 330 331
    // 表单验证规则
    rules: {
      author: [{ required: true, message: '请输入作者名称' }],
332 333
      moduleName: [{ required: true, message: '请输入所属模块' }],
      packageName: [{ required: true, message: '请输入模块包名' }],
334 335
      businessName: [{ required: true, message: '请输入业务名称' }],
    },
336
  });
337
  const { queryParams, form, rules, config } = toRefs(data);
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

  /**
   * 查询列表
   *
   * @param params 查询参数
   */
  const getList = (params: TableParam = { ...queryParams.value }) => {
    loading.value = true;
    listTable(params)
      .then((res) => {
        tableList.value = res.data.list;
        total.value = res.data.total;
      })
      .finally(() => {
        loading.value = false;
      });
  };
  getList();

357 358 359 360 361 362
  /**
   * 打开配置对话框
   *
   * @param tableName 表名称
   */
  const toConfig = (tableName: string) => {
363 364 365 366 367
    let tableComment = tableList.value.filter(
      (t) => t.tableName === tableName
    )[0].comment;
    tableComment = tableComment ? `(${tableComment})` : ' ';
    title.value = `${tableName}${tableComment}配置`;
368
    visible.value = true;
369 370
    // 查询字段配置
    getFieldConfig(tableName, false);
371 372 373 374 375 376 377 378 379 380 381 382 383
    // 查询生成配置
    getGenConfig(tableName).then((res) => {
      form.value = res.data;
      form.value.isOverride = false;
    });
  };

  /**
   * 同步
   *
   * @param tableName 表名称
   */
  const handleRefresh = (tableName: string) => {
384
    getFieldConfig(tableName, true);
385 386 387
  };

  /**
388
   * 查询字段配置
389 390 391 392
   *
   * @param tableName 表名称
   * @param requireSync 是否需要同步
   */
393 394 395
  const getFieldConfig = (tableName: string, requireSync: boolean) => {
    fieldConfigLoading.value = true;
    listFieldConfig(tableName, requireSync)
396
      .then((res) => {
397
        fieldConfigList.value = res.data;
398 399
      })
      .finally(() => {
400
        fieldConfigLoading.value = false;
401 402 403 404 405 406 407
      });
  };

  /**
   * 确定
   */
  const handleOk = () => {
408 409
    proxy.$refs.formRef.validate((valid: any) => {
      if (!valid) {
410
        config.value.fieldConfigs = fieldConfigList.value;
411 412 413 414 415 416 417 418
        config.value.genConfig = form.value;
        saveConfig(form.value.tableName, config.value).then((res) => {
          handleCancel();
          getList();
          proxy.$message.success(res.msg);
        });
      }
    });
419 420 421 422 423 424 425
  };

  /**
   * 关闭配置
   */
  const handleCancel = () => {
    visible.value = false;
426
    proxy.$refs.formRef?.resetFields();
427
    fieldConfigList.value = [];
428 429
  };

430
  /**
431
   * 生成代码
432 433 434
   *
   * @param tableName 表名称
   */
435
  const handleGenerate = (tableName: string) => {
436 437 438
    generate(tableName).then((res) => {
      proxy.$message.success(res.msg);
    });
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
  };

  /**
   * 查询
   */
  const handleQuery = () => {
    getList();
  };

  /**
   * 重置
   */
  const resetQuery = () => {
    proxy.$refs.queryRef.resetFields();
    handleQuery();
  };

  /**
   * 切换页码
   *
   * @param current 页码
   */
  const handlePageChange = (current: number) => {
    queryParams.value.page = current;
    getList();
  };

  /**
   * 切换每页条数
   *
   * @param pageSize 每页条数
   */
  const handlePageSizeChange = (pageSize: number) => {
    queryParams.value.size = pageSize;
    getList();
  };
</script>

<script lang="ts">
  export default {
479
    name: 'Generator',
480 481 482
  };
</script>

483 484 485 486 487 488 489 490
<style scoped lang="less">
  .field-config :deep(.arco-card-body) {
    padding: 0;
  }

  :deep(.gen-config.arco-form) {
    width: 50%;
  }
491 492 493 494 495 496 497 498 499

  .action-icon {
    cursor: pointer;
    margin-right: 10px;
  }

  .action-icon:hover {
    color: #0960bd;
  }
500
</style>