diff --git a/packages/crud/src/index.ts b/packages/crud/src/index.ts index 4465ad6..7af75f1 100644 --- a/packages/crud/src/index.ts +++ b/packages/crud/src/index.ts @@ -1,4 +1,4 @@ -import { App } from "vue"; +import type { App } from "vue"; import { useComponent } from "./components"; import { useProvide } from "./provide"; import global from "./utils/global"; diff --git a/packages/crud/src/provide.ts b/packages/crud/src/provide.ts index 49b5a67..292501f 100644 --- a/packages/crud/src/provide.ts +++ b/packages/crud/src/provide.ts @@ -1,4 +1,4 @@ -import { App, reactive } from "vue"; +import { type App, reactive } from "vue"; import { mitt } from "./utils/mitt"; import { emitter } from "./emitter"; import { locale } from "./locale"; diff --git a/src/modules/helper/hooks/code.ts b/src/modules/helper/hooks/code.ts index f83e6c3..f500b86 100644 --- a/src/modules/helper/hooks/code.ts +++ b/src/modules/helper/hooks/code.ts @@ -1,4 +1,4 @@ -import { isFunction, isRegExp, isString } from "lodash-es"; +import { isEmpty, isFunction, isRegExp, isString } from "lodash-es"; import { PropRules } from "../dict"; import type { EpsColumn, EpsModule } from "../types"; @@ -10,7 +10,7 @@ export function useCode() { const [label, ...arr] = comment.split(" "); // 选项列表 - const list = arr.map((e) => { + const list: any[] = arr.map((e) => { const [value, label] = e.split("-"); return { @@ -19,6 +19,17 @@ export function useCode() { }; }); + // boolean + if (list.length == 2) { + list.forEach((e) => { + if (e.value == 1) { + e.type = "success"; + } else { + e.type = "danger"; + } + }); + } + const d = { table: { label, @@ -194,6 +205,9 @@ export function useCode() { columns: [] as DeepPartial[] }; + // 选项 + const options = {}; + // 遍历 columns.forEach((e) => { // 创建组件 @@ -209,6 +223,21 @@ export function useCode() { item.required = true; } + // 字典 + const dict = item.component?.options || column.dict; + + if (dict) { + options[item.prop] = dict; + + const str = `$$options.${item.prop}$$`; + + if (!column.component) { + column.dict = str; + } + + item.component.options = str; + } + // 表单忽略 if (!["createTime", "updateTime", "id", "endTime", "endDate"].includes(item.prop)) { upsert.items.push(item); @@ -216,6 +245,11 @@ export function useCode() { // 表格忽略 if (!["id"].includes(item.prop)) { + // 默认排序 + if (item.prop == "createTime") { + column.sortable = "desc"; + } + table.columns.push(column); } @@ -292,29 +326,15 @@ export function useCode() { // 筛选 const clFilter = fieldEq .map((field) => { - const item = table.columns.find((e) => e.prop == field); + const item = upsert.items.find((e) => e.prop == field); if (item) { - if (!item.dict) { - item.dict = []; - } - - if (item.component?.name == "cl-switch") { - item.dict = [ - { label: "是", value: 1 }, - { label: "否", value: 0 } - ]; - } - - return `\n\n\n`; + return `\n\n\n`; } else { return ""; } }) + .filter(Boolean) .join("\n"); // 关键字搜索 @@ -325,8 +345,22 @@ export function useCode() { .filter((e) => !!e) .join("、"); + // 选项 + const ConstOptions = `${ + isEmpty(options) + ? "" + : "\n// 选项\nconst options = reactive(" + toCodeString(options) + ")\n" + }`; + + // Vue 依赖 + let ImportVue = ""; + + if (ConstOptions) { + ImportVue = "import { reactive } from 'vue';\n"; + } + // 代码模板 - return `