81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
|
|
import json
|
|||
|
|
|
|||
|
|
# 读取更新后的项目数据(包含正确的岗位等级)
|
|||
|
|
with open('food_projects_with_levels.json', 'r', encoding='utf-8') as f:
|
|||
|
|
updated_data = json.load(f)
|
|||
|
|
|
|||
|
|
# 生成JavaScript格式的项目详情
|
|||
|
|
js_details = "const projectDetails = {\n"
|
|||
|
|
for project_id, detail in updated_data['projects_details'].items():
|
|||
|
|
# 处理岗位数据,确保格式正确
|
|||
|
|
positions_str = json.dumps(detail['positions'], ensure_ascii=False, indent=4)
|
|||
|
|
positions_str = positions_str.replace('\n', '\n ')
|
|||
|
|
|
|||
|
|
js_details += f""" {project_id}: {{
|
|||
|
|
id: {project_id},
|
|||
|
|
name: "{detail['name']}",
|
|||
|
|
positions: {positions_str},
|
|||
|
|
unit: "{detail.get('unit', '')}",
|
|||
|
|
overview: `{detail.get('overview', '本项目暂无详细概述')}`,
|
|||
|
|
process: `{detail.get('process', '本项目暂无流程介绍')}`,
|
|||
|
|
keyPoints: `{detail.get('keyPoints', '本项目暂无关键技术点')}`
|
|||
|
|
}},
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
js_details = js_details.rstrip(',\n') + "\n};"
|
|||
|
|
|
|||
|
|
# 读取原始mock文件
|
|||
|
|
with open('src/mocks/projectLibraryMock.js', 'r', encoding='utf-8') as f:
|
|||
|
|
original_content = f.read()
|
|||
|
|
|
|||
|
|
# 找到projectDetails的开始和结束位置
|
|||
|
|
details_start = original_content.find('const projectDetails = {')
|
|||
|
|
if details_start == -1:
|
|||
|
|
print("错误:未找到projectDetails定义")
|
|||
|
|
exit(1)
|
|||
|
|
|
|||
|
|
# 找到projectDetails的结束位置
|
|||
|
|
# 需要正确匹配大括号
|
|||
|
|
brace_count = 0
|
|||
|
|
i = details_start + len('const projectDetails = {') - 1
|
|||
|
|
found_start = False
|
|||
|
|
while i < len(original_content):
|
|||
|
|
if original_content[i] == '{':
|
|||
|
|
if not found_start:
|
|||
|
|
found_start = True
|
|||
|
|
brace_count += 1
|
|||
|
|
elif original_content[i] == '}':
|
|||
|
|
brace_count -= 1
|
|||
|
|
if brace_count == 0:
|
|||
|
|
details_end = i + 1
|
|||
|
|
# 找到分号
|
|||
|
|
while details_end < len(original_content) and original_content[details_end] != ';':
|
|||
|
|
details_end += 1
|
|||
|
|
details_end += 1
|
|||
|
|
break
|
|||
|
|
i += 1
|
|||
|
|
|
|||
|
|
# 替换projectDetails部分
|
|||
|
|
new_content = original_content[:details_start] + js_details + original_content[details_end:]
|
|||
|
|
|
|||
|
|
# 备份原文件
|
|||
|
|
import datetime
|
|||
|
|
backup_name = f"src/mocks/projectLibraryMock.js.backup_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_levels"
|
|||
|
|
with open(backup_name, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(original_content)
|
|||
|
|
print(f"已备份原文件到: {backup_name}")
|
|||
|
|
|
|||
|
|
# 写入新内容
|
|||
|
|
with open('src/mocks/projectLibraryMock.js', 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(new_content)
|
|||
|
|
|
|||
|
|
print("已更新 projectLibraryMock.js 中的项目详情数据,包含正确的岗位等级")
|
|||
|
|
|
|||
|
|
# 验证更新
|
|||
|
|
print("\n验证更新后的数据:")
|
|||
|
|
for i in range(1, 4):
|
|||
|
|
detail = updated_data['projects_details'].get(str(i))
|
|||
|
|
if detail:
|
|||
|
|
print(f"\n项目{i}: {detail['name']}")
|
|||
|
|
for pos in detail['positions'][:2]:
|
|||
|
|
print(f" - {pos['position']}: {pos['level']}")
|