init: 1.0.0

This commit is contained in:
Chang lue Tsen
2025-04-25 12:08:29 +09:00
commit 8addcc584b
1031 changed files with 76472 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
package smsbao
import "fmt"
type Error int
const (
Success Error = iota
PasswordError
AccountNotFount
InsufficientBalance
IPAddressRestrictions
ContentContainsSensitiveWords
MobileNumberIsIncorrect
)
var errorDescriptions = map[Error]string{
Success: "Success",
PasswordError: "Password error",
AccountNotFount: "Account not found",
InsufficientBalance: "Insufficient balance",
IPAddressRestrictions: "IP address restrictions",
ContentContainsSensitiveWords: "Content contains sensitive words",
MobileNumberIsIncorrect: "Mobile number is incorrect",
}
var errorCodes = map[string]Error{
"0": Success,
"30": PasswordError,
"40": AccountNotFount,
"41": InsufficientBalance,
"43": IPAddressRestrictions,
"50": ContentContainsSensitiveWords,
"51": MobileNumberIsIncorrect,
}
func (e Error) String() string {
for k, v := range errorDescriptions {
if k == e {
return v
}
}
return "Unknown error"
}
func parseError(b []byte) error {
if e, ok := errorCodes[string(b)]; ok {
return e.Error()
}
return fmt.Errorf("unknown error")
}
func (e Error) Error() error {
if e == Success {
return nil
}
return fmt.Errorf("%s", e.String())
}
+67
View File
@@ -0,0 +1,67 @@
package smsbao
import (
"fmt"
"github.com/go-resty/resty/v2"
"github.com/perfect-panel/ppanel-server/pkg/templatex"
"github.com/perfect-panel/ppanel-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
}
+16
View File
@@ -0,0 +1,16 @@
package smsbao
import "testing"
func TestNewClient(t *testing.T) {
t.Skipf("Skip TestNewClient test")
client := NewClient(Config{
Template: "【XXX】您的验证码是:{{.code}},有效期 {{.expiration}}。请不要把验证码泄露给其他人。",
})
err := client.SendCode("1", "", "223322")
if err != nil {
t.Errorf("TestNewClient() error = %v", err.Error())
return
}
t.Logf("TestNewClient success")
}