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" "time" ) type SendVerifyCode struct { } var SendVerifyCodeServer = new(SendVerifyCode) // SendCode 获取手机短信验证码 func (s *SendVerifyCode) SendCode(mobile string) (string, error) { // 获取code 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, time.Minute*2); 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) fmt.Println(utils.IsNil(result)) fmt.Println(result) if utils.IsNil(result) || result == "" || result == nil { return false, errors.New("验证码过期") } if result.(string) != code { return false, errors.New("验证码错误,重新确认") } defer putils.DelKey(bizId + "_" + mobile) return true, nil }