ronglian.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package thirdparty
  4. import (
  5. "bytes"
  6. "crypto/md5"
  7. "encoding/base64"
  8. "encoding/hex"
  9. "encoding/xml"
  10. "io/ioutil"
  11. "net/http"
  12. "smart-thirdparty/errors"
  13. "smart-thirdparty/parser"
  14. "strings"
  15. "time"
  16. "github.com/jaryhe/gopkgs/logger"
  17. jsoniter "github.com/json-iterator/go"
  18. "go.uber.org/zap"
  19. )
  20. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  21. type messageCodeResult struct {
  22. StatusCode string `xml:"statusCode"`
  23. StatusMsg string `xml:"statusMsg"`
  24. templateSMS `xml:"TemplateSMS"`
  25. }
  26. type templateSMS struct {
  27. SmsMessageSid string `xml:"smsMessageSid"`
  28. DateCreated string `xml:"dateCreated"`
  29. }
  30. //构建容联云短信验证码请求体
  31. func SendTemplateSMS(to string, data []interface{}, templateId int) (err error) {
  32. var result *messageCodeResult
  33. rl := parser.Conf.ThirdParty.RongLian
  34. sendBody := map[string]interface{}{
  35. "to": to,
  36. "templateId": templateId,
  37. "appId": rl.RongLianAppId,
  38. "datas": data,
  39. }
  40. //获取时间戳 yyyymmddHHiiss
  41. signTime := time.Now().Format("20060102150405")
  42. //签名生成(大写)
  43. sign := strings.ToUpper(md5Encrypt(rl.RongLianSid + rl.RongLianToken + signTime))
  44. url := "https://" + rl.RongLianServerIp + ":" + rl.RongLianServerPort + "/" + rl.RongLianSoftVersion + "/Accounts/" + rl.RongLianSid + "/SMS/TemplateSMS?sig=" + sign
  45. //生成验证header
  46. authen := base64.URLEncoding.EncodeToString([]byte(rl.RongLianSid + ":" + signTime))
  47. //请求数据 xml
  48. xmlResult, err := vcodeHttpPost(url, sendBody, authen)
  49. if err != nil {
  50. prams, _ := json.MarshalToString(sendBody)
  51. logger.Error("thirdpary",
  52. zap.String("call", "vcodeHttpPost"),
  53. zap.String("params", prams),
  54. zap.String("error", err.Error()))
  55. return errors.VCodeSendFailed
  56. }
  57. err = xml.Unmarshal(xmlResult, &result)
  58. if result != nil && result.StatusCode == "000000" {
  59. return nil
  60. } else {
  61. prams, _ := json.MarshalToString(sendBody)
  62. logger.Error("thirdpary",
  63. zap.String("call", "vcodeHttpPost"),
  64. zap.String("params", prams),
  65. zap.String("error", result.StatusMsg))
  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. }