// +---------------------------------------------------------------------- namespace crmeb\services; /** * 复制主流商城商品 * Class CopyProductService * @package crmeb\services */ class CopyProductService { //接口地址 protected static $api = [ 'taobao' => 'https://api03.6bqb.com/taobao/detail', //https://api03.6bqb.com/app/taobao/detail 'tmall' => 'https://api03.6bqb.com/tmall/detail', 'jd' => 'https://api03.6bqb.com/jd/detail', 'pdd' => 'https://api03.6bqb.com/pdd/detail', 'suning' => 'https://api03.6bqb.com/suning/detail', '1688' => 'https://api03.6bqb.com/alibaba/detail' ]; protected static $apiKey = '';//996EF05B079F8706345938A0CD7339BB //商品默认字段 protected static $productInfo = [ 'cate_id' => '', 'store_name' => '', 'store_info' => '', 'unit_name' => '件', 'price' => 0, 'keyword' => '', 'ficti' => 0, 'ot_price' => 0, 'give_integral' => 0, 'postage' => 0, 'cost' => 0, 'image' => '', 'slider_image' => '', 'video_link' => '', 'add_time' => 0, 'stock' => 0, 'description' => '', 'description_images' => [], 'soure_link' => '', 'temp_id' => '', 'items' => [], 'attrs' => [], 'info' => [], ]; /** * 整合 * @param $url * @param $method * @param $data * @return string */ public static function makeUrl(string $url, string $method, array $data) { $param = ''; if (strtolower($method) == 'get' && $data) { foreach ($data as $key => $value) { $param .= '&' . $key . '=' . $value; } } return $url . '?apikey=' . self::$apiKey . $param; } /** * @param bool $status * @param string $msg * @param array $data */ public static function setReturn(bool $status = true, string $msg = 'SUCCESS', array $data = []) { return ['status' => $status, 'msg' => $msg, 'data' => $data]; } /** * * @param array $data */ public static function getInfo(string $type = 'taobao', array $data = [], string $apikey = '') { if (!$apikey) { return self::setReturn(false, '请先去设置复制商品apiKey'); } $url = self::$api[$type] ?? ''; $type = $type == '1688' ? 'alibaba' : $type; $action = $type . 'Info'; $deal_action = $type . 'Deal'; $method = 'get'; self::$apiKey = $apikey; if (!$data || !$url || !is_callable(self::class, $action) || !is_callable(self::class, $deal_action)) { return self::setReturn(false, '暂不支持该平台商品复制'); } switch ($type) { case 'taobao': case 'tmall': case 'jd': case 'pdd': case 'alibaba': $method = 'get'; if (!isset($data['itemid']) || !$data['itemid']) return self::setReturn(false, '缺少商品ID'); break; case 'suning': $method = 'get'; if (!isset($data['itemid']) || !$data['itemid']) return self::setReturn(false, '缺少商品ID'); if (!isset($data['shopid']) || !$data['shopid']) return self::setReturn(false, '缺少商户ID'); break; } $url = self::makeUrl($url, $method, $data); if ($cache_info = CacheService::get(md5($url))) { return self::setReturn(true, 'SUCCESS', $cache_info); } $info = self::$action($url, $data); if (!$info) return self::setReturn(false, '获取商品失败'); $info = json_decode($info, true); if (!$info || (!in_array($info['retcode'], ['0000']))) { return self::setReturn(false, $info['message'] ?? '获取商品失败', $info); } $result = $info['data']; //可能存在下一页 但是api中没有分页参数 暂留 // if (isset($info['hasNext']) && $info['hasNext']) { // $data['page'] = $info['page'] + 1; // } $result = self::$deal_action($result); //过滤采集到的规格 删除其中的空值 if ($result['items']) { foreach ($result['items'] as $k => $item) { if (isset($item['value'])) { if ($item['value'] == '') unset($result['items'][$k]); if (!$item['detail'] || !isset($item['detail'][0]) || $item['detail'][0] == '') unset($result['items'][$k]); } else { unset($result['items'][$k]); } } } if (!$result['items']) { $result['items'] = [ [ 'value' => '默认', 'detail' => [ '默认' ] ] ]; } $result['info'] = self::formatAttr(array_values($result['items'])); if (!$result['image'] && $result['slider_image']) $result['image'] = $result['slider_image'][0] ?? ''; if ($result['description']) { $result['description'] = str_replace('data-lazyload', 'src', $result['description']); } CacheService::set(md5($url), $result, 3600 * 24); return self::setReturn(true, 'SUCCESS', $result); } /** * 获取淘宝商品 * @param $url * @param $data * @param string $method * @return bool|string */ public static function taobaoInfo(string $url, array $data, string $method = 'get') { $info = HttpService::request($url, $method, $data); $result = false; if ($info) { $result = $info; } return $result; } /** * 处理获取淘宝的商品 * @param $data * @return mixed */ public static function taobaoDeal(array $data) { $info = $data['item'] ?? []; $result = self::$productInfo; if ($info) { $result['store_name'] = $info['title'] ?? ''; $result['store_info'] = $info['subTitle'] ?? ''; $result['slider_image'] = $info['images'] ?? ''; $result['description'] = $info['desc'] ?? ''; $result['description_images'] = $info['descImgs'] ?? []; $items = []; if (isset($info['props']) && $info['props']) { foreach ($info['props'] as $key => $prop) { $item['value'] = $prop['name']; $item['detail'] = []; foreach ($prop['values'] as $name) { $item['detail'][] = $name['name']; } $items[] = $item; } } $result['items'] = $items; } return $result; } /** * 获取天猫商品 * @param $url * @param $data * @param string $method * @return bool|string */ public static function tmallInfo(string $url, array $data, string $method = 'get') { $info = HttpService::request($url, $method, $data); $result = false; if ($info) { $result = $info; } return $result; } /** * 处理天猫商品 * @param $data * @return mixed */ public static function tmallDeal(array $data) { $info = $data['item'] ?? []; $result = self::$productInfo; if ($info) { $result['store_name'] = $info['title'] ?? ''; $result['store_info'] = $info['subTitle'] ?? ''; $result['slider_image'] = self::lingByHttp($info['images'] ?? ''); $result['description_images'] = self::lingByHttp($info['descImgs'] ?? []); $description = ''; foreach ($result['description_images'] as $item) { $description .= ''; } unset($item); $result['description'] = $description; $items = []; if (isset($info['props']) && $info['props']) { foreach ($info['props'] as $key => $prop) { $item['value'] = $prop['name']; $item['detail'] = []; foreach ($prop['values'] as $name) { $item['detail'][] = $name['name']; } $items[] = $item; } } $result['items'] = $items; } return $result; } /** * 获取京东商品 * @param $url * @param $data * @param string $method * @return bool|string */ public static function jdInfo(string $url, array $data, string $method = 'get') { $info = HttpService::request($url, $method, $data); $result = false; if ($info) { $result = $info; } return $result; } /** * 处理京东商品 * @param $data * @return mixed */ public static function jdDeal(array $data) { $info = $data['item'] ?? []; $result = self::$productInfo; if ($info) { $result['store_name'] = $info['name'] ?? ''; $result['store_info'] = $result['store_name']; $result['price'] = $info['price'] ?? 0; $result['ot_price'] = $info['originalPrice'] ?? 0; $result['slider_image'] = $info['images'] ?? []; $result['description'] = $info['desc'] ?? ''; $result['description_images'] = $info['descImgs'] ?? []; $result['description_images'] = array_map(function ($item) { if (strstr($item, 'http') === false) { $item = 'http:' . $item; } return $item; }, $result['description_images']); if (strstr($result['description'], '