Modify: return user information in siteConfig while logged in
This commit is contained in:
parent
5e1e76719f
commit
28df5ca833
5 changed files with 40 additions and 18 deletions
|
@ -52,6 +52,7 @@ type UserOption struct {
|
|||
ProfileOn int `json:"profile_on"`
|
||||
PreferredPolicy uint `json:"preferred_policy"`
|
||||
WebDAVKey string `json:"webdav_key"`
|
||||
PreferredTheme string `json:"preferred_theme"`
|
||||
}
|
||||
|
||||
// DeductionStorage 减少用户已用容量
|
||||
|
|
|
@ -12,6 +12,7 @@ type SiteConfig struct {
|
|||
QQLogin bool `json:"QQLogin"`
|
||||
Themes string `json:"themes"`
|
||||
DefaultTheme string `json:"defaultTheme"`
|
||||
User User `json:"user"`
|
||||
}
|
||||
|
||||
func checkSettingValue(setting map[string]string, key string) string {
|
||||
|
@ -22,7 +23,11 @@ func checkSettingValue(setting map[string]string, key string) string {
|
|||
}
|
||||
|
||||
// BuildSiteConfig 站点全局设置
|
||||
func BuildSiteConfig(settings map[string]string) Response {
|
||||
func BuildSiteConfig(settings map[string]string, user *model.User) Response {
|
||||
var userRes User
|
||||
if user != nil {
|
||||
userRes = BuildUser(*user)
|
||||
}
|
||||
return Response{
|
||||
Data: SiteConfig{
|
||||
SiteName: checkSettingValue(settings, "siteName"),
|
||||
|
@ -33,5 +38,6 @@ func BuildSiteConfig(settings map[string]string) Response {
|
|||
QQLogin: model.IsTrueVal(checkSettingValue(settings, "qq_login")),
|
||||
Themes: checkSettingValue(settings, "themes"),
|
||||
DefaultTheme: checkSettingValue(settings, "defaultTheme"),
|
||||
User: userRes,
|
||||
}}
|
||||
}
|
||||
|
|
|
@ -15,12 +15,12 @@ func TestCheckSettingValue(t *testing.T) {
|
|||
func TestBuildSiteConfig(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
res := BuildSiteConfig(map[string]string{"not exist": ""})
|
||||
res := BuildSiteConfig(map[string]string{"not exist": ""}, nil)
|
||||
asserts.Equal("", res.Data.(SiteConfig).SiteName)
|
||||
|
||||
res = BuildSiteConfig(map[string]string{"siteName": "123"})
|
||||
res = BuildSiteConfig(map[string]string{"siteName": "123"}, nil)
|
||||
asserts.Equal("123", res.Data.(SiteConfig).SiteName)
|
||||
|
||||
res = BuildSiteConfig(map[string]string{"qq_login": "1"})
|
||||
res = BuildSiteConfig(map[string]string{"qq_login": "1"}, nil)
|
||||
asserts.Equal(true, res.Data.(SiteConfig).QQLogin)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package serializer
|
||||
|
||||
import "github.com/HFO4/cloudreve/models"
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/HFO4/cloudreve/models"
|
||||
)
|
||||
|
||||
// CheckLogin 检查登录
|
||||
func CheckLogin() Response {
|
||||
|
@ -12,23 +15,28 @@ func CheckLogin() Response {
|
|||
|
||||
// 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"`
|
||||
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 struct {
|
||||
} `json:"policy"`
|
||||
}
|
||||
|
||||
// BuildUser 序列化用户
|
||||
func BuildUser(user model.User) User {
|
||||
fmt.Println(user)
|
||||
return User{
|
||||
ID: user.ID,
|
||||
Email: user.Email,
|
||||
Nickname: user.Nick,
|
||||
Status: user.Status,
|
||||
Avatar: user.Avatar,
|
||||
CreatedAt: user.CreatedAt.Unix(),
|
||||
ID: user.ID,
|
||||
Email: user.Email,
|
||||
Nickname: user.Nick,
|
||||
Status: user.Status,
|
||||
Avatar: user.Avatar,
|
||||
CreatedAt: user.CreatedAt.Unix(),
|
||||
PreferredTheme: user.OptionsSerialized.PreferredTheme,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,14 @@ func SiteConfig(c *gin.Context) {
|
|||
"defaultTheme",
|
||||
})
|
||||
|
||||
c.JSON(200, serializer.BuildSiteConfig(siteConfig))
|
||||
// 如果已登录,则同时返回用户信息
|
||||
user, _ := c.Get("user")
|
||||
if user, ok := user.(*model.User); ok {
|
||||
c.JSON(200, serializer.BuildSiteConfig(siteConfig, user))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, serializer.BuildSiteConfig(siteConfig, nil))
|
||||
}
|
||||
|
||||
// Ping 状态检查页面
|
||||
|
|
Loading…
Add table
Reference in a new issue