yl fe46cc7499 feat: 实现我的课程API接口
- 新增mycourse API接口定义 (api/api/mycourse/)
- 实现Controller层,遵循团队四层架构规范
- 实现Logic层业务逻辑,包含用户认证和模拟数据
- 自动生成Service层接口 (gf gen service)
- 支持课程状态筛选和分页功能
- 返回课程基本信息、学习进度、讲师等数据
2025-07-29 11:38:48 +08:00

130 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package mycourse
import (
"context"
v1 "hotgo/api/api/mycourse/v1"
"hotgo/internal/library/contexts"
"hotgo/internal/service"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gtime"
)
type sMyCourse struct{}
func init() {
service.RegisterMyCourse(New())
}
func New() *sMyCourse {
return &sMyCourse{}
}
// GetList 获取我的课程列表
func (s *sMyCourse) GetList(ctx context.Context, req *v1.MyCourseListReq) (res *v1.MyCourseListRes, err error) {
// 从JWT中间件解析出的用户ID
userId := contexts.GetUserId(ctx)
if userId <= 0 {
return nil, gerror.New("用户未登录")
}
// 模拟数据 - 实际项目中应该从数据库查询
// TODO: 后续可以替换为真实的数据库查询
mockData := []*v1.MyCourseItem{
{
Id: 1,
Title: "Go语言基础教程",
Description: "从零开始学习Go语言掌握基础语法和核心概念",
Cover: "https://example.com/covers/go-basic.jpg",
Instructor: "张老师",
Duration: 7200, // 2小时
Progress: 75,
Status: "learning",
EnrollTime: gtime.New("2024-01-15 10:30:00"),
LastStudyTime: gtime.New("2024-01-20 14:20:00"),
},
{
Id: 2,
Title: "Vue.js实战开发",
Description: "深入学习Vue.js框架构建现代化前端应用",
Cover: "https://example.com/covers/vue-practice.jpg",
Instructor: "李老师",
Duration: 10800, // 3小时
Progress: 100,
Status: "completed",
EnrollTime: gtime.New("2024-01-10 09:15:00"),
LastStudyTime: gtime.New("2024-01-18 16:45:00"),
},
{
Id: 3,
Title: "MySQL数据库优化",
Description: "学习MySQL性能优化技巧和最佳实践",
Cover: "https://example.com/covers/mysql-optimization.jpg",
Instructor: "王老师",
Duration: 5400, // 1.5小时
Progress: 45,
Status: "learning",
EnrollTime: gtime.New("2024-01-20 14:00:00"),
LastStudyTime: gtime.New("2024-01-22 10:30:00"),
},
{
Id: 4,
Title: "Python数据分析",
Description: "使用Python进行数据分析和可视化",
Cover: "https://example.com/covers/python-analysis.jpg",
Instructor: "赵老师",
Duration: 9000, // 2.5小时
Progress: 100,
Status: "completed",
EnrollTime: gtime.New("2024-01-05 11:00:00"),
LastStudyTime: gtime.New("2024-01-12 15:30:00"),
},
}
// 根据状态筛选
var filteredData []*v1.MyCourseItem
if req.Status != "" {
for _, item := range mockData {
if item.Status == req.Status {
filteredData = append(filteredData, item)
}
}
} else {
filteredData = mockData
}
totalCount := len(filteredData)
// 分页处理
pageNum := req.Page
pageSize := req.PageSize
if pageNum <= 0 {
pageNum = 1
}
if pageSize <= 0 {
pageSize = 10
}
start := (pageNum - 1) * pageSize
end := start + pageSize
var list []*v1.MyCourseItem
if start >= totalCount {
list = []*v1.MyCourseItem{}
} else {
if end > totalCount {
end = totalCount
}
list = filteredData[start:end]
}
res = &v1.MyCourseListRes{
List: list,
Total: totalCount,
Page: pageNum,
PageSize: pageSize,
}
return res, nil
}