优惠券小程序

HaoDanKuApi.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/10/13
  6. * Time: 14:16
  7. */
  8. namespace App\Support;
  9. use App\Log;
  10. use App\Service\HttpService;
  11. class HaoDanKuApi
  12. {
  13. const MAX_RETRY_NUM = 3;
  14. /**
  15. * 精选文案API
  16. * @param $minId integer 分页,来源于上次获取后的数据的min_id值,默认开始请求值为1
  17. * @param $back integer 每页返回条数(请在1,2,10,20,50,100,120,200,500,1000中选择一个数值返回)
  18. * @param $retry integer 重试次数
  19. * @return mixed
  20. */
  21. public static function excellent_editor($minId, $back, $retry=0)
  22. {
  23. $requestUrl = config('haodanku.request_url').'excellent_editor?';
  24. $params['apikey'] = config('haodanku.api_key');
  25. $params['min_id'] = $minId;
  26. $params['back'] = $back;
  27. $requestUrl = $requestUrl.http_build_query($params);
  28. $result = HttpService::httpGet($requestUrl);
  29. $result = json_decode($result, 1);
  30. Log::info('excellent_editor', [
  31. 'desc' => '精选文案',
  32. 'params' => $params,
  33. 'result' => $result
  34. ], 'haodanku-api');
  35. if((!isset($result['code']) || $result['code'] !=1)){
  36. if($retry<self::MAX_RETRY_NUM) {
  37. // 请求失败 发起重试
  38. $retry++;
  39. return HaoDanKuApi::excellent_editor($minId, $back, $retry);
  40. }
  41. }
  42. return $result;
  43. }
  44. /**
  45. * 淘宝超级搜索API
  46. * 本接口同时包含好单库精选高佣商品和联盟全网商品(好单库精选高佣靠前)
  47. * @param $keyword string 搜索关键词,同时也支持宝贝ID搜索即keyword=itemid(由于存在特殊符号搜索的关键词必须进行两次urlencode编码)
  48. * @param null $searchType integer 搜索来源(1.综合(好单库+联盟);2.好单库商品;3.联盟商品)
  49. * @param $minId integer 分页,来源于上次获取后的数据的min_id值,默认开始请求值为1
  50. * @param $back integer 每页返回条数(请在1,2,10,20,50,100中选择一个数值返回)
  51. * @param null $tbP integer 淘宝分页,来源于上次获取后的数据的tb_p值,默认开始请求值为1
  52. * @param null $sort integer 0.综合,1.最新,2.销量(高到低),3.销量(低到高),4.价格(低到高),5.价格(高到低),6.佣金比例(高到低)
  53. * @param null $isTmall integer 是否只取天猫商品:0否;1是,默认是0
  54. * @param null $isCoupon integer 是否只取有券商品:0否;1是,默认是0
  55. * @param null $isShopping integer 是否只取偏远地区包邮商品:0否;1是,默认是0
  56. * @param null $limitRate integer 佣金比例过滤0~100
  57. * @param null $startPrice integer 最低原价(默认为0),例如传10则只取大于等于10元的原价商品数据
  58. * @param null $sonCategory string 淘宝子类目筛选(多个请用“,”分隔,最多支持10个)
  59. * @param $retry integer 重试次数
  60. * @return mixed
  61. */
  62. public static function supersearch($keyword, $searchType = null, $minId, $back, $tbP = null, $sort = null, $isTmall = null
  63. , $isCoupon = null, $isShopping = null, $limitRate = null, $startPrice = null, $sonCategory = null, $retry = 0)
  64. {
  65. $requestUrl = config('haodanku.request_url').'supersearch?';
  66. $params['apikey'] = config('haodanku.api_key');
  67. $params['v'] = config('haodanku.v');
  68. $params['min_id'] = $minId;
  69. $params['back'] = $back;
  70. $params['keyword'] = $keyword;
  71. if(!is_null($searchType)) $params['search_type'] = $searchType;
  72. if(!is_null($tbP)) $params['tb_p'] = $tbP;
  73. if(!is_null($sort)) $params['sort'] = $sort;
  74. if(!is_null($isTmall)) $params['is_tmall'] = $isTmall;
  75. if(!is_null($isCoupon)) $params['is_coupon'] = $isCoupon;
  76. if(!is_null($isShopping)) $params['is_shopping'] = $isShopping;
  77. if(!is_null($limitRate)) $params['limitrate'] = $limitRate;
  78. if(!is_null($startPrice)) $params['startprice'] = $startPrice;
  79. if(!is_null($sonCategory)) $params['son_category'] = $sonCategory;
  80. $requestUrl = $requestUrl.http_build_query($params);
  81. $result = HttpService::httpGet($requestUrl);
  82. $result = json_decode($result, 1);
  83. Log::info('supersearch', [
  84. 'desc' => '淘宝超级搜索',
  85. 'params' => $params,
  86. 'result' => $result
  87. ], 'haodanku-api');
  88. if((!isset($result['code']) || $result['code'] !=1)){
  89. if($retry<self::MAX_RETRY_NUM) {
  90. // 请求失败 发起重试
  91. $retry++;
  92. return HaoDanKuApi::supersearch($keyword, $searchType, $minId, $back, $tbP, $sort, $isTmall, $isCoupon
  93. , $isShopping, $limitRate, $startPrice, $sonCategory, $retry);
  94. }
  95. }
  96. return $result;
  97. }
  98. /**
  99. * 淘宝商品转链
  100. * 单个商品转链,生成淘口令
  101. * @param $itemId integer 宝贝ID
  102. * @param null $xid string 团长与下游渠道合作的特殊标识,用于统计渠道推广效果(*itemid、xid二选一 或 二选二)
  103. * @param $pid string 推广位ID(需是授权淘宝号下的推广位,如果请求的时候携带了渠道id请求,则该pid需是渠道管理下面的渠道专属推广位)
  104. * @param null $relationId string 渠道ID
  105. * @param null $specialId string 会员运营ID
  106. * @param null $externalId string 淘宝客外部用户标记,如自身系统账户ID;微信ID等
  107. * @param $tbName string 授权后的淘宝名称(多授权淘宝号时用于区分哪个淘宝账户的)
  108. * @param null $activityId string 阿里妈妈推广券ID
  109. * @param null $me string 营销计划
  110. * @param null $accessToken string 授权之后的淘宝授权token
  111. * @param null $getTaoword integer 是否返回淘口令(0.否;1.是)(默认0)
  112. * @param null $title string 商品标题(用于返回淘口令*get_taoword等于1时为必填参数*,需超过8个字符)
  113. * @param null $logo string 商品主图链接地址
  114. * @param null $isSpecial integer 特殊玩法处理:1.签到红包;2.百亿补贴;3.淘宝吃货;4.超值买返;5.淘特补贴;6.首单礼金(*玩法转链请按照官方玩法使用)
  115. * @param null $promotionType string 1-自购省,2-推广赚(代理模式专属ID,代理模式必填,非代理模式不用填写该字段)
  116. * @param null $bizSceneId string 1-动态ID转链场景,2-消费者比价场景(不填默认为1)
  117. * @param int $retry integer 重试次数
  118. * @return mixed
  119. */
  120. public static function ratesurl($itemId, $xid = null, $pid, $relationId = null, $specialId = null, $externalId = null
  121. , $tbName, $activityId = null, $me = null, $accessToken = null, $getTaoword = null, $title = null, $logo = null
  122. , $isSpecial = null, $promotionType = null, $bizSceneId = null, $retry = 0)
  123. {
  124. $requestUrl = config('haodanku.request_url').'ratesurl?';
  125. $params['apikey'] = config('haodanku.api_key');
  126. $params['itemid'] = $itemId;
  127. $params['tb_name'] = $tbName;
  128. if(!is_null($xid)) $params['xid'] = $xid;
  129. if(!is_null($pid)) $params['pid'] = $pid;
  130. if(!is_null($relationId)) $params['relation_id'] = $relationId;
  131. if(!is_null($specialId)) $params['special_id'] = $specialId;
  132. if(!is_null($externalId)) $params['external_id'] = $externalId;
  133. if(!is_null($activityId)) $params['activityid'] = $activityId;
  134. if(!is_null($me)) $params['me'] = $me;
  135. if(!is_null($accessToken)) $params['access_token'] = $accessToken;
  136. if(!is_null($getTaoword)) $params['get_taoword'] = $getTaoword;
  137. if(!is_null($title)) $params['title'] = $title;
  138. if(!is_null($logo)) $params['logo'] = $logo;
  139. if(!is_null($isSpecial)) $params['is_special'] = $isSpecial;
  140. if(!is_null($promotionType))$params['promotion_type'] = $promotionType;
  141. if(!is_null($bizSceneId)) $params['biz_scene_id'] = $bizSceneId;
  142. $result = HttpService::HttpPost($requestUrl, $params);
  143. $result = json_decode($result, 1);
  144. Log::info('ratesurl', [
  145. 'desc' => '淘宝商品转链',
  146. 'params' => $params,
  147. 'result' => $result
  148. ], 'haodanku-api');
  149. if((!isset($result['code']) || $result['code'] !=1)){
  150. if($retry<self::MAX_RETRY_NUM) {
  151. // 请求失败 发起重试
  152. $retry++;
  153. return HaoDanKuApi::ratesurl($itemId, $xid, $pid, $relationId, $specialId, $externalId, $tbName, $activityId
  154. , $me, $accessToken, $getTaoword, $title, $logo, $isSpecial, $promotionType, $bizSceneId, $retry);
  155. }
  156. }
  157. return $result;
  158. }
  159. /**
  160. * 单品详情API
  161. * 该接口基于好单库和淘宝联盟的商品数据进行搜索,优先搜索好单库优质商品
  162. * @param $itemId string 获取详情的宝贝ID(目前仅支持好单库站内商品获取详情)itemid字段支持多个商品ID传入(使用英文逗号隔开),
  163. * 例如:111,222,333;批量查询上限为10个。数据将在all_data内返回.
  164. * @param int $retry integer 重试次数
  165. * @return mixed
  166. */
  167. public static function item_detail($itemId, $retry = 0)
  168. {
  169. $requestUrl = config('haodanku.request_url').'item_detail?';
  170. $params['apikey'] = config('haodanku.api_key');
  171. $params['itemid'] = $itemId;
  172. $requestUrl = $requestUrl.http_build_query($params);
  173. $result = HttpService::httpGet($requestUrl);
  174. $result = json_decode($result, 1);
  175. Log::info('item_detail', [
  176. 'desc' => '商品详情',
  177. 'params' => $params,
  178. 'result' => $result
  179. ], 'haodanku-api');
  180. if((!isset($result['code']) || $result['code'] !=1)){
  181. if($retry<self::MAX_RETRY_NUM) {
  182. // 请求失败 发起重试
  183. $retry++;
  184. return HaoDanKuApi::item_detail($itemId, $retry);
  185. }
  186. }
  187. return $result;
  188. }
  189. /**
  190. * 热搜关键词记录API
  191. * 该接口返回好单库统计的搜索热词
  192. * @param $minId integer 分页页码
  193. * @param $back integer 每页返回条数,默认10
  194. * @param int $retry 重试次数
  195. * @return mixed
  196. */
  197. public static function hot_key($minId, $back, $retry=0)
  198. {
  199. $requestUrl = config('haodanku.request_url').'hot_key?';
  200. $params['apikey'] = config('haodanku.api_key');
  201. $params['min_id'] = $minId;
  202. $params['back'] = $back;
  203. $requestUrl = $requestUrl.http_build_query($params);
  204. $result = HttpService::httpGet($requestUrl);
  205. $result = json_decode($result, 1);
  206. Log::info('hot_key', [
  207. 'desc' => '热搜关键词记录',
  208. 'params' => $params,
  209. 'result' => $result
  210. ], 'haodanku-api');
  211. if((!isset($result['code']) || $result['code'] !=1)){
  212. if($retry<self::MAX_RETRY_NUM) {
  213. // 请求失败 发起重试
  214. $retry++;
  215. return HaoDanKuApi::hot_key($minId, $back, $retry);
  216. }
  217. }
  218. return $result;
  219. }
  220. /**
  221. * 猜你喜欢API
  222. * 该接口主要是根据传过过来的宝贝ID查出这个宝贝的二级类目,取出相同类目下的产品返回
  223. * @param $itemId string 获取猜你喜欢的类型商品的宝贝ID,可以优先获取相同二级类目下的商品
  224. * @param $back int 返回条数,默认10条,最大支持50条
  225. * @param int $retry int 重试次数
  226. * @return mixed
  227. */
  228. public static function get_similar_info($itemId, $back = null, $retry = 0)
  229. {
  230. $requestUrl = config('haodanku.request_url').'get_similar_info?';
  231. $params['apikey'] = config('haodanku.api_key');
  232. $params['itemid'] = $itemId;
  233. if(!is_null($back)) $params['back'] = $back;
  234. $requestUrl = $requestUrl.http_build_query($params);
  235. $result = HttpService::httpGet($requestUrl);
  236. $result = json_decode($result, 1);
  237. Log::info('get_similar_info', [
  238. 'desc' => '猜你喜欢',
  239. 'params' => $params,
  240. 'result' => $result
  241. ], 'haodanku-api');
  242. if((!isset($result['code']) || $result['code'] !=1)){
  243. if($retry<self::MAX_RETRY_NUM) {
  244. // 请求失败 发起重试
  245. $retry++;
  246. return HaoDanKuApi::get_similar_info($itemId, $back, $retry);
  247. }
  248. }
  249. return $result;
  250. }
  251. /**
  252. * 各大榜单API
  253. * @param $saleType integer 榜单类型:sale_type=1是实时销量榜(近2小时销量),type=2是今日爆单榜,type=3是昨日爆单榜,type=4是出单指数榜
  254. * @param null $cid integer 商品类目:0全部,1女装,2男装,3内衣,4美妆,5配饰,6鞋品,7箱包,8儿童,9母婴,10居家,11美食,12数码,13家电,14其他,15车品,16文体,17宠物
  255. * @param $minId integer 作为请求地址中获取下一页的参数值,默认是1
  256. * @param $back integer 每页返回条数(请在1,2,10,20,50,100中选择一个数值返回,最多返回top100条数据)
  257. * @param null $itemType 是否只获取营销返利商品,1是,0否
  258. * @param $retry int 重试次数
  259. * @return mixed
  260. */
  261. public static function sales_list($saleType, $minId, $back, $cid = null, $itemType = null, $retry = 0)
  262. {
  263. $requestUrl = config('haodanku.request_url').'sales_list?';
  264. $params['apikey'] = config('haodanku.api_key');
  265. $params['v'] = config('haodanku.v');
  266. $params['min_id'] = $minId;
  267. $params['back'] = $back;
  268. $params['sale_type']= $saleType;
  269. if(!is_null($cid)) $params['cid'] = $cid;
  270. if(!is_null($itemType)) $params['item_type'] = $itemType;
  271. $requestUrl = $requestUrl.http_build_query($params);
  272. $result = HttpService::httpGet($requestUrl);
  273. $result = json_decode($result, 1);
  274. Log::info('sales_list', [
  275. 'desc' => '各大榜单',
  276. 'params' => $params,
  277. 'result' => $result
  278. ], 'haodanku-api');
  279. if((!isset($result['code']) || $result['code'] !=1)){
  280. if($retry<self::MAX_RETRY_NUM) {
  281. // 请求失败 发起重试
  282. $retry++;
  283. return HaoDanKuApi::sales_list($saleType, $cid, $minId, $back, $itemType, $retry);
  284. }
  285. }
  286. return $result;
  287. }
  288. public static function fieldMapping()
  289. {
  290. return [
  291. 'itemid' => 'item_id',
  292. 'itemtitle' => 'item_title',
  293. 'itemshorttitle'=> 'item_short_title',
  294. 'itemdesc' => 'item_desc',
  295. 'itemprice' => 'item_price',
  296. 'itemsale' => 'item_sale',
  297. 'itempic' => 'item_pic',
  298. 'itempic_copy' => 'item_pic_copy',
  299. 'itemendprice' => 'item_end_price',
  300. 'shoptype' => 'shop_type',
  301. 'couponurl' => 'coupon_url',
  302. 'couponmoney' => 'coupon_money',
  303. 'videoid' => 'video_id',
  304. 'couponinfo' => 'coupon_info',
  305. 'guide_article' => 'guide_article',
  306. 'text' => 'text',
  307. 'copy_text' => 'copy_text',
  308. 'time' => 'time',
  309. 'is_live' => 'is_live',
  310. 'taobao_image' => 'taobao_image',
  311. 'original_text' => 'original_text',
  312. ];
  313. }
  314. }