package mobile import ( "errors" "fmt" "math/rand" "time" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/auth2" "git.rosy.net.cn/jx-callback/business/auth2/authprovider" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/globals" "git.rosy.net.cn/jx-callback/globals/api" "github.com/KenmyZhang/aliyun-communicate" ) const ( DefVerifyCodeDuration = 5 * time.Minute TestVerifyCode = "123456" ) const ( AuthType = auth2.AuthTypeMobile ) var ( ErrVerifyCodeIsWrong = errors.New("验证码错") ) type Auther struct { authprovider.DefAuther } var ( AutherObj *Auther ) func init() { AutherObj = new(Auther) auth2.RegisterAuther(AuthType, AutherObj) } // 特殊接口 func (a *Auther) SendVerifyCode(mobileNumber string) error { code := fmt.Sprintf("%06d", rand.Intn(1000000)) globals.SugarLogger.Debugf("SendVerifyCode mobileNumber:%s, code:%s", mobileNumber, code) smsClient := aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/") _, err := smsClient.Execute(globals.AliKey, globals.AliSecret, mobileNumber, "京西菜市", "SMS_84655036", string(utils.MustMarshal(map[string]interface{}{ "code": code, }))) if err == nil { api.Cacher.Set(mobileNumber, code, DefVerifyCodeDuration) } else { globals.SugarLogger.Infof("SendVerifyCode mobileNumber:%s failed with error:%v", mobileNumber, err) } return err } func (a *Auther) VerifySecret(mobileNumber, code string) (authBind *model.AuthBind, err error) { globals.SugarLogger.Debugf("VerifySecret mobileNumber:%s, code:%s", mobileNumber, code) err = ErrVerifyCodeIsWrong if auth2.TestMobileMap[mobileNumber] == 1 && code == TestVerifyCode { err = nil } else { if value := api.Cacher.Get(mobileNumber); value != nil { if code == value.(string) { api.Cacher.Del(mobileNumber) err = nil } } } return nil, err } // 此函数为空 func (a *Auther) AddAuthBind(authBind *model.AuthBind, userName string) (err error) { return err } // 此函数为空 func (a *Auther) UnbindAuth(authInfo *auth2.AuthInfo, authType string) (err error) { return err }