fix(api): 修复 API 导入导致的 TypeScript 编译错误
- 将所有 API 文件中的 import request from '@/utils/request'替换为 import request from '@/utils/request-legacy'
- 创建了 request-legacy.ts 兼容层,保持与现有 API 代码的完全兼容性
- 支持旧的 API 响应格式 {code, message, data}
- 自动处理认证头和错误处理
- 批量更新了 30+ 个 API 文件的导入路径
- 修复了 TypeScript 编译错误,项目现在可以正常编译和运行
This commit is contained in:
57
scripts/fix-all-api-imports.sh
Normal file
57
scripts/fix-all-api-imports.sh
Normal file
@@ -0,0 +1,57 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 批量修复所有API文件的导入问题
|
||||
# 将 import request from '@/utils/request'; 替换为 import request from '@/utils/request-legacy';
|
||||
|
||||
echo "🚀 开始批量修复API文件导入..."
|
||||
|
||||
# 需要更新的文件列表
|
||||
files=(
|
||||
"src/api/cms/cmsModel/index.ts"
|
||||
"src/api/cms/cmsArticle/index.ts"
|
||||
"src/api/cms/cmsSpecValue/index.ts"
|
||||
"src/api/cms/cmsSpec/index.ts"
|
||||
"src/api/cms/cmsOrder/index.ts"
|
||||
"src/api/system/payment/index.ts"
|
||||
"src/api/shop/shopGoodsSku/index.ts"
|
||||
"src/api/system/tenant/index.ts"
|
||||
"src/api/shop/shopGoodsCategory/index.ts"
|
||||
"src/api/shop/shopGift/index.ts"
|
||||
"src/api/system/plug/index.ts"
|
||||
"src/api/system/environment/index.ts"
|
||||
"src/api/system/url/index.ts"
|
||||
"src/api/system/file/index.ts"
|
||||
"src/api/system/dict-data/index.ts"
|
||||
"src/api/system/dictionary-data/index.ts"
|
||||
"src/api/system/operation-record/index.ts"
|
||||
"src/api/system/user-file/index.ts"
|
||||
"src/api/system/white-domain/index.ts"
|
||||
"src/api/system/menu/index.ts"
|
||||
)
|
||||
|
||||
updated_count=0
|
||||
total_count=${#files[@]}
|
||||
|
||||
for file in "${files[@]}"; do
|
||||
if [ -f "$file" ]; then
|
||||
# 检查文件是否包含目标导入
|
||||
if grep -q "import request from '@/utils/request';" "$file"; then
|
||||
# 执行替换
|
||||
sed -i '' "s|import request from '@/utils/request';|import request from '@/utils/request-legacy';|g" "$file"
|
||||
echo "✅ 已更新: $file"
|
||||
((updated_count++))
|
||||
else
|
||||
echo "⏭️ 跳过: $file (未找到目标导入)"
|
||||
fi
|
||||
else
|
||||
echo "❌ 文件不存在: $file"
|
||||
fi
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "📊 更新完成:"
|
||||
echo " 总文件数: $total_count"
|
||||
echo " 已更新: $updated_count"
|
||||
echo " 跳过: $((total_count - updated_count))"
|
||||
echo ""
|
||||
echo "🎉 所有API文件导入已修复!"
|
||||
87
scripts/update-api-imports.js
Normal file
87
scripts/update-api-imports.js
Normal file
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* 批量更新API文件中的request导入
|
||||
* 将 import request from '@/utils/request' 替换为 import request from '@/utils/request-legacy'
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// 需要更新的API文件列表
|
||||
const apiFiles = [
|
||||
'src/api/system/dict/index.ts',
|
||||
'src/api/system/dictionary/index.ts',
|
||||
'src/api/system/dictionary-data/index.ts',
|
||||
'src/api/system/dict-data/index.ts',
|
||||
'src/api/system/menu/index.ts',
|
||||
'src/api/system/organization/index.ts',
|
||||
'src/api/system/operation-record/index.ts',
|
||||
'src/api/system/user-file/index.ts',
|
||||
'src/api/system/plug/index.ts',
|
||||
'src/api/system/environment/index.ts',
|
||||
'src/api/system/url/index.ts',
|
||||
'src/api/system/file/index.ts',
|
||||
'src/api/system/white-domain/index.ts',
|
||||
'src/api/cms/cmsMpAd/index.ts',
|
||||
'src/api/cms/cmsAdRecord/index.ts',
|
||||
'src/api/shop/shopGoods/index.ts',
|
||||
'src/api/shop/shopOrder/index.ts',
|
||||
'src/api/shop/shopOrderGoods/index.ts',
|
||||
'src/api/shop/shopCategory/index.ts',
|
||||
'src/api/user/coupon/index.ts',
|
||||
'src/api/user/points/index.ts',
|
||||
'src/api/user/gift/index.ts',
|
||||
'src/api/passport/index.ts'
|
||||
];
|
||||
|
||||
function updateFile(filePath) {
|
||||
try {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.log(`文件不存在: ${filePath}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
const content = fs.readFileSync(filePath, 'utf8');
|
||||
const oldImport = "import request from '@/utils/request';";
|
||||
const newImport = "import request from '@/utils/request-legacy';";
|
||||
|
||||
if (content.includes(oldImport)) {
|
||||
const updatedContent = content.replace(oldImport, newImport);
|
||||
fs.writeFileSync(filePath, updatedContent, 'utf8');
|
||||
console.log(`✅ 已更新: ${filePath}`);
|
||||
return true;
|
||||
} else {
|
||||
console.log(`⏭️ 跳过: ${filePath} (未找到目标导入)`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ 更新失败: ${filePath}`, error.message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
console.log('🚀 开始批量更新API文件导入...\n');
|
||||
|
||||
let updatedCount = 0;
|
||||
let totalCount = 0;
|
||||
|
||||
for (const filePath of apiFiles) {
|
||||
totalCount++;
|
||||
if (updateFile(filePath)) {
|
||||
updatedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\n📊 更新完成:`);
|
||||
console.log(` 总文件数: ${totalCount}`);
|
||||
console.log(` 已更新: ${updatedCount}`);
|
||||
console.log(` 跳过: ${totalCount - updatedCount}`);
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main();
|
||||
}
|
||||
|
||||
module.exports = { updateFile };
|
||||
Reference in New Issue
Block a user