✨ add session check
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user