You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.1 KiB
52 lines
1.1 KiB
package usercenter
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"math/big"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var loginOrregSMSVerificationCode sync.Map
|
|
|
|
// 登记注册用验证码
|
|
func StoreLoginOrloginOrRegSMSVerificationCode(mobile string) (string, error) {
|
|
codeInterface, ok := loginOrregSMSVerificationCode.Load(mobile)
|
|
if ok {
|
|
return codeInterface.(string), nil
|
|
}
|
|
verificationCode := randCharNumber(6)
|
|
loginOrregSMSVerificationCode.Store(mobile, verificationCode)
|
|
go func(mobile string) {
|
|
time.Sleep(time.Second * 5 * 60)
|
|
loginOrregSMSVerificationCode.Delete(mobile)
|
|
}(mobile)
|
|
return verificationCode, nil
|
|
}
|
|
|
|
// 验证注册验证码
|
|
func LoginOrRegSMSVerification(mobile, verificationCode string) bool {
|
|
codeInterface, ok := loginOrregSMSVerificationCode.LoadAndDelete(mobile)
|
|
if !ok {
|
|
return false
|
|
}
|
|
if codeInterface.(string) == verificationCode {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
// 随机验证码
|
|
func randCharNumber(size int) string {
|
|
char := "0123456789"
|
|
len64 := int64(len(char))
|
|
var s bytes.Buffer
|
|
for i := 0; i < size; i++ {
|
|
in, _ := rand.Int(rand.Reader, big.NewInt(len64))
|
|
s.WriteByte(char[in.Int64()])
|
|
}
|
|
return s.String()
|
|
}
|