Ejemplo n.º 1
0
//获取手机验证码
func GetPhoneCaptcha(c *gin.Context) {
	session := sessions.Default(c)
	phone := c.Query("phone")
	Logger.Info("get captcha phone:%v", phone)
	user := &bean.User{Phone: phone}
	userCount, err := db.Engine.Count(user)
	util.LogError(err)
	Logger.Info(userCount)

	if !userinfo.IsLegalPhoneNumber(phone) {
		c.JSON(http.StatusOK, util.JsonResult{Success: false, Msg: "请输入正确的手机号(11位)"})
		return
	}

	if userCount > 0 {
		c.JSON(http.StatusOK, util.JsonResult{Success: true, Msg: "该手机号已经被注册"})
		return
	}

	lastPhoneCaptcha := session.Get(constant.PHONE_CAPTCHA_LAST)
	lastPhoneCaptchaTimestamp, _ := lastPhoneCaptcha.(int64)
	timeDifference := (time.Now().UnixNano() - lastPhoneCaptchaTimestamp) / (1000 * 1000 * 1000)
	Logger.Debug("phone captcha time difference: %v", timeDifference)
	if timeDifference < constant.PHONE_CAPTCHA_SECONDS {
		c.JSON(http.StatusOK, util.JsonResult{Msg: fmt.Sprintf("还有%v秒可以再次发送验证码", int64(constant.PHONE_CAPTCHA_SECONDS)-timeDifference), Success: false})
		return
	}

	var captchaResult string
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < 4; i++ {
		captchaResult += strconv.Itoa(r.Intn(10))
	}

	session.Set(constant.PHONE_CAPTCHA_LAST, time.Now().UnixNano())
	session.Set(constant.PHONE_CAPTCHA, captchaResult)
	session.Set(constant.PHONE_NUMBER, phone)
	session.Save()
	c.JSON(http.StatusOK, util.JsonResult{Data: captchaResult, Success: true, Msg: "成功"})

}
Ejemplo n.º 2
0
//普通用户(游客)注册
func RegistHandler(c *gin.Context) {
	session := sessions.Default(c)
	phone := c.PostForm("phone")
	Logger.Info("register phone :%v", phone)
	phone = strings.TrimSpace(phone)
	if !userinfo.IsLegalPhoneNumber(phone) {
		render.WriteJSON(c.Writer, "请填写正确的手机号(11位)")
		return
	}

	if !strings.EqualFold(phone, stringutils.ToString(session.Get(constant.PHONE_NUMBER))) {
		c.JSON(http.StatusOK, util.JsonResult{Success: false, Msg: "下发验证码的手机号不是当前手机号"})
		return
	}

	if !strings.EqualFold(strings.ToLower(stringutils.ToString(session.Get(constant.PHONE_CAPTCHA))), strings.ToLower(c.PostForm("captcha"))) {
		c.JSON(http.StatusOK, util.JsonResult{Success: false, Msg: "验证码不正确"})
		return

		lastPhoneCaptcha := session.Get(constant.PHONE_CAPTCHA_LAST)
		lastPhoneCaptchaTimestamp, _ := lastPhoneCaptcha.(int64)
		timeDifference := (time.Now().UnixNano() - lastPhoneCaptchaTimestamp) / (1000 * 1000 * 1000 * 60)
		if timeDifference > constant.PHONE_CAPTCHA_EXPIRED_MINUTES {
			c.JSON(http.StatusOK, util.JsonResult{Success: false, Msg: fmt.Sprintf("验证码已失效,请在验证码下发后%v分钟内提交", constant.PHONE_CAPTCHA_EXPIRED_MINUTES)})
			return
		}
	}

	user := &mdl.User{Phone: phone}
	userCount, err := db.Engine.Count(user)
	util.LogError(err)

	if userCount != 0 {
		render.WriteJSON(c.Writer, util.JsonResult{Success: true, Msg: "该手机号已经被注册"})
		return
	}

	user.Salt = fmt.Sprintf("%v", rand.New(rand.NewSource(time.Now().UnixNano())).Float64())
	user.Password = c.PostForm("password")
	if len(user.Password) < 6 {
		c.JSON(http.StatusOK, util.JsonResult{Msg: "密码长度大于6"})
		return
	}
	user.Password = util.HashAndSalt(user.Password, user.Salt)
	user.Uuid = uuid.New()
	Logger.Info(user.Uuid)
	nickname := strings.TrimSpace(c.PostForm("nickname"))
	Logger.Debug("nickname: %v, size: %v", nickname, len(nickname))
	if len(nickname) == 0 {
		user.Nickname = user.Phone
	} else {
		user.Nickname = nickname
	}
	count, err := db.Engine.Insert(user)
	if err != nil || count == 0 {
		Logger.Error(err)
		c.JSON(http.StatusOK, util.JsonResult{Msg: "系统错误"})
		return
	}

	//注册成功,设置为登录
	login.SetLoginState(*user, c)

	c.JSON(http.StatusOK, util.JsonResult{Msg: "注册成功", Success: true})

}