-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
164 lines (130 loc) · 2.84 KB
/
account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package account
import (
"errors"
"fmt"
"net/http"
"../../session"
"github.com/gin-gonic/gin"
)
func ProcessRequest(ctx *gin.Context) {
var userSession session.Session
defer func() {
if userSession == nil {
userSession, _ = session.CreateSession()
}
ctx.Set(MercurySessionName, userSession)
}()
//从cookie中获取session_id
cookie, err := ctx.Request.Cookie(CookieSessionId)
if err != nil {
//不存在的话,设置user_id=0, login_status为0。表示未登录状态
ctx.Set(MercuryUserId, int64(0))
ctx.Set(MercuryUserLoginStatus, int64(0))
return
}
sessionId := cookie.Value
if len(sessionId) == 0 {
ctx.Set(MercuryUserId, int64(0))
ctx.Set(MercuryUserLoginStatus, int64(0))
return
}
//根据sessionId,获取用户的session。
userSession, err = session.Get(sessionId)
if err != nil {
ctx.Set(MercuryUserId, int64(0))
ctx.Set(MercuryUserLoginStatus, int64(0))
return
}
tmpUserId, err := userSession.Get(MercuryUserId)
if err != nil {
ctx.Set(MercuryUserId, int64(0))
ctx.Set(MercuryUserLoginStatus, int64(0))
return
}
userId, ok := tmpUserId.(int64)
if !ok || userId == 0 {
ctx.Set(MercuryUserId, int64(0))
ctx.Set(MercuryUserLoginStatus, int64(0))
return
}
ctx.Set(MercuryUserId, int64(userId))
ctx.Set(MercuryUserLoginStatus, int64(1))
return
}
func GetUserId(ctx *gin.Context) (userId int64, err error) {
tempUserId, exists := ctx.Get(MercuryUserId)
if !exists {
err = errors.New("user id not exists")
return
}
userId, ok := tempUserId.(int64)
if !ok {
err = errors.New("user id convert to int64 failed")
return
}
return
}
func IsLogin(ctx *gin.Context) (login bool) {
tempLoginStatus, exists := ctx.Get(MercuryUserLoginStatus)
if !exists {
return
}
loginStatus, ok := tempLoginStatus.(int64)
if !ok {
return
}
if loginStatus == 0 {
return
}
login = true
return
}
func SetUserId(userId int64, ctx *gin.Context) {
var userSession session.Session
tempSession, exists := ctx.Get(MercurySessionName)
if !exists {
return
}
userSession, ok := tempSession.(session.Session)
if !ok {
return
}
if userSession == nil {
return
}
userSession.Set(MercuryUserId, userId)
}
func ProcessResponse(ctx *gin.Context) {
var userSession session.Session
tempSession, exists := ctx.Get(MercurySessionName)
if !exists {
return
}
userSession, ok := tempSession.(session.Session)
if !ok {
return
}
if userSession == nil {
return
}
fmt.Printf("get session succ\n")
if userSession.IsModify() == false {
return
}
err := userSession.Save()
if err != nil {
return
}
fmt.Printf("save session succ\n")
sessionId := userSession.Id()
cookie := &http.Cookie{
Name: CookieSessionId,
Value: sessionId,
MaxAge: CookieMaxAge,
HttpOnly: true,
Path: "/",
}
http.SetCookie(ctx.Writer, cookie)
fmt.Printf("set cookie succ\n")
return
}