github.com/gorocks/apikit@v0.0.0-20170808011444-396ab031afbd/sms/alidayu/alidayu.go (about) 1 package alidayu 2 3 import ( 4 "bytes" 5 "crypto/md5" 6 "encoding/json" 7 "fmt" 8 "net/http" 9 "sort" 10 "time" 11 12 simplejson "github.com/bitly/go-simplejson" 13 "github.com/douglarek/apikit" 14 "github.com/fatih/structs" 15 "github.com/imdario/mergo" 16 ) 17 18 const url = "https://eco.taobao.com/router/rest" 19 20 // Alidayu handles communication with related methods of the Alidayu API. 21 type Alidayu struct { 22 client *apikit.Client 23 } 24 25 // New ... 26 func New(httpClient *http.Client) *Alidayu { 27 c := apikit.NewClient(httpClient) 28 return &Alidayu{client: c} 29 } 30 31 // Config the alidayu configuration. 32 type Config struct { 33 Method string `json:"method,omitempty" structs:"method"` 34 AppKey string `json:"app_key,omitempty" structs:"app_key"` 35 Timestamp string `json:"timestamp,omitempty" structs:"timestamp"` 36 Format string `json:"format,omitempty" structs:"format"` 37 Version string `json:"v,omitempty" structs:"v"` 38 PartnerID string `json:"partner_id,omitempty" structs:"partner_id"` 39 SignMethod string `json:"sign_method,omitempty" structs:"sign_method"` 40 Sign string `json:"sign,omitempty" structs:"sign"` 41 Extend string `json:"extend,omitempty" structs:"extend"` 42 SmsType string `json:"sms_type,omitempty" structs:"sms_type"` 43 SmsFreeSignName string `json:"sms_free_sign_name,omitempty" structs:"sms_free_sign_name"` 44 SmsParam string `json:"sms_param,omitempty" structs:"sms_param"` 45 RecNum string `json:"rec_num,omitempty" structs:"rec_num"` 46 SmsTemplateCode string `json:"sms_template_code,omitempty" structs:"sms_template_code"` 47 } 48 49 // DefaultConfig returns the default alidayu configuration. 50 func DefaultConfig() Config { 51 localLoc, _ := time.LoadLocation("Asia/Chongqing") 52 return Config{ 53 Format: "json", 54 Method: "alibaba.aliqin.fc.sms.num.send", 55 SignMethod: "md5", 56 Timestamp: time.Now().In(localLoc).Format("2006-01-02 15:04:05"), 57 Version: "2.0", 58 PartnerID: "apidoc", 59 Extend: "123456", 60 SmsType: "normal"} 61 } 62 63 // Merge merges the default with the given config and returns the result. 64 func (c Config) Merge(cfg Config) (config Config) { 65 config = cfg 66 mergo.Merge(&config, c) 67 return 68 } 69 70 // Sign signs an Alidayu request struct. 71 func (a *Alidayu) Sign(s interface{}, secret []byte) string { 72 m := apikit.Params(structs.Map(s)) 73 delete(m, "sign") 74 keys := make([]string, 0, len(m)) 75 for k := range m { 76 keys = append(keys, k) 77 } 78 sort.Strings(keys) 79 80 var buf bytes.Buffer 81 for _, k := range keys { 82 buf.WriteString(k) 83 buf.WriteString(m[k]) 84 } 85 86 return encrypt(buf.Bytes(), secret) 87 } 88 89 func encrypt(s, secret []byte) (h string) { 90 d := make([]byte, 0, len(s)+2*len(secret)) 91 d = append(d, secret...) 92 d = append(d, s...) 93 d = append(d, secret...) 94 return fmt.Sprintf("%X", md5.Sum(d)) 95 } 96 97 // SendSms sends a sms. 98 func (a *Alidayu) SendSms(c Config) (map[string]interface{}, error) { 99 req, err := a.client.NewRequest("POST", url, &c) 100 if err != nil { 101 return nil, err 102 } 103 res := map[string]interface{}{} 104 if _, err := a.client.Do(req, &res); err != nil { 105 return nil, err 106 } 107 return res, nil 108 } 109 110 // SmsResult judges a sms sent ok or not. 111 func SmsResult(m map[string]interface{}) (bool, error) { 112 b, err := json.Marshal(m) 113 if err != nil { 114 return false, err 115 } 116 j, err := simplejson.NewJson(b) 117 if err != nil { 118 return false, err 119 } 120 return j.Get("alibaba_aliqin_fc_sms_num_send_response").Get("result").Get("success").Bool() 121 }