123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package util
- import (
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- "net/url"
- "strings"
- "time"
- )
- type SMSManager struct {
- username string
- password string
- apikey string
- }
- func NewSMSManager(username string, password string, apikey string) *SMSManager {
- return &SMSManager{
- username: username,
- password: password,
- apikey: apikey,
- }
- }
- var defaultSMSManager *SMSManager
- var internationalSMSManager *SMSManager
- var yingxiaoSMSManager *SMSManager
- func init() {
- defaultSMSManager = &SMSManager{
- username: "yundongmai",
- password: "asdf1234",
- apikey: "da06d4451dbcd1d08f4825c56d5d52e8",
- }
- internationalSMSManager = &SMSManager{
- username: "ydmgj",
- password: "asdf1234",
- apikey: "9dede1f8011b4d2dddcbaf69c893b4bb",
- }
- yingxiaoSMSManager = &SMSManager{
- username: "ydmyx",
- password: "asdf1234",
- apikey: "83526ef70935b1a50b8f377d6120b4d4",
- }
- }
- const SMS_API_URL = "http://m.5c.com.cn/api/send/index.php"
- func SendSMS(mobile string, content string) error {
- return defaultSMSManager.Send(mobile, content)
- }
- func SendWorldSMS(mobile string, content string) error {
- return internationalSMSManager.Send(mobile, content)
- }
- func SendYXSms(mobile string, content string) error {
- return yingxiaoSMSManager.Send(mobile, content)
- }
- func SendSMSByGet(mobile string, content string) error {
- return defaultSMSManager.SendByGet(mobile, content)
- }
- func (sms *SMSManager) Send(mobile string, content string) error {
- var params = map[string]interface{}{
- "type": "send",
- "username": sms.username,
- "password": sms.password,
- "apikey": sms.apikey,
- "mobile": mobile,
- "content": content,
- "encode": "utf-8",
- }
- jsonBytes, err := json.Marshal(params)
- if err != nil {
- return err
- }
- jsonBytes = bytes.Replace(jsonBytes, []byte("\\u0026"), []byte("&"), -1)
- jsonStr := string(jsonBytes)
- var sparams = url.Values{}
- sparams.Add("format", "json")
- sparams.Add("data", jsonStr)
- jsonStr = sparams.Encode()
- res, err := http.Post(SMS_API_URL, "application/x-www-form-urlencoded", strings.NewReader(jsonStr))
- if err != nil {
- return err
- }
- defer res.Body.Close()
- jsonBytes, err = ioutil.ReadAll(res.Body)
- if err != nil {
- return err
- }
- result := make(map[string]string)
- err = json.Unmarshal(jsonBytes, &result)
- if err != nil {
- log.Println(err, string(jsonBytes))
- return err
- }
- if result["status"] != "success" {
- return errors.New(result["msg"])
- }
- return nil
- }
- func (sms *SMSManager) SendByGet(mobile string, content string) error {
- var sparams = url.Values{
- "username": []string{sms.username},
- "password": []string{sms.password},
- "apikey": []string{sms.apikey},
- "mobile": []string{mobile},
- "content": []string{content},
- "startTime": []string{fmt.Sprintf("%d", time.Now().Unix())},
- "encode": []string{"utf-8"},
- }
- paramStr := sparams.Encode()
- res, err := http.Get(SMS_API_URL + "?" + paramStr)
- if err != nil {
- return err
- }
- defer res.Body.Close()
- jsonBytes, err := ioutil.ReadAll(res.Body)
- if err != nil {
- return err
- }
- result := string(jsonBytes)
- if !strings.HasPrefix(result, "success:") {
- return errors.New(result)
- }
- return nil
- }
|