import os import logging 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, overwrite=False): # 文件存在且不允许覆盖 → 跳过 if os.path.exists(out) and not overwrite: logging.info("Skip exists file: %s", out) return 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"] = tab_chane_comment(c["column_comment"]) field["type"] = c["data_type"] # ⭐ 在这里调用组件解析 field["component"] = parse_component(c) fields.append(field) return fields def generate_vue(table, cfg , overwrite=False): 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 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, overwrite ) render( "index.vue.j2", f"{VIEW_DIR}/{entity}/index.vue", ctx, overwrite ) render( "data.ts.j2", f"{VIEW_DIR}/{entity}/data.ts", ctx, overwrite ) render( "form.ts.j2", f"{VIEW_DIR}/{entity}/form.ts", ctx, overwrite ) render( "router.ts.j2", f"{ROUTER_DIR}/{entity}.ts", ctx, overwrite ) render( "mock.ts.j2", f"{MOCK_DIR}/{entity}.ts", ctx, overwrite )