User.go
9.82 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package Api
import (
"crypto/sha256"
"encoding/hex"
"github.com/aarongao/tools"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2/bson"
"letu/DB"
"letu/Lib/Token"
"regexp"
"strconv"
"time"
)
var Regular = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$"
// @Title 登录
// @Description 用户管理 - 用户登录&注册
// @Accept json
// @Produce json
// @Param Mobile aaron string true "手机号"
// @Param Code 1 string true "验证码(使用验证码的新手机号自动注册)"
// @Param DeviceId abc123 string false "手机唯一识别码,不重复(存放于http.header中)"
// @Param Mac abc123 string false "网卡Mac地址(存放于http.header中)"
// @Param SystemType ios string false "ios,android(存放于http.header中)"
// @Param SystemVersion 13.01 string false "手机版本(存放于http.header中)"
// @Param SystemModel iphone8 string false "手机型号(存放于http.header中)"
// @Param AppVersion 1.0 string false "app版本号(存放于http.header中)"
// @Param DeviceToken abc string false "推送token(存放于http.header中)"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Token":"640bf934e425aba5d3c90998b2641f2f0ca07261d334d9615d1cd4790b5f34e7"}} 调用其它需要登陆的接口时携带token,有过期时间"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /LoginUser? [post]
func LoginUser(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
reg := regexp.MustCompile(Regular)
if !reg.MatchString(c.PostForm("Mobile")) {
c.JSON(200, tools.ResponseError{
1,
"手机号格式不正确",
})
return
}
if c.PostForm("Mobile") == "" || c.PostForm("Code") == "" {
c.JSON(200, tools.ResponseError{
1,
"手机号和验证码不能空",
})
return
}
// 生成token
tokenunit8 := sha256.Sum256([]byte(c.PostForm("Mobile") + c.PostForm("Code") + strconv.FormatInt(time.Now().UnixNano(), 10)))
token := hex.EncodeToString(tokenunit8[:32])
// 检查验证码
cacheCode := DB.Redis.Get("code_" + c.PostForm("Mobile"))
selected := bson.M{}
var User *DB.SMember
if cacheCode == c.PostForm("Code") {
selected["Mobile"] = c.PostForm("Mobile")
DB.CMember.Find(selected).One(&User)
// 验证码匹配,但手机号不存在
if User == nil {
objectID := bson.NewObjectId()
User := DB.SMember{
&objectID,
"",
"",
"",
c.PostForm("Mobile"),
"",
"",
"",
DB.SDevice{
c.Request.Header.Get("DeviceId"),
c.Request.Header.Get("Mac"),
c.Request.Header.Get("UDID"),
c.Request.Header.Get("SystemVersion"),
c.Request.Header.Get("SystemModel"),
c.Request.Header.Get("AppVersion"),
c.Request.Header.Get("AppVersion"),
c.Request.Header.Get("DeviceToken"),
},
}
DB.CMember.Insert(User)
}
} else {
c.JSON(200, tools.ResponseError{
1,
"验证码不正确",
})
return
}
// 更新用户信息
//DB.CMember.Update(
// bson.M{"_id": User.Id},
// bson.M{"$set": bson.M{"Token": token}},
//)
// 更新token
Token.SaveToken(User.Id.Hex(), token)
User.Token = token
c.JSON(200, tools.ResponseSeccess{
0,
User,
})
}
// @Title 注册客户端
// @Description 用户管理 - 注册客户端
// @Accept json
// @Produce json
// @Param DeviceId abc123 string false "手机唯一识别码,不重复(存放于http.header中)"
// @Param Mac abc123 string false "网卡Mac地址(存放于http.header中)"
// @Param SystemType ios string false "ios,android(存放于http.header中)"
// @Param SystemVersion 13.01 string false "手机版本(存放于http.header中)"
// @Param SystemModel iphone8 string false "手机型号(存放于http.header中)"
// @Param AppVersion 1.0 string false "app版本号(存放于http.header中)"
// @Param DeviceToken abc string false "推送token(存放于http.header中)"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /RegisterDevice? [post]
func RegisterDevice(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
if c.Request.Header.Get("DeviceId") == "" {
c.JSON(200, tools.ResponseError{
1,
"DeviceId不正确",
})
return
}
DB.CDevice.Upsert(
bson.M{"DeviceId":c.Request.Header.Get("DeviceId")},
bson.M{"$set": bson.M{
"Mac":c.Request.Header.Get("Mac"),
"UDID":c.Request.Header.Get("UDID"),
"SystemType":c.Request.Header.Get("SystemType"),
"SystemVersion":c.Request.Header.Get("SystemVersion"),
"SystemModel":c.Request.Header.Get("SystemModel"),
"AppVersion":c.Request.Header.Get("AppVersion"),
"DeviceToken":c.Request.Header.Get("DeviceToken"),
}},
)
c.JSON(200, tools.ResponseSeccess{
0,
"ok",
})
}
// @Title 用户信息
// @Description 用户管理 - 获取用户信息
// @Accept json
// @Produce json
// @Param id aaron string true "用户id"
// @Param Token wgergejfwe string true "用户token"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Token":"640bf934e425aba5d3c90998b2641f2f0ca07261d334d9615d1cd4790b5f34e7"}}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UserInfo? [get]
func UserInfo(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
if c.Query("Token") == "" || bson.IsObjectIdHex(c.Query("id")) == false {
c.JSON(200, tools.ResponseError{
1,
"Token或者用户id不正确",
})
return
}
if Token.GetToken(c.Query("id")) != c.Query("Token") {
c.JSON(200, tools.ResponseError{
401,
"token过期",
})
return
}
var User DB.SMember
DB.CMember.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&User)
User.Device = DB.SDevice{}
c.JSON(200, tools.ResponseSeccess{
0,
User,
})
}
// @Title 用户信息
// @Description 用户管理 - 检查Token是否过期
// @Accept json
// @Produce json
// @Param id aaron string true "用户id"
// @Param Token wgergejfwe string true "用户token"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":401,"errmsg":"token过期"}"
// @Router /CheckToken? [post]
func CheckToken(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
if c.PostForm("Token") == "" || bson.IsObjectIdHex(c.PostForm("id")) == false {
c.JSON(200, tools.ResponseError{
1,
"Token或者用户id不正确",
})
return
}
if Token.GetToken(c.PostForm("id")) != c.PostForm("Token") {
c.JSON(200, tools.ResponseError{
401,
"token过期",
})
return
}
c.JSON(200, tools.ResponseSeccess{
0,
"ok",
})
}
// @Title 修改用户信息
// @Description 用户管理 - 修改用户信息
// @Accept json
// @Produce json
// @Param id aaron string true "用户id""
// @Param Token wgergejfwe string true "用户token"
// @Param Birthday 2010.10.10 string true "生日"
// @Param FullName aarongao string true "全名"
// @Param Code 12345678 string true "6位验证码"
// @Param Mobile 18616619599 string true "手机,同用户名"
// @Param Sex 男 string true "性别"
// @Param Openid 12345 string true "微信id"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UpdateUser? [post]
func UpdateUser(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
if c.PostForm("Token") == "" || bson.IsObjectIdHex(c.PostForm("id")) == false {
c.JSON(200, tools.ResponseError{
1,
"Token或者用户id不正确",
})
return
}
if Token.GetToken(c.PostForm("id")) != c.PostForm("Token") {
c.JSON(200, tools.ResponseError{
401,
"token过期",
})
return
}
reg := regexp.MustCompile(Regular)
if !reg.MatchString(c.PostForm("Mobile")) {
c.JSON(200, tools.ResponseError{
1,
"手机号格式不正确",
})
return
}
if c.PostForm("Mobile") == "" || c.PostForm("Code") == "" {
c.JSON(200, tools.ResponseError{
1,
"手机号或验证码不能为空",
})
return
}
//if c.PostForm("Password") != c.PostForm("ConfirmPassword") {
// c.JSON(200, tools.ResponseError{
// 1,
// "2次密码不一致",
// })
// return
//}
// 检查验证码
code := DB.Redis.Get("code_" + c.PostForm("Mobile"))
if code == "" || code != c.PostForm("Code") {
c.JSON(200, tools.ResponseError{
1,
"验证码错误",
})
return
}
err := DB.CMember.Update(
bson.M{"_id": bson.ObjectIdHex(c.PostForm("id"))},
bson.M{"$set": bson.M{
"Birthday": c.PostForm("Birthday"),
"FullName": c.PostForm("FullName"),
"Mobile": c.PostForm("Mobile"),
"Sex": c.PostForm("Sex"),
}},
)
if err == nil {
var User *DB.SMember
DB.CMember.Find(bson.M{"_id": bson.ObjectIdHex(c.PostForm("id"))}).One(&User)
c.JSON(200, tools.ResponseSeccess{
0,
User,
})
} else {
c.JSON(200, tools.ResponseError{
1,
err.Error(),
})
}
}