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

346 lines
11 KiB

  1. package taobao
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/md"
  4. "encoding/json"
  5. "github.com/syyongx/php2go"
  6. "strconv"
  7. "strings"
  8. )
  9. type UrlInfo struct {
  10. CategoryID int `json:"category_id"`
  11. CouponClickURL string `json:"coupon_click_url"`
  12. CouponEndTime string `json:"coupon_end_time"`
  13. CouponInfo string `json:"coupon_info"`
  14. CouponRemainCount int `json:"coupon_remain_count"`
  15. CouponStartTime string `json:"coupon_start_time"`
  16. CouponTotalCount int `json:"coupon_total_count"`
  17. ItemID string `json:"item_id"`
  18. ItemURL string `json:"item_url"`
  19. MaxCommissionRate string `json:"max_commission_rate"`
  20. MinCommissionRate string `json:"min_commission_rate"`
  21. RewardInfo int `json:"reward_info"`
  22. }
  23. // 淘宝转链
  24. func (t *TB) ConvertUrlSC(itemId, pid, externalId string) (*md.ConvertedUrls, error) {
  25. method := "taobao.tbk.privilege.get"
  26. _, siteId, adZoneId := SplitPid(pid)
  27. args := map[string]string{
  28. "item_id": itemId,
  29. "session": t.UserSID, // 一般用站长就可以转链
  30. "adzone_id": adZoneId,
  31. "platform": "2",
  32. "site_id": siteId,
  33. }
  34. if php2go.IsNumeric(itemId) {
  35. args["biz_scene_id"] = "2"
  36. }
  37. if externalId != "" {
  38. args["external_id"] = externalId
  39. }
  40. resp, err := send(args, method, t.Svc_AK, t.Svc_SK)
  41. if err != nil {
  42. return nil, err
  43. }
  44. var tmp struct {
  45. TbkPrivilegeGetResponse struct {
  46. Result struct {
  47. Data UrlInfo `json:"data"`
  48. } `json:"result"`
  49. RequestID string `json:"request_id"`
  50. } `json:"tbk_privilege_get_response"`
  51. }
  52. if err = json.Unmarshal(resp, &tmp); err != nil {
  53. return nil, err
  54. }
  55. url := tmp.TbkPrivilegeGetResponse.Result.Data.ItemURL
  56. hasCoupon := false
  57. itemUrl := tmp.TbkPrivilegeGetResponse.Result.Data.ItemURL
  58. maxCommissionRate := tmp.TbkPrivilegeGetResponse.Result.Data.MaxCommissionRate
  59. if tmp.TbkPrivilegeGetResponse.Result.Data.CouponClickURL != "" {
  60. hasCoupon = true
  61. url = tmp.TbkPrivilegeGetResponse.Result.Data.CouponClickURL
  62. }
  63. return &md.ConvertedUrls{
  64. ItemId: itemId,
  65. URL: url,
  66. ItemUrl: itemUrl,
  67. ShortenURL: url,
  68. NoOpenAppURL: url,
  69. AppURL: url,
  70. HasCoupon: hasCoupon,
  71. CommissionRate: maxCommissionRate,
  72. }, nil
  73. }
  74. func (t *TB) WnConvertUrlSC(itemId, types, pid, externalId string) (*md.ConvertedUrls, error) {
  75. method := "taobao.tbk.sc.general.link.convert"
  76. _, siteId, adZoneId := SplitPid(pid)
  77. args := map[string]string{
  78. "session": t.UserSID, // 一般用站长就可以转链
  79. "adzone_id": adZoneId,
  80. "platform": "2",
  81. "site_id": siteId,
  82. }
  83. if types == "1" || types == "4" { // 商品ID列表
  84. args["item_id_list"] = itemId
  85. args["biz_scene_id"] = "1"
  86. if externalId != "0" && externalId != "" {
  87. args["relation_id"] = externalId
  88. }
  89. }
  90. if types == "2" { //物料列表,可以为url或淘口令
  91. args["material_list"] = itemId
  92. if externalId != "0" && externalId != "" {
  93. args["relation_id"] = externalId
  94. }
  95. }
  96. if types == "3" { // 会场ID列表
  97. args["page_id_list"] = itemId
  98. if externalId != "" {
  99. args["relation_id"] = externalId
  100. }
  101. }
  102. resp, err := send(args, method, t.Svc_AK, t.Svc_SK)
  103. if err != nil {
  104. return nil, err
  105. }
  106. var tmp struct {
  107. TbkScGeneralLinkConvertResponse struct {
  108. Data struct {
  109. MaterialUrlList struct {
  110. MaterialUrlList []struct {
  111. Msg string `json:"msg"`
  112. Code int `json:"code"`
  113. PromotionInfoDto struct {
  114. CommissionRate string `json:"commission_rate"`
  115. } `json:"promotion_info_dto"`
  116. CouponInfoDto struct {
  117. CouponEndTime string `json:"coupon_end_time"`
  118. ActivityId string `json:"activity_id"`
  119. CouponRemainCount int `json:"coupon_remain_count"`
  120. CouponAmount string `json:"coupon_amount"`
  121. CouponStartTime string `json:"coupon_start_time"`
  122. CouponDesc string `json:"coupon_desc"`
  123. CouponType int `json:"coupon_type"`
  124. } `json:"coupon_info_dto"`
  125. LinkInfoDto struct {
  126. CouponLongUrl string `json:"coupon_long_url"`
  127. MaterialType int `json:"material_type"`
  128. TpwdOriginUrl string `json:"tpwd_origin_url"`
  129. MaterialId string `json:"material_id"`
  130. CpsLongUrl string `json:"cps_long_url"`
  131. CpsShortTpwd string `json:"cps_short_tpwd"`
  132. TkBizType int `json:"tk_biz_type"`
  133. CouponShortTpwd string `json:"coupon_short_tpwd"`
  134. CpsShortUrl string `json:"cps_short_url"`
  135. CouponShortUrl string `json:"coupon_short_url"`
  136. CouponFullTpwd string `json:"coupon_full_tpwd"`
  137. CpsFullTpwd string `json:"cps_full_tpwd"`
  138. } `json:"link_info_dto"`
  139. } `json:"material_url_list"`
  140. } `json:"material_url_list"`
  141. ShopUrlList struct {
  142. ShopUrlList []struct {
  143. Msg string `json:"msg"`
  144. Code int `json:"code"`
  145. LinkInfoDto struct {
  146. MaterialType int `json:"material_type"`
  147. SellerId string `json:"seller_id"`
  148. CpsLongUrl string `json:"cps_long_url"`
  149. CpsShortTpwd string `json:"cps_short_tpwd"`
  150. CpsShortUrl string `json:"cps_short_url"`
  151. CpsFullTpwd string `json:"cps_full_tpwd"`
  152. } `json:"link_info_dto"`
  153. } `json:"shop_url_list"`
  154. } `json:"shop_url_list"`
  155. EventUrlList struct {
  156. EventUrlList []struct {
  157. Msg string `json:"msg"`
  158. Code int `json:"code"`
  159. LinkInfoDto struct {
  160. MaterialType int `json:"material_type"`
  161. PageId string `json:"page_id"`
  162. CpsLongUrl string `json:"cps_long_url"`
  163. CpsShortTpwd string `json:"cps_short_tpwd"`
  164. CpsShortUrl string `json:"cps_short_url"`
  165. CpsFullTpwd string `json:"cps_full_tpwd"`
  166. } `json:"link_info_dto"`
  167. } `json:"event_url_list"`
  168. } `json:"event_url_list"`
  169. ItemUrlList struct {
  170. ItemUrlList []struct {
  171. Msg string `json:"msg"`
  172. Code int `json:"code"`
  173. PromotionInfoDto struct {
  174. CommissionRate string `json:"commission_rate"`
  175. } `json:"promotion_info_dto"`
  176. CouponInfoDto struct {
  177. CouponEndTime string `json:"coupon_end_time"`
  178. ActivityId string `json:"activity_id"`
  179. CouponRemainCount int `json:"coupon_remain_count"`
  180. CouponAmount string `json:"coupon_amount"`
  181. CouponStartTime string `json:"coupon_start_time"`
  182. CouponDesc string `json:"coupon_desc"`
  183. } `json:"coupon_info_dto"`
  184. LinkInfoDto struct {
  185. CouponLongUrl string `json:"coupon_long_url"`
  186. MaterialType int `json:"material_type"`
  187. ItemId string `json:"item_id"`
  188. CpsLongUrl string `json:"cps_long_url"`
  189. CpsShortTpwd string `json:"cps_short_tpwd"`
  190. CouponShortTpwd string `json:"coupon_short_tpwd"`
  191. CpsShortUrl string `json:"cps_short_url"`
  192. CouponShortUrl string `json:"coupon_short_url"`
  193. CouponFullTpwd string `json:"coupon_full_tpwd"`
  194. CpsFullTpwd string `json:"cps_full_tpwd"`
  195. } `json:"link_info_dto"`
  196. } `json:"item_url_list"`
  197. } `json:"item_url_list"`
  198. } `json:"data"`
  199. BizErrorDesc int `json:"biz_error_desc"`
  200. ResultMsg string `json:"result_msg"`
  201. } `json:"tbk_sc_general_link_convert_response"`
  202. }
  203. if err = json.Unmarshal(resp, &tmp); err != nil {
  204. return nil, err
  205. }
  206. url := ""
  207. hasCoupon := false
  208. itemUrl := ""
  209. maxCommissionRate := ""
  210. tkl := ""
  211. shortTkl := ""
  212. isitem := 0
  213. for _, v := range tmp.TbkScGeneralLinkConvertResponse.Data.ItemUrlList.ItemUrlList {
  214. url = v.LinkInfoDto.CouponLongUrl
  215. tkl = v.LinkInfoDto.CouponFullTpwd
  216. shortTkl = v.LinkInfoDto.CouponShortTpwd
  217. if url == "" {
  218. url = v.LinkInfoDto.CpsLongUrl
  219. tkl = v.LinkInfoDto.CpsFullTpwd
  220. shortTkl = v.LinkInfoDto.CpsShortTpwd
  221. }
  222. if v.LinkInfoDto.CouponLongUrl != "" {
  223. hasCoupon = true
  224. }
  225. itemId = v.LinkInfoDto.ItemId
  226. itemUrl = url
  227. isitem = 1
  228. maxCommissionRate = v.PromotionInfoDto.CommissionRate
  229. }
  230. for _, v := range tmp.TbkScGeneralLinkConvertResponse.Data.MaterialUrlList.MaterialUrlList {
  231. if v.LinkInfoDto.CouponLongUrl == "" && v.LinkInfoDto.CpsLongUrl == "" {
  232. continue
  233. }
  234. url = v.LinkInfoDto.CouponLongUrl
  235. tkl = v.LinkInfoDto.CouponFullTpwd
  236. shortTkl = v.LinkInfoDto.CouponShortTpwd
  237. if url == "" {
  238. url = v.LinkInfoDto.CpsLongUrl
  239. tkl = v.LinkInfoDto.CpsFullTpwd
  240. shortTkl = v.LinkInfoDto.CpsShortTpwd
  241. }
  242. if v.LinkInfoDto.CouponLongUrl != "" {
  243. hasCoupon = true
  244. }
  245. //1—单品 2—店铺 3—会场 4-承接开放 5-优惠券
  246. if v.Code == 0 && (v.LinkInfoDto.MaterialType == 1 || v.LinkInfoDto.MaterialType == 0) {
  247. itemId = v.LinkInfoDto.MaterialId
  248. isitem = 1
  249. }
  250. itemUrl = url
  251. maxCommissionRate = v.PromotionInfoDto.CommissionRate
  252. }
  253. if isitem == 0 && len(tmp.TbkScGeneralLinkConvertResponse.Data.MaterialUrlList.MaterialUrlList) > 0 {
  254. itemId = ""
  255. }
  256. for _, v := range tmp.TbkScGeneralLinkConvertResponse.Data.EventUrlList.EventUrlList {
  257. url = v.LinkInfoDto.CpsLongUrl
  258. tkl = v.LinkInfoDto.CpsFullTpwd
  259. shortTkl = v.LinkInfoDto.CpsShortTpwd
  260. itemUrl = url
  261. }
  262. isDown := "0"
  263. if tkl == "" {
  264. isDown = "2"
  265. }
  266. if strings.Contains(string(resp), "该商品已下架") {
  267. isDown = "1"
  268. }
  269. return &md.ConvertedUrls{
  270. IsDown: isDown,
  271. Tkl: tkl,
  272. ShortTkl: shortTkl,
  273. ItemId: itemId,
  274. URL: url,
  275. ItemUrl: itemUrl,
  276. ShortenURL: url,
  277. NoOpenAppURL: url,
  278. AppURL: url,
  279. HasCoupon: hasCoupon,
  280. CommissionRate: maxCommissionRate,
  281. }, nil
  282. }
  283. type T struct {
  284. TbkScActivityInfoGetResponse struct {
  285. Data struct {
  286. ClickUrl string `json:"click_url"`
  287. PageEndTime string `json:"page_end_time"`
  288. PageName string `json:"page_name"`
  289. PageStartTime string `json:"page_start_time"`
  290. TerminalType string `json:"terminal_type"`
  291. } `json:"data"`
  292. RequestId string `json:"request_id"`
  293. } `json:"tbk_sc_activity_info_get_response"`
  294. }
  295. func (t *TB) ActivityConvertUrlSC(activityMaterialId, pid string, relationId int64) (*md.OfficialRecommendUrl, error) {
  296. method := "taobao.tbk.sc.activity.info.get"
  297. _, siteId, adZoneId := SplitPid(pid)
  298. args := map[string]string{
  299. "activity_material_id": activityMaterialId,
  300. "session": t.UserSID, // 一般用站长就可以转链
  301. "adzone_id": adZoneId,
  302. "platform": "2",
  303. "site_id": siteId,
  304. }
  305. if relationId > 0 {
  306. args["relation_id"] = strconv.FormatInt(relationId, 10)
  307. }
  308. resp, err := send(args, method, t.Svc_AK, t.Svc_SK)
  309. if err != nil {
  310. return nil, err
  311. }
  312. var tmp struct {
  313. TbkScActivityInfoGetResponse struct {
  314. Data struct {
  315. WxQrcodeUrl string `json:"wx_qrcode_url"`
  316. WxMiniprogramPath string `json:"wx_miniprogram_path"`
  317. Url string `json:"click_url"`
  318. ShortClickurl string `json:"short_click_url"`
  319. } `json:"data"`
  320. } `json:"tbk_sc_activity_info_get_response"`
  321. }
  322. if err = json.Unmarshal(resp, &tmp); err != nil {
  323. return nil, err
  324. }
  325. url := tmp.TbkScActivityInfoGetResponse.Data.Url
  326. if tmp.TbkScActivityInfoGetResponse.Data.ShortClickurl != "" {
  327. url = tmp.TbkScActivityInfoGetResponse.Data.ShortClickurl
  328. }
  329. return &md.OfficialRecommendUrl{
  330. WxQrcodeUrl: tmp.TbkScActivityInfoGetResponse.Data.WxQrcodeUrl,
  331. WxMiniprogramPath: tmp.TbkScActivityInfoGetResponse.Data.WxMiniprogramPath,
  332. Url: url,
  333. }, nil
  334. }