httplib_sms_serve.go
2.8 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
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
package sms_serve
import (
"encoding/json"
"fmt"
"time"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway"
)
//公共短信服务模块
type HttplibSmsServe struct {
service_gateway.BaseServiceGateway
baseUrL string
}
func NewHttplibHttplibSmsServe() *HttplibSmsServe {
return &HttplibSmsServe{
BaseServiceGateway: service_gateway.BaseServiceGateway{
ConnectTimeout: 100 * time.Second,
ReadWriteTimeout: 30 * time.Second,
},
baseUrL: constant.ALLIED_CREATION_USER_HOST,
}
}
//SendSms 公共短信验证码服务 发送验证码
func (smsServe HttplibSmsServe) SendSms(phone string) error {
url := smsServe.baseUrL + "/service/sendSms"
method := "post"
req := smsServe.CreateRequest(url, method)
param := map[string]string{
"phone": phone,
}
log.Logger.Debug("向公共短信验证码服务请求数据:短信验证码接口。", map[string]interface{}{
"api": method + ":" + url,
"param": param,
})
req, err := req.JSONBody(param)
if err != nil {
return fmt.Errorf("请求公共短信验证码服务失败:%w", err)
}
byteResult, err := req.Bytes()
if err != nil {
return fmt.Errorf("获取公共短信验证码服务失败:%w", err)
}
log.Logger.Debug("获取公共短信验证码服务请求数据", map[string]interface{}{
"result": string(byteResult),
})
var result service_gateway.GatewayResponse
err = json.Unmarshal(byteResult, &result)
if err != nil {
return fmt.Errorf("解析更新组织:%w", err)
}
if result.Code != 0 {
return fmt.Errorf(result.Msg)
}
return nil
}
//CheckSmsCode 公共短信验证码服务 校验验证码
func (smsServe HttplibSmsServe) CheckSmsCode(phone string, code string) error {
url := smsServe.baseUrL + "/service/sendSms"
method := "post"
req := smsServe.CreateRequest(url, method)
param := map[string]string{
"phone": phone,
"code": code,
}
log.Logger.Debug("向公共短信验证码服务请求数据:短信验证码接口。", map[string]interface{}{
"api": method + ":" + url,
"param": param,
})
req, err := req.JSONBody(param)
if err != nil {
return fmt.Errorf("请求公共短信验证码服务失败:%w", err)
}
byteResult, err := req.Bytes()
if err != nil {
return fmt.Errorf("获取公共短信验证码服务失败:%w", err)
}
log.Logger.Debug("获取公共短信验证码服务请求数据", map[string]interface{}{
"result": string(byteResult),
})
var result service_gateway.GatewayResponse
err = json.Unmarshal(byteResult, &result)
if err != nil {
return fmt.Errorf("解析更新组织:%w", err)
}
if result.Code != 0 {
return fmt.Errorf(result.Msg)
}
return nil
}