|
|
package auth
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
"fmt"
|
|
|
"github.com/astaxie/beego"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/ability/internal/repository"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/ability/models"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis"
|
|
|
s_sms "gitlab.fjmaimaimai.com/mmm-go/ability/services/sms"
|
|
|
comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
|
|
|
"html/template"
|
|
|
"strings"
|
|
|
"encoding/json"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type IAuthService interface {
|
...
|
...
|
@@ -28,7 +36,7 @@ func assertImplement(){ |
|
|
|
|
|
var(
|
|
|
//服务
|
|
|
//sms s_sms.ISmsService = &s_sms.SmsService{}
|
|
|
sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
|
|
|
|
|
|
//仓储
|
|
|
UserRepository repository.IUserRepository =&repository.UserRepository{}
|
...
|
...
|
@@ -39,6 +47,7 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe |
|
|
var (
|
|
|
user *models.Users
|
|
|
userInfo *models.UserInfo
|
|
|
result bool
|
|
|
)
|
|
|
user,err =UserRepository.GetUsersByMobile(request.Phone)
|
|
|
if err!=nil{
|
...
|
...
|
@@ -56,7 +65,11 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe |
|
|
}
|
|
|
break
|
|
|
case protocol.LoginSmdcode:
|
|
|
if result,err=CheckSmsCode(request);result && err==nil{
|
|
|
goto Success
|
|
|
}else{
|
|
|
return
|
|
|
}
|
|
|
default:
|
|
|
err =fmt.Errorf("grantType error")
|
|
|
return
|
...
|
...
|
@@ -168,5 +181,103 @@ func (s *AuthService)CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol |
|
|
}
|
|
|
//短信验证码
|
|
|
func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error){
|
|
|
return nil,nil
|
|
|
var(
|
|
|
value,key,msgContent string
|
|
|
smsInfo *protocol.SmsInfo
|
|
|
)
|
|
|
msgContent = `【买买买信息科技】{{.Code}}({{.AppName}}手机验证码,请完成验证),如非本人操作,请忽略本短信`
|
|
|
switch request.SendType {
|
|
|
case protocol.SmsLoginCode:
|
|
|
case protocol.SmsChangeMobile:
|
|
|
default:
|
|
|
err = common.NewErrorWithMsg(2,"send_type error.")
|
|
|
return
|
|
|
}
|
|
|
key = request.SendType
|
|
|
//check user phone exists
|
|
|
if !redis.Hexists(key,request.Phone){
|
|
|
smsInfo = &protocol.SmsInfo{
|
|
|
CreateTime:time.Now().Unix(),
|
|
|
}
|
|
|
goto Send
|
|
|
}else{
|
|
|
if value,err =redis.Hget(key,request.Phone);err!=nil{
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if err=json.Unmarshal([]byte(value),&smsInfo);err!=nil{
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
//第二天重置
|
|
|
if smsInfo.LastTime<comm_time.GetUnixTimeByYyyymmdd(){
|
|
|
smsInfo.Count=0
|
|
|
smsInfo.CreateTime = time.Now().Unix()
|
|
|
}
|
|
|
if smsInfo.Count>100{//TODO:limit send time
|
|
|
return
|
|
|
}
|
|
|
goto Send
|
|
|
}
|
|
|
Send:
|
|
|
{
|
|
|
smsInfo.Code = common.RandomStringWithChars(6,string(protocol.Nums))
|
|
|
smsInfo.LastTime=time.Now().Unix()
|
|
|
smsInfo.ErrorCount =0
|
|
|
//Todo Lock
|
|
|
smsInfo.Count +=1
|
|
|
if err=redis.Hset(key,request.Phone,common.AssertJson(smsInfo),-1);err!=nil{
|
|
|
return
|
|
|
}
|
|
|
tp :=template.New("sms_code")
|
|
|
tp.Parse(msgContent)
|
|
|
buf :=bytes.NewBuffer(nil)
|
|
|
tp.Execute(
|
|
|
buf,
|
|
|
map[string]string{
|
|
|
"Code":smsInfo.Code,
|
|
|
"AppName":beego.BConfig.AppName,
|
|
|
},)
|
|
|
request.Content = buf.String()
|
|
|
err = sms.Send(request)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
//验证短信验证码
|
|
|
func CheckSmsCode(request *protocol.LoginRequest)(result bool,err error){
|
|
|
var(
|
|
|
value string
|
|
|
smsInfo *protocol.SmsInfo
|
|
|
)
|
|
|
result =false
|
|
|
if value,err =redis.Hget(protocol.SmsLoginCode,request.Phone);err!=nil{
|
|
|
err = common.NewErrorWithMsg(1009,"smscode expire")
|
|
|
return
|
|
|
}
|
|
|
if err=json.Unmarshal([]byte(value),&smsInfo);err!=nil{
|
|
|
return
|
|
|
}
|
|
|
if smsInfo.ErrorCount>=5{
|
|
|
err = common.NewErrorWithMsg(1011,"smscode over error times")
|
|
|
return
|
|
|
}
|
|
|
if smsInfo.LastTime+60*5<time.Now().Unix(){
|
|
|
err = common.NewErrorWithMsg(1009,"smscode expire")
|
|
|
goto Fail
|
|
|
}
|
|
|
if smsInfo.Code == request.Code{
|
|
|
result = true
|
|
|
return
|
|
|
}else{
|
|
|
err = common.NewErrorWithMsg(1012,"smscode error")
|
|
|
goto Fail
|
|
|
}
|
|
|
Fail:
|
|
|
{
|
|
|
smsInfo.ErrorCount +=1
|
|
|
if err=redis.Hset(protocol.SmsLoginCode,request.Phone,common.AssertJson(smsInfo),-1);err!=nil{
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|