User.go 5.45 KB
package Api

import (
	"crypto/sha256"
	"encoding/hex"
	"github.com/aarongao/tools"
	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2/bson"
	"letu/DB"
	"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        "验证码(使用验证码的新手机号自动注册)"
// @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(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()
			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["Code"] = c.PostForm("Code")
		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 用户管理 - 获取用户信息
// @Accept  json
// @Produce  json
// @Param   id     aaron    string     true        "用户id"
// @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("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)

	c.JSON(200, tools.ResponseSeccess{
		0,
		User,
	})

}

// @Title 修改用户信息
// @Description 用户管理 - 修改用户信息
// @Accept  json
// @Produce  json
// @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")

	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(c.PostForm("Mobile"))
	if code == "" || code != c.PostForm("Code") {
		c.JSON(200, tools.ResponseError{
			1,
			"验证码错误",
		})
		return
	}

	err := DB.CMember.Update(
		bson.M{"Mobile": c.PostForm("Mobile")},
		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{"Mobile": c.PostForm("Mobile")}).One(&User)

		c.JSON(200, tools.ResponseSeccess{
			0,
			User,
		})
	} else {
		c.JSON(200, tools.ResponseError{
			1,
			err.Error(),
		})
	}

}