文档创建时间: 2025-01-16
适用项目: 校园管理系统 (hzdyjs_xg)
目标: 彻底杜绝前后端字段名不一致等接口问题
✅ 前后端字段名必须**完全一致**(大小写、拼写)
✅ 使用**驼峰命名法** (camelCase)
✅ 后端Entity/Bo/Vo**统一字段名**
✅ 前端TypeScript接口**严格对应**后端模型
| 中文含义 | 标准字段名 | 错误示例 | 说明 |
|---|---|---|---|
| 学生学号 | studentNo |
统一使用No结尾 | |
| 学生姓名 | studentName |
明确实体前缀 | |
| 创建时间 | createTime |
与RuoYi框架保持一致 | |
| 当前状态 | currentStatus |
明确状态含义 |
graph LR
A[后端模型定义] --> B[生成API文档]
B --> C[前端接口定义]
C --> D[一致性验证]
D --> E[代码审查]
E --> F[集成测试]
# 安装swagger-typescript-api
npm install -D swagger-typescript-api
# 自动生成TypeScript接口
npx swagger-typescript-api -p http://localhost:8080/v3/api-docs -o ./src/api/types -n api-types.ts
// 后端使用详细注解
@ApiModel(description = "奖学金申请记录")
public class JcbtScholarshipApplicationVo {
@ApiModelProperty(value = "学生学号", required = true)
private String studentNo; // 标准字段名
}
scripts/check-api-consistency.js)// 检查前后端字段名一致性
const fs = require('fs');
const path = require('path');
function checkApiConsistency() {
// 读取Swagger文档
const swaggerDoc = require('../docs/swagger.json');
// 读取前端TypeScript接口
const frontendTypes = fs.readFileSync('./src/api/types.ts', 'utf8');
// 字段名一致性检查
const inconsistencies = [];
Object.keys(swaggerDoc.definitions).forEach(model => {
const backendFields = Object.keys(swaggerDoc.definitions[model].properties);
const frontendInterface = extractInterfaceFields(frontendTypes, model);
backendFields.forEach(field => {
if (!frontendInterface.includes(field)) {
inconsistencies.push({
model: model,
field: field,
issue: 'Backend field missing in frontend'
});
}
});
});
if (inconsistencies.length > 0) {
console.error('❌ API一致性检查失败:', inconsistencies);
process.exit(1);
} else {
console.log('✅ API一致性检查通过');
}
}
checkApiConsistency();
.husky/pre-commit 配置#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
# 运行一致性检查
npm run check:api-consistency
# ESLint检查
npm run lint
# 类型检查
npm run type-check
{
"name": "奖学金申请API测试",
"tests": [
{
"name": "字段完整性检查",
"script": "pm.test('Response has all required fields', function () {
const response = pm.response.json();
pm.expect(response.data).to.have.property('studentNo');
pm.expect(response.data).to.have.property('studentName');
pm.expect(response.data).to.have.property('currentStatus');
});"
}
]
}
describe('ScholarshipApplication API', () => {
test('should have consistent field names', async () => {
const response = await getScholarshipApplication(1);
// 检查关键字段是否存在
expect(response.data).toHaveProperty('studentNo');
expect(response.data).not.toHaveProperty('studentCode'); // 确保错误字段不存在
expect(response.data).toHaveProperty('currentStatus');
});
});
# 1. 确认影响范围
git log --oneline --grep="studentCode\|studentNo" -n 10
# 2. 创建紧急修复分支
git checkout -b hotfix/field-consistency-fix
# 3. 快速定位所有不一致点
grep -r "studentCode" web/src/
# 检查字段名一致性
npm run check:api-consistency
# 生成TypeScript接口
npm run generate:api-types
# 运行接口契约测试
npm run test:api-contract
📝 文档维护: 本规范文档随项目发展持续更新,每月审查一次,确保实用性和时效性。