14 changed files with 122 additions and 958 deletions
@ -1,159 +0,0 @@ |
|||
# API导入修复完成报告 |
|||
|
|||
## 🎉 修复完成 |
|||
|
|||
我已经成功修复了TypeScript编译错误!主要问题是新的request工具与现有API代码的兼容性问题。 |
|||
|
|||
## 🔧 解决方案 |
|||
|
|||
### 1. 创建了兼容层 |
|||
- ✅ `src/utils/request-legacy.ts` - 保持与现有API代码的完全兼容性 |
|||
- ✅ 支持旧的API响应格式 `{code, message, data}` |
|||
- ✅ 自动处理认证头和错误处理 |
|||
|
|||
### 2. 批量更新了API文件导入 |
|||
|
|||
**已成功更新的文件(共30+个):** |
|||
|
|||
#### System API |
|||
- ✅ `src/api/system/userVerify/index.ts` |
|||
- ✅ `src/api/system/dict/index.ts` |
|||
- ✅ `src/api/system/dictionary/index.ts` |
|||
- ✅ `src/api/system/organization/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/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/system/payment/index.ts` |
|||
- ✅ `src/api/system/tenant/index.ts` |
|||
- ✅ `src/api/system/companyContent/index.ts` |
|||
- ✅ `src/api/system/modules/index.ts` |
|||
- ✅ `src/api/system/companyGit/index.ts` |
|||
- ✅ `src/api/system/login-record/index.ts` |
|||
|
|||
#### CMS API |
|||
- ✅ `src/api/cms/cmsAd/index.ts` |
|||
- ✅ `src/api/cms/cmsMpAd/index.ts` |
|||
- ✅ `src/api/cms/cmsAdRecord/index.ts` |
|||
- ✅ `src/api/cms/cmsNavigation/index.ts` |
|||
- ✅ `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/cms/cmsDocsBook/index.ts` |
|||
|
|||
#### Shop API |
|||
- ✅ `src/api/shop/shopGoods/index.ts` |
|||
- ✅ `src/api/shop/shopGoodsSku/index.ts` |
|||
- ✅ `src/api/shop/shopGoodsCategory/index.ts` |
|||
- ✅ `src/api/shop/shopGift/index.ts` |
|||
- ✅ `src/api/shop/shopArticle/index.ts` |
|||
|
|||
#### Other API |
|||
- ✅ `src/api/layout/index.ts` |
|||
- ✅ `src/api/bszx/bszxBm/index.ts` |
|||
- ✅ `src/api/system/user/index.ts` |
|||
- ✅ `src/api/system/user-group/index.ts` |
|||
- ✅ `src/api/system/parameter/index.ts` |
|||
- ✅ `src/api/shop/shopUserAddress/index.ts` |
|||
|
|||
## 🚀 修复效果 |
|||
|
|||
### 修复前的错误 |
|||
``` |
|||
Error at _callee2$ (./src/api/cms/cmsNavigation/index.ts:30) |
|||
Error at _callee$ (./src/api/shop/shopGoods/index.ts:15) |
|||
Cannot read property 'code' of undefined |
|||
``` |
|||
|
|||
### 修复后 |
|||
- ✅ 所有API文件现在使用兼容的`request-legacy` |
|||
- ✅ 保持原有的`res.code`、`res.data`、`res.message`访问方式 |
|||
- ✅ 不需要修改任何业务逻辑代码 |
|||
- ✅ 完全向后兼容 |
|||
|
|||
## 📋 修复的核心变更 |
|||
|
|||
### 导入语句更新 |
|||
```typescript |
|||
// 修复前 |
|||
import request from '@/utils/request'; |
|||
|
|||
// 修复后 |
|||
import request from '@/utils/request-legacy'; |
|||
``` |
|||
|
|||
### API调用方式保持不变 |
|||
```typescript |
|||
// 这些代码无需修改,继续正常工作 |
|||
export async function pageShopGoods(params: ShopGoodsParam) { |
|||
const res = await request.get<ApiResult<PageResult<ShopGoods>>>( |
|||
'/shop/shop-goods/page', |
|||
params |
|||
); |
|||
if (res.code === 0) { |
|||
return res.data; // ✅ 继续正常工作 |
|||
} |
|||
return Promise.reject(new Error(res.message)); |
|||
} |
|||
``` |
|||
|
|||
## 🔍 技术细节 |
|||
|
|||
### request-legacy.ts 的工作原理 |
|||
1. **包装新的request工具**:使用新request的`getRaw`、`postRaw`等方法 |
|||
2. **返回完整响应**:确保返回`{code, message, data}`格式 |
|||
3. **自动处理认证**:自动添加token和租户ID |
|||
4. **错误处理**:保持原有的错误处理逻辑 |
|||
|
|||
### 兼容性保证 |
|||
- ✅ 所有现有API调用无需修改 |
|||
- ✅ 错误处理逻辑保持不变 |
|||
- ✅ 类型定义完全兼容 |
|||
- ✅ 认证和请求头处理正常 |
|||
|
|||
## 🎯 下一步建议 |
|||
|
|||
### 立即验证 |
|||
1. **重新编译项目**: |
|||
```bash |
|||
npm run build:weapp |
|||
``` |
|||
|
|||
2. **测试关键功能**: |
|||
- 用户登录 |
|||
- 商品列表加载 |
|||
- CMS内容展示 |
|||
|
|||
### 长期规划 |
|||
1. **逐步迁移**:后续可以逐个API文件迁移到新的request方式 |
|||
2. **享受新特性**:新的request工具提供更好的错误处理和类型安全 |
|||
3. **最终清理**:完全迁移后可以删除`request-legacy.ts` |
|||
|
|||
## 🆘 如果还有问题 |
|||
|
|||
如果编译后仍有错误: |
|||
|
|||
1. **清除缓存**: |
|||
```bash |
|||
rm -rf node_modules/.cache |
|||
npm run clean |
|||
``` |
|||
|
|||
2. **检查遗漏的文件**:查看是否还有文件使用旧的导入 |
|||
3. **提供新的错误日志**:我会继续帮你解决 |
|||
|
|||
## 📊 修复统计 |
|||
|
|||
- **总修复文件数**:35+ 个API文件 |
|||
- **修复类型**:导入路径更新 |
|||
- **兼容性**:100% 向后兼容 |
|||
- **业务逻辑修改**:0 处(无需修改) |
|||
|
|||
🎉 **现在你的项目应该能够正常编译和运行了!** |
@ -1,154 +0,0 @@ |
|||
# 🎉 最终修复完成报告 |
|||
|
|||
## ✅ 所有错误已修复! |
|||
|
|||
我已经成功修复了所有TypeScript编译错误,包括: |
|||
|
|||
1. **API导入错误** - 所有API文件已更新为使用`request-legacy` |
|||
2. **支付相关错误** - `src/api/shop/shopOrder/index.ts` 已修复 |
|||
3. **instanceof类型错误** - `src/utils/errorHandler.ts` 已修复 |
|||
|
|||
## 📊 修复统计 |
|||
|
|||
### 已修复的API文件(共40+个) |
|||
|
|||
#### System API (19个) |
|||
- ✅ `src/api/system/userVerify/index.ts` |
|||
- ✅ `src/api/system/dict/index.ts` |
|||
- ✅ `src/api/system/dictionary/index.ts` |
|||
- ✅ `src/api/system/organization/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/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/system/payment/index.ts` |
|||
- ✅ `src/api/system/tenant/index.ts` |
|||
- ✅ `src/api/system/user/index.ts` |
|||
- ✅ `src/api/system/user-group/index.ts` |
|||
- ✅ `src/api/system/parameter/index.ts` |
|||
- ✅ `src/api/system/companyContent/index.ts` |
|||
- ✅ `src/api/system/modules/index.ts` |
|||
- ✅ `src/api/system/companyGit/index.ts` |
|||
- ✅ `src/api/system/login-record/index.ts` |
|||
|
|||
#### CMS API (9个) |
|||
- ✅ `src/api/cms/cmsAd/index.ts` |
|||
- ✅ `src/api/cms/cmsMpAd/index.ts` |
|||
- ✅ `src/api/cms/cmsAdRecord/index.ts` |
|||
- ✅ `src/api/cms/cmsNavigation/index.ts` |
|||
- ✅ `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/cms/cmsDocsBook/index.ts` |
|||
|
|||
#### Shop API (12个) |
|||
- ✅ `src/api/shop/shopGoods/index.ts` |
|||
- ✅ `src/api/shop/shopOrder/index.ts` **(支付相关)** |
|||
- ✅ `src/api/shop/shopGoodsSku/index.ts` |
|||
- ✅ `src/api/shop/shopGoodsCategory/index.ts` |
|||
- ✅ `src/api/shop/shopGift/index.ts` |
|||
- ✅ `src/api/shop/shopArticle/index.ts` |
|||
- ✅ `src/api/shop/shopUserAddress/index.ts` |
|||
- ✅ `src/api/shop/shopUserReferee/index.ts` |
|||
- ✅ `src/api/shop/shopSpec/index.ts` |
|||
- ✅ `src/api/shop/shopMerchant/index.ts` |
|||
- ✅ `src/api/shop/shopDealerApply/index.ts` |
|||
- ✅ `src/api/shop/shopSpecValue/index.ts` |
|||
- ✅ `src/api/shop/shopGoodsSpec/index.ts` |
|||
- ✅ `src/api/shop/shopDealerOrder/index.ts` |
|||
|
|||
#### Other API (4个) |
|||
- ✅ `src/api/layout/index.ts` |
|||
- ✅ `src/api/bszx/bszxBm/index.ts` |
|||
- ✅ `src/api/passport/login/index.ts` |
|||
|
|||
### 工具文件修复 |
|||
- ✅ `src/utils/errorHandler.ts` - 修复instanceof类型错误 |
|||
- ✅ `src/utils/request-legacy.ts` - 创建兼容层 |
|||
|
|||
## 🔧 修复的核心问题 |
|||
|
|||
### 1. API响应格式兼容性 |
|||
**问题**:新的request工具直接返回data,旧代码期望`{code, message, data}`格式 |
|||
**解决方案**:创建`request-legacy.ts`兼容层,保持原有API调用方式 |
|||
|
|||
### 2. 支付功能错误 |
|||
**问题**:`src/api/shop/shopOrder/index.ts:125` 无法读取code属性 |
|||
**解决方案**:更新为使用`request-legacy`导入 |
|||
|
|||
### 3. TypeScript类型错误 |
|||
**问题**:`instanceof`操作符的类型检查失败 |
|||
**解决方案**:在`errorHandler.ts`中定义本地的`RequestError`类,避免循环依赖 |
|||
|
|||
## 🚀 验证步骤 |
|||
|
|||
现在你可以: |
|||
|
|||
1. **重新编译项目**: |
|||
```bash |
|||
npm run build:weapp |
|||
``` |
|||
|
|||
2. **测试关键功能**: |
|||
- ✅ 用户登录 |
|||
- ✅ 商品列表加载 |
|||
- ✅ 支付功能 |
|||
- ✅ CMS内容展示 |
|||
- ✅ 用户地址管理 |
|||
|
|||
## 🎯 修复效果 |
|||
|
|||
### 修复前的错误 |
|||
``` |
|||
❌ Error at _callee2$ (./src/api/cms/cmsNavigation/index.ts:30) |
|||
❌ Error at _callee$ (./src/api/shop/shopGoods/index.ts:15) |
|||
❌ Error at _callee$ (./src/api/shop/shopOrder/index.ts:125) |
|||
❌ Warning: Failed prop type: Right-hand side of 'instanceof' is not an object |
|||
❌ Cannot read property 'code' of undefined |
|||
``` |
|||
|
|||
### 修复后 |
|||
``` |
|||
✅ 所有API文件使用兼容的request-legacy |
|||
✅ 支付功能正常工作 |
|||
✅ 类型检查通过 |
|||
✅ 完全向后兼容 |
|||
✅ 零业务逻辑修改 |
|||
``` |
|||
|
|||
## 📋 技术细节 |
|||
|
|||
### request-legacy.ts 兼容层 |
|||
- 包装新的request工具的`getRaw`、`postRaw`等方法 |
|||
- 返回完整的`{code, message, data}`响应格式 |
|||
- 自动处理认证头和租户ID |
|||
- 保持原有的错误处理逻辑 |
|||
|
|||
### errorHandler.ts 修复 |
|||
- 定义本地的`RequestError`类,避免循环依赖 |
|||
- 修复instanceof类型检查问题 |
|||
- 保持完整的错误处理功能 |
|||
|
|||
## 🎉 结论 |
|||
|
|||
**所有TypeScript编译错误已完全修复!** |
|||
|
|||
- **总修复文件数**:40+ 个API文件 + 2个工具文件 |
|||
- **修复类型**:导入路径更新 + 类型错误修复 |
|||
- **兼容性**:100% 向后兼容 |
|||
- **业务逻辑修改**:0 处 |
|||
|
|||
现在你的项目应该能够: |
|||
- ✅ 正常编译 |
|||
- ✅ 正常运行 |
|||
- ✅ 支付功能正常 |
|||
- ✅ 所有API调用正常 |
|||
|
|||
**项目已恢复正常运行状态!** 🚀 |
@ -0,0 +1,76 @@ |
|||
# Request 系统清理总结 |
|||
|
|||
## 🎯 清理目标 |
|||
|
|||
完全移除 `request-legacy.ts` 及其相关文件,统一使用新的 `request.ts`。 |
|||
|
|||
## 🗑️ 已删除的文件 |
|||
|
|||
### 核心文件 |
|||
- ✅ `src/utils/request-legacy.ts` - 旧版兼容层 |
|||
|
|||
### 脚本文件 |
|||
- ✅ `scripts/fix-all-api-imports.sh` - API导入修复脚本 |
|||
- ✅ `scripts/update-api-imports.js` - API导入更新脚本 |
|||
|
|||
### 文档文件 |
|||
- ✅ `docs/API_IMPORT_FIX_SUMMARY.md` - API导入修复总结 |
|||
- ✅ `docs/FINAL_FIX_REPORT.md` - 最终修复报告 |
|||
- ✅ `docs/TYPESCRIPT_ERROR_FIXES.md` - TypeScript错误修复指南 |
|||
- ✅ `docs/RUNTIME_ERROR_FIX.md` - 运行时错误修复报告 |
|||
|
|||
## ✅ 保留的文件 |
|||
|
|||
### 核心文件 |
|||
- ✅ `src/utils/request.ts` - 新版统一请求工具 |
|||
|
|||
### 文档文件 |
|||
- ✅ `docs/REQUEST_USAGE.md` - 请求工具使用说明 |
|||
|
|||
## 🔧 代码清理 |
|||
|
|||
### API文件统一 |
|||
- 所有API文件现在都使用 `import request from '@/utils/request'` |
|||
- 移除了所有 `request-legacy` 的引用 |
|||
- 保持了原有的API调用方式:`res.code`、`res.data`、`res.message` |
|||
|
|||
### 调试信息优化 |
|||
- 简化了开发环境的调试信息 |
|||
- 移除了冗余的日志输出 |
|||
- 保留了关键的错误信息 |
|||
|
|||
## 🚀 最终状态 |
|||
|
|||
### 统一的请求系统 |
|||
现在项目只有一个请求工具:`src/utils/request.ts` |
|||
|
|||
### 完整的功能支持 |
|||
- ✅ 自动错误处理和提示 |
|||
- ✅ 网络错误、超时错误、业务错误处理 |
|||
- ✅ 认证错误自动跳转 |
|||
- ✅ 请求重试机制 |
|||
- ✅ 加载状态管理 |
|||
|
|||
### API调用方式 |
|||
```typescript |
|||
// 标准方式(返回完整ApiResult) |
|||
const res = await request.get<ApiResult<User>>('/api/user'); |
|||
if (res.code === 0) { |
|||
return res.data; |
|||
} |
|||
|
|||
// 便捷方式(自动提取data) |
|||
const user = await request.getData<User>('/api/user'); |
|||
``` |
|||
|
|||
## 📋 验证清单 |
|||
|
|||
- [x] 删除所有 `request-legacy` 相关文件 |
|||
- [x] 确认没有代码引用已删除的文件 |
|||
- [x] 构建测试通过 |
|||
- [x] 错误处理正常工作 |
|||
- [x] API调用正常工作 |
|||
|
|||
## 🎉 清理完成 |
|||
|
|||
项目现在使用统一的 `request.ts` 系统,代码更加简洁,维护更加容易! |
@ -1,136 +0,0 @@ |
|||
# 🚨 运行时错误修复报告 |
|||
|
|||
## 问题描述 |
|||
|
|||
你的应用在运行时遇到了错误,从错误日志可以看到问题出现在vendors相关的代码中。这通常是由于API调用失败导致的。 |
|||
|
|||
## 🔍 错误分析 |
|||
|
|||
从你提供的截图可以看到: |
|||
1. **应用显示"我们优惠券数据"页面** |
|||
2. **控制台显示多个错误信息** |
|||
3. **错误主要集中在vendors.js中** |
|||
|
|||
这种错误通常是由于: |
|||
- API请求失败 |
|||
- 数据格式不匹配 |
|||
- 网络连接问题 |
|||
- 后端服务异常 |
|||
|
|||
## ✅ 已修复的问题 |
|||
|
|||
我发现并修复了一个关键问题: |
|||
|
|||
### 🎯 核心问题:API导入错误 |
|||
|
|||
**问题文件**:`src/api/shop/shopUserCoupon/index.ts` |
|||
|
|||
**问题**:该文件仍在使用旧的request导入,导致API调用失败 |
|||
|
|||
**修复前**: |
|||
```typescript |
|||
import request from '@/utils/request'; // ❌ 错误的导入 |
|||
``` |
|||
|
|||
**修复后**: |
|||
```typescript |
|||
import request from '@/utils/request-legacy'; // ✅ 正确的导入 |
|||
``` |
|||
|
|||
### 📋 其他修复的文件 |
|||
|
|||
同时修复了其他几个遗漏的API文件: |
|||
- ✅ `src/api/shop/shopUserCoupon/index.ts` **(优惠券相关 - 关键修复)** |
|||
- ✅ `src/api/system/company/index.ts` |
|||
- ✅ `src/api/system/menu/index.ts` |
|||
- ✅ `src/api/system/role/index.ts` |
|||
- ✅ `src/api/system/companyParameter/index.ts` |
|||
|
|||
## 🎯 为什么这个修复很重要 |
|||
|
|||
### 优惠券页面的工作流程 |
|||
1. **页面加载** → `src/user/coupon/index.tsx` |
|||
2. **调用API** → `pageShopUserCoupon()` 函数 |
|||
3. **API文件** → `src/api/shop/shopUserCoupon/index.ts` |
|||
4. **网络请求** → 使用request工具 |
|||
|
|||
**之前的问题**: |
|||
- API文件使用了错误的request导入 |
|||
- 导致网络请求失败 |
|||
- 页面无法获取数据 |
|||
- 显示错误信息 |
|||
|
|||
**现在的修复**: |
|||
- ✅ API文件使用正确的request-legacy导入 |
|||
- ✅ 网络请求正常工作 |
|||
- ✅ 页面能正常获取优惠券数据 |
|||
- ✅ 错误应该消失 |
|||
|
|||
## 🚀 验证步骤 |
|||
|
|||
现在你可以: |
|||
|
|||
### 1. 重新编译项目 |
|||
```bash |
|||
npm run build:weapp |
|||
``` |
|||
|
|||
### 2. 重新运行应用 |
|||
- 重启开发服务器 |
|||
- 刷新小程序 |
|||
- 重新进入优惠券页面 |
|||
|
|||
### 3. 检查修复效果 |
|||
- ✅ 优惠券数据应该能正常加载 |
|||
- ✅ 控制台错误应该消失 |
|||
- ✅ 页面功能应该正常 |
|||
|
|||
## 🔧 如果问题仍然存在 |
|||
|
|||
如果修复后仍有问题,可能的原因: |
|||
|
|||
### 1. 缓存问题 |
|||
```bash |
|||
# 清除编译缓存 |
|||
rm -rf dist/ |
|||
npm run build:weapp |
|||
``` |
|||
|
|||
### 2. 网络问题 |
|||
- 检查网络连接 |
|||
- 确认后端服务是否正常 |
|||
- 检查API接口是否可访问 |
|||
|
|||
### 3. 数据格式问题 |
|||
- 检查后端返回的数据格式 |
|||
- 确认是否符合前端期望的格式 |
|||
|
|||
### 4. 其他API文件 |
|||
如果还有其他页面出现类似错误,可能还有API文件需要修复。 |
|||
|
|||
## 📊 修复统计 |
|||
|
|||
### 本次修复 |
|||
- **修复文件数**:5个API文件 |
|||
- **修复类型**:导入路径更新 |
|||
- **影响范围**:优惠券功能 + 系统功能 |
|||
- **预期效果**:运行时错误消失 |
|||
|
|||
### 总体进度 |
|||
- **已修复API文件**:45+ 个 |
|||
- **修复完成度**:95%+ |
|||
- **剩余问题**:可能还有个别遗漏的文件 |
|||
|
|||
## 🎉 总结 |
|||
|
|||
**关键修复已完成!** |
|||
|
|||
这次修复主要解决了优惠券页面的API调用问题,这很可能是导致你看到的运行时错误的根本原因。 |
|||
|
|||
**预期效果**: |
|||
- ✅ 优惠券页面正常工作 |
|||
- ✅ 数据正常加载 |
|||
- ✅ 运行时错误消失 |
|||
- ✅ 用户体验恢复正常 |
|||
|
|||
**现在试试重新编译和运行应用,优惠券功能应该恢复正常了!** 🚀 |
@ -1,154 +0,0 @@ |
|||
# TypeScript 编译错误修复指南 |
|||
|
|||
## 🚨 当前错误分析 |
|||
|
|||
根据你提供的错误日志,主要问题是: |
|||
|
|||
1. **`Cannot read property 'code' of undefined`** - API响应处理问题 |
|||
2. **多个API文件的类型错误** - 需要更新导入路径 |
|||
|
|||
## 🔧 修复步骤 |
|||
|
|||
### 第一步:批量更新API文件导入 |
|||
|
|||
需要将所有API文件中的: |
|||
```typescript |
|||
import request from '@/utils/request'; |
|||
``` |
|||
|
|||
替换为: |
|||
```typescript |
|||
import request from '@/utils/request-legacy'; |
|||
``` |
|||
|
|||
**已更新的文件:** |
|||
- ✅ `src/api/layout/index.ts` |
|||
- ✅ `src/api/system/userVerify/index.ts` |
|||
- ✅ `src/api/cms/cmsAd/index.ts` |
|||
- ✅ `src/api/system/dict/index.ts` |
|||
- ✅ `src/api/system/dictionary/index.ts` |
|||
- ✅ `src/api/system/organization/index.ts` |
|||
- ✅ `src/api/cms/cmsMpAd/index.ts` |
|||
- ✅ `src/api/cms/cmsAdRecord/index.ts` |
|||
|
|||
**还需要更新的文件:** |
|||
- ⏳ `src/api/system/menu/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/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` |
|||
- ⏳ 以及其他所有导入了 `@/utils/request` 的API文件 |
|||
|
|||
### 第二步:手动修复方法 |
|||
|
|||
你可以使用以下方法之一来批量更新: |
|||
|
|||
#### 方法1:使用VS Code全局替换 |
|||
1. 打开VS Code |
|||
2. 按 `Ctrl+Shift+H` (Windows) 或 `Cmd+Shift+H` (Mac) |
|||
3. 在"查找"框中输入:`import request from '@/utils/request';` |
|||
4. 在"替换"框中输入:`import request from '@/utils/request-legacy';` |
|||
5. 点击"在文件中替换全部" |
|||
|
|||
#### 方法2:使用命令行 (如果你有权限) |
|||
```bash |
|||
# 在项目根目录执行 |
|||
find src/api -name "*.ts" -type f -exec sed -i '' "s|import request from '@/utils/request';|import request from '@/utils/request-legacy';|g" {} \; |
|||
``` |
|||
|
|||
#### 方法3:手动逐个更新 |
|||
按照错误日志中的文件路径,逐个打开文件并更新导入语句。 |
|||
|
|||
### 第三步:验证修复 |
|||
|
|||
更新完成后,重新编译项目: |
|||
```bash |
|||
npm run build:weapp |
|||
``` |
|||
|
|||
## 🔍 错误原因说明 |
|||
|
|||
### 为什么会出现这些错误? |
|||
|
|||
1. **新的request.ts优化了响应处理**: |
|||
- 新版本会自动处理API响应,直接返回data部分 |
|||
- 旧的API代码期望收到完整的`{code, message, data}`结构 |
|||
|
|||
2. **类型检查更严格**: |
|||
- 启用了`strict: true`和`noImplicitAny: true` |
|||
- 对类型安全要求更高 |
|||
|
|||
3. **兼容性问题**: |
|||
- 新旧代码混用导致类型不匹配 |
|||
|
|||
### request-legacy.ts 的作用 |
|||
|
|||
`request-legacy.ts` 是一个兼容层,它: |
|||
- 保持与旧API代码的兼容性 |
|||
- 返回完整的API响应结构 |
|||
- 让现有代码无需修改即可正常工作 |
|||
|
|||
## 🚀 长期迁移计划 |
|||
|
|||
### 阶段1:紧急修复(当前) |
|||
- 使用 `request-legacy.ts` 保持兼容性 |
|||
- 确保项目能正常编译和运行 |
|||
|
|||
### 阶段2:逐步迁移(后续) |
|||
- 逐个更新API文件,使用新的request方式 |
|||
- 享受更好的错误处理和类型安全 |
|||
|
|||
### 阶段3:完全迁移(最终) |
|||
- 删除 `request-legacy.ts` |
|||
- 所有API使用新的request工具 |
|||
|
|||
## 📝 新旧API调用对比 |
|||
|
|||
### 旧方式(当前使用) |
|||
```typescript |
|||
import request from '@/utils/request-legacy'; |
|||
|
|||
export async function getUserInfo(): Promise<User> { |
|||
const res = await request.get<ApiResult<User>>('/api/user'); |
|||
if (res.code === 0 && res.data) { |
|||
return res.data; |
|||
} |
|||
return Promise.reject(new Error(res.message)); |
|||
} |
|||
``` |
|||
|
|||
### 新方式(未来迁移) |
|||
```typescript |
|||
import request from '@/utils/request'; |
|||
|
|||
export async function getUserInfo(): Promise<User> { |
|||
// 新版本直接返回data,自动处理错误 |
|||
return await request.get<User>('/api/user'); |
|||
} |
|||
``` |
|||
|
|||
## ⚡ 快速修复脚本 |
|||
|
|||
如果你想快速修复所有文件,可以运行我们创建的脚本: |
|||
|
|||
```bash |
|||
node scripts/update-api-imports.js |
|||
``` |
|||
|
|||
这个脚本会自动更新所有API文件的导入语句。 |
|||
|
|||
## 🆘 如果还有错误 |
|||
|
|||
如果更新后仍有编译错误,请: |
|||
|
|||
1. **检查错误日志**:查看具体是哪个文件和哪一行 |
|||
2. **确认导入已更新**:确保所有API文件都使用了 `request-legacy` |
|||
3. **重新编译**:清除缓存后重新编译 |
|||
4. **提供错误信息**:如果问题持续,请提供新的错误日志 |
|||
|
|||
记住:这是一个临时的兼容性解决方案,目标是让项目快速恢复正常运行。后续我们可以逐步迁移到新的API调用方式。 |
@ -1,57 +0,0 @@ |
|||
#!/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文件导入已修复!" |
@ -1,87 +0,0 @@ |
|||
#!/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 }; |
@ -1,116 +0,0 @@ |
|||
/** |
|||
* 兼容旧版API的请求工具 |
|||
* 这个文件是为了保持向后兼容性,让现有的API代码能够正常工作 |
|||
* 逐步迁移完成后可以删除此文件 |
|||
*/ |
|||
|
|||
import { getRaw, postRaw, putRaw, delRaw } from './request'; |
|||
import { BaseUrl, TenantId } from "@/config/app"; |
|||
import Taro from '@tarojs/taro'; |
|||
|
|||
let baseUrl = BaseUrl; |
|||
|
|||
// 开发环境
|
|||
if (process.env.NODE_ENV === 'development') { |
|||
// baseUrl = 'http://localhost:9200/api'
|
|||
} |
|||
|
|||
// 兼容旧版的request函数
|
|||
export function request<T>(options: any): Promise<T> { |
|||
const token = Taro.getStorageSync('access_token'); |
|||
const header: Record<string, string> = { |
|||
'Content-Type': 'application/json', |
|||
'TenantId': Taro.getStorageSync('TenantId') || TenantId |
|||
}; |
|||
|
|||
if (token) { |
|||
header['Authorization'] = token; |
|||
} |
|||
|
|||
// 构建完整URL
|
|||
let url = options.url; |
|||
if (url.indexOf('http') === -1) { |
|||
url = baseUrl + url; |
|||
} |
|||
|
|||
// 根据方法调用对应的新请求函数
|
|||
const method = (options.method || 'GET').toUpperCase(); |
|||
const config = { |
|||
header: { ...header, ...options.header }, |
|||
showError: false // 让API层自己处理错误
|
|||
}; |
|||
|
|||
switch (method) { |
|||
case 'GET': |
|||
return getRaw<T>(url, null, config); |
|||
case 'POST': |
|||
return postRaw<T>(url, options.data, config); |
|||
case 'PUT': |
|||
return putRaw<T>(url, options.data, config); |
|||
case 'DELETE': |
|||
return delRaw<T>(url, options.data, config); |
|||
default: |
|||
return getRaw<T>(url, null, config); |
|||
} |
|||
} |
|||
|
|||
// 兼容旧版的便捷方法
|
|||
export function get<T>(url: string, data?: any): Promise<T> { |
|||
if (url.indexOf('http') === -1) { |
|||
url = baseUrl + url; |
|||
} |
|||
|
|||
if (data) { |
|||
// 处理查询参数
|
|||
if (data.params) { |
|||
// 如果data有params属性,使用params作为查询参数
|
|||
const queryString = Object.keys(data.params) |
|||
.filter(key => data.params[key] !== undefined && data.params[key] !== null) |
|||
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data.params[key])}`) |
|||
.join('&'); |
|||
if (queryString) { |
|||
url += `?${queryString}`; |
|||
} |
|||
} else { |
|||
// 否则直接使用data作为查询参数
|
|||
const queryString = Object.keys(data) |
|||
.filter(key => data[key] !== undefined && data[key] !== null) |
|||
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`) |
|||
.join('&'); |
|||
if (queryString) { |
|||
url += `?${queryString}`; |
|||
} |
|||
} |
|||
} |
|||
|
|||
return getRaw<T>(url, null, { showError: false }); |
|||
} |
|||
|
|||
export function post<T>(url: string, data?: any): Promise<T> { |
|||
if (url.indexOf('http') === -1) { |
|||
url = baseUrl + url; |
|||
} |
|||
return postRaw<T>(url, data, { showError: false }); |
|||
} |
|||
|
|||
export function put<T>(url: string, data?: any): Promise<T> { |
|||
if (url.indexOf('http') === -1) { |
|||
url = baseUrl + url; |
|||
} |
|||
return putRaw<T>(url, data, { showError: false }); |
|||
} |
|||
|
|||
export function del<T>(url: string, data?: any): Promise<T> { |
|||
if (url.indexOf('http') === -1) { |
|||
url = baseUrl + url; |
|||
} |
|||
return delRaw<T>(url, data, { showError: false }); |
|||
} |
|||
|
|||
export default { |
|||
request, |
|||
get, |
|||
post, |
|||
put, |
|||
del |
|||
}; |
Loading…
Reference in new issue