CopyProductService.php 19 KB

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