|
|
@@ -284,6 +284,8 @@ func login(c *gin.Context) {
|
|
|
func registerCode(c *gin.Context) {
|
|
|
data := share.GetJsonAnyParam(c)
|
|
|
phone, _ := data("phone")
|
|
|
+
|
|
|
+ email, _ := data("email")
|
|
|
//判断是否为手机格式
|
|
|
if !checkPhone(c, cast.ToString(phone)) {
|
|
|
return
|
|
|
@@ -292,37 +294,52 @@ func registerCode(c *gin.Context) {
|
|
|
b, err := dao.ExistsPhone(cast.ToString(phone))
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
- c.JSON(http.StatusOK, CreateResultError(200, "数据库错误!!!"))
|
|
|
+ c.JSON(http.StatusOK, CreateResultError(200, "数据库错误!"))
|
|
|
return
|
|
|
}
|
|
|
if b {
|
|
|
c.JSON(http.StatusOK, CreateResultError(200, "手机号已注册!!!"))
|
|
|
return
|
|
|
}
|
|
|
+ //判断是否已经注册
|
|
|
+ b, err = dao.ExistsEmail(cast.ToString(email))
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ c.JSON(http.StatusOK, CreateResultError(200, "数据库错误!"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if b {
|
|
|
+ c.JSON(http.StatusOK, CreateResultError(200, "邮箱已注册!!!"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
//判断是否重复注册
|
|
|
get := configs.RedisDb.Exists(ctx, "PhoneVerificationCodeTime_"+cast.ToString(phone))
|
|
|
if get.Val() == 1 {
|
|
|
c.JSON(http.StatusOK, CreateResultError(200, "验证码已经发送,请等待"))
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
//发送短信
|
|
|
//sms := sendSms(b, cast.ToString(phone))
|
|
|
//TODO 模拟发送短信
|
|
|
//sms := testSendSms()
|
|
|
- email, _ := data("email")
|
|
|
- sms := testSendMail(cast.ToString(email))
|
|
|
+ sms := testSendSms(cast.ToString(email))
|
|
|
fmt.Println("发送成功!!!:验证码为:", sms)
|
|
|
+ //五分钟有效期
|
|
|
configs.RedisDb.Set(ctx, "PhoneVerificationCode_"+cast.ToString(phone), sms, 60*5*time.Second)
|
|
|
- configs.RedisDb.Set(ctx, "PhoneVerificationCodeTime_"+cast.ToString(phone), time.Now(), 60*time.Second)
|
|
|
- c.JSON(http.StatusOK, CreateResult())
|
|
|
+ //60秒有效期
|
|
|
+ configs.RedisDb.Set(ctx, "PhoneVerificationCodeTime_"+cast.ToString(phone), time.Now(), 5*time.Second)
|
|
|
|
|
|
+ c.JSON(http.StatusOK, CreateResult())
|
|
|
}
|
|
|
|
|
|
-func testSendSms() string {
|
|
|
+func testSendSms(email string) string {
|
|
|
mins := big.NewInt(100000)
|
|
|
maxs := big.NewInt(999999)
|
|
|
randomNum := share.RandomInt(mins, maxs)
|
|
|
+ sms := randomNum.String()
|
|
|
+ go SendEmail(cast.ToString(email), fmt.Sprint("您的验证码为:", sms))
|
|
|
+
|
|
|
//if matched {
|
|
|
// //bools, err = configs.Engine.Table("user").Where("username = ?", username).Exist(&user)
|
|
|
//
|
|
|
@@ -331,7 +348,8 @@ func testSendSms() string {
|
|
|
//} else {
|
|
|
// share.SendSms(strconv.Itoa(int(randomNum.Int64())), username)
|
|
|
//}
|
|
|
- return randomNum.String()
|
|
|
+
|
|
|
+ return sms
|
|
|
}
|
|
|
|
|
|
func testSendMail(email string) string {
|
|
|
@@ -349,14 +367,8 @@ func testSendMail(email string) string {
|
|
|
return randomNum.String()
|
|
|
}
|
|
|
|
|
|
-func SendEmail(from, to, content string) {
|
|
|
- mail := util.NewMail()
|
|
|
- if mail == nil {
|
|
|
- return
|
|
|
- }
|
|
|
- //字符串转 io.WriterTo
|
|
|
- contentReader := strings.NewReader(content)
|
|
|
- mail.Send(from, []string{to}, contentReader)
|
|
|
+func SendEmail(to, content string) {
|
|
|
+ util.SendMail("用户注册验证码", content, to)
|
|
|
}
|
|
|
|
|
|
func register(c *gin.Context) {
|