Commit 23d1901bc8728244dfea66545a4e73466a3c2235
1 parent
7aca2129
Exists in
v1.2
and in
2 other branches
更新登陆接口
Showing
3 changed files
with
233 additions
and
95 deletions
Show diff stats
API/User.go
... | ... | @@ -7,92 +7,188 @@ import ( |
7 | 7 | "github.com/gin-gonic/gin" |
8 | 8 | "gopkg.in/mgo.v2/bson" |
9 | 9 | "letu/DB" |
10 | + "regexp" | |
10 | 11 | "strconv" |
11 | 12 | "time" |
12 | 13 | ) |
13 | 14 | |
14 | -// @Title 创建用户 | |
15 | -// @Description 用户注册 | |
15 | +// | |
16 | +//// @Title 创建用户 | |
17 | +//// @Description 用户注册 | |
18 | +//// @Accept json | |
19 | +//// @Produce json | |
20 | +//// @Param password 1 string true "密码" | |
21 | +//// @Param confirmpassword 1 string true "确认密码" | |
22 | +//// @Param birthday 2010.10.10 string true "生日" | |
23 | +//// @Param fullname aarongao string true "全名" | |
24 | +//// @Param code 12345678 string true "6位验证码" | |
25 | +//// @Param mobile 18616619599 string true "手机,同用户名" | |
26 | +//// @Param openid 12345 string true "微信id" | |
27 | +//// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}" | |
28 | +//// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" | |
29 | +//// @Router /CreateUser? [post] | |
30 | +//func CreateUser(c *gin.Context) { | |
31 | +// c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | |
32 | +// c.Header("Access-Control-Allow-Credentials", "true") | |
33 | +// | |
34 | +// if c.PostForm("mobile") == "" { | |
35 | +// c.JSON(200, tools.ResponseError{ | |
36 | +// 1, | |
37 | +// "必须有手机号", | |
38 | +// }) | |
39 | +// return | |
40 | +// } | |
41 | +// if c.PostForm("password") != c.PostForm("confirmpassword") { | |
42 | +// c.JSON(200, tools.ResponseError{ | |
43 | +// 1, | |
44 | +// "密码错误", | |
45 | +// }) | |
46 | +// return | |
47 | +// } | |
48 | +// | |
49 | +// // 检查验证码 | |
50 | +// code := DB.Redis.Get(c.PostForm("mobile")) | |
51 | +// if code == "" || code != c.PostForm("code") { | |
52 | +// c.JSON(200, tools.ResponseError{ | |
53 | +// 1, | |
54 | +// "验证码错误", | |
55 | +// }) | |
56 | +// return | |
57 | +// } | |
58 | +// | |
59 | +// objectID := bson.NewObjectId() | |
60 | +// err := DB.CMember.Insert(DB.SMember{ | |
61 | +// &objectID, | |
62 | +// c.PostForm("password"), | |
63 | +// c.PostForm("birthday"), | |
64 | +// c.PostForm("fullname"), | |
65 | +// c.PostForm("mobile"), | |
66 | +// c.PostForm("openid"), | |
67 | +// "", | |
68 | +// }) | |
69 | +// if err == nil{ | |
70 | +// c.JSON(200, tools.ResponseSeccess{ | |
71 | +// 0, | |
72 | +// "ok", | |
73 | +// }) | |
74 | +// }else{ | |
75 | +// c.JSON(200, tools.ResponseError{ | |
76 | +// 0, | |
77 | +// "此手机号已经注册", | |
78 | +// }) | |
79 | +// } | |
80 | +// | |
81 | +// | |
82 | +//} | |
83 | + | |
84 | +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}$" | |
85 | + | |
86 | +// @Title 登录 | |
87 | +// @Description 用户登录&注册 | |
16 | 88 | // @Accept json |
17 | 89 | // @Produce json |
18 | -// @Param password 1 string true "密码" | |
19 | -// @Param confirmpassword 1 string true "确认密码" | |
20 | -// @Param birthday 2010.10.10 string true "生日" | |
21 | -// @Param fullname aarongao string true "全名" | |
22 | -// @Param code 12345678 string true "6位验证码" | |
23 | -// @Param mobile 18616619599 string true "手机,同用户名" | |
24 | -// @Param openid 12345 string true "微信id" | |
25 | -// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}" | |
90 | +// @Param mobile aaron string true "手机号" | |
91 | +// @Param password 1 string true "密码或验证码(使用验证码的新手机号自动注册)" | |
92 | +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Username":"admin","Password":"123","Birthday":"","FullName":"","Mobile":"","Openid":"","Token":"640bf934e425aba5d3c90998b2641f2f0ca07261d334d9615d1cd4790b5f34e7"}} 调用其它需要登陆的接口时携带token,有过期时间" | |
26 | 93 | // @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" |
27 | -// @Router /CreateUser? [post] | |
28 | -func CreateUser(c *gin.Context) { | |
94 | +// @Router /LoginUser? [post] | |
95 | +func LoginUser(c *gin.Context) { | |
29 | 96 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
30 | 97 | c.Header("Access-Control-Allow-Credentials", "true") |
31 | 98 | |
32 | - if c.PostForm("mobile") == "" { | |
99 | + reg := regexp.MustCompile(Regular) | |
100 | + if !reg.MatchString(c.PostForm("mobile")) { | |
101 | + | |
33 | 102 | c.JSON(200, tools.ResponseError{ |
34 | 103 | 1, |
35 | - "必须有手机号", | |
104 | + "手机号格式不正确", | |
36 | 105 | }) |
37 | 106 | return |
38 | 107 | } |
39 | - if c.PostForm("password") != c.PostForm("confirmpassword") { | |
108 | + | |
109 | + if c.PostForm("mobile") == "" || c.PostForm("password") == "" { | |
40 | 110 | c.JSON(200, tools.ResponseError{ |
41 | 111 | 1, |
42 | - "密码错误", | |
112 | + "空", | |
43 | 113 | }) |
44 | 114 | return |
45 | 115 | } |
46 | 116 | |
117 | + // 生成token | |
118 | + tokenunit8 := sha256.Sum256([]byte(c.PostForm("mobile") + c.PostForm("password") + strconv.FormatInt(time.Now().UnixNano(), 10))) | |
119 | + token := hex.EncodeToString(tokenunit8[:32]) | |
120 | + | |
47 | 121 | // 检查验证码 |
48 | - code := DB.Redis.Get(c.PostForm("mobile")) | |
49 | - if code == "" || code != c.PostForm("code") { | |
50 | - c.JSON(200, tools.ResponseError{ | |
51 | - 1, | |
52 | - "验证码错误", | |
53 | - }) | |
54 | - return | |
55 | - } | |
122 | + cacheCode := DB.Redis.Get(c.PostForm("mobile")) | |
123 | + selected := bson.M{} | |
124 | + var User *DB.SMember | |
125 | + if cacheCode == c.PostForm("password") { | |
126 | + selected["Mobile"] = c.PostForm("mobile") | |
127 | + DB.CMember.Find(selected).One(&User) | |
56 | 128 | |
57 | - objectID := bson.NewObjectId() | |
58 | - err := DB.CMember.Insert(DB.SMember{ | |
59 | - &objectID, | |
60 | - c.PostForm("password"), | |
61 | - c.PostForm("birthday"), | |
62 | - c.PostForm("fullname"), | |
63 | - c.PostForm("mobile"), | |
64 | - c.PostForm("openid"), | |
65 | - "", | |
66 | - }) | |
67 | - if err == nil{ | |
68 | - c.JSON(200, tools.ResponseSeccess{ | |
69 | - 0, | |
70 | - "ok", | |
71 | - }) | |
72 | - }else{ | |
73 | - c.JSON(200, tools.ResponseError{ | |
74 | - 0, | |
75 | - "此手机号已经注册", | |
76 | - }) | |
129 | + // 验证码匹配,但手机号不存在 | |
130 | + if User == nil { | |
131 | + objectID := bson.NewObjectId() | |
132 | + oUser := DB.SMember{ | |
133 | + &objectID, | |
134 | + "", | |
135 | + "", | |
136 | + "", | |
137 | + c.PostForm("mobile"), | |
138 | + "", | |
139 | + token, | |
140 | + } | |
141 | + DB.CMember.Insert(oUser) | |
142 | + //if err == nil { | |
143 | + c.JSON(200, tools.ResponseSeccess{ | |
144 | + 0, | |
145 | + oUser, | |
146 | + }) | |
147 | + return | |
148 | + //} | |
149 | + } | |
150 | + | |
151 | + } else { | |
152 | + selected["Mobile"] = c.PostForm("mobile") | |
153 | + selected["Password"] = c.PostForm("password") | |
154 | + DB.CMember.Find(selected).One(&User) | |
155 | + if User == nil { | |
156 | + c.JSON(200, tools.ResponseError{ | |
157 | + 1, | |
158 | + "用户不存在或密码不正确", | |
159 | + }) | |
160 | + return | |
161 | + } | |
77 | 162 | } |
78 | 163 | |
164 | + // 更新用户信息 | |
165 | + DB.CMember.Update( | |
166 | + bson.M{"_id": User.Id}, | |
167 | + bson.M{"$set": bson.M{"Token": token}}, | |
168 | + ) | |
169 | + | |
170 | + User.Token = token | |
171 | + c.JSON(200, tools.ResponseSeccess{ | |
172 | + 0, | |
173 | + User, | |
174 | + }) | |
79 | 175 | |
80 | 176 | } |
81 | 177 | |
82 | -// @Title 登录 | |
83 | -// @Description 用户登录 | |
178 | +// @Title 用户信息 | |
179 | +// @Description 获取用户信息 | |
84 | 180 | // @Accept json |
85 | 181 | // @Produce json |
86 | -// @Param mobile aaron string true "用户名" | |
87 | -// @Param password 1 string true "密码" | |
88 | -// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Username":"admin","Password":"123","Birthday":"","FullName":"","Mobile":"","Openid":"","Token":"640bf934e425aba5d3c90998b2641f2f0ca07261d334d9615d1cd4790b5f34e7"}} 调用其它需要登陆的接口时携带token,有过期时间" | |
182 | +// @Param id aaron string true "用户id" | |
183 | +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Username":"admin","Password":"123","Birthday":"","FullName":"","Mobile":"","Openid":"","Token":"640bf934e425aba5d3c90998b2641f2f0ca07261d334d9615d1cd4790b5f34e7"}}" | |
89 | 184 | // @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" |
90 | -// @Router /LoginUser? [post] | |
91 | -func LoginUser(c *gin.Context) { | |
185 | +// @Router /UserInfo? [get] | |
186 | +func UserInfo(c *gin.Context) { | |
92 | 187 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
93 | 188 | c.Header("Access-Control-Allow-Credentials", "true") |
94 | 189 | |
95 | - if c.PostForm("mobile") == "" || c.PostForm("password") == "" { | |
190 | + | |
191 | + if c.Query("id") == "" { | |
96 | 192 | c.JSON(200, tools.ResponseError{ |
97 | 193 | 1, |
98 | 194 | "空", |
... | ... | @@ -101,7 +197,7 @@ func LoginUser(c *gin.Context) { |
101 | 197 | } |
102 | 198 | |
103 | 199 | var User *DB.SMember |
104 | - DB.CMember.Find(bson.M{"Mobile": c.PostForm("mobile"), "Password": c.PostForm("password")}).One(&User) | |
200 | + DB.CMember.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&User) | |
105 | 201 | |
106 | 202 | if User == nil { |
107 | 203 | |
... | ... | @@ -111,16 +207,6 @@ func LoginUser(c *gin.Context) { |
111 | 207 | }) |
112 | 208 | } else { |
113 | 209 | |
114 | - // 生成token | |
115 | - tokenunit8 := sha256.Sum256([]byte(c.PostForm("mobile") + c.PostForm("password") + strconv.FormatInt(time.Now().UnixNano(), 10))) | |
116 | - token := hex.EncodeToString(tokenunit8[:32]) | |
117 | - // 更新用户信息 | |
118 | - DB.CMember.Update( | |
119 | - bson.M{"_id": User.Id}, | |
120 | - bson.M{"$set": bson.M{"Token": token}}, | |
121 | - ) | |
122 | - | |
123 | - User.Token = token | |
124 | 210 | c.JSON(200, tools.ResponseSeccess{ |
125 | 211 | 0, |
126 | 212 | User, |
... | ... | @@ -129,40 +215,70 @@ func LoginUser(c *gin.Context) { |
129 | 215 | |
130 | 216 | } |
131 | 217 | |
132 | -// @Title 用户信息 | |
133 | -// @Description 获取用户信息 | |
218 | +// @Title 修改用户信息 | |
219 | +// @Description 修改用户信息 | |
134 | 220 | // @Accept json |
135 | 221 | // @Produce json |
136 | -// @Param id aaron string true "用户id" | |
137 | -// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Username":"admin","Password":"123","Birthday":"","FullName":"","Mobile":"","Openid":"","Token":"640bf934e425aba5d3c90998b2641f2f0ca07261d334d9615d1cd4790b5f34e7"}}" | |
222 | +// @Param password 1 string true "密码" | |
223 | +// @Param confirmpassword 1 string true "确认密码" | |
224 | +// @Param birthday 2010.10.10 string true "生日" | |
225 | +// @Param fullname aarongao string true "全名" | |
226 | +// @Param code 12345678 string true "6位验证码" | |
227 | +// @Param mobile 18616619599 string true "手机,同用户名" | |
228 | +// @Param openid 12345 string true "微信id" | |
229 | +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}" | |
138 | 230 | // @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" |
139 | -// @Router /UserInfo? [get] | |
140 | -func UserInfo(c *gin.Context) { | |
231 | +// @Router /UpdateUser? [post] | |
232 | +func UpdateUser(c *gin.Context) { | |
141 | 233 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
142 | 234 | c.Header("Access-Control-Allow-Credentials", "true") |
143 | 235 | |
144 | - if c.Query("id") == "" { | |
236 | + reg := regexp.MustCompile(Regular) | |
237 | + if !reg.MatchString(c.PostForm("mobile")) { | |
238 | + | |
145 | 239 | c.JSON(200, tools.ResponseError{ |
146 | 240 | 1, |
147 | - "空", | |
241 | + "手机号格式不正确", | |
242 | + }) | |
243 | + return | |
244 | + } | |
245 | + if c.PostForm("password") != c.PostForm("confirmpassword") { | |
246 | + c.JSON(200, tools.ResponseError{ | |
247 | + 1, | |
248 | + "2次密码不一致", | |
148 | 249 | }) |
149 | 250 | return |
150 | 251 | } |
151 | 252 | |
152 | - var User *DB.SMember | |
153 | - DB.CMember.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&User) | |
154 | - | |
155 | - if User == nil { | |
156 | - | |
253 | + // 检查验证码 | |
254 | + code := DB.Redis.Get(c.PostForm("mobile")) | |
255 | + if code == "" || code != c.PostForm("code") { | |
157 | 256 | c.JSON(200, tools.ResponseError{ |
158 | 257 | 1, |
159 | - "空", | |
258 | + "验证码错误", | |
160 | 259 | }) |
161 | - } else { | |
260 | + return | |
261 | + } | |
162 | 262 | |
263 | + objectID := bson.NewObjectId() | |
264 | + err := DB.CMember.Insert(DB.SMember{ | |
265 | + &objectID, | |
266 | + c.PostForm("password"), | |
267 | + c.PostForm("birthday"), | |
268 | + c.PostForm("fullname"), | |
269 | + c.PostForm("mobile"), | |
270 | + c.PostForm("openid"), | |
271 | + "", | |
272 | + }) | |
273 | + if err == nil { | |
163 | 274 | c.JSON(200, tools.ResponseSeccess{ |
164 | 275 | 0, |
165 | - User, | |
276 | + "ok", | |
277 | + }) | |
278 | + } else { | |
279 | + c.JSON(200, tools.ResponseError{ | |
280 | + 0, | |
281 | + "此手机号已经注册", | |
166 | 282 | }) |
167 | 283 | } |
168 | 284 | ... | ... |
README.md
... | ... | @@ -15,10 +15,10 @@ |
15 | 15 | 1. [所有景区基础信息](#allscenic-get) |
16 | 16 | 1. [查询商品信息](#commodityinfo-get) |
17 | 17 | 1. [增加投诉](#createcomplaint-post) |
18 | -1. [用户注册](#createuser-post) | |
18 | +1. [增加调查](#investigation-save-post) | |
19 | 19 | 1. [查询设备信息](#iteminfo-get) |
20 | 20 | 1. [查询线路信息](#lineinfo-get) |
21 | -1. [用户登录](#loginuser-post) | |
21 | +1. [用户登录&注册](#loginuser-post) | |
22 | 22 | 1. [返回景区基础信息](#scenicinfo-get) |
23 | 23 | 1. [发送短信验证码](#sms-send-post) |
24 | 24 | 1. [标签列表](#tags-get) |
... | ... | @@ -27,6 +27,7 @@ |
27 | 27 | 1. [更新等待时间](#updateitemtime-post) |
28 | 28 | 1. [更新线路](#updateline-post) |
29 | 29 | 1. [更新景区基础信息](#updatescenic-post) |
30 | +1. [修改用户信息](#updateuser-post) | |
30 | 31 | 1. [上传](#upload-post) |
31 | 32 | 1. [获取用户信息](#userinfo-get) |
32 | 33 | |
... | ... | @@ -154,22 +155,18 @@ |
154 | 155 | |
155 | 156 | |
156 | 157 | |
157 | -<a name="createuser-post"></a> | |
158 | +<a name="investigation-save-post"></a> | |
158 | 159 | |
159 | -#### /CreateUser (POST) | |
160 | +#### /Investigation/Save (POST) | |
160 | 161 | |
161 | 162 | |
162 | -用户注册 | |
163 | +增加调查 | |
163 | 164 | |
164 | 165 | | Param Name | Example | Data Type | Description | Required? | |
165 | 166 | |-----|-----|-----|-----|-----| |
166 | -| password | 1 | string | 密码 | Yes | | |
167 | -| confirmpassword | 1 | string | 确认密码 | Yes | | |
168 | -| birthday | 2010.10.10 | string | 生日 | Yes | | |
169 | -| fullname | aarongao | string | 全名 | Yes | | |
170 | -| code | 12345678 | string | 6位验证码 | Yes | | |
171 | -| mobile | 18616619599 | string | 手机,同用户名 | Yes | | |
172 | -| openid | 12345 | string | 微信id | Yes | | |
167 | +| UserId | 1111111 | string | UserId | Yes | | |
168 | +| Mobile | 18616619599 | string | 联系电话 | Yes | | |
169 | +| type | 1 | string | 类型 | Yes | | |
173 | 170 | |
174 | 171 | |
175 | 172 | | Code | Type | Model | Message | |
... | ... | @@ -222,12 +219,12 @@ |
222 | 219 | #### /LoginUser (POST) |
223 | 220 | |
224 | 221 | |
225 | -用户登录 | |
222 | +用户登录&注册 | |
226 | 223 | |
227 | 224 | | Param Name | Example | Data Type | Description | Required? | |
228 | 225 | |-----|-----|-----|-----|-----| |
229 | -| mobile | aaron | string | 用户名 | Yes | | |
230 | -| password | 1 | string | 密码 | Yes | | |
226 | +| mobile | aaron | string | 手机号 | Yes | | |
227 | +| password | 1 | string | 密码或验证码(使用验证码的新手机号自动注册) | Yes | | |
231 | 228 | |
232 | 229 | |
233 | 230 | | Code | Type | Model | Message | |
... | ... | @@ -370,6 +367,31 @@ |
370 | 367 | |
371 | 368 | |
372 | 369 | |
370 | +<a name="updateuser-post"></a> | |
371 | + | |
372 | +#### /UpdateUser (POST) | |
373 | + | |
374 | + | |
375 | +修改用户信息 | |
376 | + | |
377 | +| Param Name | Example | Data Type | Description | Required? | | |
378 | +|-----|-----|-----|-----|-----| | |
379 | +| password | 1 | string | 密码 | Yes | | |
380 | +| confirmpassword | 1 | string | 确认密码 | Yes | | |
381 | +| birthday | 2010.10.10 | string | 生日 | Yes | | |
382 | +| fullname | aarongao | string | 全名 | Yes | | |
383 | +| code | 12345678 | string | 6位验证码 | Yes | | |
384 | +| mobile | 18616619599 | string | 手机,同用户名 | Yes | | |
385 | +| openid | 12345 | string | 微信id | Yes | | |
386 | + | |
387 | + | |
388 | +| Code | Type | Model | Message | | |
389 | +|-----|-----|-----|-----| | |
390 | +| 200 | object | [ResponseSeccess](#github.com.aarongao.tools.ResponseSeccess) | {"errcode":0,"result":"ok"} | | |
391 | +| 500 | object | [ResponseError](#github.com.aarongao.tools.ResponseError) | {"errcode":1,"errmsg":"错误原因"} | | |
392 | + | |
393 | + | |
394 | + | |
373 | 395 | <a name="upload-post"></a> |
374 | 396 | |
375 | 397 | #### /Upload (POST) | ... | ... |
main.go
... | ... | @@ -68,7 +68,7 @@ func main() { |
68 | 68 | r.GET("/ItemInfo", Api.ItemInfo) |
69 | 69 | r.GET("/CommodityInfo", Api.CommodityInfo) |
70 | 70 | r.POST("/CreateComplaint", Api.CreateComplaint) |
71 | - r.POST("/CreateUser", Api.CreateUser) | |
71 | + //r.POST("/CreateUser", Api.CreateUser) | |
72 | 72 | r.POST("/LoginUser", Api.LoginUser) |
73 | 73 | r.GET("/UserInfo", Api.UserInfo) |
74 | 74 | r.GET("/ScenicInfo", Api.ScenicInfo) | ... | ... |