ronglian.go 2.9 KB

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