1
|
package auth
|
1
|
package auth
|
2
|
|
2
|
|
3
|
import (
|
3
|
import (
|
|
|
4
|
+ "bytes"
|
4
|
"fmt"
|
5
|
"fmt"
|
|
|
6
|
+ "github.com/astaxie/beego"
|
5
|
"gitlab.fjmaimaimai.com/mmm-go/ability/internal/repository"
|
7
|
"gitlab.fjmaimaimai.com/mmm-go/ability/internal/repository"
|
6
|
"gitlab.fjmaimaimai.com/mmm-go/ability/models"
|
8
|
"gitlab.fjmaimaimai.com/mmm-go/ability/models"
|
7
|
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
|
9
|
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
|
8
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
|
10
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
|
9
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
11
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
12
|
+ "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis"
|
|
|
13
|
+ s_sms "gitlab.fjmaimaimai.com/mmm-go/ability/services/sms"
|
|
|
14
|
+ comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
|
|
|
15
|
+ "html/template"
|
10
|
"strings"
|
16
|
"strings"
|
|
|
17
|
+ "encoding/json"
|
|
|
18
|
+ "time"
|
11
|
)
|
19
|
)
|
12
|
|
20
|
|
13
|
type IAuthService interface {
|
21
|
type IAuthService interface {
|
|
@@ -28,7 +36,7 @@ func assertImplement(){ |
|
@@ -28,7 +36,7 @@ func assertImplement(){ |
28
|
|
36
|
|
29
|
var(
|
37
|
var(
|
30
|
//服务
|
38
|
//服务
|
31
|
- //sms s_sms.ISmsService = &s_sms.SmsService{}
|
39
|
+ sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
|
32
|
|
40
|
|
33
|
//仓储
|
41
|
//仓储
|
34
|
UserRepository repository.IUserRepository =&repository.UserRepository{}
|
42
|
UserRepository repository.IUserRepository =&repository.UserRepository{}
|
|
@@ -39,6 +47,7 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe |
|
@@ -39,6 +47,7 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe |
39
|
var (
|
47
|
var (
|
40
|
user *models.Users
|
48
|
user *models.Users
|
41
|
userInfo *models.UserInfo
|
49
|
userInfo *models.UserInfo
|
|
|
50
|
+ result bool
|
42
|
)
|
51
|
)
|
43
|
user,err =UserRepository.GetUsersByMobile(request.Phone)
|
52
|
user,err =UserRepository.GetUsersByMobile(request.Phone)
|
44
|
if err!=nil{
|
53
|
if err!=nil{
|
|
@@ -56,7 +65,11 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe |
|
@@ -56,7 +65,11 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe |
56
|
}
|
65
|
}
|
57
|
break
|
66
|
break
|
58
|
case protocol.LoginSmdcode:
|
67
|
case protocol.LoginSmdcode:
|
|
|
68
|
+ if result,err=CheckSmsCode(request);result && err==nil{
|
59
|
goto Success
|
69
|
goto Success
|
|
|
70
|
+ }else{
|
|
|
71
|
+ return
|
|
|
72
|
+ }
|
60
|
default:
|
73
|
default:
|
61
|
err =fmt.Errorf("grantType error")
|
74
|
err =fmt.Errorf("grantType error")
|
62
|
return
|
75
|
return
|
|
@@ -168,5 +181,103 @@ func (s *AuthService)CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol |
|
@@ -168,5 +181,103 @@ func (s *AuthService)CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol |
168
|
}
|
181
|
}
|
169
|
//短信验证码
|
182
|
//短信验证码
|
170
|
func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error){
|
183
|
func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error){
|
171
|
- return nil,nil
|
184
|
+ var(
|
|
|
185
|
+ value,key,msgContent string
|
|
|
186
|
+ smsInfo *protocol.SmsInfo
|
|
|
187
|
+ )
|
|
|
188
|
+ msgContent = `【买买买信息科技】{{.Code}}({{.AppName}}手机验证码,请完成验证),如非本人操作,请忽略本短信`
|
|
|
189
|
+ switch request.SendType {
|
|
|
190
|
+ case protocol.SmsLoginCode:
|
|
|
191
|
+ case protocol.SmsChangeMobile:
|
|
|
192
|
+ default:
|
|
|
193
|
+ err = common.NewErrorWithMsg(2,"send_type error.")
|
|
|
194
|
+ return
|
|
|
195
|
+ }
|
|
|
196
|
+ key = request.SendType
|
|
|
197
|
+ //check user phone exists
|
|
|
198
|
+ if !redis.Hexists(key,request.Phone){
|
|
|
199
|
+ smsInfo = &protocol.SmsInfo{
|
|
|
200
|
+ CreateTime:time.Now().Unix(),
|
|
|
201
|
+ }
|
|
|
202
|
+ goto Send
|
|
|
203
|
+ }else{
|
|
|
204
|
+ if value,err =redis.Hget(key,request.Phone);err!=nil{
|
|
|
205
|
+ log.Error(err)
|
|
|
206
|
+ return
|
|
|
207
|
+ }
|
|
|
208
|
+ if err=json.Unmarshal([]byte(value),&smsInfo);err!=nil{
|
|
|
209
|
+ log.Error(err)
|
|
|
210
|
+ return
|
|
|
211
|
+ }
|
|
|
212
|
+ //第二天重置
|
|
|
213
|
+ if smsInfo.LastTime<comm_time.GetUnixTimeByYyyymmdd(){
|
|
|
214
|
+ smsInfo.Count=0
|
|
|
215
|
+ smsInfo.CreateTime = time.Now().Unix()
|
|
|
216
|
+ }
|
|
|
217
|
+ if smsInfo.Count>100{//TODO:limit send time
|
|
|
218
|
+ return
|
|
|
219
|
+ }
|
|
|
220
|
+ goto Send
|
|
|
221
|
+ }
|
|
|
222
|
+ Send:
|
|
|
223
|
+ {
|
|
|
224
|
+ smsInfo.Code = common.RandomStringWithChars(6,string(protocol.Nums))
|
|
|
225
|
+ smsInfo.LastTime=time.Now().Unix()
|
|
|
226
|
+ smsInfo.ErrorCount =0
|
|
|
227
|
+ //Todo Lock
|
|
|
228
|
+ smsInfo.Count +=1
|
|
|
229
|
+ if err=redis.Hset(key,request.Phone,common.AssertJson(smsInfo),-1);err!=nil{
|
|
|
230
|
+ return
|
|
|
231
|
+ }
|
|
|
232
|
+ tp :=template.New("sms_code")
|
|
|
233
|
+ tp.Parse(msgContent)
|
|
|
234
|
+ buf :=bytes.NewBuffer(nil)
|
|
|
235
|
+ tp.Execute(
|
|
|
236
|
+ buf,
|
|
|
237
|
+ map[string]string{
|
|
|
238
|
+ "Code":smsInfo.Code,
|
|
|
239
|
+ "AppName":beego.BConfig.AppName,
|
|
|
240
|
+ },)
|
|
|
241
|
+ request.Content = buf.String()
|
|
|
242
|
+ err = sms.Send(request)
|
|
|
243
|
+ }
|
|
|
244
|
+ return
|
|
|
245
|
+}
|
|
|
246
|
+//验证短信验证码
|
|
|
247
|
+func CheckSmsCode(request *protocol.LoginRequest)(result bool,err error){
|
|
|
248
|
+ var(
|
|
|
249
|
+ value string
|
|
|
250
|
+ smsInfo *protocol.SmsInfo
|
|
|
251
|
+ )
|
|
|
252
|
+ result =false
|
|
|
253
|
+ if value,err =redis.Hget(protocol.SmsLoginCode,request.Phone);err!=nil{
|
|
|
254
|
+ err = common.NewErrorWithMsg(1009,"smscode expire")
|
|
|
255
|
+ return
|
|
|
256
|
+ }
|
|
|
257
|
+ if err=json.Unmarshal([]byte(value),&smsInfo);err!=nil{
|
|
|
258
|
+ return
|
|
|
259
|
+ }
|
|
|
260
|
+ if smsInfo.ErrorCount>=5{
|
|
|
261
|
+ err = common.NewErrorWithMsg(1011,"smscode over error times")
|
|
|
262
|
+ return
|
|
|
263
|
+ }
|
|
|
264
|
+ if smsInfo.LastTime+60*5<time.Now().Unix(){
|
|
|
265
|
+ err = common.NewErrorWithMsg(1009,"smscode expire")
|
|
|
266
|
+ goto Fail
|
|
|
267
|
+ }
|
|
|
268
|
+ if smsInfo.Code == request.Code{
|
|
|
269
|
+ result = true
|
|
|
270
|
+ return
|
|
|
271
|
+ }else{
|
|
|
272
|
+ err = common.NewErrorWithMsg(1012,"smscode error")
|
|
|
273
|
+ goto Fail
|
|
|
274
|
+ }
|
|
|
275
|
+ Fail:
|
|
|
276
|
+ {
|
|
|
277
|
+ smsInfo.ErrorCount +=1
|
|
|
278
|
+ if err=redis.Hset(protocol.SmsLoginCode,request.Phone,common.AssertJson(smsInfo),-1);err!=nil{
|
|
|
279
|
+ return
|
|
|
280
|
+ }
|
|
|
281
|
+ }
|
|
|
282
|
+ return
|
172
|
} |
283
|
} |