login_access.go
4.4 KB
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
package domain
import (
"github.com/linmadan/egglib-go/core/application"
"time"
)
//登录的平台
const (
LoginPlatformApp string = "app"
LoginPlatformWeb string = "web"
)
const (
LoginPwd = "signInPassword"
LoginSmsCode = "signInCaptcha"
)
const (
DeviceTypeIos = "1"
DeviceTypeAndroid = "2"
DeviceTypeWeb = "4"
)
const (
InvalidAccessToken = 901
InvalidRefreshToken = 902
InvalidSign = 903
InvalidClientId = 904
InvalidUUid = 905
)
var codeMsg = map[int]string{
InvalidAccessToken: "access token 过期或无效,需刷新令牌",
InvalidRefreshToken: "refresh token 过期或失效,需重新进行登录认证操作",
InvalidSign: "sign 签名无效,需重新登录手机 APP",
InvalidClientId: "client id 或 client secret 无效,需强制更新手机 APP",
InvalidUUid: "uuid 无效",
}
// 登录凭证存储
type LoginAccess struct {
LoginAccessId int64 `json:"loginAccessId"`
UserBaseId int64 `json:"userBaseId"`
UserId int64 `json:"userId"`
OrgIds []int64 `json:"orgIds"`
// 账号
Account string `json:"account"`
// 对应平台
Platform string `json:"platform"`
// 公司id
CompanyId int64 `json:"companyId"`
// 组织id
OrganizationId int64 `json:"organizationId"`
// 登录凭证存储
AccessToken string `json:"accessToken"`
// 刷新登录凭证
RefreshToken string `json:"refreshToken"`
// 登录凭证到期时间,时间戳精度秒
AccessExpired int64 `json:"accessExpired"`
// 刷新登录凭证到期时间,时间戳精度秒
RefreshExpired int64 `json:"refreshExpired"`
// 创建时间
CreatedTime time.Time `json:"createdTime"`
// 更新时间
UpdatedTime time.Time `json:"updatedTime"`
}
type LoginAccessRepository interface {
Save(loginAccess *LoginAccess) (*LoginAccess, error)
Remove(loginAccess *LoginAccess) (*LoginAccess, error)
FindOne(queryOptions map[string]interface{}) (*LoginAccess, error)
Find(queryOptions map[string]interface{}) (int64, []*LoginAccess, error)
}
func (loginAccess *LoginAccess) Identify() interface{} {
if loginAccess.LoginAccessId == 0 {
return nil
}
return loginAccess.LoginAccessId
}
func (loginAccess *LoginAccess) Update(data map[string]interface{}) error {
if account, ok := data["account"]; ok {
loginAccess.Account = account.(string)
}
if platform, ok := data["platform"]; ok {
loginAccess.Platform = platform.(string)
}
if companyId, ok := data["companyId"]; ok {
loginAccess.CompanyId = companyId.(int64)
}
if organizationId, ok := data["organizationId"]; ok {
loginAccess.OrganizationId = organizationId.(int64)
}
if accessToken, ok := data["accessToken"]; ok {
loginAccess.AccessToken = accessToken.(string)
}
if refreshToken, ok := data["refreshToken"]; ok {
loginAccess.RefreshToken = refreshToken.(string)
}
if accessExpired, ok := data["accessExpired"]; ok {
loginAccess.AccessExpired = accessExpired.(int64)
}
if refreshExpired, ok := data["refreshExpired"]; ok {
loginAccess.RefreshExpired = refreshExpired.(int64)
}
if createdTime, ok := data["createdTime"]; ok {
loginAccess.CreatedTime = createdTime.(time.Time)
}
if updatedTime, ok := data["updatedTime"]; ok {
loginAccess.UpdatedTime = updatedTime.(time.Time)
}
return nil
}
func ParsePlatform(deviceType string) string {
if deviceType == DeviceTypeWeb {
return LoginPlatformWeb
}
return LoginPlatformApp
}
func ParseCodeMsg(code int) string {
if v, ok := codeMsg[code]; ok {
return v
}
return ""
}
func (loginAccess *LoginAccess) ResetLoginAccess(loginToken LoginToken) (interface{}, error) {
loginAccess.UserId = int64(loginToken.UserId)
loginAccess.UserBaseId = int64(loginToken.UserBaseId)
loginAccess.Account = loginToken.Account
loginAccess.Platform = loginToken.Platform
loginAccess.CompanyId = int64(loginToken.CompanyId)
loginAccess.OrganizationId = loginToken.OrgId
loginAccess.OrgIds = loginToken.OrgIds
loginAccess.UpdatedTime = time.Now()
accessTokenStr, err := loginToken.GenerateAccessToken()
if err != nil {
return nil, err
}
loginAccess.AccessToken = accessTokenStr
loginAccess.AccessExpired = loginToken.ExpiresAt
refreshTokenStr, err := loginToken.GenerateRefreshToken()
if err != nil {
return nil, err
}
loginAccess.RefreshToken = refreshTokenStr
loginAccess.RefreshExpired = loginToken.ExpiresAt
return nil, nil
}
func NewApplicationError(code int) error {
return &application.ServiceError{
Code: code,
Message: ParseCodeMsg(code),
}
}