Feat: user profile
This commit is contained in:
parent
f91792bc64
commit
33a917cc75
6 changed files with 67 additions and 7 deletions
|
@ -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 = ?", "")
|
||||
}
|
||||
|
||||
// 计算总数用于分页
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
||||
// 需要携带签名验证的
|
||||
|
|
|
@ -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 {
|
||||
// 列出分享
|
||||
|
|
Loading…
Add table
Reference in a new issue