#!/usr/bin/env python3 # -*- coding: utf-8 -*- def fix_mockdata_structure(): """完全重构mockData.js的结构""" file_path = 'src/data/mockData.js' try: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() print("开始重构文件结构...") # 找到profileOverview的开始位置 profile_start = content.find('// 个人档案页面数据\n profileOverview: {') if profile_start == -1: profile_start = content.find('profileOverview: {') if profile_start != -1: print(f"找到profileOverview定义位置: {profile_start}") # 在profileOverview之前添加正确的结束符 before_profile = content[:profile_start] # 确保在profileOverview之前正确关闭之前的对象 if not before_profile.rstrip().endswith(','): before_profile = before_profile.rstrip() + ',\n\n ' # 提取profileOverview之后的内容 after_profile_start = content[profile_start:] # 找到profileOverview的结束位置 profile_end = after_profile_start.find('};\n\n// 课程直播间的课程列表数据') if profile_end == -1: profile_end = after_profile_start.find('};\n\n// 生成公共课程列表') if profile_end == -1: profile_end = after_profile_start.find('};\n\nmockData.courseLiveList') if profile_end != -1: print(f"找到profileOverview结束位置: {profile_end}") profile_content = after_profile_start[:profile_end + 2] # 包含 }; after_profile = after_profile_start[profile_end + 3:] # 不包含 }; # 修复profileOverview内容,确保结构正确 profile_content = profile_content.replace('};\n\n// 课程直播间', '};\n\n// 在mockData对象内\n\n// 课程直播间') # 重新组合内容 new_content = before_profile + profile_content.rstrip() # 如果profileOverview后面还有内容,确保正确关闭mockData对象 if after_profile.strip(): new_content += '\n};\n\n' + after_profile else: new_content += '\n};\n\nexport { mockData };' else: print("未找到profileOverview结束位置") return else: print("未找到profileOverview定义") return # 保存修复后的文件 with open(file_path, 'w', encoding='utf-8') as f: f.write(new_content) print("文件结构重构完成!") # 验证语法 import subprocess result = subprocess.run(['node', '-c', file_path], capture_output=True, text=True) if result.returncode == 0: print("✅ JavaScript语法检查通过!") else: print(f"❌ JavaScript语法仍有错误: {result.stderr}") except Exception as e: print(f"修复过程中出错: {e}") if __name__ == "__main__": fix_mockdata_structure()