You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.5 KiB
70 lines
1.5 KiB
package ytx
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"golang.org/x/text/encoding/simplifiedchinese"
|
|
"golang.org/x/text/transform"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
YtxSmsTypeCaptcha = "1" // 验证码
|
|
YtxSmsTypeMessage = "2" // 通知
|
|
)
|
|
|
|
var Sms = &sms{}
|
|
|
|
type sms struct {
|
|
url string
|
|
spCode string
|
|
loginName string
|
|
password string
|
|
}
|
|
|
|
// InitSms @Title 初始化
|
|
func InitSms(url ,spCode, loginName, password string) *sms {
|
|
Sms.url = url
|
|
Sms.spCode = Sms.encode(spCode)
|
|
Sms.loginName = Sms.encode(loginName)
|
|
Sms.password = Sms.encode(password)
|
|
return Sms
|
|
}
|
|
|
|
// Send @Title 发送短信
|
|
func (s *sms) Send(phone, content, msgType string) error {
|
|
now := time.Now()
|
|
data := url.Values{
|
|
"SpCode": {s.spCode},
|
|
"LoginName": {s.loginName},
|
|
"Password": {s.password},
|
|
"MessageContent": {s.encode(content)},
|
|
"MessageType": {msgType},
|
|
"UserNumber": {phone},
|
|
"SerialNumber": {fmt.Sprintf("%s%.6d", now.Format("20060102150405"), now.UnixNano()/1e3-now.Unix()*1e6)},
|
|
"ScheduleTime": {now.Format("20060102150405")},
|
|
"ExtendAccessNum": {},
|
|
"f": {},
|
|
"AutographId": {},
|
|
}
|
|
bytes, err := request(get, fmt.Sprintf("%s?%s", s.url, data.Encode()), "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
query, err := url.ParseQuery(string(bytes))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if query.Get("result") == "0" {
|
|
return nil
|
|
}
|
|
return errors.New(query.Get("description"))
|
|
}
|
|
|
|
// @Title 编码转换
|
|
func (s *sms) encode(source string) string {
|
|
result, _, _ := transform.String(simplifiedchinese.GBK.NewEncoder(), source)
|
|
return result
|
|
}
|