1
This commit is contained in:
61
services/print_server/app_server/verify_code.go
Normal file
61
services/print_server/app_server/verify_code.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package app_server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-print/globals"
|
||||
"git.rosy.net.cn/jx-print/putils"
|
||||
"git.rosy.net.cn/jx-print/services/api"
|
||||
aliyunsmsclient "github.com/KenmyZhang/aliyun-communicate"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
type SendVerifyCode struct {
|
||||
}
|
||||
|
||||
var SendVerifyCodeServer = new(SendVerifyCode)
|
||||
|
||||
// SendCode 获取手机短信验证码
|
||||
func (s *SendVerifyCode) SendCode(mobile string) (string, error) {
|
||||
// 获取code
|
||||
smsCode := ""
|
||||
code := putils.GetKey(mobile)
|
||||
if code != "" {
|
||||
smsCode = code.(string)
|
||||
} else {
|
||||
smsCode = fmt.Sprintf("%06d", rand.Intn(1000000))
|
||||
}
|
||||
|
||||
// 发送短信
|
||||
response, err := api.SMSClient.Execute(globals.AliKey, globals.AliSecret, mobile, globals.SmsSignName, globals.SmsMobileVerifyTemplate, string(utils.MustMarshal(map[string]interface{}{
|
||||
"code": smsCode,
|
||||
})))
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if response.Code != aliyunsmsclient.ResponseCodeOk {
|
||||
return "", fmt.Errorf("发送短信出错:%s", response.Message)
|
||||
}
|
||||
|
||||
if err := putils.SetKey(response.BizId+"_"+mobile, smsCode, 5*60); err != nil {
|
||||
globals.SugarLogger.Debugf("redis set key err key[%s] value[%s]:", mobile, smsCode)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return response.BizId, nil
|
||||
}
|
||||
|
||||
// VerifySecret 检查验证码
|
||||
func (s *SendVerifyCode) VerifySecret(mobile, bizId, code string) (bool, error) {
|
||||
result := putils.GetKey(bizId + "_" + mobile)
|
||||
if result != "" {
|
||||
return false, errors.New("验证码过期")
|
||||
}
|
||||
if result.(string) != code {
|
||||
return false, errors.New("验证码错误,重新确认")
|
||||
}
|
||||
putils.DelKey(bizId + "_" + mobile)
|
||||
return true, nil
|
||||
}
|
||||
Reference in New Issue
Block a user