第三方api接口
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

taobao.go 9.1 KiB

1 년 전
1 년 전
8 달 전
1 년 전
2 달 전
1 년 전
11 달 전
1 년 전
1 년 전
11 달 전
1 년 전
1 년 전
1 년 전
1 년 전
1 년 전
1 년 전
1 년 전
1 년 전
8 달 전
1 년 전
8 달 전
1 년 전
8 달 전
2 달 전
8 달 전
2 달 전
8 달 전
2 달 전
1 년 전
8 달 전
1 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. "time"
  11. "xorm.io/xorm"
  12. )
  13. // TaoBaoArgs is args
  14. type TaoBaoArgs struct {
  15. Keyword string
  16. PageIndex string
  17. PageSize string
  18. Sort string
  19. StartPrice string
  20. EndPrice string
  21. IsTmall string
  22. IsCoupon string
  23. StartCommissionRate string
  24. EndCommissionRate string
  25. }
  26. func TaoBaoFeaturedList(eg *xorm.Engine, dbName, Platform string, AccTaobaoShareId int, postData map[string]string) []map[string]string {
  27. var goods = make([]map[string]string, 0)
  28. sdk, err := NewTaobaoSDK(eg, dbName, Platform, AccTaobaoShareId, "")
  29. if err != nil {
  30. return goods
  31. }
  32. list, err := sdk.TaoBaoFeaturedList(postData)
  33. if list != nil {
  34. for _, v := range *list {
  35. var tmp = map[string]string{
  36. "gid": zhios_third_party_utils.AnyToString(v.ItemID),
  37. "title": v.Title,
  38. "img": v.PictURL,
  39. "sales": zhios_third_party_utils.IntToStr(v.Volume),
  40. "price": zhios_third_party_utils.Float64ToStr(zhios_third_party_utils.StrToFloat64(v.ZkFinalPrice) - float64(v.CouponAmount)),
  41. "cost_price": v.ZkFinalPrice,
  42. "coupon": zhios_third_party_utils.IntToStr(v.CouponAmount),
  43. "commission_rate": zhios_third_party_utils.AnyToString(zhios_third_party_utils.StrToFloat64(v.CommissionRate) / 100),
  44. "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)),
  45. "pvd": md.PVD_TB,
  46. "share_content": "",
  47. }
  48. goods = append(goods, tmp)
  49. }
  50. }
  51. return goods
  52. }
  53. func TaoBaoSearchList(eg *xorm.Engine, dbName, Platform string, AccTaobaoShareId int, postData map[string]string) []map[string]string {
  54. var goods = make([]map[string]string, 0)
  55. sdk, err := NewTaobaoSDK(eg, dbName, Platform, AccTaobaoShareId, "")
  56. if err != nil {
  57. return goods
  58. }
  59. list, err := sdk.TaoBaoSearchList(postData)
  60. if list != nil {
  61. for _, v := range *list {
  62. imgList := make([]string, 0)
  63. if len(v.SmallImages.String) == 0 {
  64. imgList = append(imgList, v.PictURL)
  65. } else {
  66. for _, v1 := range v.SmallImages.String {
  67. imgList = append(imgList, v1)
  68. }
  69. }
  70. var tmp = map[string]string{
  71. "img_list": zhios_third_party_utils.SerializeStr(imgList),
  72. "gid": zhios_third_party_utils.AnyToString(v.ItemID),
  73. "title": v.Title,
  74. "img": v.PictURL,
  75. "sales": zhios_third_party_utils.IntToStr(v.Volume),
  76. "price": zhios_third_party_utils.Float64ToStr(zhios_third_party_utils.StrToFloat64(v.ZkFinalPrice) - zhios_third_party_utils.StrToFloat64(v.CouponAmount)),
  77. "cost_price": v.ZkFinalPrice,
  78. "coupon": v.CouponAmount,
  79. "commission_rate": zhios_third_party_utils.AnyToString(zhios_third_party_utils.StrToFloat64(v.CommissionRate) / 100),
  80. "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)),
  81. "pvd": md.PVD_TB,
  82. "share_content": "",
  83. }
  84. goods = append(goods, tmp)
  85. }
  86. }
  87. return goods
  88. }
  89. // TaoBaoSearchList is return taobao search list
  90. func (t *TB) TaoBaoFeaturedList(args map[string]string) (*[]md.TBFeatureStruct, error) {
  91. list, err := t.FeaturedList(
  92. args,
  93. )
  94. if err != nil {
  95. return nil, err
  96. }
  97. return list, nil
  98. }
  99. func (t *TB) TaoBaoSearchList(args map[string]string) (*[]Material, error) {
  100. list, err := t.SearchList(
  101. args,
  102. )
  103. if err != nil {
  104. return nil, err
  105. }
  106. return list, nil
  107. }
  108. // 商品详情
  109. func (t *TB) ItemDetailById(id, bizSceneId string) (*Material, error) {
  110. if php2go.IsNumeric(id) {
  111. return t.ItemDetailByLink("https://item.taobao.com/item.htm?id="+id, "2")
  112. }
  113. return t.ItemDetailByLink("https://uland.taobao.com/item/edetail?id="+id, "")
  114. }
  115. func (t *TB) ItemDetailByLink(l, bizSceneId string) (*Material, error) {
  116. args := map[string]string{
  117. "q": l,
  118. "adzone_id": AdZoneId(t.SelfPromoIdWeb), // 默认使用安卓, 转链时候才要针对不同的渠道
  119. "session": t.SID,
  120. "page_size": "1",
  121. "page_no": "1",
  122. }
  123. if bizSceneId == "2" {
  124. args["biz_scene_id"] = "2"
  125. }
  126. method := TBK_DG_MTL_OPT
  127. //判断是否跟随官方 需要换接口
  128. if t.AuthType == 1 {
  129. method = TBK_SC_MTL_OPT
  130. Sid := t.UserSID
  131. args["session"] = Sid
  132. _, args["site_id"], args["adzone_id"] = SplitPid(t.SelfPromoIdWeb)
  133. }
  134. resp, err := send(args, method, t.Web_AK, t.Web_SK)
  135. if err != nil {
  136. return nil, err
  137. }
  138. fmt.Println("淘宝", args)
  139. fmt.Println("淘宝", string(resp))
  140. // todo 可能还需返回的说明, 里面包含详细介绍的图片
  141. // "https://mdetail.tmall.com/templates/pages/desc?id=" + id
  142. // "https://h5.m.taobao.com/app/detail/desc.html?type=1&f=&sellerType=B&_isH5Des=true#!id=" + id
  143. // 如果是跟随官方 结构不一样
  144. if t.AuthType == 1 {
  145. var tmp scTbkScMaterialOptionalUpgradeResponse
  146. if err = json.Unmarshal(resp, &tmp); err != nil {
  147. return nil, err
  148. }
  149. if len(tmp.TbkScMaterialOptionalUpgradeResponse.ResultList.MapData) == 0 {
  150. return nil, err
  151. }
  152. data := tmp.TbkScMaterialOptionalUpgradeResponse.ResultList.MapData[0]
  153. if strings.Contains(data.ItemBasicInfo.Volume, "万+") {
  154. data.ItemBasicInfo.Volume = zhios_third_party_utils.IntToStr(zhios_third_party_utils.StrToInt(strings.ReplaceAll(data.ItemBasicInfo.Volume, "万+", "")) * 10000)
  155. }
  156. data.ItemBasicInfo.Volume = strings.ReplaceAll(data.ItemBasicInfo.Volume, "+", "")
  157. tmp1 := Material{
  158. CategoryID: data.ItemBasicInfo.CategoryId,
  159. CategoryName: data.ItemBasicInfo.CategoryName,
  160. CommissionRate: zhios_third_party_utils.Float64ToStr(zhios_third_party_utils.StrToFloat64(data.PublishInfo.IncomeRate) * 100),
  161. ItemID: data.ItemId,
  162. Nick: data.ItemBasicInfo.ShopTitle,
  163. PictURL: data.ItemBasicInfo.PictUrl,
  164. SellerID: int(data.ItemBasicInfo.SellerId),
  165. ShopTitle: data.ItemBasicInfo.ShopTitle,
  166. ShortTitle: data.ItemBasicInfo.ShortTitle,
  167. Title: data.ItemBasicInfo.Title,
  168. ZkFinalPrice: data.PricePromotionInfo.ZkFinalPrice,
  169. ReservePrice: data.PricePromotionInfo.ReservePrice,
  170. Volume: zhios_third_party_utils.StrToInt(data.ItemBasicInfo.Volume),
  171. UserType: data.ItemBasicInfo.UserType,
  172. TkTotalSales: data.ItemBasicInfo.Volume,
  173. }
  174. tmp1.SmallImages = data.ItemBasicInfo.SmallImages
  175. if len(data.ItemBasicInfo.SmallImages.String) == 0 {
  176. tmp1.SmallImages.String = []string{tmp1.PictURL}
  177. }
  178. if zhios_third_party_utils.StrToFloat64(data.PricePromotionInfo.FinalPromotionPrice) > 0 {
  179. for _, v1 := range data.PricePromotionInfo.FinalPromotionPathList.FinalPromotionPathMapData {
  180. tmp1.CouponAmount = zhios_third_party_utils.Float64ToStrByPrec(zhios_third_party_utils.StrToFloat64(v1.PromotionFee)+zhios_third_party_utils.StrToFloat64(tmp1.CouponAmount), 2)
  181. tmp1.CouponStartTime = zhios_third_party_utils.IntToStr(zhios_third_party_utils.StrToInt(v1.PromotionStartTime) / 1000)
  182. tmp1.CouponEndTime = zhios_third_party_utils.IntToStr(zhios_third_party_utils.StrToInt(v1.PromotionEndTime) / 1000)
  183. tmp1.CouponID = v1.PromotionId
  184. if v1.PromotionTitle == "商品券" || v1.PromotionTitle == "店铺券" {
  185. if zhios_third_party_utils.StrToInt64(v1.PromotionEndTime)/1000 > time.Now().Unix() {
  186. tmp1.GoodsCoupon = "1"
  187. }
  188. }
  189. }
  190. }
  191. return &tmp1, nil
  192. } else {
  193. var tmp wrapperStruct
  194. if err = json.Unmarshal(resp, &tmp); err != nil {
  195. return nil, err
  196. }
  197. if len(tmp.TbkDgMaterialOptionalResponse.ResultList.MapData) == 0 {
  198. return nil, err
  199. }
  200. data := tmp.TbkDgMaterialOptionalResponse.ResultList.MapData[0]
  201. itemID, ok := data.ItemID.(float64)
  202. if ok {
  203. data.ItemID = int64(itemID)
  204. }
  205. return &data, nil
  206. }
  207. }
  208. func (t *TB) ItemDetailByIdKey(l, bizSceneId string) (*Material, error) {
  209. l += "&"
  210. l = strings.ReplaceAll(l, "?", "&")
  211. ex := php2go.Explode("&id=", l)
  212. id := ""
  213. if len(ex) > 1 {
  214. ex1 := php2go.Explode("&", ex[1])
  215. if len(ex1) > 0 {
  216. id = ex1[0]
  217. }
  218. }
  219. if id == "" || id == "622130940777" {
  220. return nil, errors.New("找不到商品")
  221. }
  222. args := map[string]string{
  223. "num_iids": id,
  224. "session": t.SID,
  225. "platform": "2",
  226. }
  227. if bizSceneId == "2" {
  228. //args["biz_scene_id"] = "2"
  229. }
  230. method := "taobao.tbk.item.info.get"
  231. //判断是否跟随官方 需要换接口
  232. if t.AuthType == 1 {
  233. Sid := t.UserSID
  234. args["session"] = Sid
  235. }
  236. resp, err := send(args, method, t.Web_AK, t.Web_SK)
  237. if err != nil {
  238. return nil, err
  239. }
  240. var tmp tbDetailStruct
  241. if err = json.Unmarshal(resp, &tmp); err != nil {
  242. return nil, err
  243. }
  244. if len(tmp.TbkItemInfoGetResponse.Results.NBbkItem) == 0 {
  245. return nil, err
  246. }
  247. data := tmp.TbkItemInfoGetResponse.Results.NBbkItem[0]
  248. data.ItemID = data.InputNumIid
  249. itemID, ok := data.ItemID.(float64)
  250. if ok {
  251. data.ItemID = int64(itemID)
  252. }
  253. return &data, nil
  254. }