第三方api接口
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.

286 line
5.8 KiB

  1. package zhimeng
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/md"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/shopspring/decimal"
  8. "github.com/tidwall/gjson"
  9. "strings"
  10. )
  11. // SDK is zhimeng 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. }
  43. sdk.response = res
  44. return sdk
  45. }
  46. // WithArgs is post data to api
  47. func (sdk *SDK) WithArgs(args map[string]interface{}) *SDK {
  48. // args["appkey"] = svc.SysCfgGet(c, md.KEY_CFG_ZM_AK)
  49. // args["secret_key"] = svc.SysCfgGet(c, md.KEY_CFG_ZM_SK)
  50. res, err := Send(sdk.Action, sdk.operation, args)
  51. if err != nil {
  52. }
  53. // for k, v := range args {
  54. // fmt.Printf("%s:%v \n", k, v)
  55. // }
  56. fmt.Println("唯品会请求", args, string(res))
  57. sdk.response = res
  58. return sdk
  59. }
  60. // Result is response data from api , return interface{}
  61. func (sdk *SDK) Result() (*SDK, error) {
  62. if sdk.err != nil {
  63. return nil, sdk.err
  64. }
  65. tmp := struct {
  66. Msg string `json:"msg"`
  67. Success int `json:"success"`
  68. Data interface{} `json:"data"`
  69. }{}
  70. if err := json.Unmarshal(sdk.response, &tmp); err != nil {
  71. return nil, err
  72. }
  73. if tmp.Success != StatusSuc {
  74. return nil, errors.New("没有商品")
  75. }
  76. if gjson.GetBytes(sdk.response, "data").String() == "[]" {
  77. return nil, errors.New("no result")
  78. }
  79. sdk.data = tmp.Data
  80. return sdk, nil
  81. }
  82. // WithJDDetailImageList is insert jd detail image list when api response
  83. func (sdk *SDK) WithJDDetailImageList(gid string) {
  84. if len(sdk.response) == 0 || sdk.response == nil {
  85. return
  86. }
  87. fmt.Println(gjson.GetBytes(sdk.response, "data").String())
  88. if gjson.GetBytes(sdk.response, "data").String() == "[]" {
  89. return
  90. }
  91. fmt.Println("debug", sdk.response)
  92. data, ok := sdk.data.(map[string]interface{})
  93. if !ok {
  94. }
  95. // debug
  96. sdk.data = data
  97. }
  98. // ToObject is the interface is Slice and get the first item
  99. func (sdk *SDK) ToObject() *SDK {
  100. s, ok := sdk.ToInterface().([]interface{})
  101. if !ok {
  102. return sdk
  103. }
  104. sdk.data = s[len(s)-len(s)]
  105. return sdk
  106. }
  107. // ToInterface is data to Interface
  108. func (sdk *SDK) ToInterface() interface{} {
  109. return sdk.data
  110. }
  111. // ToMapStringInterface is data to map[string]string
  112. func (sdk *SDK) ToMapStringInterface(item interface{}) map[string]interface{} {
  113. data, err := json.Marshal(item)
  114. if err != nil {
  115. }
  116. m := make(map[string]interface{})
  117. if err = json.Unmarshal(data, &m); err != nil {
  118. }
  119. for key, v := range m {
  120. switch v.(type) {
  121. case int:
  122. t, ok := v.(string)
  123. if !ok {
  124. }
  125. m[key] = t
  126. case int32:
  127. t, ok := v.(string)
  128. if !ok {
  129. }
  130. m[key] = t
  131. case int64:
  132. t, ok := v.(string)
  133. if !ok {
  134. }
  135. m[key] = t
  136. case float64:
  137. vstr := fmt.Sprintf("%v", v)
  138. if strings.Contains(vstr, "e+") {
  139. decimalNum, err := decimal.NewFromString(vstr)
  140. if err != nil {
  141. }
  142. vstr = decimalNum.String()
  143. }
  144. m[key] = vstr
  145. case nil:
  146. m[key] = v
  147. default:
  148. m[key] = v
  149. }
  150. }
  151. return m
  152. }
  153. // ToStruct is struct return
  154. func (sdk *SDK) ToStruct(r *md.MoreDetailResponse) error {
  155. // debug
  156. data, err := json.Marshal(sdk.ToMapStringInterface(sdk.data))
  157. if err != nil {
  158. }
  159. if err = json.Unmarshal(data, r); err != nil {
  160. return err
  161. }
  162. return nil
  163. }
  164. // PddToMapStringInterface is data to map[string]string
  165. func (sdk *SDK) PddToMapStringInterface(item interface{}) []map[string]interface{} {
  166. data, err := json.Marshal(item)
  167. if err != nil {
  168. }
  169. m1 := make([]map[string]interface{}, 10, 10)
  170. if err = json.Unmarshal(data, &m1); err != nil {
  171. }
  172. for _, m := range m1 {
  173. for key, v := range m {
  174. switch v.(type) {
  175. case int:
  176. t, ok := v.(string)
  177. if !ok {
  178. }
  179. m[key] = t
  180. case int32:
  181. t, ok := v.(string)
  182. if !ok {
  183. }
  184. m[key] = t
  185. case int64:
  186. t, ok := v.(string)
  187. if !ok {
  188. }
  189. m[key] = t
  190. case float64:
  191. vstr := fmt.Sprintf("%v", v)
  192. if strings.Contains(vstr, "e+") {
  193. decimalNum, err := decimal.NewFromString(vstr)
  194. if err != nil {
  195. }
  196. vstr = decimalNum.String()
  197. }
  198. m[key] = vstr
  199. case nil:
  200. m[key] = v
  201. default:
  202. m[key] = v
  203. }
  204. }
  205. }
  206. return m1
  207. }
  208. // PddToStruct is struct return for Pdd data is list by one
  209. func (sdk *SDK) PddToStruct(r *md.MoreDetailResponse) error {
  210. // debug
  211. data, err := json.Marshal(sdk.PddToMapStringInterface(sdk.data)[0])
  212. if err != nil {
  213. }
  214. if err = json.Unmarshal(data, r); err != nil {
  215. return err
  216. }
  217. return nil
  218. }
  219. // VipToMapStringInterface is data to map[string]string
  220. func (sdk *SDK) VipToMapStringInterface() map[string]interface{} {
  221. data, err := json.Marshal(sdk.data)
  222. if err != nil {
  223. }
  224. m := make(map[string]interface{})
  225. if err = json.Unmarshal(data, &m); err != nil {
  226. }
  227. for key, v := range m {
  228. switch v.(type) {
  229. case int:
  230. t, ok := v.(string)
  231. if !ok {
  232. }
  233. m[key] = t
  234. case int32:
  235. t, ok := v.(string)
  236. if !ok {
  237. }
  238. m[key] = t
  239. case int64:
  240. t, ok := v.(string)
  241. if !ok {
  242. }
  243. m[key] = t
  244. case float64:
  245. vstr := fmt.Sprintf("%v", v)
  246. if strings.Contains(vstr, "e+") {
  247. decimalNum, err := decimal.NewFromString(vstr)
  248. if err != nil {
  249. }
  250. vstr = decimalNum.String()
  251. }
  252. m[key] = vstr
  253. case nil:
  254. m[key] = v
  255. default:
  256. m[key] = v
  257. }
  258. }
  259. return m
  260. }