CopyProductService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/23
  6. */
  7. namespace crmeb\services;
  8. use think\facade\Cache;
  9. /**
  10. * 复制主流商城商品
  11. * Class CopyProductService
  12. * @package crmeb\services
  13. */
  14. class CopyProductService
  15. {
  16. //接口地址
  17. protected static $api = [
  18. 'taobao' => 'https://api03.6bqb.com/taobao/detail', //https://api03.6bqb.com/app/taobao/detail
  19. 'tmall' => 'https://api03.6bqb.com/tmall/detail',
  20. 'jd' => 'https://api03.6bqb.com/jd/detail',
  21. 'pdd' => 'https://api03.6bqb.com/pdd/detail',
  22. 'suning' => 'https://api03.6bqb.com/suning/detail',
  23. ];
  24. protected static $apiKey = '';
  25. //商品默认字段
  26. protected static $productInfo = [
  27. 'cate_id' => '',
  28. 'store_name' => '',
  29. 'store_info' => '',
  30. 'unit_name' => '件',
  31. 'price' => 0,
  32. 'keyword' => '',
  33. 'ficti' => 0,
  34. 'ot_price' => 0,
  35. 'give_integral' => 0,
  36. 'postage' => 0,
  37. 'cost' => 0,
  38. 'image' => '',
  39. 'slider_image' => '',
  40. 'video_link' => '',
  41. 'add_time' => 0,
  42. 'stock' => 0,
  43. 'description' => '',
  44. 'description_images' => [],
  45. 'soure_link' => '',
  46. 'temp_id' => '',
  47. 'items' => [],
  48. 'attrs' => [],
  49. 'info' => [],
  50. ];
  51. /**
  52. * 整合
  53. * @param $url
  54. * @param $method
  55. * @param $data
  56. * @return string
  57. */
  58. public static function makeUrl(string $url, string $method, array $data)
  59. {
  60. $param = '';
  61. if (strtolower($method) == 'get' && $data) {
  62. foreach ($data as $key => $value) {
  63. $param .= '&' . $key . '=' . $value;
  64. }
  65. }
  66. return $url . '?apikey=' . self::$apiKey . $param;
  67. }
  68. /**
  69. * @param bool $status
  70. * @param string $msg
  71. * @param array $data
  72. */
  73. public static function setReturn(bool $status = true, string $msg = 'SUCCESS', array $data = [])
  74. {
  75. return ['status' => $status, 'msg' => $msg, 'data' => $data];
  76. }
  77. /**
  78. *
  79. * @param array $data
  80. */
  81. public static function getInfo(string $type = 'taobao', array $data = [], string $apikey = '')
  82. {
  83. if (!$apikey) {
  84. return self::setReturn(false, '请先去设置复制商品apiKey');
  85. }
  86. $url = self::$api[$type] ?? '';
  87. $action = $type . 'Info';
  88. $deal_action = $type . 'Deal';
  89. $method = 'get';
  90. self::$apiKey = $apikey;
  91. if (!$data || !$url || !is_callable(self::class, $action) || !is_callable(self::class, $deal_action)) {
  92. return self::setReturn(false, '暂不支持该平台商品复制');
  93. }
  94. switch ($type) {
  95. case 'taobao':
  96. case 'tmall':
  97. case 'jd':
  98. case 'pdd':
  99. $method = 'get';
  100. if (!isset($data['itemid']) || !$data['itemid'])
  101. return self::setReturn(false, '缺少商品ID');
  102. break;
  103. case 'suning':
  104. $method = 'get';
  105. if (!isset($data['itemid']) || !$data['itemid'])
  106. return self::setReturn(false, '缺少商品ID');
  107. if (!isset($data['shopid']) || !$data['shopid'])
  108. return self::setReturn(false, '缺少商户ID');
  109. break;
  110. }
  111. $url = self::makeUrl($url, $method, $data);
  112. if ($cache_info = Cache::get(md5($url))) {
  113. return self::setReturn(true, 'SUCCESS', $cache_info);
  114. }
  115. /*
  116. * 测试 节省次数用
  117. if (!$info = Cache::get('info'.md5($url))) {
  118. $info = self::$action($url, $data);
  119. Cache::set('info'.md5($url),$info);
  120. }
  121. */
  122. $info = self::$action($url, $data);
  123. if (!$info) return self::setReturn(false, '获取商品失败');
  124. $info = json_decode($info, true);
  125. if (!$info || (!in_array($info['retcode'], ['0000']))) {
  126. return self::setReturn(false, $info['data'], $info);
  127. }
  128. $result = $info['data'];
  129. /*
  130. //可能存在下一页 但是api中没有分页参数 暂留
  131. if (isset($info['hasNext']) && $info['hasNext']) {
  132. $data['page'] = $info['page'] + 1;
  133. }
  134. */
  135. $result = self::$deal_action($result);
  136. if ($result['items']) {
  137. foreach ($result['items'] as $k => $item) {
  138. if ($item['value'] == '') unset($result['items'][$k]);
  139. }
  140. $result['info'] = self::formatAttr($result['items']);
  141. }
  142. if (!$result['image'] && $result['slider_image'])
  143. $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 $attr
  406. * @return array
  407. */
  408. public static function formatAttr(array $attr)
  409. {
  410. $value = attr_format($attr)[1];
  411. $valueNew = [];
  412. $count = 0;
  413. foreach ($value as $key => $item) {
  414. $detail = $item['detail'];
  415. sort($item['detail'], SORT_STRING);
  416. $suk = implode(',', $item['detail']);
  417. $sukValue[$suk]['image'] = '';
  418. $sukValue[$suk]['price'] = 0;
  419. $sukValue[$suk]['cost'] = 0;
  420. $sukValue[$suk]['ot_price'] = 0;
  421. $sukValue[$suk]['stock'] = 0;
  422. $sukValue[$suk]['bar_code'] = '';
  423. $sukValue[$suk]['weight'] = 0;
  424. $sukValue[$suk]['volume'] = 0;
  425. $sukValue[$suk]['brokerage'] = 0;
  426. $sukValue[$suk]['brokerage_two'] = 0;
  427. foreach (array_keys($detail) as $k => $title) {
  428. if ($title == '') continue;
  429. $header[$k]['title'] = $title;
  430. $header[$k]['align'] = 'center';
  431. $header[$k]['minWidth'] = 120;
  432. }
  433. foreach (array_values($detail) as $k => $v) {
  434. if ($v == '') continue;
  435. $valueNew[$count]['value' . ($k + 1)] = $v;
  436. $header[$k]['key'] = 'value' . ($k + 1);
  437. }
  438. $valueNew[$count]['detail'] = $detail;
  439. $valueNew[$count]['image'] = $sukValue[$suk]['image'] ?? '';
  440. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  441. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  442. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  443. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  444. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  445. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  446. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  447. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  448. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  449. $count++;
  450. }
  451. $header[] = ['title' => '图片', 'slot' => 'image', 'align' => 'center', 'minWidth' => 80];
  452. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 95];
  453. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 95];
  454. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 95];
  455. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 95];
  456. $header[] = ['title' => '商品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 120];
  457. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 95];
  458. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 95];
  459. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  460. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  461. return $info;
  462. }
  463. }