User.go
4.44 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
package Api
import (
"crypto/sha256"
"encoding/hex"
"github.com/aarongao/tools"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2/bson"
"letu/DB"
"strconv"
"time"
)
// @Title 创建用户
// @Description 用户注册
// @Accept json
// @Produce json
// @Param password 1 string true "密码"
// @Param confirmpassword 1 string true "确认密码"
// @Param birthday 2010.10.10 string true "生日"
// @Param fullname aarongao string true "全名"
// @Param code 12345678 string true "6位验证码"
// @Param mobile 18616619599 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 /CreateUser? [post]
func CreateUser(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
if c.PostForm("mobile") == "" {
c.JSON(200, tools.ResponseError{
1,
"必须有手机号",
})
return
}
if c.PostForm("password") != c.PostForm("confirmpassword") {
c.JSON(200, tools.ResponseError{
1,
"密码错误",
})
return
}
// 检查验证码
code := DB.Redis.Get(c.PostForm("mobile"))
if code == "" || code != c.PostForm("code") {
c.JSON(200, tools.ResponseError{
1,
"验证码错误",
})
return
}
objectID := bson.NewObjectId()
err := DB.CMember.Insert(DB.SMember{
&objectID,
c.PostForm("password"),
c.PostForm("birthday"),
c.PostForm("fullname"),
c.PostForm("mobile"),
c.PostForm("openid"),
"",
})
if err == nil{
c.JSON(200, tools.ResponseSeccess{
0,
"ok",
})
}else{
c.JSON(200, tools.ResponseError{
0,
"此手机号已经注册",
})
}
}
// @Title 登录
// @Description 用户登录
// @Accept json
// @Produce json
// @Param mobile aaron string true "用户名"
// @Param password 1 string true "密码"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Username":"admin","Password":"123","Birthday":"","FullName":"","Mobile":"","Openid":"","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")
if c.PostForm("mobile") == "" || c.PostForm("password") == "" {
c.JSON(200, tools.ResponseError{
1,
"空",
})
return
}
var User *DB.SMember
DB.CMember.Find(bson.M{"Mobile": c.PostForm("mobile"), "Password": c.PostForm("password")}).One(&User)
if User == nil {
c.JSON(200, tools.ResponseError{
1,
"空",
})
} else {
// 生成token
tokenunit8 := sha256.Sum256([]byte(c.PostForm("mobile") + c.PostForm("password") + strconv.FormatInt(time.Now().UnixNano(), 10)))
token := hex.EncodeToString(tokenunit8[:32])
// 更新用户信息
DB.CMember.Update(
bson.M{"_id": User.Id},
bson.M{"$set": bson.M{"Token": token}},
)
User.Token = token
c.JSON(200, tools.ResponseSeccess{
0,
User,
})
}
}
// @Title 用户信息
// @Description 获取用户信息
// @Accept json
// @Produce json
// @Param id aaron string true "用户id"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Username":"admin","Password":"123","Birthday":"","FullName":"","Mobile":"","Openid":"","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("id") == "" {
c.JSON(200, tools.ResponseError{
1,
"空",
})
return
}
var User *DB.SMember
DB.CMember.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&User)
if User == nil {
c.JSON(200, tools.ResponseError{
1,
"空",
})
} else {
c.JSON(200, tools.ResponseSeccess{
0,
User,
})
}
}