118 lines
3.5 KiB
JavaScript
118 lines
3.5 KiB
JavaScript
|
|
const fs = require('fs');
|
|||
|
|
|
|||
|
|
// 读取projectLibraryMock.js文件
|
|||
|
|
const mockFilePath = './src/mocks/projectLibraryMock.js';
|
|||
|
|
let mockFileContent = fs.readFileSync(mockFilePath, 'utf-8');
|
|||
|
|
|
|||
|
|
// 找到getMockProjectDetail函数的开始
|
|||
|
|
const functionStart = mockFileContent.indexOf('export const getMockProjectDetail = (id) => {');
|
|||
|
|
if (functionStart === -1) {
|
|||
|
|
console.error('❌ 未找到getMockProjectDetail函数');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 找到函数的结束位置
|
|||
|
|
let braceCount = 0;
|
|||
|
|
let functionEnd = -1;
|
|||
|
|
let inString = false;
|
|||
|
|
let stringChar = '';
|
|||
|
|
|
|||
|
|
for (let i = functionStart; i < mockFileContent.length; i++) {
|
|||
|
|
const char = mockFileContent[i];
|
|||
|
|
const prevChar = i > 0 ? mockFileContent[i - 1] : '';
|
|||
|
|
|
|||
|
|
// 处理字符串
|
|||
|
|
if (!inString && (char === '"' || char === "'" || char === '`')) {
|
|||
|
|
inString = true;
|
|||
|
|
stringChar = char;
|
|||
|
|
} else if (inString && char === stringChar && prevChar !== '\\') {
|
|||
|
|
inString = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 计算大括号
|
|||
|
|
if (!inString) {
|
|||
|
|
if (char === '{') braceCount++;
|
|||
|
|
if (char === '}') {
|
|||
|
|
braceCount--;
|
|||
|
|
if (braceCount === 0) {
|
|||
|
|
functionEnd = i + 1;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (functionEnd === -1) {
|
|||
|
|
console.error('❌ 未找到getMockProjectDetail函数的结束位置');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 提取当前函数
|
|||
|
|
const currentFunction = mockFileContent.substring(functionStart, functionEnd);
|
|||
|
|
|
|||
|
|
// 检查当前函数是否已经返回正确格式
|
|||
|
|
if (currentFunction.includes('success:') || currentFunction.includes('success :')) {
|
|||
|
|
console.log('✅ getMockProjectDetail函数可能已经返回正确格式,检查具体实现...');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 查找getMockProjectsList中的projects数组
|
|||
|
|
const projectsListStart = mockFileContent.indexOf('export const getMockProjectsList');
|
|||
|
|
const projectsStart = mockFileContent.indexOf('const projects = [', projectsListStart);
|
|||
|
|
const projectsEnd = mockFileContent.indexOf('];', projectsStart) + 2;
|
|||
|
|
|
|||
|
|
if (projectsStart === -1 || projectsEnd === -1) {
|
|||
|
|
console.error('❌ 未找到项目数据');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 提取projects数组内容
|
|||
|
|
const projectsContent = mockFileContent.substring(projectsStart + 'const projects = '.length, projectsEnd - 1);
|
|||
|
|
|
|||
|
|
// 构建新的getMockProjectDetail函数
|
|||
|
|
const newFunction = `export const getMockProjectDetail = (id) => {
|
|||
|
|
// 使用与getMockProjectsList相同的数据
|
|||
|
|
const projects = ${projectsContent};
|
|||
|
|
|
|||
|
|
const project = projects.find(p => p.id === parseInt(id));
|
|||
|
|
|
|||
|
|
if (!project) {
|
|||
|
|
return {
|
|||
|
|
success: false,
|
|||
|
|
code: 404,
|
|||
|
|
message: "项目不存在",
|
|||
|
|
data: null
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 构造返回数据,确保包含success字段
|
|||
|
|
return {
|
|||
|
|
success: true,
|
|||
|
|
code: 0,
|
|||
|
|
message: "success",
|
|||
|
|
data: {
|
|||
|
|
id: project.id,
|
|||
|
|
title: project.name,
|
|||
|
|
category: "智能开发",
|
|||
|
|
description: project.overview || "暂无项目概述",
|
|||
|
|
overview: project.overview || "暂无项目概述",
|
|||
|
|
applicablePositions: project.positions || [],
|
|||
|
|
correspondingUnits: project.unit ? [project.unit] : [],
|
|||
|
|
process: project.process || "",
|
|||
|
|
keyPoints: project.keyPoints || "",
|
|||
|
|
attachments: []
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
}`;
|
|||
|
|
|
|||
|
|
// 替换函数
|
|||
|
|
mockFileContent =
|
|||
|
|
mockFileContent.substring(0, functionStart) +
|
|||
|
|
newFunction +
|
|||
|
|
mockFileContent.substring(functionEnd);
|
|||
|
|
|
|||
|
|
// 写入文件
|
|||
|
|
fs.writeFileSync(mockFilePath, mockFileContent, 'utf-8');
|
|||
|
|
|
|||
|
|
console.log('✅ getMockProjectDetail函数已修复!');
|
|||
|
|
console.log('函数现在返回包含success字段的正确格式');
|
|||
|
|
console.log('项目详情弹窗应该能正常打开了');
|