Modify: return user information in siteConfig while logged in

pull/247/head
HFO4 5 years ago
parent 5e1e76719f
commit 28df5ca833

@ -52,6 +52,7 @@ type UserOption struct {
ProfileOn int `json:"profile_on"` ProfileOn int `json:"profile_on"`
PreferredPolicy uint `json:"preferred_policy"` PreferredPolicy uint `json:"preferred_policy"`
WebDAVKey string `json:"webdav_key"` WebDAVKey string `json:"webdav_key"`
PreferredTheme string `json:"preferred_theme"`
} }
// DeductionStorage 减少用户已用容量 // DeductionStorage 减少用户已用容量

@ -12,6 +12,7 @@ type SiteConfig struct {
QQLogin bool `json:"QQLogin"` QQLogin bool `json:"QQLogin"`
Themes string `json:"themes"` Themes string `json:"themes"`
DefaultTheme string `json:"defaultTheme"` DefaultTheme string `json:"defaultTheme"`
User User `json:"user"`
} }
func checkSettingValue(setting map[string]string, key string) string { func checkSettingValue(setting map[string]string, key string) string {
@ -22,7 +23,11 @@ func checkSettingValue(setting map[string]string, key string) string {
} }
// BuildSiteConfig 站点全局设置 // 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{ return Response{
Data: SiteConfig{ Data: SiteConfig{
SiteName: checkSettingValue(settings, "siteName"), SiteName: checkSettingValue(settings, "siteName"),
@ -33,5 +38,6 @@ func BuildSiteConfig(settings map[string]string) Response {
QQLogin: model.IsTrueVal(checkSettingValue(settings, "qq_login")), QQLogin: model.IsTrueVal(checkSettingValue(settings, "qq_login")),
Themes: checkSettingValue(settings, "themes"), Themes: checkSettingValue(settings, "themes"),
DefaultTheme: checkSettingValue(settings, "defaultTheme"), DefaultTheme: checkSettingValue(settings, "defaultTheme"),
User: userRes,
}} }}
} }

@ -15,12 +15,12 @@ func TestCheckSettingValue(t *testing.T) {
func TestBuildSiteConfig(t *testing.T) { func TestBuildSiteConfig(t *testing.T) {
asserts := assert.New(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) 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) 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) asserts.Equal(true, res.Data.(SiteConfig).QQLogin)
} }

@ -1,6 +1,9 @@
package serializer package serializer
import "github.com/HFO4/cloudreve/models" import (
"fmt"
"github.com/HFO4/cloudreve/models"
)
// CheckLogin 检查登录 // CheckLogin 检查登录
func CheckLogin() Response { func CheckLogin() Response {
@ -18,10 +21,14 @@ type User struct {
Status int `json:"status"` Status int `json:"status"`
Avatar string `json:"avatar"` Avatar string `json:"avatar"`
CreatedAt int64 `json:"created_at"` CreatedAt int64 `json:"created_at"`
PreferredTheme string `json:"preferred_theme"`
Policy struct {
} `json:"policy"`
} }
// BuildUser 序列化用户 // BuildUser 序列化用户
func BuildUser(user model.User) User { func BuildUser(user model.User) User {
fmt.Println(user)
return User{ return User{
ID: user.ID, ID: user.ID,
Email: user.Email, Email: user.Email,
@ -29,6 +36,7 @@ func BuildUser(user model.User) User {
Status: user.Status, Status: user.Status,
Avatar: user.Avatar, Avatar: user.Avatar,
CreatedAt: user.CreatedAt.Unix(), CreatedAt: user.CreatedAt.Unix(),
PreferredTheme: user.OptionsSerialized.PreferredTheme,
} }
} }

@ -23,7 +23,14 @@ func SiteConfig(c *gin.Context) {
"defaultTheme", "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 状态检查页面 // Ping 状态检查页面

Loading…
Cancel
Save