account 歸併
This commit is contained in:
@@ -16,27 +16,30 @@ type Account struct {
|
||||
}
|
||||
|
||||
func AccountRead(w http.ResponseWriter, r *http.Request, cb func(account *Account)) {
|
||||
// 從Cookie中獲取session_id
|
||||
|
||||
// 獲取Cookie
|
||||
cookie, err := r.Cookie("session_id")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("401 - 未登錄"))
|
||||
w.Write([]byte("401 - 未登錄, 請登錄後再進行操作"))
|
||||
return
|
||||
}
|
||||
|
||||
// 獲取當前session
|
||||
// 獲取會話
|
||||
session := Session{ID: cookie.Value}
|
||||
if err := configs.ORMDB().Take(&session).Error; err != nil {
|
||||
http.SetCookie(w, &http.Cookie{Name: "session_id", Value: "", Path: "/", MaxAge: -1})
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("401 - 會話已過期"))
|
||||
w.Write([]byte("401 - 會話已過期, 請重新登錄"))
|
||||
return
|
||||
}
|
||||
|
||||
// 獲取當前用戶
|
||||
user := User{ID: session.UserID}
|
||||
if err := configs.ORMDB().Model(&user).Select("id, name, email, created_at, updated_at").Find(&user).Error; err != nil {
|
||||
http.SetCookie(w, &http.Cookie{Name: "session_id", Value: "", Path: "/", MaxAge: -1})
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("401 - 用戶不存在"))
|
||||
w.Write([]byte("401 - 用戶不存在, 請重新登錄"))
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -1,54 +1,15 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"main/configs"
|
||||
"main/models"
|
||||
"main/utils"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 獲取當前賬戶信息(重寫, 爲輸出增加sid字段)
|
||||
func AccountGet(w http.ResponseWriter, r *http.Request) {
|
||||
var account struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
SessionID string `json:"session_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// 獲取Cookie
|
||||
cookie, err := r.Cookie("session_id")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("401 - 未登錄"))
|
||||
return
|
||||
}
|
||||
|
||||
// 獲取會話
|
||||
session := models.Session{ID: cookie.Value}
|
||||
if err := configs.ORMDB().Take(&session).Error; err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("401 - 會話已過期"))
|
||||
return
|
||||
}
|
||||
|
||||
// 獲取用戶
|
||||
user := models.User{ID: session.UserID}
|
||||
configs.ORMDB().Model(&user).Select("id, name, email, created_at, updated_at").Find(&user)
|
||||
|
||||
account.ID = user.ID
|
||||
account.Name = user.Name
|
||||
account.Email = user.Email
|
||||
account.SessionID = session.ID
|
||||
account.CreatedAt = user.CreatedAt
|
||||
account.UpdatedAt = user.UpdatedAt
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Write(utils.ToJSON(account))
|
||||
models.AccountRead(w, r, func(account *models.Account) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Write(utils.ToJSON(account))
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user