105 lines
2.7 KiB
Go
105 lines
2.7 KiB
Go
package mobile
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
|
|
"git.rosy.net.cn/jx-callback/business/model/dao"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
"git.rosy.net.cn/jx-callback/globals/api"
|
|
aliyunsmsclient "github.com/KenmyZhang/aliyun-communicate"
|
|
)
|
|
|
|
const (
|
|
DefVerifyCodeDuration = 5 * time.Minute
|
|
TestMobile = "91112345678"
|
|
TestVerifyCode = "123456"
|
|
)
|
|
|
|
var (
|
|
warningMap = map[string]int{
|
|
"isv.AMOUNT_NOT_ENOUGH": 1,
|
|
"isv.ACCOUNT_ABNORMAL": 1,
|
|
"isv.OUT_OF_SERVICE": 1,
|
|
"isv.DAY_LIMIT_CONTROL": 1,
|
|
}
|
|
)
|
|
|
|
const (
|
|
LoginType = "mobile"
|
|
)
|
|
|
|
var (
|
|
ErrVerifyCodeIsWrong = errors.New("验证码错")
|
|
)
|
|
|
|
type Auther struct {
|
|
}
|
|
|
|
var (
|
|
auther *Auther
|
|
)
|
|
|
|
func init() {
|
|
auther = new(Auther)
|
|
auth.RegisterAuther(LoginType, auther)
|
|
}
|
|
|
|
func 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/")
|
|
response, err := smsClient.Execute(globals.AliKey, globals.AliSecret, mobileNumber, "京西菜市", "SMS_84655036", string(utils.MustMarshal(map[string]interface{}{
|
|
"code": code,
|
|
})))
|
|
api.Cacher.Set(mobileNumber, code, DefVerifyCodeDuration)
|
|
if err == nil && response.Code == aliyunsmsclient.ResponseCodeOk {
|
|
} else {
|
|
if err == nil {
|
|
if warningMap[response.Code] == 1 {
|
|
globals.SugarLogger.Warnf("SendVerifyCode mobileNumber:%s failed with response:%s", mobileNumber, utils.Format4Output(response, false))
|
|
} else {
|
|
globals.SugarLogger.Infof("SendVerifyCode mobileNumber:%s failed with response:%s", mobileNumber, utils.Format4Output(response, false))
|
|
}
|
|
err = fmt.Errorf("发送短信出错:%s", response.Message)
|
|
} else {
|
|
globals.SugarLogger.Warnf("SendVerifyCode mobileNumber:%s failed with error:%v", mobileNumber, err)
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func VerifyCode(mobileNumber, code string) (err error) {
|
|
globals.SugarLogger.Debugf("VerifyCode mobileNumber:%s, code:%s", mobileNumber, code)
|
|
|
|
err = ErrVerifyCodeIsWrong
|
|
if mobileNumber == TestMobile && code == TestVerifyCode {
|
|
err = nil
|
|
} else {
|
|
if value := api.Cacher.Get(mobileNumber); value != nil {
|
|
if code == value.(string) {
|
|
api.Cacher.Del(mobileNumber)
|
|
err = nil
|
|
}
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (a *Auther) Login(mobileNum, verifyCode string) (userID, LoginType string, err error) {
|
|
if err = VerifyCode(mobileNum, verifyCode); err == nil {
|
|
_, err = dao.GetWeiXinUserByIDs(dao.GetDB(), mobileNum, "", "", "")
|
|
err = auth.ConvertErr2NoUser(err, mobileNum)
|
|
}
|
|
return "", "", err
|
|
}
|
|
|
|
func (a *Auther) Logout(loginInfo *auth.LoginInfo) error {
|
|
return nil
|
|
}
|