附近小店
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

open_platform.go 3.3 KiB

2 månader sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package utils
  2. import (
  3. "applet/app/cfg"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "sort"
  8. "time"
  9. )
  10. // 调用开放平台的封装
  11. type OpenPlatformReqClient struct {
  12. AppKey string
  13. AppSecret string
  14. Method string
  15. Version string
  16. Timestamp string
  17. Nonce string
  18. Sign string
  19. BizData map[string]interface{}
  20. params map[string]string
  21. ReturnData ReturnDataResp
  22. }
  23. type ReturnDataResp struct {
  24. Code int `json:"code"`
  25. Msg string `json:"msg"`
  26. Data interface{} `json:"data"`
  27. }
  28. func NewOpenPlatformReqClient(appKey, method, version, appSecret string, bizData map[string]interface{}) (*OpenPlatformReqClient, error) {
  29. if appKey == "" || appSecret == "" || method == "" || version == "" {
  30. return nil, errors.New("appKey,method,version not allow empty")
  31. }
  32. nowStr := AnyToString(time.Now().Unix())
  33. nonce := UUIDString()
  34. return &OpenPlatformReqClient{
  35. AppKey: appKey,
  36. AppSecret: appSecret,
  37. Method: method,
  38. Version: version,
  39. Nonce: nonce,
  40. Timestamp: nowStr,
  41. params: map[string]string{"app_key": appKey, "method": method, "version": version, "timestamp": nowStr, "nonce": nonce},
  42. BizData: bizData,
  43. }, nil
  44. }
  45. func (client *OpenPlatformReqClient) CurlOpen() error {
  46. if client.params == nil {
  47. return errors.New("params not allow empty")
  48. }
  49. url := cfg.ZhiosOpen.URL + "/api/open/gw"
  50. fmt.Printf("%#v\n", string(Serialize(client.params)))
  51. resp, err := CurlPost(url, Serialize(client.params), nil)
  52. if err != nil {
  53. return err
  54. }
  55. err = json.Unmarshal(resp, &client.ReturnData)
  56. if err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. /*func (client *OpenPlatformReqClient) CreateParams() *OpenPlatformReqClient {
  62. client.params["timestamp"] = client.Timestamp
  63. client.params["nonce"] = client.Nonce
  64. //client.params["biz_data"] = SerializeStr(client.BizData)
  65. return client
  66. }*/
  67. func (client *OpenPlatformReqClient) SetBizDataToParams() *OpenPlatformReqClient {
  68. client.params["biz_data"] = SerializeStr(client.BizData)
  69. return client
  70. }
  71. func (client *OpenPlatformReqClient) SetParams(key, value string) *OpenPlatformReqClient {
  72. client.params[key] = value
  73. return client
  74. }
  75. func (client *OpenPlatformReqClient) GetParams(key string) string {
  76. return client.params[key]
  77. }
  78. func (client *OpenPlatformReqClient) CreateSign() *OpenPlatformReqClient {
  79. /*if client.BizData != nil {
  80. for key := range client.BizData {
  81. client.params[key] = AnyToString2(client.BizData[key])
  82. }
  83. }*/
  84. var keys []string
  85. for key := range client.params {
  86. keys = append(keys, key)
  87. }
  88. sort.Strings(keys)
  89. str := ""
  90. for _, key := range keys {
  91. str += fmt.Sprintf("%v=%v&", key, client.params[key])
  92. }
  93. str = client.AppSecret + str[:len(str)-1] + client.AppSecret
  94. fmt.Printf("sign: %s\n", str)
  95. client.Sign = Md5(str)
  96. client.params["sign"] = client.Sign
  97. if client.BizData != nil {
  98. for key := range client.BizData {
  99. if _, ok := client.params[key]; ok {
  100. delete(client.params, key)
  101. }
  102. }
  103. }
  104. return client
  105. }
  106. func (client *OpenPlatformReqClient) VerifySign(sign string) bool {
  107. if sign == "" || client.Sign == "" {
  108. return false
  109. }
  110. if client.Sign == sign {
  111. return true
  112. }
  113. return false
  114. }
  115. func (client *OpenPlatformReqClient) ResetNonce() *OpenPlatformReqClient {
  116. client.Nonce = UUIDString()
  117. return client
  118. }
  119. func (client *OpenPlatformReqClient) ResetTimestamp() *OpenPlatformReqClient {
  120. client.Timestamp = AnyToString(time.Now().Unix())
  121. return client
  122. }