user.go
5.94 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package models
import (
"crypto/md5"
"encoding/hex"
"errors"
"github.com/satori/go.uuid"
"log"
db "loginserver/database"
"strconv"
"strings"
)
type UserLoginInfo struct {
Type string `json:"login_type"`
Name string `json:"login_name"`
Password string `json:"login_password"`
}
type UserRegisterInfo struct {
Type string `json:"register_type"`
Name string `json:"register_name"`
Password string `json:"register_password"`
}
type UserInfo struct {
Id int
UserPassword string
UserName string
}
func (u *UserLoginInfo) UserLogin() (result interface{}, err error) {
var userInfo UserInfo
switch u.Type {
case "normal":
r, err := GetUserInfoByUserName(u.Name)
if err != nil {
return r, err
}
userInfo = r.(UserInfo)
if userInfo.UserPassword != MD5(u.Password) {
err = errors.New("密码错误")
return "105004", err
}
break
default:
err = errors.New("不存在的登陆类型")
return "105000", err
}
res := make(map[string]interface{}, 0)
res["user_id"] = userInfo.Id
token, err := GetUserAccessToken(userInfo.UserName)
if err != nil {
return token, err
}
res["token"] = token
log.Println(u.Name, " login success")
return res, nil
}
func (u *UserRegisterInfo) UserRegister() (result interface{}, err error) {
switch u.Type {
case "account": //账号密码注册
res, err := CheckUserNameRepeat(u.Name)
if err != nil {
return res, err
}
errno, err := InsertUser(u.Name, u.Password)
if err != nil {
return errno, err
}
break
default:
err = errors.New("不存在的注册类型")
return "105001", err
break
}
log.Println(u.Name, " register success")
return nil, nil
}
//检查用户名是否重复
func CheckUserNameRepeat(userName string) (result interface{}, err error) {
sql := ` select user_password from icesimba_polysdk_user where user_name = ?`
r, err := db.Engine.Query(sql, userName)
if err != nil {
return "104001", err
}
if len(r) == 0 {
return nil, nil
}
log.Println(userName)
err = errors.New("该名称已被使用")
return "105001", err
}
//获取用户信息
func GetUserInfoByUserName(userName string) (result interface{}, err error) {
u := make([]UserInfo, 0)
sql := ` select id,user_name,user_password from icesimba_polysdk_user where user_name = ? limit 1`
err = db.Engine.Sql(sql, userName).Find(&u)
if err != nil {
return "104001", err
}
if len(u) == 0 {
err = errors.New("不存在的用户名")
return "105003", err
}
return u[0], nil
}
//获取用户信息
func GetUserInfoByUserId(userId int) (result interface{}, err error) {
u := make([]UserInfo, 0)
sql := ` select id,user_name,user_password from icesimba_polysdk_user where id = ? limit 1`
err = db.Engine.Sql(sql, userId).Find(&u)
if err != nil {
return "104001", err
}
if len(u) == 0 {
err = errors.New("不存在的用户id")
return "105003", err
}
return u[0], nil
}
//修改用户密码
func ChangeUserPassword(userId int, originPassword string, newPassword string) (result interface{}, err error) {
r, err := GetUserInfoByUserId(userId)
if err != nil {
return r, err
}
password := r.(UserInfo).UserPassword
if MD5(originPassword) != password {
err = errors.New("密码错误,无法修改!")
return "105004", err
}
if newPassword == originPassword {
err = errors.New("新旧密码一致!无法修改")
return "105002", err
}
password = MD5(newPassword)
sql := ` update icesimba_polysdk_user set user_password = ? where id = ?`
_, err = db.Engine.Exec(sql, password, userId)
if err != nil {
return "104001", err
}
return
}
//添加用户
func InsertUser(userName string, userPassword string) (result interface{}, err error) {
password := MD5(userPassword)
sql := ` insert into icesimba_polysdk_user(user_name,user_password) values(?,?) `
_, err = db.Engine.Exec(sql, userName, password)
if err != nil {
return "104001", err
}
return nil, nil
}
//授权
func GetUserAccessToken(userName string) (result string, err error) {
userInfo, err := GetUserInfoByUserName(userName)
if err != nil {
return userInfo.(string), err
}
userId := userInfo.(UserInfo).Id
uuid := uuid.NewV4().String()
uuid = strings.Replace(uuid, "-", "", -1)
sql := ` replace into icesimba_user_token(user_id,user_token) values(?,?)`
_, err = db.Engine.Exec(sql, userId, uuid)
if err != nil {
return "104002", err
}
return uuid, nil
}
//验证授权
func CheckUserAccessToken(token string) (result string, err error) {
sql := ` select user_id from icesimba_user_token where user_token = ? limit 1`
r, err := db.Engine.Query(sql, token)
if err != nil {
return "104002", err
}
if len(r) == 0 {
err = errors.New("token已失效")
return "", nil
}
return string(r[0]["user_id"]), nil
}
//生成外部应用授权
func GetThirdpartAccessToken(userId int) (result string, err error) {
uuid := uuid.NewV4().String()
sql := ` replace into icesimba_thirdpart_token(user_id,user_thirdpart_token) values(?,?)`
_, err = db.Engine.Exec(sql, userId, uuid)
if err != nil {
return "104002", err
}
return "", nil
}
//验证外部应用授权
func CheckThirdpartAccessToken(token string) (result string, err error) {
sql := ` select user_id from icesimba_user_thirdpart_token where user_thirdpart_token = ? limit 1`
r, err := db.Engine.Query(sql, token)
if err != nil {
return "104002", err
}
if len(r) == 0 {
err = errors.New("token已失效")
return "false", nil
}
return string(r[0]["user_id"]), nil
}
func MD5(str string) string {
w := md5.New()
w.Write([]byte(str))
has := w.Sum(nil)
str = hex.EncodeToString(has)
return str
}
func CheckRequestToken(token string) (result interface{}, err error) {
sql := ` select user_id from icesimba_user_token where user_token = ? limit 1`
r, err := db.Engine.Query(sql, token)
if err != nil {
return "104002", err
}
if len(r) == 0 {
err = errors.New("token已失效")
return "105005", err
}
id := string(r[0]["user_id"])
idInt, err := strconv.Atoi(id)
if err != nil {
return "104000", err
}
return idInt, nil
}