httplib_sms_serve.go
4.0 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
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
138
139
140
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.SMS_SERVE_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
}
//SendSms 公共短信验证码服务 发送验证码
func (smsServe HttplibSmsServe) SendNoticeSms(phone string, tplId int, tplValues map[string]interface{}) error {
url := smsServe.baseUrL + "/service/sendNoticeSms"
method := "post"
req := smsServe.CreateRequest(url, method)
param := map[string]interface{}{
"phone": phone,
"tplId": tplId,
"tplValues": tplValues,
}
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 {
if code == constant.SMSCODE_ALL_POWER {
return nil
}
url := smsServe.baseUrL + "/service/checkSmsCode"
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
}