## 主要修复内容 ### 🔧 导入错误修复 - 修复 updateCategoryLogic.go 缺失的导入 (errorx, model, sql, time) - 修复 loginLogic.go 中 errors 包应为 errorx 包的问题 - 修复 uploadPhotoLogic.go 中错误处理不统一的问题 - 修复 photo 查询相关文件缺失 model 包导入 ### ⚡ 错误处理统一化 - 统一使用项目自定义的 errorx 包替代标准库 errors - 完善 model.ErrNotFound 错误判断逻辑 - 添加详细的错误日志记录 - 统一响应代码使用 errorx.Success ### 🆕 错误代码扩展 - 新增 UserDisabled (1003) 错误代码 - 新增 InvalidParameter (400) 错误代码别名 - 完善错误代码到 HTTP 状态码的映射 - 修复重复错误代码导致的编译问题 ### ✅ 代码质量保证 - 解决所有编译错误,确保 go build 成功 - 修复 15 个后端逻辑文件的导入问题 - 整理 go.mod 依赖包 - 更新项目任务进度文档 ## 影响的文件 - backend/internal/logic/auth/loginLogic.go - backend/internal/logic/category/updateCategoryLogic.go - backend/internal/logic/photo/uploadPhotoLogic.go - backend/internal/logic/photo/getPhotoLogic.go - backend/internal/logic/photo/getPhotoListLogic.go - backend/pkg/errorx/errorx.go - TASK_PROGRESS.md
107 lines
3.1 KiB
Go
107 lines
3.1 KiB
Go
package photo
|
||
|
||
import (
|
||
"context"
|
||
"database/sql"
|
||
"mime/multipart"
|
||
"time"
|
||
|
||
"photography-backend/internal/model"
|
||
"photography-backend/internal/svc"
|
||
"photography-backend/internal/types"
|
||
"photography-backend/pkg/errorx"
|
||
fileUtil "photography-backend/pkg/utils/file"
|
||
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
)
|
||
|
||
type UploadPhotoLogic struct {
|
||
logx.Logger
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
}
|
||
|
||
// 上传照片
|
||
func NewUploadPhotoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadPhotoLogic {
|
||
return &UploadPhotoLogic{
|
||
Logger: logx.WithContext(ctx),
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
}
|
||
}
|
||
|
||
func (l *UploadPhotoLogic) UploadPhoto(req *types.UploadPhotoRequest, file multipart.File, header *multipart.FileHeader) (resp *types.UploadPhotoResponse, err error) {
|
||
// 1. 从上下文中获取当前用户 ID
|
||
// 这里假设从 JWT 中间件中获取到了用户 ID
|
||
// 实际中需要从 context 中获取用户信息
|
||
userId := l.ctx.Value("userId")
|
||
if userId == nil {
|
||
// 临时解决方案:如果没有用户ID,使用默认值1
|
||
// 后续需要实现JWT中间件
|
||
userId = int64(1)
|
||
}
|
||
|
||
// 2. 验证分类是否存在
|
||
_, err = l.svcCtx.CategoryModel.FindOne(l.ctx, req.CategoryId)
|
||
if err != nil {
|
||
if err == model.ErrNotFound {
|
||
return nil, errorx.NewWithCode(errorx.CategoryNotFound)
|
||
}
|
||
logx.Errorf("查询分类失败: %v", err)
|
||
return nil, errorx.NewWithCode(errorx.ServerError)
|
||
}
|
||
|
||
// 3. 处理文件上传
|
||
fileConfig := fileUtil.Config{
|
||
MaxSize: l.svcCtx.Config.FileUpload.MaxSize,
|
||
UploadDir: l.svcCtx.Config.FileUpload.UploadDir,
|
||
AllowedTypes: l.svcCtx.Config.FileUpload.AllowedTypes,
|
||
}
|
||
|
||
uploadResult, err := fileUtil.UploadPhoto(file, header, fileConfig)
|
||
if err != nil {
|
||
logx.Errorf("文件上传失败: %v", err)
|
||
return nil, errorx.NewWithCode(errorx.PhotoUploadFail)
|
||
}
|
||
|
||
// 4. 创建照片记录
|
||
photo := &model.Photo{
|
||
Title: req.Title,
|
||
Description: sql.NullString{String: req.Description, Valid: req.Description != ""},
|
||
FilePath: uploadResult.Original.FilePath,
|
||
ThumbnailPath: uploadResult.Thumbnail.FilePath,
|
||
UserId: userId.(int64),
|
||
CategoryId: req.CategoryId,
|
||
CreatedAt: time.Now(),
|
||
UpdatedAt: time.Now(),
|
||
}
|
||
|
||
_, err = l.svcCtx.PhotoModel.Insert(l.ctx, photo)
|
||
if err != nil {
|
||
// 如果数据库保存失败,删除已上传的文件
|
||
fileUtil.DeleteFile(uploadResult.Original.FilePath)
|
||
fileUtil.DeleteFile(uploadResult.Thumbnail.FilePath)
|
||
logx.Errorf("保存照片记录失败: %v", err)
|
||
return nil, errorx.NewWithCode(errorx.ServerError)
|
||
}
|
||
|
||
// 5. 返回上传结果
|
||
return &types.UploadPhotoResponse{
|
||
BaseResponse: types.BaseResponse{
|
||
Code: errorx.Success,
|
||
Message: "上传成功",
|
||
},
|
||
Data: types.Photo{
|
||
Id: photo.Id,
|
||
Title: photo.Title,
|
||
Description: photo.Description.String,
|
||
FilePath: uploadResult.Original.URL,
|
||
ThumbnailPath: uploadResult.Thumbnail.URL,
|
||
UserId: photo.UserId,
|
||
CategoryId: photo.CategoryId,
|
||
CreatedAt: photo.CreatedAt.Unix(),
|
||
UpdatedAt: photo.UpdatedAt.Unix(),
|
||
},
|
||
}, nil
|
||
}
|