附近小店
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

152 linhas
3.3 KiB

  1. package zhimeng
  2. import (
  3. "applet/app/utils/logx"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "github.com/shopspring/decimal"
  9. "github.com/tidwall/gjson"
  10. )
  11. // SDK is zm sdk
  12. type SDK struct {
  13. Action string
  14. operation string
  15. response []byte
  16. SmsKey string
  17. SmsSecret string
  18. data interface{}
  19. err error
  20. }
  21. // Init is init action
  22. // In some condition, such as send Sms, need pass sms key and secret after 'action'
  23. func (sdk *SDK) Init(action string, keys ...string) {
  24. sdk.Action = action
  25. //if keys[0] == "" || keys[1] == "" {
  26. // sdk.err = errors.New("智盟短信未配置")
  27. //}
  28. if len(keys) > 1 {
  29. sdk.SmsKey = keys[0]
  30. sdk.SmsSecret = keys[1]
  31. }
  32. }
  33. // SelectFunction is select api with operation
  34. func (sdk *SDK) SelectFunction(operation string) *SDK {
  35. sdk.operation = operation
  36. return sdk
  37. }
  38. // WithSMSArgs is SMS
  39. func (sdk *SDK) WithSMSArgs(args map[string]interface{}) *SDK {
  40. res, err := SMSend(sdk.Action, sdk.operation, sdk.SmsKey, sdk.SmsSecret, args)
  41. if err != nil {
  42. logx.Error(err)
  43. }
  44. sdk.response = res
  45. return sdk
  46. }
  47. // WithArgs is post data to api
  48. func (sdk *SDK) WithArgs(args map[string]interface{}) *SDK {
  49. // args["appkey"] = svc.SysCfgGet(c, md.KEY_CFG_ZM_AK)
  50. // args["secret_key"] = svc.SysCfgGet(c, md.KEY_CFG_ZM_SK)
  51. res, err := Send(sdk.Action, sdk.operation, args)
  52. if err != nil {
  53. logx.Error(err)
  54. }
  55. // for k, v := range args {
  56. // fmt.Printf("%s:%v \n", k, v)
  57. // }
  58. fmt.Println("唯品会请求", args, string(res))
  59. sdk.response = res
  60. return sdk
  61. }
  62. // Result is response data from api , return interface{}
  63. func (sdk *SDK) Result() (*SDK, error) {
  64. if sdk.err != nil {
  65. return nil, sdk.err
  66. }
  67. tmp := struct {
  68. Msg string `json:"msg"`
  69. Success int `json:"success"`
  70. Data interface{} `json:"data"`
  71. }{}
  72. if err := json.Unmarshal(sdk.response, &tmp); err != nil {
  73. return nil, logx.Error("【Resp】" + string(sdk.response) + ", 【Error】" + err.Error())
  74. }
  75. if tmp.Success != StatusSuc {
  76. return nil, logx.Error(string(sdk.response))
  77. }
  78. if gjson.GetBytes(sdk.response, "data").String() == "[]" {
  79. return nil, errors.New("no result")
  80. }
  81. sdk.data = tmp.Data
  82. return sdk, nil
  83. }
  84. // ToInterface is data to Interface
  85. func (sdk *SDK) ToInterface() interface{} {
  86. return sdk.data
  87. }
  88. // ToMapStringInterface is data to map[string]string
  89. func (sdk *SDK) ToMapStringInterface(item interface{}) map[string]interface{} {
  90. data, err := json.Marshal(item)
  91. if err != nil {
  92. logx.Error("ToMapStringString marshal error : " + err.Error())
  93. }
  94. m := make(map[string]interface{})
  95. if err = json.Unmarshal(data, &m); err != nil {
  96. logx.Error("ToMapStringString unmarshal error : " + err.Error())
  97. }
  98. for key, v := range m {
  99. switch v.(type) {
  100. case int:
  101. t, ok := v.(string)
  102. if !ok {
  103. logx.Warn("int convert error")
  104. }
  105. m[key] = t
  106. case int32:
  107. t, ok := v.(string)
  108. if !ok {
  109. logx.Warn("int32 convert error")
  110. }
  111. m[key] = t
  112. case int64:
  113. t, ok := v.(string)
  114. if !ok {
  115. logx.Warn("int64 convert error")
  116. }
  117. m[key] = t
  118. case float64:
  119. vstr := fmt.Sprintf("%v", v)
  120. if strings.Contains(vstr, "e+") {
  121. decimalNum, err := decimal.NewFromString(vstr)
  122. if err != nil {
  123. panic(logx.Errorf("decimal.NewFromString error, vstr:%s, err:%v", vstr, err))
  124. }
  125. vstr = decimalNum.String()
  126. }
  127. m[key] = vstr
  128. case nil:
  129. m[key] = v
  130. default:
  131. m[key] = v
  132. }
  133. }
  134. return m
  135. }