短信发送方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.0 KiB

  1. package sms
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "git.tetele.net/tgo/helper"
  7. "io/ioutil"
  8. "log"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "github.com/json-iterator/go"
  13. "github.com/tjfoc/gmsm/sm3"
  14. )
  15. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  16. // 密码加密
  17. func Md5Str(str ...string) string {
  18. var build strings.Builder
  19. if len(str) > 0 {
  20. for _, v := range str {
  21. build.WriteString(v)
  22. }
  23. } else {
  24. return ""
  25. }
  26. h := md5.New()
  27. h.Write([]byte(build.String())) // 需要加密的字符串
  28. cipher2Str := h.Sum(nil)
  29. sMd5 := hex.EncodeToString(cipher2Str) // 输出加密结果
  30. return sMd5
  31. }
  32. /*
  33. * 连接多个字符串
  34. */
  35. func StringJoin(s ...string) string {
  36. var build strings.Builder
  37. if len(s) > 0 {
  38. for _, v := range s {
  39. build.WriteString(v)
  40. }
  41. }
  42. return build.String()
  43. }
  44. /**
  45. * post 请求
  46. */
  47. func PostJsonData(url string, param interface{}, header ...map[string]string) ([]byte, error) {
  48. httpClient := &http.Client{}
  49. params, err := json.Marshal(param)
  50. if err != nil {
  51. return []byte(""), err
  52. }
  53. req, err := http.NewRequest("POST", url, bytes.NewBuffer(params))
  54. if err != nil {
  55. return []byte(""), err
  56. }
  57. req.Header.Set("Content-Type", "application/json")
  58. if len(header) > 0 {
  59. for _, item := range header {
  60. for k, v := range item {
  61. req.Header[k] = []string{v}
  62. }
  63. }
  64. }
  65. resp, err := httpClient.Do(req)
  66. if err != nil {
  67. return []byte(""), err
  68. }
  69. defer resp.Body.Close()
  70. body, err := ioutil.ReadAll(resp.Body)
  71. if err != nil {
  72. return []byte(""), err
  73. }
  74. return body, nil
  75. }
  76. // ums验签
  77. func UmsSetSign(data, header map[string]string,secretKey string) string {
  78. str := helper.HttpBuildQuery(data)
  79. // url encode
  80. str = url.QueryEscape(str)
  81. str = header["x-sp-code"] + header["x-app-key"] + secretKey + str + header["x-request-id"]
  82. log.Println("加密前数据: ", str)
  83. // ms3加密
  84. hash := sm3.New()
  85. hash.Write([]byte(str))
  86. sign_byte := hash.Sum(nil)
  87. sign := hex.EncodeToString(sign_byte)
  88. log.Println("加密后数据: ", sign)
  89. return sign
  90. }