add session check

This commit is contained in:
landaiqing
2024-10-17 16:21:46 +08:00
parent 5bfca64315
commit b5d88a7ccd
5 changed files with 70 additions and 8 deletions

View File

@@ -4,8 +4,28 @@ import (
"encoding/json"
"github.com/gin-gonic/gin"
"schisandra-cloud-album/global"
"time"
)
// ResponseData 返回数据
type ResponseData struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresAt int64 `json:"expires_at"`
UID *string `json:"uid"`
UserInfo UserInfo `json:"user_info"`
}
type UserInfo struct {
Username string `json:"username,omitempty"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
Gender string `json:"gender"`
Status int64 `json:"status"`
CreateAt time.Time `json:"create_at"`
}
// SetSession sets session data with key and data
func SetSession(c *gin.Context, key string, data interface{}) error {
session, err := global.Session.Get(c.Request, key)
@@ -28,7 +48,7 @@ func SetSession(c *gin.Context, key string, data interface{}) error {
}
// GetSession gets session data with key
func GetSession(c *gin.Context, key string) interface{} {
func GetSession(c *gin.Context, key string) *ResponseData {
session, err := global.Session.Get(c.Request, key)
if err != nil {
global.LOG.Error("GetSession failed: ", err)
@@ -39,13 +59,13 @@ func GetSession(c *gin.Context, key string) interface{} {
global.LOG.Error("GetSession failed: ", "key not found")
return nil
}
var data interface{}
data := ResponseData{}
err = json.Unmarshal(jsonData.([]byte), &data)
if err != nil {
global.LOG.Error("GetSession failed: ", err)
return nil
}
return data
return &data
}
// DelSession deletes session data with key