#!/usr/bin/env python3 import json import re import os from datetime import datetime # 读取mock文件 mock_file = 'src/mocks/projectLibraryMock.js' with open(mock_file, 'r', encoding='utf-8') as f: content = f.read() # 创建备份 backup_file = f"{mock_file}.backup_modal_fix_{datetime.now().strftime('%Y%m%d_%H%M%S')}" with open(backup_file, 'w', encoding='utf-8') as f: f.write(content) print(f"已创建备份:{backup_file}") # 岗位等级排序权重 level_order = { "普通岗": 0, "技术骨干岗": 1, "储备干部岗": 2 } # 处理每个项目,添加applicablePositions def fix_positions_field(text): # 查找所有positions数组 pattern = r'("positions"\s*:\s*\[)((?:\s*\{[^}]+\}\s*,?\s*)+)(\])' def process_match(match): prefix = match.group(1) positions_content = match.group(2) suffix = match.group(3) # 提取所有岗位对象 pos_pattern = r'\{\s*"level"\s*:\s*"([^"]+)"\s*,\s*"position"\s*:\s*"([^"]+)"\s*\}' positions = [] for m in re.finditer(pos_pattern, positions_content): level = m.group(1) position = m.group(2) positions.append({ 'level': level, 'position': position, 'order': level_order.get(level, 99) }) # 按等级排序 positions.sort(key=lambda x: x['order']) # 重建positions数组(已排序) sorted_positions = [] for p in positions: sorted_positions.append(f''' {{ "level": "{p['level']}", "position": "{p['position']}" }}''') new_positions = prefix + ','.join(sorted_positions) + '\n ' + suffix # 添加applicablePositions(保持相同的排序) applicable = [] for p in positions: applicable.append(f''' {{ "level": "{p['level']}", "position": "{p['position']}" }}''') applicable_positions = ',\n "applicablePositions": [' + ','.join(applicable) + '\n ]' return new_positions + applicable_positions # 替换所有positions数组 text = re.sub(pattern, process_match, text) return text # 处理unit字段,添加compoundUnits和verticalUnits def fix_unit_fields(text): # 查找所有unit字段 pattern = r'"unit"\s*:\s*"([^"]+)"' def process_match(match): unit_content = match.group(1) result = f'"unit": "{unit_content}"' # 拆分单元信息 if ' / ' in unit_content: parts = unit_content.split(' / ') compound = parts[0].strip() vertical = parts[1].strip() result += f',\n "compoundUnits": ["{compound}"],\n "verticalUnits": ["{vertical}"]' else: compound = unit_content.strip() result += f',\n "compoundUnits": ["{compound}"],\n "verticalUnits": []' return result # 只处理getMockProjectDetail函数中的unit字段 detail_start = text.find('export const getMockProjectDetail') if detail_start > 0: # 找到函数结束位置 func_end = text.find('export const', detail_start + 1) if func_end == -1: func_end = len(text) # 只在这个函数范围内替换 before = text[:detail_start] func_content = text[detail_start:func_end] after = text[func_end:] # 检查是否已经有compoundUnits if '"compoundUnits"' not in func_content: func_content = re.sub(pattern, process_match, func_content) text = before + func_content + after return text # 应用修复 new_content = fix_positions_field(content) new_content = fix_unit_fields(new_content) # 写入文件 with open(mock_file, 'w', encoding='utf-8') as f: f.write(new_content) # 验证语法 result = os.popen(f'node -c {mock_file} 2>&1').read() if result: print(f"❌ 语法错误:{result}") # 恢复备份 with open(backup_file, 'r', encoding='utf-8') as f: content = f.read() with open(mock_file, 'w', encoding='utf-8') as f: f.write(content) print("已恢复备份") else: print("✓ 语法验证通过") print("✓ 成功添加applicablePositions字段(已排序)") print("✓ 成功添加compoundUnits和verticalUnits字段") print("✓ 岗位已按照\"普通岗→技术骨干岗→储备干部岗\"排序")