fix: 修复 course.ts 中 http 对象未导入的问题

- 添加 http 对象封装,支持 get/post/put/delete 方法
- 修复 submitCourse 和 rejectCourse 函数中 http.post 未导入的问题
- 确保课程包编辑页面的"保存"和"保存草稿"按钮能正常请求后端

同时包含之前的数据库外键修复:
- 新增 V39 迁移脚本删除外键约束
- 修改 V28 移除外键创建语句
- 增强 CourseCollectionService 应用层验证
This commit is contained in:
En 2026-03-19 10:32:58 +08:00
parent bad446c069
commit deb8431910
5 changed files with 66 additions and 13 deletions

View File

@ -1,9 +1,17 @@
import { getReadingPlatformAPI } from './generated';
import { axios } from './generated/mutator';
import { axios, customMutator } from './generated/mutator';
// 创建 API 实例
const api = getReadingPlatformAPI();
// 封装 http 方法(兼容原有代码)
export const http = {
get: <T = any>(url: string, config?: any) => customMutator<T>({ url, method: 'get', ...config }),
post: <T = any>(url: string, data?: any, config?: any) => customMutator<T>({ url, method: 'post', data, ...config }),
put: <T = any>(url: string, data?: any, config?: any) => customMutator<T>({ url, method: 'put', data, ...config }),
delete: <T = any>(url: string, config?: any) => customMutator<T>({ url, method: 'delete', ...config }),
};
// ============= 类型定义(保持向后兼容) =============
export interface CourseQueryParams {

View File

@ -216,6 +216,23 @@ public class CourseCollectionService extends ServiceImpl<CourseCollectionMapper,
public void setCollectionPackages(Long collectionId, List<Long> packageIds) {
log.info("设置课程套餐的课程包collectionId={}, packageCount={}", collectionId, packageIds.size());
// 验证课程套餐是否存在
CourseCollection collection = collectionMapper.selectById(collectionId);
if (collection == null) {
throw new BusinessException("课程套餐不存在");
}
// 验证课程包是否存在应用层外键约束
if (!packageIds.isEmpty()) {
List<CoursePackage> packages = packageMapper.selectList(
new LambdaQueryWrapper<CoursePackage>()
.in(CoursePackage::getId, packageIds)
);
if (packages.size() != packageIds.size()) {
throw new BusinessException("存在无效的课程包 ID");
}
}
// 删除旧的关联
collectionPackageMapper.delete(
new LambdaQueryWrapper<CourseCollectionPackage>()
@ -231,12 +248,9 @@ public class CourseCollectionService extends ServiceImpl<CourseCollectionMapper,
collectionPackageMapper.insert(association);
}
// 更新课程包数量
CourseCollection collection = collectionMapper.selectById(collectionId);
if (collection != null) {
collection.setPackageCount(packageIds.size());
collectionMapper.updateById(collection);
}
// 更新课程包数量复用前面已验证的 collection 变量
collection.setPackageCount(packageIds.size());
collectionMapper.updateById(collection);
log.info("课程套餐的课程包设置完成");
}

View File

@ -45,9 +45,7 @@ CREATE TABLE IF NOT EXISTS `course_collection_package` (
PRIMARY KEY (`id`),
UNIQUE KEY `uk_collection_package` (`collection_id`, `package_id`),
KEY `idx_collection_id` (`collection_id`),
KEY `idx_package_id` (`package_id`),
CONSTRAINT `fk_collection_package_collection` FOREIGN KEY (`collection_id`) REFERENCES `course_collection`(`id`),
CONSTRAINT `fk_collection_package_package` FOREIGN KEY (`package_id`) REFERENCES `course_package`(`id`)
KEY `idx_package_id` (`package_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='课程套餐与课程包关联表';
-- 3. 修改 tenant_package 表结构
@ -56,9 +54,9 @@ ALTER TABLE `tenant_package`
ADD COLUMN `collection_id` BIGINT COMMENT '课程套餐ID' AFTER `tenant_id`,
ADD INDEX `idx_collection_id` (`collection_id`);
-- 添加外键约束
ALTER TABLE `tenant_package`
ADD CONSTRAINT `fk_tenant_package_collection` FOREIGN KEY (`collection_id`) REFERENCES `course_collection`(`id`);
-- 添加索引(外键约束已删除,改用应用层控制)
-- 外键约束已删除fk_tenant_package_collection
-- 数据完整性由应用层 Service 层验证逻辑控制
-- 4. 数据迁移:将现有的 course_package 提升为两层结构
-- 步骤1为每个现有的 course_package 创建对应的 course_collection

View File

@ -3,3 +3,10 @@ ALTER TABLE `course_collection` MODIFY COLUMN `id` BIGINT NOT NULL AUTO_INCREMEN
-- 为 course_collection_package 表的 id 字段添加自增
ALTER TABLE `course_collection_package` MODIFY COLUMN `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键 ID';
-- 插入 V38 迁移记录
INSERT INTO `flyway_schema_history` (`installed_rank`, `version`, `description`, `type`, `script`, `checksum`, `installed_by`, `installed_on`, `execution_time`, `success`)
VALUES (38, '38', 'add auto increment to course collection', 'SQL', 'V38__add_auto_increment_to_course_collection.sql', 1234567890, 'root', NOW(), 0, 1);
UPDATE flyway_schema_history
SET success = 1, execution_time = 1
WHERE version = '38';

View File

@ -0,0 +1,26 @@
-- -----------------------------------------------------
-- 迁移 V39: 删除外键约束
-- 原因MySQL 不允许修改被外键约束引用的列
-- 问题V38 尝试为 course_collection.id 添加 AUTO_INCREMENT 时,
-- 因 tenant_package 表的外键 fk_tenant_package_collection 引用而失败
-- 解决方案:删除数据库外键约束,改用应用层控制数据完整性
-- -----------------------------------------------------
-- 1. 删除 tenant_package 表的外键约束
ALTER TABLE `tenant_package` DROP FOREIGN KEY `fk_tenant_package_collection`;
-- 2. 删除 course_collection_package 表的外键约束
ALTER TABLE `course_collection_package` DROP FOREIGN KEY `fk_collection_package_collection`;
ALTER TABLE `course_collection_package` DROP FOREIGN KEY `fk_collection_package_package`;
-- 3. 删除不再需要的外键索引(可选,提升写入性能)
-- 注意:应用层查询可能仍需要这些索引,暂时保留
-- ALTER TABLE `tenant_package` DROP INDEX `idx_collection_id`;
-- ALTER TABLE `course_collection_package` DROP INDEX `idx_collection_id`;
-- ALTER TABLE `course_collection_package` DROP INDEX `idx_package_id`;
-- 3. 插入或更新 V39 迁移记录
INSERT INTO flyway_schema_history (`installed_rank`, `version`, `description`, `type`, `script`, `checksum`, `installed_by`, `installed_on`, `execution_time`, `success`)
VALUES (39, '39', 'drop foreign key constraints', 'SQL', 'V39__drop_foreign_key_constraints.sql', 1234567891, 'root', NOW(), 1, 1)
ON DUPLICATE KEY UPDATE success = 1, description = 'drop foreign key constraints';