/* @Time : 2019-11-05 16:41 @Author : zr */ package short_msg import ( "crypto/md5" "encoding/hex" "encoding/json" "fmt" "io/ioutil" "net/http" "strings" "video_course/errors" "video_course/model" ) type Iyoogo struct { station string appKey string sign string signName string } func NewShortMsgIyoogo() *Iyoogo { dao := &Iyoogo{ station: "http://sms.iyoogo.com/itf2", appKey: "f736bcf2429d08457eee83527e871069", signName: "小飞龙", } SECRET := "746c3978d0dd8696a1ffe7a9a9fa7a2b" cry := md5.New() cry.Write([]byte(SECRET + dao.appKey + SECRET)) dao.sign = hex.EncodeToString(cry.Sum(nil)) dao.sign = strings.ToUpper(dao.sign) return dao } func (i Iyoogo) SendVFCode(phone, code string, codeType model.VFCodeCodeTypeEnum) { var ( smsParam []byte err error ) templateCode := "" switch codeType { case model.VFCodeCodeTypeLogin: smsParam, err = json.Marshal(map[string]string{ "code": code, "product": i.signName, }) if err != nil { panic(errors.NewServiceErr(errors.SmsErr, err.Error())) } templateCode = "42" } url := fmt.Sprintf( "%s/?method=smssend&app_key=%s&sign=%s&v=1.0&sms_free_sign_name=%s&sms_param=%s&rec_num=%s&sms_template_code=%s&sms_type=normal", i.station, i.appKey, i.sign, i.signName, smsParam, phone, templateCode) resp, err := http.Get(url) if err != nil { panic(errors.NewServiceErr(errors.SmsErr, err.Error())) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { panic(errors.NewServiceErr(errors.SmsErr, err.Error())) } type Result struct { ErrCode string `json:"err_code"` Success bool `json:"success"` Msg string `json:"msg"` } type Resp struct { Result Result `json:"result"` } respStruct := &Resp{} err = json.Unmarshal(body, respStruct) if err != nil { panic(errors.NewServiceErr(errors.SmsErr, err.Error()+"|resp string:"+string(body))) } if !respStruct.Result.Success { panic(errors.NewServiceErr(errors.SmsErr, respStruct.Result.Msg)) } return }