第三方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 7.0 KiB

1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
9 months ago
1 year ago
10 months ago
1 year ago
10 months ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. imgList := make([]string, 0)
  62. if len(v.SmallImages.String) == 0 {
  63. imgList = append(imgList, v.PictURL)
  64. } else {
  65. for _, v1 := range v.SmallImages.String {
  66. imgList = append(imgList, v1)
  67. }
  68. }
  69. var tmp = map[string]string{
  70. "img_list": zhios_third_party_utils.SerializeStr(imgList),
  71. "gid": zhios_third_party_utils.AnyToString(v.ItemID),
  72. "title": v.Title,
  73. "img": v.PictURL,
  74. "sales": zhios_third_party_utils.IntToStr(v.Volume),
  75. "price": zhios_third_party_utils.Float64ToStr(zhios_third_party_utils.StrToFloat64(v.ZkFinalPrice) - zhios_third_party_utils.StrToFloat64(v.CouponAmount)),
  76. "cost_price": v.ZkFinalPrice,
  77. "coupon": v.CouponAmount,
  78. "commission_rate": zhios_third_party_utils.AnyToString(zhios_third_party_utils.StrToFloat64(v.CommissionRate) / 100),
  79. "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)),
  80. "pvd": md.PVD_TB,
  81. "share_content": "",
  82. }
  83. goods = append(goods, tmp)
  84. }
  85. }
  86. return goods
  87. }
  88. // TaoBaoSearchList is return taobao search list
  89. func (t *TB) TaoBaoFeaturedList(args map[string]string) (*[]md.TBFeatureStruct, error) {
  90. list, err := t.FeaturedList(
  91. args,
  92. )
  93. if err != nil {
  94. return nil, err
  95. }
  96. return list, nil
  97. }
  98. func (t *TB) TaoBaoSearchList(args map[string]string) (*[]Material, error) {
  99. list, err := t.SearchList(
  100. args,
  101. )
  102. if err != nil {
  103. return nil, err
  104. }
  105. return list, nil
  106. }
  107. // 商品详情
  108. func (t *TB) ItemDetailById(id, bizSceneId string) (*Material, error) {
  109. if php2go.IsNumeric(id) {
  110. return t.ItemDetailByLink("https://item.taobao.com/item.htm?id="+id, "2")
  111. }
  112. return t.ItemDetailByLink("https://uland.taobao.com/item/edetail?id="+id, "")
  113. }
  114. func (t *TB) ItemDetailByLink(l, bizSceneId string) (*Material, error) {
  115. args := map[string]string{
  116. "q": l,
  117. "adzone_id": AdZoneId(t.SelfPromoIdWeb), // 默认使用安卓, 转链时候才要针对不同的渠道
  118. "session": t.SID,
  119. "page_size": "1",
  120. "page_no": "1",
  121. }
  122. if bizSceneId == "2" {
  123. args["biz_scene_id"] = "2"
  124. }
  125. method := TBK_DG_MTL_OPT
  126. //判断是否跟随官方 需要换接口
  127. if t.AuthType == 1 {
  128. method = TBK_SC_MTL_OPT
  129. Sid := t.UserSID
  130. args["session"] = Sid
  131. _, args["site_id"], args["adzone_id"] = SplitPid(t.SelfPromoIdWeb)
  132. }
  133. resp, err := send(args, method, t.Web_AK, t.Web_SK)
  134. if err != nil {
  135. return nil, err
  136. }
  137. fmt.Println("淘宝", args)
  138. fmt.Println("淘宝", string(resp))
  139. // todo 可能还需返回的说明, 里面包含详细介绍的图片
  140. // "https://mdetail.tmall.com/templates/pages/desc?id=" + id
  141. // "https://h5.m.taobao.com/app/detail/desc.html?type=1&f=&sellerType=B&_isH5Des=true#!id=" + id
  142. // 如果是跟随官方 结构不一样
  143. if t.AuthType == 1 {
  144. var tmp scMaterialOptionalStruct
  145. if err = json.Unmarshal(resp, &tmp); err != nil {
  146. return nil, err
  147. }
  148. if len(tmp.TbkScMaterialOptionalResponse.ResultList.MapData) == 0 {
  149. return nil, errors.New("没有数据")
  150. }
  151. data := tmp.TbkScMaterialOptionalResponse.ResultList.MapData[0]
  152. itemID, ok := data.ItemID.(float64)
  153. if ok {
  154. data.ItemID = int64(itemID)
  155. }
  156. return &data, nil
  157. } else {
  158. var tmp wrapperStruct
  159. if err = json.Unmarshal(resp, &tmp); err != nil {
  160. return nil, err
  161. }
  162. if len(tmp.TbkDgMaterialOptionalResponse.ResultList.MapData) == 0 {
  163. return nil, err
  164. }
  165. data := tmp.TbkDgMaterialOptionalResponse.ResultList.MapData[0]
  166. itemID, ok := data.ItemID.(float64)
  167. if ok {
  168. data.ItemID = int64(itemID)
  169. }
  170. return &data, nil
  171. }
  172. }
  173. func (t *TB) ItemDetailByIdKey(l, bizSceneId string) (*Material, error) {
  174. l += "&"
  175. l = strings.ReplaceAll(l, "?", "&")
  176. ex := php2go.Explode("&id=", l)
  177. id := ""
  178. if len(ex) > 1 {
  179. ex1 := php2go.Explode("&", ex[1])
  180. if len(ex1) > 0 {
  181. id = ex1[0]
  182. }
  183. }
  184. if id == "" || id == "622130940777" {
  185. return nil, errors.New("找不到商品")
  186. }
  187. args := map[string]string{
  188. "num_iids": id,
  189. "session": t.SID,
  190. "platform": "2",
  191. }
  192. if bizSceneId == "2" {
  193. //args["biz_scene_id"] = "2"
  194. }
  195. method := "taobao.tbk.item.info.get"
  196. //判断是否跟随官方 需要换接口
  197. if t.AuthType == 1 {
  198. Sid := t.UserSID
  199. args["session"] = Sid
  200. }
  201. resp, err := send(args, method, t.Web_AK, t.Web_SK)
  202. if err != nil {
  203. return nil, err
  204. }
  205. var tmp tbDetailStruct
  206. if err = json.Unmarshal(resp, &tmp); err != nil {
  207. return nil, err
  208. }
  209. if len(tmp.TbkItemInfoGetResponse.Results.NBbkItem) == 0 {
  210. return nil, err
  211. }
  212. data := tmp.TbkItemInfoGetResponse.Results.NBbkItem[0]
  213. data.ItemID = data.InputNumIid
  214. itemID, ok := data.ItemID.(float64)
  215. if ok {
  216. data.ItemID = int64(itemID)
  217. }
  218. return &data, nil
  219. }