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

taobao.go 6.7 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package taobao
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/md"
  4. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "github.com/syyongx/php2go"
  9. "strings"
  10. "xorm.io/xorm"
  11. )
  12. //TaoBaoArgs is args
  13. type TaoBaoArgs struct {
  14. Keyword string
  15. PageIndex string
  16. PageSize string
  17. Sort string
  18. StartPrice string
  19. EndPrice string
  20. IsTmall string
  21. IsCoupon string
  22. StartCommissionRate string
  23. EndCommissionRate string
  24. }
  25. func TaoBaoFeaturedList(eg *xorm.Engine, dbName, Platform string, AccTaobaoShareId int, postData map[string]string) []map[string]string {
  26. var goods = make([]map[string]string, 0)
  27. sdk, err := NewTaobaoSDK(eg, dbName, Platform, AccTaobaoShareId)
  28. if err != nil {
  29. return goods
  30. }
  31. list, err := sdk.TaoBaoFeaturedList(postData)
  32. if list != nil {
  33. for _, v := range *list {
  34. var tmp = map[string]string{
  35. "gid": zhios_third_party_utils.AnyToString(v.ItemID),
  36. "title": v.Title,
  37. "img": v.PictURL,
  38. "sales": zhios_third_party_utils.IntToStr(v.Volume),
  39. "price": zhios_third_party_utils.Float64ToStr(zhios_third_party_utils.StrToFloat64(v.ZkFinalPrice) - float64(v.CouponAmount)),
  40. "cost_price": v.ZkFinalPrice,
  41. "coupon": zhios_third_party_utils.IntToStr(v.CouponAmount),
  42. "commission_rate": zhios_third_party_utils.AnyToString(zhios_third_party_utils.StrToFloat64(v.CommissionRate) / 100),
  43. "commission": zhios_third_party_utils.Float64ToStr((zhios_third_party_utils.StrToFloat64(v.ZkFinalPrice) - float64(v.CouponAmount)) * (zhios_third_party_utils.StrToFloat64(v.CommissionRate) / 100 / 100)),
  44. "pvd": md.PVD_TB,
  45. "share_content": "",
  46. }
  47. goods = append(goods, tmp)
  48. }
  49. }
  50. return goods
  51. }
  52. func TaoBaoSearchList(eg *xorm.Engine, dbName, Platform string, AccTaobaoShareId int, postData map[string]string) []map[string]string {
  53. var goods = make([]map[string]string, 0)
  54. sdk, err := NewTaobaoSDK(eg, dbName, Platform, AccTaobaoShareId)
  55. if err != nil {
  56. return goods
  57. }
  58. list, err := sdk.TaoBaoSearchList(postData)
  59. if list != nil {
  60. for _, v := range *list {
  61. var tmp = map[string]string{
  62. "gid": zhios_third_party_utils.AnyToString(v.ItemID),
  63. "title": v.Title,
  64. "img": v.PictURL,
  65. "sales": zhios_third_party_utils.IntToStr(v.Volume),
  66. "price": zhios_third_party_utils.Float64ToStr(zhios_third_party_utils.StrToFloat64(v.ZkFinalPrice) - zhios_third_party_utils.StrToFloat64(v.CouponAmount)),
  67. "cost_price": v.ZkFinalPrice,
  68. "coupon": v.CouponAmount,
  69. "commission_rate": zhios_third_party_utils.AnyToString(zhios_third_party_utils.StrToFloat64(v.CommissionRate) / 100),
  70. "commission": zhios_third_party_utils.Float64ToStr((zhios_third_party_utils.StrToFloat64(v.ZkFinalPrice) - zhios_third_party_utils.StrToFloat64(v.CouponAmount)) * (zhios_third_party_utils.StrToFloat64(v.CommissionRate) / 100 / 100)),
  71. "pvd": md.PVD_TB,
  72. "share_content": "",
  73. }
  74. goods = append(goods, tmp)
  75. }
  76. }
  77. return goods
  78. }
  79. // TaoBaoSearchList is return taobao search list
  80. func (t *TB) TaoBaoFeaturedList(args map[string]string) (*[]md.TBFeatureStruct, error) {
  81. list, err := t.FeaturedList(
  82. args,
  83. )
  84. if err != nil {
  85. return nil, err
  86. }
  87. return list, nil
  88. }
  89. func (t *TB) TaoBaoSearchList(args map[string]string) (*[]Material, error) {
  90. list, err := t.SearchList(
  91. args,
  92. )
  93. if err != nil {
  94. return nil, err
  95. }
  96. return list, nil
  97. }
  98. // 商品详情
  99. func (t *TB) ItemDetailById(id, bizSceneId string) (*Material, error) {
  100. if php2go.IsNumeric(id) {
  101. return t.ItemDetailByLink("https://item.taobao.com/item.htm?id="+id, "2")
  102. }
  103. return t.ItemDetailByLink("https://uland.taobao.com/item/edetail?id="+id, "")
  104. }
  105. func (t *TB) ItemDetailByLink(l, bizSceneId string) (*Material, error) {
  106. args := map[string]string{
  107. "q": l,
  108. "adzone_id": AdZoneId(t.SelfPromoIdWeb), // 默认使用安卓, 转链时候才要针对不同的渠道
  109. "session": t.SID,
  110. "page_size": "1",
  111. "page_no": "1",
  112. }
  113. if bizSceneId == "2" {
  114. args["biz_scene_id"] = "2"
  115. }
  116. method := TBK_DG_MTL_OPT
  117. //判断是否跟随官方 需要换接口
  118. if t.AuthType == 1 {
  119. method = TBK_SC_MTL_OPT
  120. Sid := t.UserSID
  121. args["session"] = Sid
  122. _, args["site_id"], args["adzone_id"] = SplitPid(t.SelfPromoIdWeb)
  123. }
  124. resp, err := send(args, method, t.Web_AK, t.Web_SK)
  125. if err != nil {
  126. return nil, err
  127. }
  128. fmt.Println("淘宝", args)
  129. fmt.Println("淘宝", string(resp))
  130. // todo 可能还需返回的说明, 里面包含详细介绍的图片
  131. // "https://mdetail.tmall.com/templates/pages/desc?id=" + id
  132. // "https://h5.m.taobao.com/app/detail/desc.html?type=1&f=&sellerType=B&_isH5Des=true#!id=" + id
  133. // 如果是跟随官方 结构不一样
  134. if t.AuthType == 1 {
  135. var tmp scMaterialOptionalStruct
  136. if err = json.Unmarshal(resp, &tmp); err != nil {
  137. return nil, err
  138. }
  139. if len(tmp.TbkScMaterialOptionalResponse.ResultList.MapData) == 0 {
  140. return nil, errors.New("没有数据")
  141. }
  142. data := tmp.TbkScMaterialOptionalResponse.ResultList.MapData[0]
  143. itemID, ok := data.ItemID.(float64)
  144. if ok {
  145. data.ItemID = int64(itemID)
  146. }
  147. return &data, nil
  148. } else {
  149. var tmp wrapperStruct
  150. if err = json.Unmarshal(resp, &tmp); err != nil {
  151. return nil, err
  152. }
  153. if len(tmp.TbkDgMaterialOptionalResponse.ResultList.MapData) == 0 {
  154. return nil, err
  155. }
  156. data := tmp.TbkDgMaterialOptionalResponse.ResultList.MapData[0]
  157. itemID, ok := data.ItemID.(float64)
  158. if ok {
  159. data.ItemID = int64(itemID)
  160. }
  161. return &data, nil
  162. }
  163. }
  164. func (t *TB) ItemDetailByIdKey(l, bizSceneId string) (*Material, error) {
  165. l += "&"
  166. l = strings.ReplaceAll(l, "?", "&")
  167. ex := php2go.Explode("&id=", l)
  168. id := ""
  169. if len(ex) > 1 {
  170. ex1 := php2go.Explode("&", ex[1])
  171. if len(ex1) > 0 {
  172. id = ex1[0]
  173. }
  174. }
  175. if id == "" || id == "622130940777" {
  176. return nil, errors.New("找不到商品")
  177. }
  178. args := map[string]string{
  179. "num_iids": id,
  180. "session": t.SID,
  181. "platform": "2",
  182. }
  183. if bizSceneId == "2" {
  184. //args["biz_scene_id"] = "2"
  185. }
  186. method := "taobao.tbk.item.info.get"
  187. //判断是否跟随官方 需要换接口
  188. if t.AuthType == 1 {
  189. Sid := t.UserSID
  190. args["session"] = Sid
  191. }
  192. resp, err := send(args, method, t.Web_AK, t.Web_SK)
  193. if err != nil {
  194. return nil, err
  195. }
  196. var tmp tbDetailStruct
  197. if err = json.Unmarshal(resp, &tmp); err != nil {
  198. return nil, err
  199. }
  200. if len(tmp.TbkItemInfoGetResponse.Results.NBbkItem) == 0 {
  201. return nil, err
  202. }
  203. data := tmp.TbkItemInfoGetResponse.Results.NBbkItem[0]
  204. data.ItemID = data.InputNumIid
  205. itemID, ok := data.ItemID.(float64)
  206. if ok {
  207. data.ItemID = int64(itemID)
  208. }
  209. return &data, nil
  210. }