-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_sms.go
47 lines (40 loc) · 1.29 KB
/
a_sms.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
package pipe
import (
"context"
"github.com/kataras/iris/v12"
"github.com/pkg/errors"
)
// 短信相关
type SmsPipe struct {
Mobile string `json:"mobile,omitempty" bson:"mobile,omitempty"`
TemplateId string `json:"template_id,omitempty" bson:"template_id,omitempty"`
Code string `json:"code,omitempty" bson:"code,omitempty"`
}
var (
// SmsSend 短信发送
// 必传params SmsPipe
// 必传db SmsClient 的实例
SmsSend = &RunnerContext[any, *SmsPipe, *SmsClient, string]{
Name: "短信验证码发送",
Key: "sms_send",
call: func(ctx iris.Context, origin any, params *SmsPipe, db *SmsClient, more ...any) *RunResp[string] {
code, err := db.SendBeforeCheck(ctx, params.TemplateId, params.Mobile)
return NewPipeResultErr(code, err)
},
}
// SmsValid 短信验证码验证
// 必传params SmsPipe
// 必传db SmsClient 的实例
SmsValid = &RunnerContext[any, *SmsPipe, *SmsClient, bool]{
Name: "短信验证码验证",
Key: "sms_valid",
call: func(ctx iris.Context, origin any, params *SmsPipe, db *SmsClient, more ...any) *RunResp[bool] {
pass := db.Valid(ctx, params.Mobile, params.Code)
if !pass {
return NewPipeErr[bool](errors.New("短信验证码验证失败"))
}
db.DelKey(context.TODO(), params.Mobile)
return NewPipeResult(pass)
},
}
)