package officialWebsiteser import ( "91porn-server/common/stderr" "91porn-server/models/v/officialWebsitemod" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo/options" ) type AlbumListReq struct { SortType int64 `json:"sortType" form:"sortType"` // 1: 创建时间 目前仅支持一种排序方式,后续增加后再完善这个字段 PageNumber int64 `json:"pageNumber" form:"pageNumber"` PageSize int64 `json:"pageSize" form:"pageSize"` } type AlbumListResp struct { HasNext bool `json:"hasNext"` List []*AlbumResp `json:"list"` } type AlbumResp struct { ID string `json:"id"` HeroID string `json:"heroID"` Title string `json:"title"` SeoSlug string `json:"seoSlug"` Description string `json:"description"` Cover string `json:"cover"` IsHot bool `json:"isHot"` Tags []string `json:"tags"` } func (a *AlbumResp) FromModel(album *officialWebsitemod.Album) { if album == nil { return } a.ID = album.ID.Hex() a.HeroID = album.HeroID.Hex() a.Title = album.Title a.SeoSlug = album.SeoSlug a.Description = album.Description a.Cover = album.Cover a.IsHot = album.IsHot a.Tags = make([]string, 0) for _, tag := range album.Tags { a.Tags = append(a.Tags, tag.Name) } } func AlbumList(req *AlbumListReq) (resp AlbumListResp, err error) { var sort = bson.D{} switch req.SortType { // 目前仅支持一种排序方式,后续增加后再完善这个字段 default: sort = bson.D{{Key: "createdAt", Value: -1}} } var albumMod = &officialWebsitemod.Album{} var skip, limit = transPageAndPageNumber(req.PageSize, req.PageNumber) var ops = options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit + 1) albumsData, err := albumMod.FindMany(bson.M{"isActive": true}, ops) if err != nil { return } resp = AlbumListResp{ HasNext: len(albumsData) > int(limit), List: make([]*AlbumResp, 0), } for _, v := range albumsData { var albumResp = &AlbumResp{} albumResp.FromModel(&v) resp.List = append(resp.List, albumResp) } if resp.HasNext { resp.List = resp.List[:len(resp.List)-1] } return resp, nil } type AlbumDetailReq struct { ID string `uri:"id" form:"id"` PageNumber int64 `json:"pageNumber" form:"pageNumber"` PageSize int64 `json:"pageSize" form:"pageSize"` } type AlbumDetailResp struct { AlbumResp Videos []AlbumDetailVideoResp `json:"videos"` } type AlbumDetailVideoResp struct { ID string `json:"id"` Title string `json:"title"` SeoSlug string `json:"seoSlug"` Description string `json:"description"` Cover string `json:"cover"` Url string `json:"url"` IsHot bool `json:"isHot"` Tags []VideoTagResp `json:"tags"` WatchCount int64 `json:"watchCount"` LikeCount int64 `json:"likeCount"` CommentCount int64 `json:"commentCount"` CollectCount int64 `json:"collectCount"` } func AlbumDetail(req *AlbumDetailReq) (resp AlbumDetailResp, err error) { if req.ID == "" { err = stderr.ErrParamError return } // 兼容 id 与 seoSlug:24 位 hex 按 _id 查,否则按 seoSlug 查 filter := bson.M{} if objID, e := primitive.ObjectIDFromHex(req.ID); e == nil { filter["_id"] = objID } else { filter["seoSlug"] = req.ID } albumMod := &officialWebsitemod.Album{} err = albumMod.FindOne(filter) if err != nil { return } resp.FromModel(albumMod) // 组装获取专辑下视频列表 filter = bson.M{"albumId": albumMod.ID} var skip, limit = transPageAndPageNumber(req.PageSize, req.PageNumber) var option = options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}).SetSkip(skip).SetLimit(limit + 1) videoMod := &officialWebsitemod.Video{} var videos []officialWebsitemod.Video videos, err = videoMod.FindMany(filter, option) if err != nil { return } for _, video := range videos { var videoResp = AlbumDetailVideoResp{ ID: video.ID.Hex(), Title: video.Title, SeoSlug: video.SeoSlug, Description: video.Description, Cover: video.Cover, Url: video.Url, IsHot: video.IsHot, WatchCount: video.WatchCount, LikeCount: video.LikeCount, CommentCount: video.CommentCount, CollectCount: video.CollectCount, } for _, tag := range video.Tags { videoResp.Tags = append(videoResp.Tags, VideoTagResp{ ID: tag.ID.Hex(), Name: tag.Name, }) } resp.Videos = append(resp.Videos, videoResp) } return }