diff --git a/models/share.go b/models/share.go index 74b3767..83d656b 100644 --- a/models/share.go +++ b/models/share.go @@ -234,7 +234,7 @@ func ListShares(uid uint, page, pageSize int, order string, publicOnly bool) ([] dbChain := DB dbChain = dbChain.Where("user_id = ?", uid) if publicOnly { - dbChain.Where("password = ?", "") + dbChain = dbChain.Where("password = ?", "") } // 计算总数用于分页 diff --git a/models/user.go b/models/user.go index a239116..80a25c0 100644 --- a/models/user.go +++ b/models/user.go @@ -54,7 +54,7 @@ type User struct { // UserOption 用户个性化配置字段 type UserOption struct { - ProfileOff int `json:"profile_off,omitempty"` + ProfileOff bool `json:"profile_off,omitempty"` PreferredPolicy uint `json:"preferred_policy"` PreferredTheme string `json:"preferred_theme"` } @@ -181,9 +181,7 @@ func GetUserByEmail(email string) (User, error) { // NewUser 返回一个新的空 User func NewUser() User { - options := UserOption{ - ProfileOn: 1, - } + options := UserOption{} return User{ Avatar: "default", OptionsSerialized: options, diff --git a/pkg/serializer/user.go b/pkg/serializer/user.go index c118e2b..c9d035d 100644 --- a/pkg/serializer/user.go +++ b/pkg/serializer/user.go @@ -16,7 +16,7 @@ func CheckLogin() Response { // User 用户序列化器 type User struct { - ID uint `json:"id"` + ID string `json:"id"` Email string `json:"user_name"` Nickname string `json:"nickname"` Status int `json:"status"` @@ -67,7 +67,7 @@ type storage struct { func BuildUser(user model.User) User { tags, _ := model.GetTagsByUID(user.ID) return User{ - ID: user.ID, + ID: hashid.HashID(user.ID, hashid.UserID), Email: user.Email, Nickname: user.Nick, Status: user.Status, diff --git a/routers/controllers/share.go b/routers/controllers/share.go index 49ad750..a264f13 100644 --- a/routers/controllers/share.go +++ b/routers/controllers/share.go @@ -217,3 +217,14 @@ func ShareThumb(c *gin.Context) { c.JSON(200, ErrorResponse(err)) } } + +// GetUserShare 查看给定用户的分享 +func GetUserShare(c *gin.Context) { + var service share.ShareUserGetService + if err := c.ShouldBindQuery(&service); err == nil { + res := service.Get(c) + c.JSON(200, res) + } else { + c.JSON(200, ErrorResponse(err)) + } +} diff --git a/routers/router.go b/routers/router.go index 42e6fdf..59a4830 100644 --- a/routers/router.go +++ b/routers/router.go @@ -107,6 +107,11 @@ func InitMasterRouter() *gin.Engine { user.GET("authn/:username", controllers.StartLoginAuthn) // WebAuthn登陆 user.POST("authn/finish/:username", controllers.FinishLoginAuthn) + // 获取用户主页展示用分享 + user.GET("profile/:id", + middleware.HashID(hashid.UserID), + controllers.GetUserShare, + ) } // 需要携带签名验证的 diff --git a/service/share/visit.go b/service/share/visit.go index b0077d1..8de632f 100644 --- a/service/share/visit.go +++ b/service/share/visit.go @@ -15,6 +15,12 @@ import ( "path" ) +// ShareUserGetService 获取用户的分享服务 +type ShareUserGetService struct { + Type string `form:"type" binding:"required,eq=hot|eq=default"` + Page uint `form:"page" binding:"required,min=1"` +} + // ShareGetService 获取分享服务 type ShareGetService struct { Password string `form:"password" binding:"max=255"` @@ -41,6 +47,46 @@ type ShareListService struct { Keywords string `form:"keywords"` } +// Get 获取给定用户的分享 +func (service *ShareUserGetService) Get(c *gin.Context) serializer.Response { + // 取得用户 + userID, _ := c.Get("object_id") + user, err := model.GetActiveUserByID(userID.(uint)) + if err != nil || user.OptionsSerialized.ProfileOff { + return serializer.Err(serializer.CodeNotFound, "用户不存在", err) + } + + // 列出分享 + hotNum := model.GetIntSetting("hot_share_num", 10) + if service.Type == "default" { + hotNum = 10 + } + orderBy := "created_at desc" + if service.Type == "hot" { + orderBy = "views desc" + } + shares, total := model.ListShares(user.ID, int(service.Page), hotNum, orderBy, true) + // 列出分享对应的文件 + for i := 0; i < len(shares); i++ { + shares[i].Source() + } + + res := serializer.BuildShareList(shares, total) + res.Data.(map[string]interface{})["user"] = struct { + ID string `json:"id"` + Nick string `json:"nick"` + Group string `json:"group"` + Date string `json:"date"` + }{ + hashid.HashID(user.ID, hashid.UserID), + user.Nick, + user.Group.Name, + user.CreatedAt.Format("2006-01-02 15:04:05"), + } + + return res +} + // Search 搜索公共分享 func (service *ShareListService) Search(c *gin.Context) serializer.Response { // 列出分享