#!/usr/bin/env python3 import json import re # 读取视觉设计项目案例数据 with open('网页未导入数据/视觉设计产业/视觉设计项目案例.json', 'r', encoding='utf-8') as f: projects_data = json.load(f) # 读取mock文件 with open('src/mocks/projectLibraryMock.js', 'r', encoding='utf-8') as f: content = f.read() # 为每个项目创建附件数据 for idx, project in enumerate(projects_data, 1): attachments_str = project.get("附件", "") if attachments_str: # 将附件字符串分割成列表 attachments = [a.strip() for a in attachments_str.split(',')] # 创建附件对象列表 attachments_list = [] for attachment in attachments: ext = attachment.split('.')[-1].lower() # 根据文件扩展名判断类型 file_type = 'document' if ext in ['jpg', 'jpeg', 'png', 'gif']: file_type = 'image' elif ext in ['mp4', 'avi', 'mov']: file_type = 'video' elif ext == 'pdf': file_type = 'pdf' elif ext == 'psd': file_type = 'design' elif ext in ['doc', 'docx']: file_type = 'word' attachments_list.append({ 'name': attachment, 'type': file_type, 'url': f'/attachments/{attachment}' }) # 在对应项目的keyPoints字段后添加attachments字段 # 查找项目 pattern = rf'"id": {idx},.*?"keyPoints": ".*?"' match = re.search(pattern, content, re.DOTALL) if match: end_pos = match.end() # 转换为JavaScript格式的数组 js_attachments = json.dumps(attachments_list, ensure_ascii=False, indent=6) # 调整缩进 js_attachments = js_attachments.replace('\n', '\n ') # 添加attachments字段 insert_str = f',\n "attachments": {js_attachments}' content = content[:end_pos] + insert_str + content[end_pos:] # 写入修改后的内容 with open('src/mocks/projectLibraryMock.js', 'w', encoding='utf-8') as f: f.write(content) print("附件数据已成功添加到mock文件中")