You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/pkg/serializer/user.go

75 lines
2.0 KiB

package serializer
import (
"fmt"
"github.com/HFO4/cloudreve/models"
)
5 years ago
// CheckLogin 检查登录
func CheckLogin() Response {
return Response{
Code: CodeCheckLogin,
Msg: "未登录",
}
}
// User 用户序列化器
type User struct {
ID uint `json:"id"`
Email string `json:"user_name"`
Nickname string `json:"nickname"`
Status int `json:"status"`
Avatar string `json:"avatar"`
CreatedAt int64 `json:"created_at"`
PreferredTheme string `json:"preferred_theme"`
Policy Policy `json:"policy"`
Group Group `json:"group"`
}
type Policy struct {
SaveType string `json:"saveType"`
MaxSize string `json:"maxSize"`
AllowedType []string `json:"allowedType"`
UploadURL string `json:"upUrl"`
AllowGetSource bool `json:"allowSource"`
}
type Group struct {
AllowShare bool `json:"allowShare"`
AllowRemoteDownload bool `json:"allowRemoteDownload"`
AllowTorrentDownload bool `json:"allowTorrentDownload"`
5 years ago
}
// BuildUser 序列化用户
func BuildUser(user model.User) User {
aria2Option := user.Group.GetAria2Option()
5 years ago
return User{
ID: user.ID,
Email: user.Email,
Nickname: user.Nick,
Status: user.Status,
Avatar: user.Avatar,
CreatedAt: user.CreatedAt.Unix(),
PreferredTheme: user.OptionsSerialized.PreferredTheme,
Policy: Policy{
SaveType: user.Policy.Type,
MaxSize: fmt.Sprintf("%.2fmb", float64(user.Policy.MaxSize)/1024*1024),
AllowedType: user.Policy.OptionsSerialized.FileType,
UploadURL: user.Policy.Server,
AllowGetSource: user.Policy.IsOriginLinkEnable,
},
Group: Group{
AllowShare: user.Group.ShareEnabled,
AllowRemoteDownload: aria2Option[0],
AllowTorrentDownload: aria2Option[2],
},
5 years ago
}
}
// BuildUserResponse 序列化用户响应
func BuildUserResponse(user model.User) Response {
return Response{
Data: BuildUser(user),
}
}