diff --git a/model/gmodel/fs_user_material_gen.go b/model/gmodel/fs_user_material_gen.go index 4de4076d..5f880096 100644 --- a/model/gmodel/fs_user_material_gen.go +++ b/model/gmodel/fs_user_material_gen.go @@ -6,13 +6,14 @@ import ( // fs_user_material 用户素材表 type FsUserMaterial struct { - Id int64 `gorm:"primary_key;default:0;" json:"id"` // 用户 ID - Module *string `gorm:"default:'';" json:"module"` // 所属模块 - UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID - ResourceId *string `gorm:"default:'';" json:"resource_id"` // 资源ID - ResourceUrl *string `gorm:"default:'';" json:"resource_url"` // 资源 URL - Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式,存储图像分率 - CreateAt *int64 `gorm:"default:0;" json:"create_at"` // 上传时间 + Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // 用户 ID + Module *string `gorm:"default:'';" json:"module"` // 所属模块 + UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID + GuestId *int64 `gorm:"index;default:0;" json:"guest_id"` // 游客 ID + ResourceId *string `gorm:"default:'';" json:"resource_id"` // 资源ID + ResourceUrl *string `gorm:"default:'';" json:"resource_url"` // 资源 URL + Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式,存储图像分率 + CreateAt *int64 `gorm:"default:0;" json:"create_at"` // 上传时间 } type FsUserMaterialModel struct { db *gorm.DB diff --git a/model/gmodel/fs_user_material_logic.go b/model/gmodel/fs_user_material_logic.go index 89dcc405..73111e58 100644 --- a/model/gmodel/fs_user_material_logic.go +++ b/model/gmodel/fs_user_material_logic.go @@ -1,6 +1,12 @@ package gmodel -import "context" +import ( + "context" + "fusenapi/utils/handler" + "reflect" + + "gorm.io/gorm" +) // TODO: 使用model的属性做你想做的 @@ -13,3 +19,39 @@ func (p *FsUserMaterialModel) CreateOrUpdate(ctx context.Context, req *FsUserMat } return req, err } + +func (m *FsUserMaterialModel) FindAll(ctx context.Context, rowBuilder *gorm.DB, filterMap map[string]string, orderBy string) ([]*FsUserMaterial, error) { + var resp []*FsUserMaterial + // 过滤 + if filterMap != nil { + rowBuilder = rowBuilder.Scopes(handler.FilterData(filterMap)) + } + + // 排序 + if orderBy != "" { + var fieldsMap = make(map[string]struct{}) + s := reflect.TypeOf(&FsUserMaterial{}).Elem() //通过反射获取type定义 + for i := 0; i < s.NumField(); i++ { + fieldsMap[s.Field(i).Tag.Get("json")] = struct{}{} + } + rowBuilder = rowBuilder.Scopes(handler.OrderCheck(orderBy, fieldsMap)) + } + + result := rowBuilder.WithContext(ctx).Find(&resp) + if result.Error != nil { + return nil, result.Error + } else { + return resp, nil + } +} + +func (m *FsUserMaterialModel) RowSelectBuilder(selectData []string) *gorm.DB { + var rowBuilder = m.db.Table(m.name) + + if selectData != nil { + rowBuilder = rowBuilder.Select(selectData) + } else { + rowBuilder = rowBuilder.Select("*") + } + return rowBuilder +} diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index 91852e0d..a06a6fea 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -92,6 +92,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/user/order-cancel", Handler: UserOrderCancelHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/user/logo-list", + Handler: UserLogoListHandler(serverCtx), + }, }, ) } diff --git a/server/home-user-auth/internal/handler/userlogolisthandler.go b/server/home-user-auth/internal/handler/userlogolisthandler.go new file mode 100644 index 00000000..873ab4d5 --- /dev/null +++ b/server/home-user-auth/internal/handler/userlogolisthandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/home-user-auth/internal/logic" + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" +) + +func UserLogoListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.UserLogoListReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUserLogoListLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UserLogoList(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/home-user-auth/internal/logic/userlogolistlogic.go b/server/home-user-auth/internal/logic/userlogolistlogic.go new file mode 100644 index 00000000..b3615e04 --- /dev/null +++ b/server/home-user-auth/internal/logic/userlogolistlogic.go @@ -0,0 +1,71 @@ +package logic + +import ( + "errors" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" + + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" +) + +type UserLogoListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserLogoListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogoListLogic { + return &UserLogoListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UserLogoListLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *UserLogoListLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *UserLogoListLogic) UserLogoList(req *types.UserLogoListReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + // 定义用户ID + var userId int64 + var guestId int64 + + // 检查用户是否是游客 + if userinfo.IsGuest() { + // 如果是,使用游客ID和游客键名格式 + guestId = userinfo.GuestId + } else { + // 否则,使用用户ID和用户键名格式 + userId = userinfo.UserId + } + + userMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn) + userMaterialRSB := userMaterialModel.RowSelectBuilder(nil).Where("module = ?", "logo").Where("user_id = ?", userId).Where("guest_id = ?", guestId).Order("id desc") + list, err := userMaterialModel.FindAll(l.ctx, userMaterialRSB, nil, "") + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info") + } + return resp.SetStatus(basic.CodeOK, []interface{}{ + list, + }) +} diff --git a/server/home-user-auth/internal/types/types.go b/server/home-user-auth/internal/types/types.go index 81011e49..7ab699ff 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -5,6 +5,12 @@ import ( "fusenapi/utils/basic" ) +type UserLogoListReq struct { +} + +type UserLogoListRes struct { +} + type UserOrderDeleteReq struct { ID int64 `form:"id"` //订单id } diff --git a/server/upload/internal/logic/uploadlogologic.go b/server/upload/internal/logic/uploadlogologic.go index df4283c9..b5a4ede5 100644 --- a/server/upload/internal/logic/uploadlogologic.go +++ b/server/upload/internal/logic/uploadlogologic.go @@ -1,7 +1,6 @@ package logic import ( - "fmt" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" @@ -46,20 +45,18 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us return resp.SetStatus(basic.CodeUnAuth) } - // 定义用户ID - var uid int64 + var userId int64 + var guestId int64 // 检查用户是否是游客 if userinfo.IsGuest() { // 如果是,使用游客ID和游客键名格式 - uid = userinfo.GuestId + guestId = userinfo.GuestId } else { // 否则,使用用户ID和用户键名格式 - uid = userinfo.UserId + userId = userinfo.UserId } - fmt.Println(uid) - var logoWidth int64 var logoHeight int64 // 查看sku是否存在 @@ -118,7 +115,8 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us userMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn) _, err := userMaterialModel.CreateOrUpdate(l.ctx, &gmodel.FsUserMaterial{ Module: &module, - UserId: &uid, + UserId: &userId, + GuestId: &guestId, ResourceId: &req.ResourceId, ResourceUrl: &req.ResourceUrl, Metadata: &resultStr, diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index bc07a158..c5684eb7 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -68,8 +68,18 @@ service home-user-auth { @handler UserOrderCancelHandler get /api/user/order-cancel (UserOrderCancelReq) returns (response); + // 用户logo列表 + @handler UserLogoListHandler + get /api/user/logo-list (UserLogoListReq) returns (response); } +type ( + UserLogoListReq { + } + UserLogoListRes { + } +) + type ( UserOrderDeleteReq { ID int64 `form:"id"` //订单id