yl 06b7842580 fix: 适配无认证环境的mycourse接口
- 移除用户未登录的错误返回
- 在无JWT认证时使用默认用户ID进行测试
- 清理未使用的gerror导入
- 适配同事调整的路由配置(无需认证组)
2025-07-29 12:52:36 +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/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) {
// 获取用户ID测试环境下使用默认用户ID
userId := contexts.GetUserId(ctx)
if userId <= 0 {
// 测试环境下使用默认用户ID
userId = 1
}
// 模拟数据 - 实际项目中应该从数据库查询
// 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
}