123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/10/14
- * Time: 17:48
- */
- namespace App\Service;
- use App\Log;
- use App\Models\Redis\RedisModel;
- use App\Models\UserInterestGoods;
- use App\Support\HaoDanKuApi;
- use App\Support\TaoBaoApi;
- class GoodsService
- {
- public static function salesTypeList()
- {
- return [
- ['title' => '好货推荐', 'value' => 1],
- ['title' => '今日搜榜', 'value' => 2],
- ];
- }
- public static function salesList($minId, $pageSize, $saleType)
- {
- # 获取本接口推广位ID
- $promotionId = GoodsCommonService::getPromotionId();
- # 请求数据
- $list = HaoDanKuApi::sales_list($saleType, $minId, $pageSize);
- $redisKey = 'yhq-smallapp::salesList-'.$saleType.'-'.$minId.'-'.$pageSize;
- # 将推广位ID拼接进去
- if(isset($list['code']) && 1 == $list['code']) {
- RedisModel::set($redisKey, json_encode($list, 256));
- RedisModel::expire($redisKey, 7*86400);# 缓存7天
- } else {
- $redisValue = RedisModel::get($redisKey);
- if(!empty($redisValue)) {
- $redisValue = json_decode($redisValue, 1);
- $list = $redisValue;
- }
- }
- $fieldMapping = HaoDanKuApi::fieldMapping();
- $res = [];
- # 处理图片链接等信息
- if(isset($list['data']) && !empty($list['data'])){
- foreach($list['data'] as $value) {
- $item = [];
- foreach($value as $k=>$v) {
- $newKey = $fieldMapping[$k] ?? null;
- if(!empty($newKey)) {
- $item[$newKey] = $v;
- }
- }
- $item['item_pic'] = isset($item['item_pic']) ? $item['item_pic'].'_310x310.jpg' : '';
- $item['promotion_id'] = $promotionId;
- $item['shop_type'] = GoodsCommonService::getShopType($item['shop_type'], 1);
- $res[] = $item;
- }
- }
- $data = [
- 'data' => $res,
- 'min_id' => $list['min_id'] ?? null,
- ];
- return $data;
- }
- public static function excellentEditor($minId, $pageSize)
- {
- # 获取本接口推广位ID
- $promotionId = GoodsCommonService::getPromotionId();
- # 请求数据
- $list = HaoDanKuApi::excellent_editor($minId, $pageSize);
- $redisKey = 'yhq-smallapp::excellentEditor-'.$minId.'-'.$pageSize;
- # 将推广位ID拼接进去
- if(isset($list['code']) && 1 == $list['code']) {
- RedisModel::set($redisKey, json_encode($list, 256));
- RedisModel::expire($redisKey, 7*86400);# 缓存7天
- } else {
- $redisValue = RedisModel::get($redisKey);
- if(!empty($redisValue)) {
- $redisValue = json_decode($redisValue, 1);
- $list = $redisValue;
- }
- }
- $fieldMapping = HaoDanKuApi::fieldMapping();
- $itemIdList = [];
- $res = [];
- # 处理图片链接等信息
- if(isset($list['data']) && !empty($list['data'])){
- foreach($list['data'] as $value) {
- $item = [];
- foreach($value as $k=>$v) {
- $newKey = $fieldMapping[$k] ?? null;
- if(!empty($newKey)) {
- $item[$newKey] = $v;
- }
- }
- $item['item_pic'] = isset($item['item_pic']) ? $item['item_pic'].'_310x310.jpg' : '';
- $item['time'] = isset($item['time']) ? date('Y-m-d H:i:s', $item['time']) : '';
- $item['copy_text'] = str_replace("<br>", '<br>', $item['copy_text']);
- $item['promotion_id'] = $promotionId;
- $item['shop_type'] = GoodsCommonService::getShopType($item['shop_type'], 1);
- $itemIdList[] = $item['item_id'];
- $res[] = $item;
- }
- }
- # 优选接口不会返回商品标题,只有短标题,需要额外通过查询商品详情页获取商品标题
- if(count($itemIdList) <= 10) {
- $itemIdList = implode(',', $itemIdList);
- $itemInfoList = self::itemDetail($itemIdList, $promotionId);
- $itemTitleList = [];
- foreach($itemInfoList as $value) {
- $itemTitleList[$value['item_id']] = $value['item_title'];
- }
- } else {
- $itemIdArr = array_chunk($itemIdList, 10);
- $itemTitleList = [];
- foreach($itemIdArr as $items) {
- $items = implode(',', $items);
- $itemInfoList = self::itemDetail($items, $promotionId);
- foreach($itemInfoList as $value) {
- $itemTitleList[$value['item_id']] = $value['item_title'];
- }
- }
- }
- foreach($res as $k=>$v) {
- $res[$k]['item_title'] = $itemTitleList[$v['item_id']] ?? '';
- }
- $data = [
- 'data' => $res,
- 'min_id' => $list['min_id'] ?? null,
- ];
- return $data;
- }
- public static function goodsSearch($userId, $minId, $pageSize, $keyword, $isCoupon, $sortNum, $tbP, $pageNo)
- {
- # 记录用户搜索关键词
- UserService::userSearch($keyword, $userId);
- # 获取本接口推广位ID
- $promotionId = GoodsCommonService::getPromotionId();
- # 好单库
- $list = GoodsService::superSearch($minId, $pageSize, $keyword, $isCoupon, $sortNum, $tbP, $promotionId);
- if(empty($list)) {
- # 淘宝客
- $list = GoodsService::materialSearch($minId, $pageSize, $keyword, $isCoupon, $sortNum, $tbP, $pageNo, $promotionId);
- }
- return $list;
- }
- # 好单库 淘宝超级搜索API
- public static function superSearch($minId, $pageSize, $keyword, $isCoupon, $sortNum, $tbP, $promotionId)
- {
- # 处理排序字段
- switch($sortNum){
- case 1: $sort = 0; break; // 综合
- case 2: $sort = 2; break; // 销量高到低
- case 3: $sort = 4; break; // 价格低到高
- case 4: $sort = 5; break; // 价格高到低
- default:$sort = 0; break;
- }
- $list = HaoDanKuApi::supersearch($keyword, null, $minId, $pageSize, $tbP, $sort, null, $isCoupon);
- $fieldMapping = HaoDanKuApi::fieldMapping();
- $data = [];
- if(isset($list['code']) && 1 == $list['code']) {
- $res = [];
- # 处理图片链接
- if(isset($list['data']) && !empty($list['data'])){
- foreach($list['data'] as $value) {
- $item = [];
- foreach($value as $k=>$v) {
- $newKey = $fieldMapping[$k] ?? null;
- if(!empty($newKey)) {
- $item[$newKey] = $v;
- }
- }
- $item['item_pic'] = isset($item['item_pic']) ? $item['item_pic'].'_310x310.jpg' : '';
- $item['promotion_id'] = $promotionId;
- $item['shop_type'] = GoodsCommonService::getShopType($item['shop_type'], 1);
- $res[] = $item;
- }
- }
- $data = [
- 'data' => $res,
- 'min_id' => $list['min_id'] ?? null,
- 'tb_p' => $list['tb_p'] ?? null,
- ];
- }
- return $data;
- }
- # 淘宝联盟 物料搜索API
- public static function materialSearch($startId, $pageSize, $keyword, $isCoupon, $sortNum, $tbP, $pageNo, $promotionId)
- {
- $method = 'taobao.tbk.dg.material.optional';
- $paramsArr['page_size'] = $pageSize;
- $paramsArr['q'] = $keyword;
- // $paramsArr['page_result_key'] = $tbP;
- $paramsArr['adzone_id'] = '114488850210';
- $paramsArr['page_no'] = $pageNo;
- if(!empty($promotionId)){
- $arr = explode('_', $promotionId);
- $paramsArr['adzone_id'] = $arr[3] ?? $paramsArr['adzone_id'];
- }
- switch($sortNum){
- case 1: break;// 综合
- case 2: $paramsArr['sort'] = 'total_sales_des'; break;// 销量从高到低
- case 3: $paramsArr['sort'] = 'price_asc'; break;// 价格低到高
- case 4: $paramsArr['sort'] = 'price_des'; break;// 价格高到低
- default: break;
- }
- if(1 == $isCoupon) {
- $paramsArr['has_coupon'] = true;
- }
- $data = TaoBaoApi::createRequest($method, $paramsArr);
- $list = $data['tbk_dg_material_optional_response']['result_list']['map_data'] ?? [];
- $total = $data['tbk_dg_material_optional_response']['total_results'] ?? 0;
- $pageResultKey = $data['tbk_dg_material_optional_response']['page_result_key'] ?? null;
- $fieldMapping = TaoBaoApi::fieldMapping();
- $res = [];
- foreach($list as $value) {
- $item = [];
- foreach($value as $k=>$v) {
- $newKey = $fieldMapping[$k] ?? null;
- if(!empty($newKey)) {
- $item[$newKey] = $v;
- }
- }
- $item['shop_type'] = GoodsCommonService::getShopType($item['shop_type'], 2);
- $item['item_price'] = isset($item['zk_final_price']) ? $item['zk_final_price'] :
- (isset($item['item_price']) ? $item['item_price'] : 0);
- $item['item_end_price'] = (isset($item['coupon_money']) && $item['item_price'] > 0) ?
- $item['item_price'] - $item['coupon_money'] : $item['item_price'];
- $item['promotion_id'] = $promotionId;
- $res[] = $item;
- }
- $data = [
- 'data' => $res,
- 'min_id' => null,
- 'tb_p' => $pageResultKey,
- ];
- return $data;
- }
- public static function goodsDetail($itemId, $promotionId, $userId=null, $isMultiple = null)
- {
- # 处理商品ID
- if(is_array($itemId)) {
- $itemId = implode(',', $itemId);
- }
- # 好单库
- $data = GoodsService::itemDetail($itemId, $promotionId);
- if(empty($data) && !$isMultiple) {
- # 淘宝客
- $data = GoodsService::itemInfo($itemId, $promotionId);
- }
- # 判断商品是否已收藏
- if(!$isMultiple) {
- $hadCollect = UserInterestGoods::hadInterest($userId, $itemId);
- $data['is_collect'] = $hadCollect ? 1 : 0;
- }
- return $data;
- }
- # 好单库商品详情
- public static function itemDetail($itemId, $promotionId)
- {
- # 请求数据
- $res = HaoDanKuApi::item_detail($itemId);
- $data = [];
- if(isset($res['code']) && 1 == $res['code']) {
- $fieldMapping = HaoDanKuApi::fieldMapping();
- # 单独一个商品信息
- if(!empty($res['data'])) {
- foreach($res['data'] as $k=>$v) {
- $newKey = $fieldMapping[$k] ?? null;
- if(!empty($newKey)) {
- $data[$newKey] = $v;
- }
- }
- $data['shop_type'] = GoodsCommonService::getShopType($data['shop_type'], 1);
- $data['item_pic'] = !empty($data['item_pic']) ? $data['item_pic'].'_310x310.jpg' : '';
- $data['item_pic_copy'] = !empty($data['item_pic_copy']) ? 'http://img-haodanku-com.cdn.fudaiapp.com/'.$data['item_pic_copy'] : '';
- $data['taobao_image'] = !empty($data['taobao_image']) ? explode(',', $data['taobao_image']) : [];
- $data['video_url'] = (!empty($data['video_id']) && $data['video_id'] > 0) ?
- 'http://cloud.video.taobao.com/play/u/1/p/1/e/6/t/1/' . $data['video_id'] . '.mp4' : '';
- $data['item_title'] = $data['item_title'] ?? '';
- # 单个商品调取时才会直接返回淘口令
- $taoWordInfo = self::createTaobaoWord($itemId, $promotionId, $data['item_title']);
- $data['taoword'] = $taoWordInfo['taoword'];
- $data['model'] = $taoWordInfo['model'];
- # 判断返回的商品ID是否与请求商品ID一致
- if($itemId != $data['item_id']) {
- $data['item_id'] = $itemId;
- }
- }
- # 一次返回多个商品信息
- if(!empty($res['all_data'])) {
- $itemIdArr = explode(',', $itemId);
- foreach($res['all_data'] as $value) {
- $item = [];
- foreach($value as $k=>$v) {
- $newKey = $fieldMapping[$k] ?? null;
- if(!empty($newKey)) {
- $item[$newKey] = $v;
- }
- }
- $item['shop_type'] = GoodsCommonService::getShopType($item['shop_type'], 1);
- $item['item_pic'] = !empty($item['item_pic']) ? $item['item_pic'].'_310x310.jpg' : '';
- $item['item_pic_copy'] = !empty($item['item_pic_copy']) ? 'http://img-haodanku-com.cdn.fudaiapp.com/'.$item['item_pic_copy'] : '';
- $item['taobao_image'] = !empty($item['taobao_image']) ? explode(',', $item['taobao_image']) : [];
- $item['video_url'] = (!empty($item['video_id']) && $item['video_id'] > 0) ?
- 'http://cloud.video.taobao.com/play/u/1/p/1/e/6/t/1/' . $item['video_id'] . '.mp4' : '';
- if(!in_array($item['item_id'], $itemIdArr)) {
- continue;
- }
- $data[] = $item;
- }
- }
- }
- return $data;
- }
- # 淘宝客商品详情
- public static function itemInfo($itemId, $promotionId)
- {
- $method = 'taobao.tbk.item.info.get';
- $paramsArr['num_iids'] = $itemId;
- $res = TaoBaoApi::createRequest($method, $paramsArr);
- $list = $res['tbk_item_info_get_response']['results']['n_tbk_item'] ?? [];
- $fieldMapping = TaoBaoApi::fieldMapping();
- $data = [];
- foreach($list as $value) {
- $item = [];
- foreach($value as $k=>$v) {
- $newKey = $fieldMapping[$k] ?? null;
- if(!empty($newKey)) {
- $item[$newKey] = $v;
- }
- }
- $item['item_pic_copy'] = '';
- $item['taobao_image'] = $item['taobao_image']['string'] ?? [];
- $item['video_url'] = '';
- $item['is_live'] = 0;
- $item['shop_type'] = GoodsCommonService::getShopType($item['shop_type'], 1);
- # 查询商品券信息
- $keyword = GoodsCommonService::getTaobaoUrl($itemId);
- $itemInfo = GoodsService::materialSearch(null, 1, $keyword, null, 1, null, 1, $promotionId);
- // Log::info('test', $itemInfo, 'test_ss');
- $itemInfo = !empty($itemInfo['data'][0]) ? $itemInfo['data'][0] : [];
- $item['coupon_money'] = $itemInfo['coupon_money'] ?? 0;
- $item['coupon_url'] = !empty($itemInfo['coupon_url']) ? 'https:'.$itemInfo['coupon_url'] : '';
- $item['coupon_info'] = $itemInfo['coupon_info'] ?? '';
- $item['item_desc'] = $itemInfo['item_desc'] ?? '';
- $item['item_short_title'] = $itemInfo['item_short_title'] ?? '';
- $item['item_price'] = isset($item['zk_final_price']) ? $item['zk_final_price'] :
- (isset($item['item_price']) ? $item['item_price'] : 0);
- $item['item_end_price'] = (isset($item['coupon_money']) && $item['item_price'] > 0) ?
- $item['item_price'] - $item['coupon_money'] : $item['item_price'];
- # 获取商品淘口令
- $itemUrl = !empty($itemInfo['url']) ? $itemInfo['url'] :
- (!empty($itemInfo['coupon_url']) ? $itemInfo['coupon_url'] : '');
- $itemUrl = 'https:'.$itemUrl;
- $taoWordInfo = GoodsService::tpwdCreate($itemUrl);
- $item['taoword'] = $taoWordInfo['taoword'];
- $item['model'] = $taoWordInfo['model'];
- $data[] = $item;
- }
- if(1 == count($data)) {
- return $data[0];
- }
- return $data;
- }
- public static function sameGoods($itemId)
- {
- # 请求数据
- $res = HaoDanKuApi::get_similar_info($itemId);
- $promotionId = GoodsCommonService::getPromotionId();
- $data = [];
- if(isset($res['code']) && 1 == $res['code']) {
- $fieldMapping = HaoDanKuApi::fieldMapping();
- # 单独一个商品信息
- if (!empty($res['data'])) {
- foreach($res['data'] as $value) {
- $item = [];
- foreach($value as $k=>$v) {
- $newKey = $fieldMapping[$k] ?? null;
- if(!empty($newKey)) {
- $item[$newKey] = $v;
- }
- }
- $item['item_pic'] = !empty($item['item_pic']) ? $item['item_pic'].'_310x310.jpg' : '';
- $item['item_pic_copy'] = !empty($item['item_pic_copy']) ? 'http://img-haodanku-com.cdn.fudaiapp.com/'.$item['item_pic_copy'] : '';
- $item['promotion_id'] = $promotionId;
- $item['shop_type'] = GoodsCommonService::getShopType($item['shop_type'], 1);
- $data[] = $item;
- }
- }
- }
- return $data;
- }
- public static function createTaobaoWord($itemId, $promotionId, $itemTitle)
- {
- # 是否直接通过好单库接口获取淘口令
- $getTaobaoWord = false;
- if(!empty($itemTitle)) {
- if(mb_strlen($itemTitle) >= 8) {
- $getTaobaoWord = true;
- }
- }
- # 好单库转链
- $res = GoodsService::rateUrl($itemId, $promotionId, $getTaobaoWord, $itemTitle);
- # 如果好单库接口可以直接生成淘口令则直接返回
- if(!empty($res)) {
- if(!empty($res['taoword'])) {
- return [
- 'taoword' => $res['taoword'],
- 'model' => $res['new_model'],
- ];
- } else {
- $itemUrl = !empty($res['item_url']) ? $res['item_url'] :
- (!empty($res['coupon_click_url']) ? $res['coupon_click_url'] : '');
- }
- }
- if(empty($itemUrl)) {
- # 淘宝搜索接口获取链接
- $keyword = GoodsCommonService::getTaobaoUrl($itemId);
- $itemInfo = GoodsService::materialSearch(null, 1, $keyword, null, 1, null, 1, $promotionId);
- $itemInfo = !empty($itemInfo['data'][0]) ? $itemInfo['data'][0] : [];
- $itemUrl = !empty($itemInfo['url']) ? $itemInfo['url'] :
- (isset($itemInfo['coupon_share_url']) && !empty($itemInfo['coupon_share_url'] ? $itemInfo['coupon_share_url'] : ''));
- $itemUrl = 'https:'.$itemUrl;
- }
- $response = GoodsService::tpwdCreate($itemUrl);
- return $response;
- }
- public static function rateUrl($itemId, $promotionId, $getTaobaoWord, $itemTitle)
- {
- $tbName = config('haodanku.tb_name');
- $response = HaoDanKuApi::ratesurl($itemId, null, $promotionId, null, null, null, $tbName
- ,null, null, null, $getTaobaoWord, $itemTitle);
- if(isset($response['code']) && 1 == $response['code']) {
- # 请求成功
- return $response['data'];
- }
- return [];
- }
- public static function tpwdCreate($itemUrl)
- {
- if(empty($itemUrl)){
- return [
- 'taoword' => null,
- 'model' => null,
- ];
- }
- $method = 'taobao.tbk.tpwd.create';
- $paramsArr['url'] = $itemUrl;
- $res = TaoBaoApi::createRequest($method, $paramsArr);
- $data = $res['tbk_tpwd_create_response']['data'] ?? [];
- return [
- 'taoword' => $data['password_simple'] ?? null,
- 'model' => $data['model'] ?? null,
- ];
- }
- }
|