-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.go
More file actions
215 lines (184 loc) · 4.31 KB
/
base.go
File metadata and controls
215 lines (184 loc) · 4.31 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package base
import (
// "crypto/md5"
"database/sql"
"encoding/json"
"flag"
"fmt"
"github.com/KerryJava/goserver/other"
_ "github.com/go-sql-driver/mysql"
"github.com/golang/glog"
"github.com/jinzhu/gorm"
"net/http"
"time"
// "strconv"
)
//import "github.com/KerryJava/goserver/main/"
var (
PrepareStmt *sql.Stmt
db *gorm.DB = OrmDB
)
type Base struct {
}
type LoginParam struct {
Phone int64 `json:"phone"`
Passwd string `json:"passwd"`
Log LoginLog `json:log`
}
type CheckTokenReply struct {
Status int `json:"status"`
StatusMsg string `json:"statusmsg"`
UserData User `json:"userdata"`
}
type User struct {
ID int64 `json:"userid"`
Phone int64 `json:"phone"`
Name string `json:"name"`
Passwd string `json:"-"`
Token string `json:"token"`
}
type LoginLog struct {
Token string `json:"-"`
Userid int64 `json:"-"`
Channel int32 `json:channel`
Device string `json:device`
Screen string `json:screen`
Create_Time time.Time `json:"-"`
}
func (LoginLog) TableName() string {
return "login_log"
}
func (User) TableName() string {
return "user"
}
func (user User) Print() {
glog.V(8).Infof("%v\n", user)
}
func init() {
fmt.Println("base init")
flag.Parse()
glog.Info("init prepare statement")
PrepareStmt, _ = DB.Prepare("SELECT id, Name, phone FROM user WHERE Name=?")
RawQuery(true)
}
func RawQuery(isPrepare bool) {
// Prepare statement for select data
var stmtOut *sql.Stmt
if isPrepare {
stmtOut = PrepareStmt
} else {
stmtOut, _ = DB.Prepare("SELECT id, Name, phone FROM user WHERE Name=?")
defer stmtOut.Close()
}
//defer stmtOut.Close()
var chkErr, err error
/*
chkErr := checkErr(err)
if chkErr != nil {
glog.Info(chkErr)
}
*/
// Execute the query
rows, err := stmtOut.Query("testuser")
var id, Name, phone []byte
for rows.Next() {
// Scan the value to []byte
err = rows.Scan(&id, &Name, &phone)
chkErr = checkErr(err)
if chkErr != nil {
fmt.Println(chkErr)
}
// Use the string value
//fmt.Println(string(id), string(Name), string(phone))
}
}
func (h *Base) Login(r *http.Request, param *LoginParam, reply *CheckTokenReply) error {
other.Desc()
var user = User{}
// phone, err := strconv.ParseInt(param.Phone, 10, 64)
phone := param.Phone
passwd := param.Passwd
if passwd == "" {
return ErrParams
}
// if err != nil {
// fmt.Println(err)
// }
OrmDB.Where(&User{Phone: phone, Passwd: param.Passwd}).First(&user)
//isSucc := false
msg := "fail"
if user.Phone == param.Phone {
// isSucc = true
msg = "success"
} else {
return ErrLogin
}
token, _ := LoginHandler(&user)
user.Token = token
glog.V(8).Info(user.Phone)
user.Print()
logJson, _ := json.Marshal(param.Log)
glog.V(10).Infof("%#v", logJson)
loginlog := new(LoginLog)
db.FirstOrCreate(&loginlog, LoginLog{Userid: user.ID})
_ = json.Unmarshal(logJson, loginlog)
// loginlog.Userid = user.ID
loginlog.Token = user.Token
loginlog.Create_Time = time.Now()
//db.Updates("screen")
db.Where(&LoginLog{Userid: user.ID}).Delete(&LoginLog{})
db.Save(&loginlog)
reply.Status = 1
reply.UserData = user
reply.StatusMsg = msg
return nil
}
func Gentoken() string {
/*
crutime := time.Now().Unix()
fmt.Println("crutime-->", crutime)
h := md5.New()
fmt.Println("h-->", h)
fmt.Println("strconv.FormatInt(crutime, 10)-->", strconv.FormatInt(crutime, 10))
io.WriteString(h, strconv.FormatInt(crutime, 10))
fmt.Println("h-->", h)
token := fmt.Sprintf("%x", h.Sum(nil))
fmt.Println("token--->", token)
fmt.Println(len("8e1a188743c6077110da3c9778183031"))
*/
return ""
}
func (h *Base) Contact(r *http.Request, param *LoginParam, reply *CheckTokenReply) error {
other.Desc()
var user = User{}
// phone, err := strconv.ParseInt(param.Phone, 10, 64)
phone := param.Phone
passwd := param.Passwd
if passwd == "" {
return ErrParams
}
// if err != nil {
// fmt.Println(err)
// }
OrmDB.Where(&User{Phone: phone, Passwd: param.Passwd}).First(&user)
//isSucc := false
msg := "fail"
if user.Phone == param.Phone {
// isSucc = true
msg = "success"
} else {
return ErrLogin
}
fmt.Println(user.Phone)
user.Print()
reply.Status = 1
reply.UserData = user
reply.StatusMsg = msg
return nil
}
func checkErr(err error) error {
if err == nil || err == sql.ErrNoRows {
return nil
}
return err
}