diff --git a/API/User.go b/API/User.go index 427133e..dad8007 100644 --- a/API/User.go +++ b/API/User.go @@ -7,92 +7,188 @@ import ( "github.com/gin-gonic/gin" "gopkg.in/mgo.v2/bson" "letu/DB" + "regexp" "strconv" "time" ) -// @Title 创建用户 -// @Description 用户注册 +// +//// @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, +// "此手机号已经注册", +// }) +// } +// +// +//} + +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 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"}" +// @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 /CreateUser? [post] -func CreateUser(c *gin.Context) { +// @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") == "" { + reg := regexp.MustCompile(Regular) + if !reg.MatchString(c.PostForm("mobile")) { + c.JSON(200, tools.ResponseError{ 1, - "必须有手机号", + "手机号格式不正确", }) return } - if c.PostForm("password") != c.PostForm("confirmpassword") { + + if c.PostForm("mobile") == "" || c.PostForm("password") == "" { c.JSON(200, tools.ResponseError{ 1, - "密码错误", + "空", }) return } + // 生成token + tokenunit8 := sha256.Sum256([]byte(c.PostForm("mobile") + c.PostForm("password") + strconv.FormatInt(time.Now().UnixNano(), 10))) + token := hex.EncodeToString(tokenunit8[:32]) + // 检查验证码 - code := DB.Redis.Get(c.PostForm("mobile")) - if code == "" || code != c.PostForm("code") { - c.JSON(200, tools.ResponseError{ - 1, - "验证码错误", - }) - return - } + cacheCode := DB.Redis.Get(c.PostForm("mobile")) + selected := bson.M{} + var User *DB.SMember + if cacheCode == c.PostForm("password") { + selected["Mobile"] = c.PostForm("mobile") + DB.CMember.Find(selected).One(&User) - 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, - "此手机号已经注册", - }) + // 验证码匹配,但手机号不存在 + if User == nil { + objectID := bson.NewObjectId() + oUser := DB.SMember{ + &objectID, + "", + "", + "", + c.PostForm("mobile"), + "", + token, + } + DB.CMember.Insert(oUser) + //if err == nil { + c.JSON(200, tools.ResponseSeccess{ + 0, + oUser, + }) + return + //} + } + + } else { + selected["Mobile"] = c.PostForm("mobile") + selected["Password"] = c.PostForm("password") + DB.CMember.Find(selected).One(&User) + if User == nil { + c.JSON(200, tools.ResponseError{ + 1, + "用户不存在或密码不正确", + }) + return + } } + // 更新用户信息 + 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 用户登录 +// @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,有过期时间" +// @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 /LoginUser? [post] -func LoginUser(c *gin.Context) { +// @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.PostForm("mobile") == "" || c.PostForm("password") == "" { + + if c.Query("id") == "" { c.JSON(200, tools.ResponseError{ 1, "空", @@ -101,7 +197,7 @@ func LoginUser(c *gin.Context) { } var User *DB.SMember - DB.CMember.Find(bson.M{"Mobile": c.PostForm("mobile"), "Password": c.PostForm("password")}).One(&User) + DB.CMember.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&User) if User == nil { @@ -111,16 +207,6 @@ func LoginUser(c *gin.Context) { }) } 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, @@ -129,40 +215,70 @@ func LoginUser(c *gin.Context) { } -// @Title 用户信息 -// @Description 获取用户信息 +// @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"}}" +// @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 /UserInfo? [get] -func UserInfo(c *gin.Context) { +// @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.Query("id") == "" { + reg := regexp.MustCompile(Regular) + if !reg.MatchString(c.PostForm("mobile")) { + c.JSON(200, tools.ResponseError{ 1, - "空", + "手机号格式不正确", + }) + return + } + if c.PostForm("password") != c.PostForm("confirmpassword") { + c.JSON(200, tools.ResponseError{ + 1, + "2次密码不一致", }) return } - var User *DB.SMember - DB.CMember.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&User) - - if User == nil { - + // 检查验证码 + code := DB.Redis.Get(c.PostForm("mobile")) + if code == "" || code != c.PostForm("code") { c.JSON(200, tools.ResponseError{ 1, - "空", + "验证码错误", }) - } else { + 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, - User, + "ok", + }) + } else { + c.JSON(200, tools.ResponseError{ + 0, + "此手机号已经注册", }) } diff --git a/README.md b/README.md index bddc426..54d4bb5 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ 1. [所有景区基础信息](#allscenic-get) 1. [查询商品信息](#commodityinfo-get) 1. [增加投诉](#createcomplaint-post) -1. [用户注册](#createuser-post) +1. [增加调查](#investigation-save-post) 1. [查询设备信息](#iteminfo-get) 1. [查询线路信息](#lineinfo-get) -1. [用户登录](#loginuser-post) +1. [用户登录&注册](#loginuser-post) 1. [返回景区基础信息](#scenicinfo-get) 1. [发送短信验证码](#sms-send-post) 1. [标签列表](#tags-get) @@ -27,6 +27,7 @@ 1. [更新等待时间](#updateitemtime-post) 1. [更新线路](#updateline-post) 1. [更新景区基础信息](#updatescenic-post) +1. [修改用户信息](#updateuser-post) 1. [上传](#upload-post) 1. [获取用户信息](#userinfo-get) @@ -154,22 +155,18 @@ - + -#### /CreateUser (POST) +#### /Investigation/Save (POST) -用户注册 +增加调查 | Param Name | Example | Data Type | Description | Required? | |-----|-----|-----|-----|-----| -| password | 1 | string | 密码 | Yes | -| confirmpassword | 1 | string | 确认密码 | Yes | -| birthday | 2010.10.10 | string | 生日 | Yes | -| fullname | aarongao | string | 全名 | Yes | -| code | 12345678 | string | 6位验证码 | Yes | -| mobile | 18616619599 | string | 手机,同用户名 | Yes | -| openid | 12345 | string | 微信id | Yes | +| UserId | 1111111 | string | UserId | Yes | +| Mobile | 18616619599 | string | 联系电话 | Yes | +| type | 1 | string | 类型 | Yes | | Code | Type | Model | Message | @@ -222,12 +219,12 @@ #### /LoginUser (POST) -用户登录 +用户登录&注册 | Param Name | Example | Data Type | Description | Required? | |-----|-----|-----|-----|-----| -| mobile | aaron | string | 用户名 | Yes | -| password | 1 | string | 密码 | Yes | +| mobile | aaron | string | 手机号 | Yes | +| password | 1 | string | 密码或验证码(使用验证码的新手机号自动注册) | Yes | | Code | Type | Model | Message | @@ -370,6 +367,31 @@ + + +#### /UpdateUser (POST) + + +修改用户信息 + +| Param Name | Example | Data Type | Description | Required? | +|-----|-----|-----|-----|-----| +| password | 1 | string | 密码 | Yes | +| confirmpassword | 1 | string | 确认密码 | Yes | +| birthday | 2010.10.10 | string | 生日 | Yes | +| fullname | aarongao | string | 全名 | Yes | +| code | 12345678 | string | 6位验证码 | Yes | +| mobile | 18616619599 | string | 手机,同用户名 | Yes | +| openid | 12345 | string | 微信id | Yes | + + +| Code | Type | Model | Message | +|-----|-----|-----|-----| +| 200 | object | [ResponseSeccess](#github.com.aarongao.tools.ResponseSeccess) | {"errcode":0,"result":"ok"} | +| 500 | object | [ResponseError](#github.com.aarongao.tools.ResponseError) | {"errcode":1,"errmsg":"错误原因"} | + + + #### /Upload (POST) diff --git a/main.go b/main.go index 525ef6d..739341b 100644 --- a/main.go +++ b/main.go @@ -68,7 +68,7 @@ func main() { r.GET("/ItemInfo", Api.ItemInfo) r.GET("/CommodityInfo", Api.CommodityInfo) r.POST("/CreateComplaint", Api.CreateComplaint) - r.POST("/CreateUser", Api.CreateUser) + //r.POST("/CreateUser", Api.CreateUser) r.POST("/LoginUser", Api.LoginUser) r.GET("/UserInfo", Api.UserInfo) r.GET("/ScenicInfo", Api.ScenicInfo) -- libgit2 0.21.0