golang 的 rabbitmq 消费项目
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.

sdk.go 8.6 KiB

6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package zhimeng
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "applet/app/md"
  9. "applet/app/utils"
  10. "applet/app/utils/cache"
  11. "applet/app/utils/logx"
  12. "github.com/shopspring/decimal"
  13. "github.com/tidwall/gjson"
  14. )
  15. // SDK is zm sdk
  16. type SDK struct {
  17. Action string
  18. operation string
  19. response []byte
  20. SmsKey string
  21. SmsSecret string
  22. data interface{}
  23. err error
  24. }
  25. // Init is init action
  26. // In some condition, such as send Sms, need pass sms key and secret after 'action'
  27. func (sdk *SDK) Init(action string, keys ...string) {
  28. sdk.Action = action
  29. //if keys[0] == "" || keys[1] == "" {
  30. // sdk.err = errors.New("智盟短信未配置")
  31. //}
  32. if len(keys) > 1 {
  33. sdk.SmsKey = keys[0]
  34. sdk.SmsSecret = keys[1]
  35. }
  36. }
  37. // SelectFunction is select api with operation
  38. func (sdk *SDK) SelectFunction(operation string) *SDK {
  39. sdk.operation = operation
  40. return sdk
  41. }
  42. // WithSMSArgs is SMS
  43. func (sdk *SDK) WithSMSArgs(args map[string]interface{}) *SDK {
  44. res, err := SMSend(sdk.Action, sdk.operation, sdk.SmsKey, sdk.SmsSecret, args)
  45. if err != nil {
  46. logx.Error(err)
  47. }
  48. sdk.response = res
  49. return sdk
  50. }
  51. // WithArgs is post data to api
  52. func (sdk *SDK) WithArgs(args map[string]interface{}) *SDK {
  53. // args["appkey"] = svc.SysCfgGet(c, md.KEY_CFG_ZM_AK)
  54. // args["secret_key"] = svc.SysCfgGet(c, md.KEY_CFG_ZM_SK)
  55. res, err := Send(sdk.Action, sdk.operation, args)
  56. if err != nil {
  57. logx.Error(err)
  58. }
  59. // for k, v := range args {
  60. // fmt.Printf("%s:%v \n", k, v)
  61. // }
  62. fmt.Println("唯品会请求", args, string(res))
  63. sdk.response = res
  64. return sdk
  65. }
  66. // Result is response data from api , return interface{}
  67. func (sdk *SDK) Result() (*SDK, error) {
  68. if sdk.err != nil {
  69. return nil, sdk.err
  70. }
  71. tmp := struct {
  72. Msg string `json:"msg"`
  73. Success int `json:"success"`
  74. Data interface{} `json:"data"`
  75. }{}
  76. if err := json.Unmarshal(sdk.response, &tmp); err != nil {
  77. return nil, logx.Error("【Resp】" + string(sdk.response) + ", 【Error】" + err.Error())
  78. }
  79. if tmp.Success != StatusSuc {
  80. return nil, logx.Error(string(sdk.response))
  81. }
  82. if gjson.GetBytes(sdk.response, "data").String() == "[]" {
  83. return nil, errors.New("no result")
  84. }
  85. sdk.data = tmp.Data
  86. return sdk, nil
  87. }
  88. // WithJDDetailImageList is insert jd detail image list when api response
  89. func (sdk *SDK) WithJDDetailImageList(gid string) {
  90. if len(sdk.response) == 0 || sdk.response == nil {
  91. logx.Warn("Please call after Result")
  92. return
  93. }
  94. fmt.Println(gjson.GetBytes(sdk.response, "data").String())
  95. if gjson.GetBytes(sdk.response, "data").String() == "[]" {
  96. return
  97. }
  98. fmt.Println("debug", sdk.response)
  99. data, ok := sdk.data.(map[string]interface{})
  100. if !ok {
  101. logx.Warn("Type error to map[string]interface")
  102. }
  103. // debug
  104. data["detail_img_list"] = getJDDetailImageList(gid)
  105. sdk.data = data
  106. }
  107. // ToObject is the interface is Slice and get the first item
  108. func (sdk *SDK) ToObject() *SDK {
  109. s, ok := sdk.ToInterface().([]interface{})
  110. if !ok {
  111. logx.Errorf("\nToOject cantnot convert to []interface{}, 【Data】: %#v\n", sdk.data)
  112. return sdk
  113. }
  114. sdk.data = s[len(s)-len(s)]
  115. return sdk
  116. }
  117. // ToInterface is data to Interface
  118. func (sdk *SDK) ToInterface() interface{} {
  119. return sdk.data
  120. }
  121. // ToMapStringInterface is data to map[string]string
  122. func (sdk *SDK) ToMapStringInterface(item interface{}) map[string]interface{} {
  123. data, err := json.Marshal(item)
  124. if err != nil {
  125. logx.Error("ToMapStringString marshal error : " + err.Error())
  126. }
  127. m := make(map[string]interface{})
  128. if err = json.Unmarshal(data, &m); err != nil {
  129. logx.Error("ToMapStringString unmarshal error : " + err.Error())
  130. }
  131. for key, v := range m {
  132. switch v.(type) {
  133. case int:
  134. t, ok := v.(string)
  135. if !ok {
  136. logx.Warn("int convert error")
  137. }
  138. m[key] = t
  139. case int32:
  140. t, ok := v.(string)
  141. if !ok {
  142. logx.Warn("int32 convert error")
  143. }
  144. m[key] = t
  145. case int64:
  146. t, ok := v.(string)
  147. if !ok {
  148. logx.Warn("int64 convert error")
  149. }
  150. m[key] = t
  151. case float64:
  152. vstr := fmt.Sprintf("%v", v)
  153. if strings.Contains(vstr, "e+") {
  154. decimalNum, err := decimal.NewFromString(vstr)
  155. if err != nil {
  156. panic(logx.Errorf("decimal.NewFromString error, vstr:%s, err:%v", vstr, err))
  157. }
  158. vstr = decimalNum.String()
  159. }
  160. m[key] = vstr
  161. case nil:
  162. m[key] = v
  163. default:
  164. m[key] = v
  165. }
  166. }
  167. return m
  168. }
  169. // ToStruct is struct return
  170. func (sdk *SDK) ToStruct(r *md.MoreDetailResponse) error {
  171. // debug
  172. data, err := json.Marshal(sdk.ToMapStringInterface(sdk.data))
  173. if err != nil {
  174. panic(logx.Error("ToStruct marshal error : " + err.Error()))
  175. }
  176. if err = json.Unmarshal(data, r); err != nil {
  177. return logx.Error("ToStruct Unmarshal error : " + err.Error())
  178. }
  179. return nil
  180. }
  181. func (sdk *SDK) ToOrderStruct(r *md.VipOrder) error {
  182. // debug
  183. data, err := json.Marshal(sdk.ToMapStringInterface(sdk.data))
  184. if err != nil {
  185. panic(logx.Error("ToStruct marshal error : " + err.Error()))
  186. }
  187. if err = json.Unmarshal(data, r); err != nil {
  188. return logx.Error("ToStruct Unmarshal error : " + err.Error())
  189. }
  190. return nil
  191. }
  192. // PddToMapStringInterface is data to map[string]string
  193. func (sdk *SDK) PddToMapStringInterface(item interface{}) []map[string]interface{} {
  194. data, err := json.Marshal(item)
  195. if err != nil {
  196. logx.Error("PddToMapStringString marshal error : " + err.Error())
  197. }
  198. m1 := make([]map[string]interface{}, 10, 10)
  199. if err = json.Unmarshal(data, &m1); err != nil {
  200. logx.Error("PddToMapStringString unmarshal error : " + err.Error())
  201. }
  202. for _, m := range m1 {
  203. for key, v := range m {
  204. switch v.(type) {
  205. case int:
  206. t, ok := v.(string)
  207. if !ok {
  208. logx.Warn("int convert error")
  209. }
  210. m[key] = t
  211. case int32:
  212. t, ok := v.(string)
  213. if !ok {
  214. logx.Warn("int32 convert error")
  215. }
  216. m[key] = t
  217. case int64:
  218. t, ok := v.(string)
  219. if !ok {
  220. logx.Warn("int64 convert error")
  221. }
  222. m[key] = t
  223. case float64:
  224. vstr := fmt.Sprintf("%v", v)
  225. if strings.Contains(vstr, "e+") {
  226. decimalNum, err := decimal.NewFromString(vstr)
  227. if err != nil {
  228. panic(logx.Errorf("decimal.NewFromString error, vstr:%s, err:%v", vstr, err))
  229. }
  230. vstr = decimalNum.String()
  231. }
  232. m[key] = vstr
  233. case nil:
  234. m[key] = v
  235. default:
  236. m[key] = v
  237. }
  238. }
  239. }
  240. return m1
  241. }
  242. // PddToStruct is struct return for Pdd data is list by one
  243. func (sdk *SDK) PddToStruct(r *md.MoreDetailResponse) error {
  244. // debug
  245. data, err := json.Marshal(sdk.PddToMapStringInterface(sdk.data)[0])
  246. if err != nil {
  247. panic(logx.Error("PddToStruct marshal error : " + err.Error()))
  248. }
  249. if err = json.Unmarshal(data, r); err != nil {
  250. return logx.Error("PddToStruct Unmarshal error : " + err.Error())
  251. }
  252. return nil
  253. }
  254. // VipToMapStringInterface is data to map[string]string
  255. func (sdk *SDK) VipToMapStringInterface() map[string]interface{} {
  256. data, err := json.Marshal(sdk.data)
  257. if err != nil {
  258. logx.Error("ToMapStringString marshal error : " + err.Error())
  259. }
  260. m := make(map[string]interface{})
  261. if err = json.Unmarshal(data, &m); err != nil {
  262. logx.Error("ToMapStringString unmarshal error : " + err.Error())
  263. }
  264. for key, v := range m {
  265. switch v.(type) {
  266. case int:
  267. t, ok := v.(string)
  268. if !ok {
  269. logx.Warn("int convert error")
  270. }
  271. m[key] = t
  272. case int32:
  273. t, ok := v.(string)
  274. if !ok {
  275. logx.Warn("int32 convert error")
  276. }
  277. m[key] = t
  278. case int64:
  279. t, ok := v.(string)
  280. if !ok {
  281. logx.Warn("int64 convert error")
  282. }
  283. m[key] = t
  284. case float64:
  285. vstr := fmt.Sprintf("%v", v)
  286. if strings.Contains(vstr, "e+") {
  287. decimalNum, err := decimal.NewFromString(vstr)
  288. if err != nil {
  289. panic(logx.Errorf("decimal.NewFromString error, vstr:%s, err:%v", vstr, err))
  290. }
  291. vstr = decimalNum.String()
  292. }
  293. m[key] = vstr
  294. case nil:
  295. m[key] = v
  296. default:
  297. m[key] = v
  298. }
  299. }
  300. return m
  301. }
  302. // getJDDetailImageList is get the detail images
  303. func getJDDetailImageList(gid string) []string {
  304. cacheKey := "getJDDetailImageList_gid_" + gid
  305. imgCache := struct {
  306. Data []string
  307. CacheTime int64
  308. Expired int64
  309. }{}
  310. if cc, err := cache.Bytes(cache.Get(cacheKey)); cc != nil && err == nil {
  311. utils.Unserialize(cc, &imgCache)
  312. if time.Now().Unix() > imgCache.Expired {
  313. logx.Info("Cache is expired , Get new one")
  314. } else if imgCache.Data != nil {
  315. logx.Infof("Get Cache %s", cacheKey)
  316. return imgCache.Data
  317. }
  318. }
  319. list, err := ScrapJDDetailImageList(gid)
  320. if err != nil {
  321. logx.Error("getJDDetailImageList" + err.Error())
  322. return []string{}
  323. }
  324. now := time.Now()
  325. // 设定10分钟过期时间
  326. d, _ := time.ParseDuration("+10m")
  327. expired := now.Add(d)
  328. expired.Unix()
  329. imgCache.Data = list
  330. imgCache.CacheTime = now.Unix()
  331. imgCache.Expired = expired.Unix()
  332. cache.SetEx(cacheKey, utils.Serialize(imgCache), 300)
  333. return imgCache.Data
  334. }