Files
2026-10-26 16:28:14 +08:00

77 lines
1.8 KiB
Go

package fengniao
import (
"crypto/sha1"
"errors"
"fmt"
"net/http"
"videoweb/pub/common/httputil"
"videoweb/pub/common/log"
)
type requestBody struct {
MercId string `json:"mercId"`
Secret string `json:"secret"`
TemplateId string `json:"templateId"`
Code string `json:"smsCode"`
Mobile string `json:"mobile"`
SignType string `json:"signType"` // 固定sha1
Sign string `json:"sign"` // 签名
}
type msg struct {
Status string `json:"status"`
TaskId string `json:"taskId"`
}
type responseBody struct {
Code int `json:"code"`
Msg msg `json:"msg"`
Err string `json:"err"`
}
type FengNiao struct {
Url string
MercId string
Secret string
Tmpl string
}
// Send 发送, 目前蜂鸟貌似只支持数字
func (f *FengNiao) Send(mobile string, code string) error {
content := fmt.Sprintf(f.Tmpl, code)
data := fmt.Sprintf("mercId=%s&templateId=%s&smsCode=%s&mobile=%s&secret=%s", f.MercId, f.Tmpl, code, mobile, f.Secret)
sha := sha1.New()
_, err := sha.Write([]byte(data))
if err != nil {
log.Error("fengniao send error", log.Any("mobile", mobile), log.Any("content", content), log.E(err))
return err
}
body := &requestBody{
MercId: f.MercId,
Secret: f.Secret,
TemplateId: f.Tmpl,
Code: code,
Mobile: mobile,
SignType: "sha1",
Sign: fmt.Sprintf("%x", sha.Sum(nil)),
}
rp := responseBody{}
statusCode, err := httputil.Client().POSTJsonWithJResp(&rp, f.Url, nil, body)
if err != nil {
log.Error("fengniao POSTJsonWithResp err", log.E(err))
return err
}
if statusCode != http.StatusOK {
log.Error("fengniao status code not 200")
return errors.New("fengniao statusCode not 200")
}
if rp.Code != 200 {
log.Error("fengniao code not 200", log.Any("errdesc", rp.Err))
err = errors.New(rp.Err)
}
return err
}