Files
2021-09-30 12:09:45 +08:00

93 lines
2.2 KiB
Go

package platform
import (
"SciencesServer/utils"
"errors"
"strings"
"time"
)
// 短信API
type Sms struct{}
type SmsParam struct {
Mobile []string `json:"mobile"`
Content string `json:"content"`
}
type SmsSendMode int
const (
// SmsSendModeForAlone 常规单独发送
SmsSendModeForAlone SmsSendMode = iota + 1
// SmsSendModeForGroup 常规群发
SmsSendModeForGroup
// SmsSendModeForVariable 变量发送
SmsSendModeForVariable
)
const (
smsSendURLForAlone string = "http://122.144.203.27:8001/mt.ashx"
smsSendURLForGroup string = "http://122.144.203.27:8001/mts.ashx"
smsSendURLForVariable string = "http://122.144.203.27:8001/mts_var_json.ashx"
)
const (
keyForAccount string = "113934"
keyForPassword string = "zytDuehC"
)
type SmsSend func(mode SmsSendMode, params *SmsParam) error
var smsSendModeHandle = map[SmsSendMode]func(params *SmsParam) (string, utils.Method, map[string]interface{}){
SmsSendModeForAlone: alone,
SmsSendModeForGroup: group,
}
func alone(params *SmsParam) (string, utils.Method, map[string]interface{}) {
return smsSendURLForAlone, utils.MethodForGet, map[string]interface{}{
"msg": params.Content, "pn": params.Mobile[0],
}
}
func group(params *SmsParam) (string, utils.Method, map[string]interface{}) {
return smsSendURLForGroup, utils.MethodForPost, map[string]interface{}{
"msg": params.Content, "pn": strings.Join(params.Mobile, ","),
}
}
func (this *Sms) Send() SmsSend {
return func(mode SmsSendMode, params *SmsParam) error {
now := time.Now().Format("20060102150405")
_params := map[string]interface{}{
"account": keyForAccount,
"pswd": strings.ToUpper(utils.Md5String(keyForAccount + keyForPassword + now)),
"ts": now,
}
handle, has := smsSendModeHandle[mode]
if !has {
return errors.New("未知的短信模式")
}
url, method, parameter := handle(params)
if len(parameter) > 0 {
for k, v := range parameter {
_params[k] = v
}
}
client := utils.NewClient(url, method, _params)
_, err := client.Request(utils.RequestBodyFormatForFormData, utils.Headers{
ContentType: utils.RequestContentTypeForXWWWFormUrlencoded,
})
return err
}
}
func NewSms() *Sms {
return &Sms{}
}