sms_lib.go
1.4 KB
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
package smslib
import (
"context"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/gateway"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"net/http"
)
type SMSService struct {
gateway.Service
}
func (svc *SMSService) SendSmsCode(ctx context.Context, request RequestSendSmsCode) (*DataSendSmsCode, error) {
var result DataSendSmsCode
if err := svc.Do(ctx, "/service/sendSms", http.MethodPost, request, &result); err != nil {
return nil, err
}
return &result, nil
}
func (svc *SMSService) CheckSmsCode(ctx context.Context, request RequestCheckSmsCode) (*DataCheckSmsCode, error) {
var result DataCheckSmsCode
if err := svc.Do(ctx, "/service/checkSmsCode", http.MethodPost, request, &result); err != nil {
return nil, err
}
return &result, nil
}
func SendSmsCodeHandler(smsService SMSService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Phone string `json:"phone"`
}
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}
var (
err error
)
defer func() {
if err != nil {
logx.Error(err)
xerr.ErrorHandlerCtx(r.Context(), xerr.NewErrMsgErr(err.Error(), err))
}
}()
_, err = smsService.SendSmsCode(r.Context(), RequestSendSmsCode{
Phone: req.Phone,
})
if err != nil {
return
}
httpx.Ok(w)
}
}