通过验证码注册
This commit is contained in:
		@@ -12,6 +12,7 @@ type User struct {
 | 
			
		||||
	Gold      int       `json:"gold"`
 | 
			
		||||
	Name      string    `json:"name"`
 | 
			
		||||
	Email     string    `json:"email" gorm:"unique;not null"`
 | 
			
		||||
	Mobile    string    `json:"mobile" gorm:"unique;not null"`
 | 
			
		||||
	Password  string    `json:"-"`
 | 
			
		||||
	Slat      string    `json:"-"`
 | 
			
		||||
	Admin     bool      `json:"admin"`
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										48
									
								
								models/code.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								models/code.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"main/configs"
 | 
			
		||||
	"math/rand"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Code struct {
 | 
			
		||||
	Email  string    `json:"email"`
 | 
			
		||||
	Mobile string    `json:"mobile"`
 | 
			
		||||
	Code   string    `json:"code"`
 | 
			
		||||
	Expire time.Time `json:"expire"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	configs.ORMDB().AutoMigrate(&Code{})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func CodeCreate(email, mobile string) string {
 | 
			
		||||
	code := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
 | 
			
		||||
	configs.ORMDB().Create(&Code{
 | 
			
		||||
		Email:  email,
 | 
			
		||||
		Mobile: mobile,
 | 
			
		||||
		Code:   code,
 | 
			
		||||
		Expire: time.Now().Add(time.Minute * 5),
 | 
			
		||||
	})
 | 
			
		||||
	return code
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func EmailCheck(email, code string) (err error) {
 | 
			
		||||
	var data Code
 | 
			
		||||
	configs.ORMDB().Where("email = ?", email).First(&data)
 | 
			
		||||
	if data.Code == code && data.Expire.After(time.Now()) {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return fmt.Errorf("验证码错误")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func MobileCheck(mobile, code string) (err error) {
 | 
			
		||||
	var data Code
 | 
			
		||||
	configs.ORMDB().Where("mobile = ?", mobile).First(&data)
 | 
			
		||||
	if data.Code == code && data.Expire.After(time.Now()) {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return fmt.Errorf("验证码错误")
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user