You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

113 lines
2.7 KiB

import os
from jinja2 import Environment, FileSystemLoader
from db import get_table, get_columns
from utils import *
import yaml
env = Environment(loader=FileSystemLoader("templates/vue"))
def render(template, out, ctx):
tpl = env.get_template(template)
content = tpl.render(**ctx)
os.makedirs(os.path.dirname(out), exist_ok=True)
with open(out, "w", encoding="utf-8") as f:
f.write(content)
def build_fields(table):
cols = get_columns(table)
fields = []
for c in cols:
field = {}
field["name"] = to_camel(c["column_name"])
field["comment"] = c["column_comment"]
field["type"] = c["data_type"]
# ⭐ 在这里调用组件解析
field["component"] = parse_component(c)
fields.append(field)
return fields
def generate(table):
with open("./config.yml", "r", encoding="utf-8") as f:
cfg = yaml.safe_load(f)
cfg = resolve_config(cfg)
API_DIR = cfg["frontend"]["root"]+"/"+cfg["frontend"]["api"]
VIEW_DIR = cfg["frontend"]["root"]+"/"+cfg["frontend"]["views"]
ROUTER_DIR = cfg["frontend"]["root"]+"/"+cfg["frontend"]["router"]
MOCK_DIR = cfg["frontend"]["root"]+"/"+cfg["frontend"]["mock"]
EDIT_FIELDS = cfg["frontend"]["editFields"]
table_info = get_table(table)
entity = table.replace("health_","")
ctx = {
"table":table,
"table_comment":table_info["table_comment"],
"old_table": to_kebab(table),
"entity":entity,
"editFields": to_class_join(EDIT_FIELDS),
"fields":build_fields(table)
}
render(
"api.ts.j2",
f"{API_DIR}/{entity}.ts",
ctx
)
render(
"index.vue.j2",
f"{VIEW_DIR}/{entity}/index.vue",
ctx
)
render(
"data.ts.j2",
f"{VIEW_DIR}/{entity}/data.ts",
ctx
)
render(
"form.ts.j2",
f"{VIEW_DIR}/{entity}/form.ts",
ctx
)
render(
"router.ts.j2",
f"{ROUTER_DIR}/{entity}.ts",
ctx
)
render(
"mock.ts.j2",
f"{MOCK_DIR}/{entity}.ts",
ctx
)
# ... existing code ...
if __name__ == "__main__":
import sys
# 从命令行参数获取表名
if len(sys.argv) > 1:
table_name = sys.argv[1]
print(f"=== 生成前端代码1:{table_name} ===")
generate(table_name)
else:
# 如果没有提供参数,提示用户输入
table_name = input("请输入表名 (例如 health_user): ").strip()
if table_name:
print(f"=== 生成前端代码2:{table_name} ===")
generate(table_name)
else:
print("❌ 未输入表名,退出程序")