-
Notifications
You must be signed in to change notification settings - Fork 15
/
common.go
137 lines (113 loc) · 3.39 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package common
import (
"context"
"fmt"
"github.com/iimeta/iim-client/internal/config"
"github.com/iimeta/iim-client/internal/consts"
"github.com/iimeta/iim-client/internal/dao"
"github.com/iimeta/iim-client/internal/errors"
"github.com/iimeta/iim-client/internal/model"
"github.com/iimeta/iim-client/internal/service"
"github.com/iimeta/iim-client/utility/logger"
"github.com/iimeta/iim-client/utility/redis"
)
type sCommon struct{}
func init() {
service.RegisterCommon(New())
}
func New() service.ICommon {
return &sCommon{}
}
// 发送短信验证码
func (s *sCommon) SmsCode(ctx context.Context, params model.SendSmsReq) (*model.SendSmsRes, error) {
if !config.Cfg.App.Debug {
defer func() {
val, _ := redis.Incr(ctx, fmt.Sprintf(consts.LOCK_CODE, params.Mobile))
if val == 1 {
_, _ = redis.Expire(ctx, fmt.Sprintf(consts.LOCK_CODE, params.Mobile), 30*60) // 锁定30分钟
}
}()
val, err := redis.GetInt(ctx, fmt.Sprintf(consts.LOCK_CODE, params.Mobile))
if err == nil && val >= 5 {
return nil, errors.New("发送验证码过于频繁, 请稍后再试")
}
}
switch params.Channel {
// 需要判断账号是否存在
case consts.CHANNEL_LOGIN, consts.CHANNEL_FORGET_ACCOUNT:
if !dao.User.IsAccountExist(ctx, params.Mobile) {
return nil, errors.New("账号不存在或密码错误")
}
// 需要判断账号是否存在
case consts.CHANNEL_REGISTER, consts.CHANNEL_CHANGE_MOBILE:
if dao.User.IsAccountExist(ctx, params.Mobile) {
return nil, errors.New("手机号已被他人使用")
}
default:
return nil, errors.New("发送异常")
}
// 发送短信验证码
code, err := service.Sms().Send(ctx, params.Channel, params.Mobile)
if err != nil {
logger.Error(ctx, err)
return nil, err
}
if config.Cfg.App.Debug {
return &model.SendSmsRes{
IsDebug: true,
SmsCode: code,
}, nil
}
return nil, nil
}
// 发送邮件验证码
func (s *sCommon) EmailCode(ctx context.Context, params model.SendEmailReq) (*model.SendEmailRes, error) {
if !config.Cfg.App.Debug {
defer func() {
val, _ := redis.Incr(ctx, fmt.Sprintf(consts.LOCK_CODE, params.Email))
if val == 1 {
_, _ = redis.Expire(ctx, fmt.Sprintf(consts.LOCK_CODE, params.Email), 30*60) // 锁定30分钟
}
}()
val, err := redis.GetInt(ctx, fmt.Sprintf(consts.LOCK_CODE, params.Email))
if err == nil && val >= 5 {
return nil, errors.New("发送验证码过于频繁, 请稍后再试")
}
}
switch params.Channel {
// 需要判断账号是否存在
case consts.CHANNEL_LOGIN:
if !dao.User.IsAccountExist(ctx, params.Email) {
return nil, errors.New("账号不存在或密码错误")
}
// 需要判断账号是否存在
case consts.CHANNEL_FORGET_ACCOUNT:
if !dao.User.IsAccountExist(ctx, params.Email) {
return nil, errors.New("账号不存在")
}
// 需要判断账号是否存在
case consts.CHANNEL_REGISTER, consts.CHANNEL_CHANGE_EMAIL:
if dao.User.IsAccountExist(ctx, params.Email) {
return nil, errors.New("邮箱已被他人使用")
}
default:
return nil, errors.New("发送异常")
}
// 发送邮件验证码
code, err := service.Email().Send(ctx, params.Channel, params.Email)
if err != nil {
logger.Error(ctx, err)
return nil, err
}
if config.Cfg.App.Debug {
return &model.SendEmailRes{
IsDebug: true,
Code: code,
}, nil
}
return nil, nil
}
// 公共设置
func (s *sCommon) Setting(ctx context.Context) error {
return nil
}