-
Notifications
You must be signed in to change notification settings - Fork 2
/
login.go
72 lines (67 loc) · 1.84 KB
/
login.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
package hbookermodel
import (
"encoding/json"
"fmt"
"log"
"regexp"
"strconv"
"strings"
)
type Login struct {
Tip
Data LoginData `json:"data"`
}
type LoginData struct {
LoginToken string `json:"login_token"`
UserCode string `json:"user_code"`
ReaderInfo ReaderInfo `json:"reader_info"`
PropInfo PropInfo `json:"prop_info"`
IsSetYoung string `json:"is_set_young"`
}
type Register struct {
Tip
Data LoginData `json:"data"`
}
type Authenticate struct {
AppVersion string `json:"app_version"`
DeviceToken string `json:"device_token"`
LoginToken string `json:"login_token"`
Account string `json:"account"`
}
func (authenticate *Authenticate) SetAppVersion(appVersion string) *Authenticate {
// Regular expression to match semantic versioning (e.g., 1.0.0, 2.9.290)
if regexp.MustCompile(`^\d+\.\d+\.\d+$`).MatchString(appVersion) {
authenticate.AppVersion = appVersion
}
return authenticate
}
func (authenticate *Authenticate) SetDeviceToken(deviceToken string) *Authenticate {
authenticate.DeviceToken = deviceToken
return authenticate
}
func (authenticate *Authenticate) SetLoginToken(loginToken string) *Authenticate {
if len(loginToken) == 32 {
authenticate.LoginToken = loginToken
}
return authenticate
}
func (authenticate *Authenticate) SetAccount(account string) *Authenticate {
if unquoted, err := strconv.Unquote(fmt.Sprintf(`"%s"`, account)); err == nil {
account = unquoted
}
// Check if the (possibly decoded) string contains "书客".
if strings.Contains(account, "书客") {
authenticate.Account = account
}
return authenticate
}
func (authenticate *Authenticate) GetAuthenticate() map[string]string {
var result map[string]string
m, _ := json.Marshal(authenticate)
err := json.Unmarshal(m, &result)
if err != nil {
log.Panicln("Error unmarshalling Authenticate:", err)
return nil
}
return result
}