Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 6m27s
feat: 添加版本和构建时间变量 fix: 修正短信队列类型注释错误 style: 清理未使用的代码和测试文件 docs: 更新安装文档中的下载链接 chore: 迁移数据库脚本添加日志和订阅配置
68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package smsbao
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
"github.com/perfect-panel/server/pkg/templatex"
|
|
"github.com/perfect-panel/server/pkg/tool"
|
|
)
|
|
|
|
const BaseURL = "https://api.smsbao.com"
|
|
|
|
type Config struct {
|
|
Access string `json:"access"`
|
|
Secret string `json:"secret"`
|
|
Template string `json:"template"`
|
|
}
|
|
|
|
type Client struct {
|
|
config *Config
|
|
client *resty.Client
|
|
}
|
|
|
|
func NewClient(config Config) *Client {
|
|
client := resty.New()
|
|
client.SetBaseURL(BaseURL)
|
|
return &Client{
|
|
config: &config,
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
func (c *Client) SendCode(area, mobile, code string) error {
|
|
apiUrl := "/sms"
|
|
text, err := templatex.RenderToString(c.config.Template, map[string]interface{}{
|
|
"code": code,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to render sms template: %s", err.Error())
|
|
}
|
|
param := map[string]string{
|
|
"u": c.config.Access,
|
|
"p": tool.Md5Encode(c.config.Secret, false),
|
|
"m": mobile,
|
|
"c": text,
|
|
}
|
|
if area != "86" {
|
|
apiUrl = "/wsms"
|
|
param["m"] = fmt.Sprintf("+%s%s", area, mobile)
|
|
}
|
|
resp, err := c.client.R().SetQueryParams(param).Get(apiUrl)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = parseError(resp.Body())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) GetSendCodeContent(code string) string {
|
|
text, _ := templatex.RenderToString(c.config.Template, map[string]interface{}{
|
|
"code": code,
|
|
})
|
|
return text
|
|
}
|