CopyProductService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. namespace ln\services;
  3. use stdClass;
  4. use think\facade\Cache;
  5. /**
  6. * 复制主流商城商品
  7. * Class CopyProductService
  8. * @package ln\services
  9. */
  10. class CopyProductService
  11. {
  12. //接口地址
  13. protected static $api = [
  14. 'taobao' => 'https://api03.6bqb.com/taobao/detail', //https://api03.6bqb.com/app/taobao/detail
  15. 'tmall' => 'https://api03.6bqb.com/tmall/detail',
  16. 'jd' => 'https://api03.6bqb.com/jd/detail',
  17. 'pdd' => 'https://api03.6bqb.com/pdd/detail',
  18. 'suning' => 'https://api03.6bqb.com/suning/detail',
  19. 'alibaba' => 'https://api03.6bqb.com/alibaba/detail'
  20. ];
  21. protected static $apiKey = '';
  22. //商品默认字段
  23. protected static $productInfo = [
  24. 'cate_id' => '',
  25. 'store_name' => '',
  26. 'store_info' => '',
  27. 'unit_name' => '件',
  28. 'price' => 0,
  29. 'keyword' => '',
  30. 'ficti' => 0,
  31. 'ot_price' => 0,
  32. 'give_integral' => 0,
  33. 'postage' => 0,
  34. 'cost' => 0,
  35. 'image' => '',
  36. 'slider_image' => '',
  37. 'video_link' => '',
  38. 'add_time' => 0,
  39. 'stock' => 0,
  40. 'description' => '',
  41. 'description_images' => [],
  42. 'soure_link' => '',
  43. 'temp_id' => '',
  44. 'items' => [],
  45. 'attrs' => [],
  46. 'info' => [],
  47. ];
  48. /**
  49. * 整合
  50. * @param $url
  51. * @param $method
  52. * @param $data
  53. * @return string
  54. */
  55. public static function makeUrl(string $url, string $method, array $data)
  56. {
  57. $param = '';
  58. if (strtolower($method) == 'get' && $data) {
  59. foreach ($data as $key => $value) {
  60. $param .= '&' . $key . '=' . $value;
  61. }
  62. }
  63. return $url . '?apikey=' . self::$apiKey . $param;
  64. }
  65. /**
  66. * @param bool $status
  67. * @param string $msg
  68. * @param array $data
  69. */
  70. public static function setReturn(bool $status = true, string $msg = 'SUCCESS', array $data = [])
  71. {
  72. return ['status' => $status, 'msg' => $msg, 'data' => $data];
  73. }
  74. /**
  75. *
  76. * @param array $data
  77. */
  78. public static function getInfo(string $type = 'taobao', array $data = [], string $apikey = '')
  79. {
  80. if (!$apikey) {
  81. return self::setReturn(false, '请先去设置复制商品apiKey');
  82. }
  83. $url = self::$api[$type] ?? '';
  84. $action = $type . 'Info';
  85. $deal_action = $type . 'Deal';
  86. $method = 'get';
  87. self::$apiKey = $apikey;
  88. if (!$data || !$url || !is_callable(self::class, $action) || !is_callable(self::class, $deal_action)) {
  89. return self::setReturn(false, '暂不支持该平台商品复制');
  90. }
  91. switch ($type) {
  92. case 'taobao':
  93. case 'tmall':
  94. case 'jd':
  95. case 'pdd':
  96. $method = 'get';
  97. if (!isset($data['itemid']) || !$data['itemid'])
  98. return self::setReturn(false, '缺少商品ID');
  99. break;
  100. case 'suning':
  101. $method = 'get';
  102. if (!isset($data['itemid']) || !$data['itemid'])
  103. return self::setReturn(false, '缺少商品ID');
  104. if (!isset($data['shopid']) || !$data['shopid'])
  105. return self::setReturn(false, '缺少商户ID');
  106. break;
  107. }
  108. $url = self::makeUrl($url, $method, $data);
  109. if ($cache_info = Cache::get(md5($url))) {
  110. return self::setReturn(true, 'SUCCESS', $cache_info);
  111. }
  112. /*
  113. * 测试 节省次数用
  114. */
  115. // if (!$info = Cache::get('info'.md5($url))) {
  116. // $info = self::$action($url, $data);
  117. // Cache::set('info'.md5($url),$info);
  118. // }
  119. $info = self::$action($url, $data);
  120. if (!$info) return self::setReturn(false, '获取商品失败');
  121. $info = json_decode($info, true);
  122. if (!$info || (!in_array($info['retcode'], ['0000']))) {
  123. return self::setReturn(false, $info['data'], $info);
  124. }
  125. $result = $info['data'];
  126. /*
  127. 可能存在下一页 但是api中没有分页参数 暂留
  128. */
  129. // if (isset($info['hasNext']) && $info['hasNext']) {
  130. // $data['page'] = $info['page'] + 1;
  131. // }
  132. $result = self::$deal_action($result);
  133. if ($result['items']) {
  134. foreach ($result['items'] as $k => $item) {
  135. if ($item['value'] == '') unset($result['items'][$k]);
  136. }
  137. $result['spec_type'] = 1;
  138. $result['info'] = self::formatAttr($result['items']);
  139. }else{
  140. $result['spec_type'] = 0;
  141. $result['info'] = null;
  142. }
  143. if (!$result['image'] && $result['slider_image']) $result['image'] = $result['slider_image'][0] ?? '';
  144. if($result['description']){
  145. $result['description'] = str_replace('data-lazyload','src',$result['description']);
  146. $pattern = '/<img size=(.*?)>/';
  147. $replacement = '<img src="';
  148. $result['description'] = preg_replace($pattern,$replacement,$result['description']);
  149. $result['description'] = preg_replace('/<\/img>/','">',$result['description']);
  150. }
  151. Cache::set(md5($url), $result, 3600 * 24);
  152. return self::setReturn(true, 'SUCCESS', $result);
  153. }
  154. /**
  155. * 获取淘宝商品
  156. * @param $url
  157. * @param $data
  158. * @param string $method
  159. * @return bool|string
  160. */
  161. public static function taobaoInfo(string $url, array $data, string $method = 'get')
  162. {
  163. $info = HttpService::request($url, $method, $data);
  164. $result = false;
  165. if ($info) {
  166. $result = $info;
  167. }
  168. return $result;
  169. }
  170. /**
  171. * 处理获取淘宝的商品
  172. * @param $data
  173. * @return mixed
  174. */
  175. public static function taobaoDeal(array $data)
  176. {
  177. $info = $data['item'] ?? [];
  178. $result = self::$productInfo;
  179. if ($info) {
  180. $result['store_name'] = $info['title'] ?? '';
  181. $result['store_info'] = $info['subTitle'] ?? '';
  182. $result['slider_image'] = $info['images'] ?? '';
  183. $result['description'] = $info['desc'] ?? '';
  184. $result['description_images'] = $info['descImgs'] ?? [];
  185. $items = [];
  186. if (isset($info['props']) && $info['props']) {
  187. foreach ($info['props'] as $key => $prop) {
  188. $item['value'] = $prop['name'];
  189. $item['detail'] = [];
  190. foreach ($prop['values'] as $name) {
  191. $item['detail'][] = $name['name'];
  192. }
  193. $items[] = $item;
  194. }
  195. }
  196. $result['items'] = $items;
  197. }
  198. return $result;
  199. }
  200. /**
  201. * 获取天猫商品
  202. * @param $url
  203. * @param $data
  204. * @param string $method
  205. * @return bool|string
  206. */
  207. public static function tmallInfo(string $url, array $data, string $method = 'get')
  208. {
  209. $info = HttpService::request($url, $method, $data);
  210. $result = false;
  211. if ($info) {
  212. $result = $info;
  213. }
  214. return $result;
  215. }
  216. /**
  217. * 处理天猫商品
  218. * @param $data
  219. * @return mixed
  220. */
  221. public static function tmallDeal(array $data)
  222. {
  223. $info = $data['item'] ?? [];
  224. $result = self::$productInfo;
  225. if ($info) {
  226. $result['store_name'] = $info['title'] ?? '';
  227. $result['store_info'] = $info['subTitle'] ?? '';
  228. $result['slider_image'] = $info['images'] ?? '';
  229. $result['description'] = $info['desc'] ?? '';
  230. $result['description_images'] = $info['descImgs'] ?? [];
  231. $items = [];
  232. if (isset($info['props']) && $info['props']) {
  233. foreach ($info['props'] as $key => $prop) {
  234. $item['value'] = $prop['name'];
  235. $item['detail'] = [];
  236. foreach ($prop['values'] as $name) {
  237. $item['detail'][] = $name['name'];
  238. }
  239. $items[] = $item;
  240. }
  241. }
  242. $result['items'] = $items;
  243. }
  244. return $result;
  245. }
  246. /**
  247. * 获取京东商品
  248. * @param $url
  249. * @param $data
  250. * @param string $method
  251. * @return bool|string
  252. */
  253. public static function jdInfo(string $url, array $data, string $method = 'get')
  254. {
  255. $info = HttpService::request($url, $method, $data);
  256. $result = false;
  257. if ($info) {
  258. $result = $info;
  259. }
  260. return $result;
  261. }
  262. /**
  263. * 处理京东商品
  264. * @param $data
  265. * @return mixed
  266. */
  267. public static function jdDeal(array $data)
  268. {
  269. $info = $data['item'] ?? [];
  270. $result = self::$productInfo;
  271. if ($info) {
  272. $result['store_name'] = $info['name'] ?? '';
  273. $result['store_info'] = $result['store_name'];
  274. $result['price'] = $info['price'] ?? 0;
  275. $result['ot_price'] = $info['originalPrice'] ?? 0;
  276. $result['slider_image'] = $info['images'] ?? [];
  277. $result['description'] = $info['desc'] ?? '';
  278. $result['description_images'] = $info['descImgs'] ?? [];
  279. $items = [];
  280. if (isset($info['skuProps']) && $info['skuProps']) {
  281. foreach ($info['skuProps'] as $key => $prop) {
  282. $item['value'] = $info['saleProp'][$key] ?? '';
  283. $item['detail'] = $prop;
  284. $items[] = $item;
  285. }
  286. }
  287. $result['items'] = $items;
  288. }
  289. return $result;
  290. }
  291. /**
  292. * 获取拼多多商品
  293. * @param $url
  294. * @param $data
  295. * @param string $method
  296. * @return bool|string
  297. */
  298. public static function pddInfo(string $url, array $data, string $method = 'get')
  299. {
  300. $info = HttpService::request($url, $method, $data);
  301. $result = false;
  302. if ($info) {
  303. $result = $info;
  304. }
  305. return $result;
  306. }
  307. /**
  308. * 处理拼多多商品
  309. * @param $data
  310. * @return mixed
  311. */
  312. public static function pddDeal(array $data)
  313. {
  314. $info = $data['item'] ?? [];
  315. $result = self::$productInfo;
  316. if ($info) {
  317. $result['store_name'] = $info['goodsName'] ?? '';
  318. $result['store_info'] = $info['goodsDesc'] ?? '';
  319. $result['image'] = $info['thumbUrl'] ?? '';
  320. $result['slider_image'] = $info['banner'] ?? [];
  321. $result['video_link'] = $info['video']['videoUrl'] ?? '';
  322. $result['price'] = $info['maxNormalPrice'] ?? 0;
  323. $result['ot_price'] = $info['marketPrice'] ?? 0;
  324. $descImgs = [];
  325. if (isset($info['detail']) && $info['detail']) {
  326. foreach ($info['detail'] as $img) {
  327. if (isset($img['url']) && $img['url']) $descImgs[] = $img['url'];
  328. }
  329. }
  330. $result['description_images'] = $descImgs;
  331. $items = [];
  332. if (isset($info['skus']) && $info['skus']) {
  333. $i = $y = 0;
  334. foreach ($info['skus'] as $sku) {
  335. foreach ($sku['specs'] as $key => $spec) {
  336. if ($i == 0) $items[$y]['value'] = $spec['spec_key'];
  337. $items[$y]['detail'][] = $spec['spec_value'];
  338. $y++;
  339. }
  340. $i++;
  341. }
  342. }
  343. foreach ($items as $k => $item) {
  344. $items[$k]['detail'] = array_unique($item['detail']);
  345. }
  346. $result['items'] = $items;
  347. }
  348. return $result;
  349. }
  350. /**
  351. * 获取苏宁商品
  352. * @param $url
  353. * @param $data
  354. * @param string $method
  355. * @return bool|string
  356. */
  357. public static function suningInfo(string $url, array $data, string $method = 'get')
  358. {
  359. $info = HttpService::request($url, $method, $data);
  360. $result = false;
  361. if ($info) {
  362. $result = $info;
  363. }
  364. return $result;
  365. }
  366. /**
  367. * 处理苏宁商品
  368. * @param $data
  369. * @return mixed
  370. */
  371. public static function suningDeal(array $data)
  372. {
  373. $result = self::$productInfo;
  374. if ($data) {
  375. $result['store_name'] = $data['title'] ?? '';
  376. $result['store_info'] = $result['store_name'];
  377. $result['slider_image'] = $data['images'] ?? [];
  378. $result['price'] = $data['price'] ?? 0;
  379. $result['desc'] = $data['desc'] ?? '';
  380. $items = [];
  381. if (isset($data['passSubList']) && $data['passSubList']) {
  382. $i = 0;
  383. foreach ($data['passSubList'] as $passSUb) {
  384. $j = 0;
  385. foreach ($passSUb as $key => $sub) {
  386. if ($i == 0) $items[$j]['value'] = $key;
  387. foreach ($sub as $value) {
  388. if (isset($value['characterValueDisplayName']) && $value['characterValueDisplayName'])
  389. $items[$j]['detail'][] = $value['characterValueDisplayName'];
  390. }
  391. $j++;
  392. }
  393. $i++;
  394. }
  395. }
  396. foreach ($items as $k => $item) {
  397. $items[$k]['detail'] = array_unique($item['detail']);
  398. }
  399. $result['items'] = $items;
  400. }
  401. return $result;
  402. }
  403. /**
  404. *
  405. * @param string $url
  406. * @param array $data
  407. * @param string $method
  408. * @return bool|string
  409. */
  410. public static function alibabaInfo(string $url, array $data, string $method = 'get')
  411. {
  412. $info = HttpService::request($url, $method, $data);
  413. $result = false;
  414. if ($info) {
  415. $result = $info;
  416. }
  417. return $result;
  418. }
  419. /**
  420. * @param array $data
  421. * @return array
  422. */
  423. public static function alibabaDeal(array $data)
  424. {
  425. $result = self::$productInfo;
  426. if ($data) {
  427. $result['store_name'] = $data['title'] ?? '';
  428. $result['store_info'] = $result['store_name'];
  429. $result['slider_image'] = $data['images'] ?? [];
  430. $result['price'] = $data['price'] ?? 0;
  431. $result['description'] = $data['desc'] ?? '';
  432. $items = [];
  433. if (isset($data['skuProps']) && $data['skuProps']) {
  434. $i = 0;
  435. foreach ($data['skuProps'] as $passSUb) {
  436. $items[$i]['value'] = $passSUb['prop'];
  437. $items[$i]['detail'] = array_column($passSUb['value'], 'name');
  438. $i++;
  439. }
  440. }
  441. foreach ($items as $k => $item) {
  442. $items[$k]['detail'] = array_unique($item['detail']);
  443. }
  444. $result['items'] = $items;
  445. }
  446. return $result;
  447. }
  448. /**
  449. * 格式化规格
  450. * @param $attr
  451. * @return array
  452. */
  453. public static function formatAttr(array $attr)
  454. {
  455. $value = attr_format($attr)[1];
  456. $valueNew = [];
  457. $count = 0;
  458. foreach ($value as $key => $item) {
  459. $detail = $item['detail'];
  460. sort($item['detail'], SORT_STRING);
  461. $suk = implode(',', $item['detail']);
  462. $sukValue[$suk]['image'] = '';
  463. $sukValue[$suk]['price'] = 0;
  464. $sukValue[$suk]['cost'] = 0;
  465. $sukValue[$suk]['ot_price'] = 0;
  466. $sukValue[$suk]['stock'] = 0;
  467. $sukValue[$suk]['bar_code'] = '';
  468. $sukValue[$suk]['weight'] = 0;
  469. $sukValue[$suk]['volume'] = 0;
  470. $sukValue[$suk]['brokerage'] = 0;
  471. $sukValue[$suk]['brokerage_two'] = 0;
  472. foreach (array_keys($detail) as $k => $title) {
  473. if ($title == '') continue;
  474. $header[$k]['title'] = $title;
  475. $header[$k]['align'] = 'center';
  476. $header[$k]['minWidth'] = 120;
  477. }
  478. foreach (array_values($detail) as $k => $v) {
  479. if ($v == '') continue;
  480. $valueNew[$count]['value' . ($k + 1)] = $v;
  481. $header[$k]['key'] = 'value' . ($k + 1);
  482. }
  483. $valueNew[$count]['detail'] = $detail;
  484. $valueNew[$count]['image'] = $sukValue[$suk]['image'] ?? '';
  485. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  486. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  487. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  488. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  489. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  490. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  491. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  492. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  493. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  494. $count++;
  495. }
  496. $header[] = ['title' => '图片', 'slot' => 'image', 'align' => 'center', 'minWidth' => 80];
  497. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 95];
  498. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 95];
  499. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 95];
  500. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 95];
  501. $header[] = ['title' => '商品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 120];
  502. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 95];
  503. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 95];
  504. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  505. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  506. return $info;
  507. }
  508. }