fix_contract2.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import re
  2. path = r'd:\Users\chenjun\kyj-yanglao-web-new\src\views\elderly\apply\check-in\ContractForm.vue'
  3. with open(path, 'r', encoding='utf-8') as f:
  4. content = f.read()
  5. # 修复有 class="fill" 的双重嵌套:
  6. # <template v-if="isTextMode"><span class="fill"><template v-if="isTextMode">{{ contractForm.field || '___' }}</template><input v-else v-model="contractForm.field" class="text-input" placeholder="请输入" style="max-width: 180px;" /></span></template><input v-else v-model="contractForm.field" class="text-input" placeholder="请输入" style="max-width: 180px;" />
  7. # ->
  8. # <template v-if="isTextMode"><span class="fill">{{ contractForm.field || '___' }}</span></template><input v-else v-model="contractForm.field" class="text-input" placeholder="请输入" style="max-width: 180px;" />
  9. pattern_nested = r'<template v-if="isTextMode"><span class="fill"><template v-if="isTextMode">(\{\{\s*contractForm\.(\w+)\s*\|\|\s*\'[^\']*\'\s*\}\})</template><input v-else v-model="contractForm\.\w+" class="text-input" placeholder="请输入" style="max-width: 180px;" /></span></template><input v-else v-model="contractForm\.\w+" class="text-input" placeholder="请输入" style="max-width: 180px;" />'
  10. matches = re.findall(pattern_nested, content)
  11. print(f"Found nested fill patterns: {len(matches)}")
  12. def repl_nested(m):
  13. inner_expr = m.group(1) # {{ contractForm.field || '___' }}
  14. field = m.group(2) # field
  15. return f'<template v-if="isTextMode"><span class="fill">{inner_expr}</span></template><input v-else v-model="contractForm.{field}" class="text-input" placeholder="请输入" style="max-width: 180px;" />'
  16. content_new = re.sub(pattern_nested, repl_nested, content)
  17. # 检查是否还有残余的双重嵌套问题
  18. remaining = re.findall(r'<template v-if="isTextMode"><span class="fill"><template', content_new)
  19. print(f"Remaining nested after fix: {len(remaining)}")
  20. # 检查是否还有未被正确替换的原始模式
  21. remaining_orig = re.findall(r"<span class=\"fill\">{{\s*contractForm", content_new)
  22. print(f"Remaining original fill patterns: {len(remaining_orig)}")
  23. # 检查未被正确替换的直接变量
  24. remaining_var = re.findall(r"[^>]\{\{\s*contractForm\.\w+\s*\|\|\s*'", content_new)
  25. print(f"Remaining raw variable patterns: {len(remaining_var)}")
  26. # 保存
  27. with open(path, 'w', encoding='utf-8') as f:
  28. f.write(content_new)
  29. print("Done!")