phone_code.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Package sms
  2. /**
  3. * @ File:
  4. * @ Date: 2021/5/21 11:12
  5. * @ Author: JYQ
  6. * @ Description:
  7. */
  8. package sms
  9. import (
  10. "crypto/md5"
  11. "encoding/hex"
  12. "encoding/json"
  13. "fmt"
  14. "io/ioutil"
  15. "net/http"
  16. pb "sportfitness/base/api/grpc/base"
  17. "sportfitness/base/assembly/base/repository/postgre"
  18. "strings"
  19. "git.beswell.com/gframe/application"
  20. )
  21. type Iyoogo struct {
  22. station string
  23. appKey string
  24. sign string
  25. signName string
  26. }
  27. func NewShortMsgIyoogo() *Iyoogo {
  28. dao := &Iyoogo{
  29. station: "http://sms.iyoogo.com/itf5",
  30. appKey: "07e54cc3bec2fc46adad77486ef857a9",
  31. //signName: "佰意兴科技",
  32. }
  33. SECRET := "fb25c484b5a7880ed25a0d303a5f654d"
  34. cry := md5.New()
  35. cry.Write([]byte(SECRET + dao.appKey + SECRET))
  36. dao.sign = hex.EncodeToString(cry.Sum(nil))
  37. dao.sign = strings.ToUpper(dao.sign)
  38. return dao
  39. }
  40. func (i Iyoogo) SendCode(vf *postgre.VCode, templateCode, sName string) {
  41. var smsParam []byte
  42. var err error
  43. switch vf.CodeType {
  44. case pb.VerifyType_SignIn:
  45. smsParam, err = json.Marshal(map[string]string{
  46. "code": vf.Code,
  47. })
  48. if err != nil {
  49. return
  50. }
  51. }
  52. url := fmt.Sprintf(
  53. "%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",
  54. i.station, i.appKey, i.sign, sName, smsParam, vf.Account, templateCode)
  55. resp, err := http.Get(url)
  56. if err != nil {
  57. err = application.ErrorBusinessF(application.ErrorCodeInternal, err.Error())
  58. panic(err)
  59. }
  60. defer resp.Body.Close()
  61. body, err := ioutil.ReadAll(resp.Body)
  62. if err != nil {
  63. err = application.ErrorBusinessF(application.ErrorCodeInternal, err.Error())
  64. panic(err)
  65. }
  66. type Result struct {
  67. ErrCode string `json:"err_code"`
  68. Success bool `json:"success"`
  69. Msg string `json:"msg"`
  70. }
  71. type Resp struct {
  72. Result Result `json:"result"`
  73. }
  74. respStruct := &Resp{}
  75. err = json.Unmarshal(body, respStruct)
  76. if err != nil {
  77. err = application.ErrorBusinessF(application.ErrorCodeInternal, err.Error()+"|resp string:"+string(body))
  78. panic(err)
  79. }
  80. if !respStruct.Result.Success {
  81. err = application.ErrorBusinessF(application.ErrorCodeInternal, respStruct.Result.Msg)
  82. panic(err)
  83. }
  84. }
  85. type PhoneCode struct {
  86. }
  87. func (p PhoneCode) SendCode(vf *postgre.VCode, templateCode, sName string) {
  88. iyoogo := NewShortMsgIyoogo()
  89. iyoogo.SendCode(vf, templateCode, sName)
  90. }