ronglian.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package thirdparty
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "gd_management/common.in/config"
  6. "gd_management/errors"
  7. "encoding/base64"
  8. "encoding/hex"
  9. "encoding/json"
  10. "encoding/xml"
  11. "io/ioutil"
  12. "net/http"
  13. "strings"
  14. "time"
  15. "go.uber.org/zap"
  16. )
  17. type messageCodeResult struct {
  18. StatusCode string `xml:"statusCode"`
  19. StatusMsg string `xml:"statusMsg"`
  20. templateSMS `xml:"TemplateSMS"`
  21. }
  22. type templateSMS struct {
  23. SmsMessageSid string `xml:"smsMessageSid"`
  24. DateCreated string `xml:"dateCreated"`
  25. }
  26. //构建容联云短信验证码请求体
  27. func SendTemplateSMS(to string, data []interface{}, templateId int, rl config.RongLianNode) (err error) {
  28. var result *messageCodeResult
  29. sendBody := map[string]interface{}{
  30. "to": to,
  31. "templateId": templateId,
  32. "appId": rl.RongLianAppId,
  33. "datas": data,
  34. }
  35. //获取时间戳 yyyymmddHHiiss
  36. signTime := time.Now().Format("20060102150405")
  37. //签名生成(大写)
  38. sign := strings.ToUpper(md5Encrypt(rl.RongLianSid + rl.RongLianToken + signTime))
  39. url := "https://" + rl.RongLianServerIp + ":" + rl.RongLianServerPort + "/" + rl.RongLianSoftVersion + "/Accounts/" + rl.RongLianSid + "/SMS/TemplateSMS?sig=" + sign
  40. //生成验证header
  41. authen := base64.URLEncoding.EncodeToString([]byte(rl.RongLianSid + ":" + signTime))
  42. //请求数据 xml
  43. xmlResult, err := vcodeHttpPost(url, sendBody, authen)
  44. if err != nil {
  45. prams, _ := json.Marshal(sendBody)
  46. l.Error("thirdpary",
  47. zap.String("call", "vcodeHttpPost"),
  48. zap.String("params", string(prams)),
  49. zap.String("error", err.Error()))
  50. return errors.VCodeSendFailed
  51. }
  52. err = xml.Unmarshal(xmlResult, &result)
  53. if result != nil && result.StatusCode == "000000" {
  54. return nil
  55. } else {
  56. if result.StatusCode == "160040" {
  57. return errors.VCodeOutOfLimit
  58. }
  59. prams, _ :=json.Marshal(sendBody)
  60. //prams, _ := json.MarshalToString(sendBody)
  61. s, _ := json.Marshal(result)
  62. l.Error("thirdpary",
  63. zap.String("call", "vcodeHttpPost"),
  64. zap.String("params", string(prams)),
  65. zap.String("result", string(s)))
  66. return errors.VCodeSendFailed
  67. }
  68. }
  69. //请求第三方接口
  70. func vcodeHttpPost(url string, data map[string]interface{}, authen string) (body []byte, err error) {
  71. bytesData, err := json.Marshal(data)
  72. if err != nil {
  73. return nil, err
  74. }
  75. reader := bytes.NewReader(bytesData)
  76. request, err := http.NewRequest("POST", url, reader)
  77. if err != nil {
  78. return nil, err
  79. }
  80. request.Header.Set("Content-Type", "application/json;charset=UTF-8")
  81. request.Header.Set("Authorization", authen)
  82. client := http.Client{}
  83. resp, err := client.Do(request)
  84. if err != nil {
  85. return nil, err
  86. }
  87. defer resp.Body.Close()
  88. respBytes, err := ioutil.ReadAll(resp.Body)
  89. if err != nil {
  90. return nil, err
  91. }
  92. return respBytes, nil
  93. }
  94. func md5Encrypt(str string) string {
  95. h := md5.New()
  96. h.Write([]byte(str))
  97. return hex.EncodeToString(h.Sum(nil))
  98. }