iyoogo.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. @Time : 2019-11-05 16:41
  3. @Author : zr
  4. */
  5. package short_msg
  6. import (
  7. "crypto/md5"
  8. "encoding/hex"
  9. "encoding/json"
  10. "fmt"
  11. "io/ioutil"
  12. "net/http"
  13. "strings"
  14. "video_course/errors"
  15. "video_course/model"
  16. )
  17. type Iyoogo struct {
  18. station string
  19. appKey string
  20. sign string
  21. signName string
  22. }
  23. func NewShortMsgIyoogo() *Iyoogo {
  24. dao := &Iyoogo{
  25. station: "http://sms.iyoogo.com/itf2",
  26. appKey: "f736bcf2429d08457eee83527e871069",
  27. signName: "小飞龙",
  28. }
  29. SECRET := "746c3978d0dd8696a1ffe7a9a9fa7a2b"
  30. cry := md5.New()
  31. cry.Write([]byte(SECRET + dao.appKey + SECRET))
  32. dao.sign = hex.EncodeToString(cry.Sum(nil))
  33. dao.sign = strings.ToUpper(dao.sign)
  34. return dao
  35. }
  36. func (i Iyoogo) SendVFCode(phone, code string, codeType model.VFCodeCodeTypeEnum) {
  37. var (
  38. smsParam []byte
  39. err error
  40. )
  41. templateCode := ""
  42. switch codeType {
  43. case model.VFCodeCodeTypeLogin:
  44. smsParam, err = json.Marshal(map[string]string{
  45. "code": code,
  46. "product": i.signName,
  47. })
  48. if err != nil {
  49. panic(errors.NewServiceErr(errors.SmsErr, err.Error()))
  50. }
  51. templateCode = "42"
  52. }
  53. url := fmt.Sprintf(
  54. "%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",
  55. i.station, i.appKey, i.sign, i.signName, smsParam, phone, templateCode)
  56. resp, err := http.Get(url)
  57. if err != nil {
  58. panic(errors.NewServiceErr(errors.SmsErr, err.Error()))
  59. }
  60. defer resp.Body.Close()
  61. body, err := ioutil.ReadAll(resp.Body)
  62. if err != nil {
  63. panic(errors.NewServiceErr(errors.SmsErr, err.Error()))
  64. }
  65. type Result struct {
  66. ErrCode string `json:"err_code"`
  67. Success bool `json:"success"`
  68. Msg string `json:"msg"`
  69. }
  70. type Resp struct {
  71. Result Result `json:"result"`
  72. }
  73. respStruct := &Resp{}
  74. err = json.Unmarshal(body, respStruct)
  75. if err != nil {
  76. panic(errors.NewServiceErr(errors.SmsErr, err.Error()+"|resp string:"+string(body)))
  77. }
  78. if !respStruct.Result.Success {
  79. panic(errors.NewServiceErr(errors.SmsErr, respStruct.Result.Msg))
  80. }
  81. return
  82. }