From b4886a77e458634027c111a08b5fb431825fb1ef Mon Sep 17 00:00:00 2001 From: icssoa <2570063477@qq.com> Date: Thu, 16 Mar 2023 16:24:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=8F=9C=E5=8D=95=E7=9A=84?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E8=A7=84=E5=88=99=E6=B7=BB=E5=8A=A0=20?= =?UTF-8?q?=E2=80=9C=E7=BB=84=E6=A8=A1=E5=BC=8F=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/modules/magic/dict/index.ts | 79 ++++++++++++++++++----- src/modules/magic/hooks/ai-code.ts | 5 +- src/modules/magic/hooks/menu.ts | 13 ++-- src/modules/magic/utils/index.ts | 78 ++++++++++++++++------ src/modules/magic/views/ai-code/index.vue | 4 +- 6 files changed, 134 insertions(+), 47 deletions(-) diff --git a/package.json b/package.json index 673afee..6a513f3 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "lint:eslint": "eslint \"{src}/**/*.{vue,ts,tsx}\" --fix" }, "dependencies": { - "@cool-vue/crud": "^6.1.10", + "@cool-vue/crud": "^6.1.11", "@element-plus/icons-vue": "^2.0.10", "@vueuse/core": "^9.1.0", "@wangeditor/editor": "^5.1.23", diff --git a/src/modules/magic/dict/index.ts b/src/modules/magic/dict/index.ts index e938832..29704d7 100644 --- a/src/modules/magic/dict/index.ts +++ b/src/modules/magic/dict/index.ts @@ -1,3 +1,5 @@ +import { Merge } from "/@/cool"; + export const Colors = [ "#409EFF", "#67C23A", @@ -11,7 +13,52 @@ export const Colors = [ "#FB78F2" ]; -export const PropRules = [ +export const PropRules: { + test?: any[]; + group?: string[]; + table?: Merge< + DeepPartial, + { + name?: string; + props?: { + [key: string]: any; + }; + } + >; + form?: ClForm.Item; + handler?: string; + order?: number; +}[] = [ + { + group: ["province", "city", "district"], + table: { + label: "省市区", + formatter(row) { + return row.province + "-" + row.city + "-" + row.district; + } + }, + form: { + label: "省市区", + prop: "pca", + hook: "pca", + component: { + name: "cl-distpicker" + } + } + }, + { + test: ["address", "addr"], + table: { + showOverflowTooltip: true + }, + form: { + name: "el-input", + props: { + type: "textarea", + rows: 3 + } + } + }, { test: ["createTime"], table: { @@ -105,12 +152,10 @@ export const PropRules = [ } }, form: { - component: { - name: "el-date-picker", - props: { - type: "daterange", - valueFormat: "YYYY-MM-DD" - } + name: "el-date-picker", + props: { + type: "daterange", + valueFormat: "YYYY-MM-DD" } } }, @@ -118,13 +163,11 @@ export const PropRules = [ { test: ["times", "timeRange", "timeScope"], form: { - component: { - name: "el-date-picker", - props: { - type: "datetimerange", - valueFormat: "YYYY-MM-DD HH:mm:ss", - defaultTime: [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)] - } + name: "el-date-picker", + props: { + type: "datetimerange", + valueFormat: "YYYY-MM-DD HH:mm:ss", + defaultTime: [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)] } } }, @@ -167,12 +210,14 @@ export const PropRules = [ { test: ["num", "price", "age", "amount", "stock"], form: { - name: "el-input-number", hook: { bind: ["number"] }, - props: { - min: 0 + component: { + name: "el-input-number", + props: { + min: 0 + } } } }, diff --git a/src/modules/magic/hooks/ai-code.ts b/src/modules/magic/hooks/ai-code.ts index d98c09d..7c83df9 100644 --- a/src/modules/magic/hooks/ai-code.ts +++ b/src/modules/magic/hooks/ai-code.ts @@ -101,10 +101,7 @@ export function useChatGPT() { // 发送 function send(data: { name: string; columns: string[]; module: string }) { - socket?.emit("data", { - ...data, - apiKey: apiKey.value - }); + socket?.emit("data", data); } return { diff --git a/src/modules/magic/hooks/menu.ts b/src/modules/magic/hooks/menu.ts index 83c7033..03385e0 100644 --- a/src/modules/magic/hooks/menu.ts +++ b/src/modules/magic/hooks/menu.ts @@ -1,7 +1,7 @@ import { ElMessage } from "element-plus"; import { last } from "lodash-es"; import { MenuData } from "../types"; -import { createComponent } from "../utils"; +import { createComponent, toCodeString } from "../utils"; import { service } from "/@/cool"; export function useCode() { @@ -20,7 +20,12 @@ export function useCode() { // 遍历 columns.forEach((e) => { // 组件 - const { item, column } = createComponent(e); + const { item, column, isHidden } = createComponent(e, columns); + + // 过滤隐藏 + if (isHidden) { + return false; + } // 验证规则 if (!e.nullable) { @@ -165,10 +170,10 @@ export function useCode() { const { service } = useCool(); // cl-upsert - const Upsert = useUpsert(${JSON.stringify(upsert)}); + const Upsert = useUpsert(${toCodeString(upsert)}); // cl-table - const Table = useTable(${JSON.stringify(table)}); + const Table = useTable(${toCodeString(table)}); // cl-crud const Crud = useCrud( diff --git a/src/modules/magic/utils/index.ts b/src/modules/magic/utils/index.ts index 6846a51..36767fd 100644 --- a/src/modules/magic/utils/index.ts +++ b/src/modules/magic/utils/index.ts @@ -80,32 +80,50 @@ const handler = { }; // 创建组件 -export function createComponent(entity: Entity) { +export function createComponent(entity: Entity, columns: Entity[]) { const prop = entity.propertyName; let label = entity.comment; let d: any; + let isHidden = false; PropRules.find((r) => { - const s = r.test.find((e) => { - if (isRegExp(e)) { - return e.test(prop); - } + let s = false; - if (isFunction(e)) { - return e(prop); - } - - if (isString(e)) { - if (e == prop) { - return true; + if (r.test) { + s = !!r.test.find((e) => { + if (isRegExp(e)) { + return e.test(prop); } - const re = new RegExp(`${e}$`); - return re.test(prop.toLocaleLowerCase()); - } + if (isFunction(e)) { + return e(prop); + } - return false; - }); + if (isString(e)) { + if (e == prop) { + return true; + } + + const re = new RegExp(`${e}$`); + return re.test(prop.toLocaleLowerCase()); + } + + return false; + }); + } + + if (r.group) { + if ( + r.group.includes(prop) && + r.group.some((e) => columns.find((c) => c.propertyName == e)) + ) { + if (r.group[0] == prop) { + s = true; + } else { + isHidden = true; + } + } + } if (s) { if (r.handler) { @@ -122,7 +140,7 @@ export function createComponent(entity: Entity) { } } - return !!s; + return s; }); function parse(v: any) { @@ -145,6 +163,28 @@ export function createComponent(entity: Entity) { return { column: parse(d?.table), - item: parse(d?.form) + item: parse(d?.form), + isHidden }; } + +// 转成代码字符串 +export function toCodeString(data: any) { + const arr: string[][] = []; + + let code = JSON.stringify(data, (key, value) => { + if (isFunction(value)) { + const str = value.toString(); + arr.push([JSON.stringify({ [key]: str }), str]); + return str; + } else { + return value; + } + }); + + arr.forEach((e) => { + code = code.replace(e[0].substring(1, e[0].length - 1), e[1]); + }); + + return code; +} diff --git a/src/modules/magic/views/ai-code/index.vue b/src/modules/magic/views/ai-code/index.vue index 7bb70ea..0f4e40b 100644 --- a/src/modules/magic/views/ai-code/index.vue +++ b/src/modules/magic/views/ai-code/index.vue @@ -36,7 +36,7 @@ /> -
Key
+
其他你想做的事?