CvtaobaoController.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Seller\Controller;
  15. use Admin\Model\GoodsModel;
  16. class CvtaobaoController extends CommonController{
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->breadcrumb1='商品管理';
  20. $this->breadcrumb2='采集淘宝商品';
  21. }
  22. function index(){
  23. //SELLERUID
  24. $this->display();
  25. }
  26. function caiji_ajax()
  27. {
  28. $arr_data = I('post.');
  29. $itemid = $arr_data['id'];
  30. $arr = $arr_data['ld_data'];
  31. //$itemInfoModel = $arr['data']['itemInfoModel'];
  32. $data = $arr['data'];
  33. $itemInfoModel = $data['itemInfoModel'];
  34. $item = array();
  35. $item['id'] = $g['id'];
  36. $item['merchid'] = $merchid;
  37. if (!(empty($merchid)))
  38. {
  39. if (empty($_W['merch_user']['goodschecked']))
  40. {
  41. $item['checked'] = 1;
  42. }
  43. else
  44. {
  45. $item['checked'] = 0;
  46. }
  47. }
  48. $item['pcate'] = $pcate;
  49. $item['ccate'] = $ccate;
  50. $item['tcate'] = $tcate;
  51. $item['cates'] = $pcate . ',' . $ccate . ',' . $tcate;
  52. $item['itemId'] = $itemInfoModel['itemId'];
  53. $item['title'] = $itemInfoModel['title'];
  54. $item['pics'] = $itemInfoModel['picsPath'];
  55. $params = array();
  56. if (isset($data['props']))
  57. {
  58. $props = $data['props'];
  59. foreach ($props as $pp )
  60. {
  61. $params[] = array('title' => $pp['name'], 'value' => $pp['value']);
  62. }
  63. }
  64. $item['params'] = $params;
  65. $specs = array();
  66. $options = array();
  67. if (isset($data['skuModel']))
  68. {
  69. $skuModel = $data['skuModel'];
  70. if (isset($skuModel['skuProps']))
  71. {
  72. $skuProps = $skuModel['skuProps'];
  73. foreach ($skuProps as $prop )
  74. {
  75. $spec_items = array();
  76. foreach ($prop['values'] as $spec_item )
  77. {
  78. $spec_items[] = array('valueId' => $spec_item['valueId'], 'title' => $spec_item['name'], 'thumb' => $spec_item['imgUrl']);
  79. }
  80. $spec = array('propId' => $prop['propId'], 'title' => $prop['propName'], 'items' => $spec_items);
  81. $specs[] = $spec;
  82. }
  83. }
  84. if (isset($skuModel['ppathIdmap']))
  85. {
  86. $ppathIdmap = $skuModel['ppathIdmap'];
  87. foreach ($ppathIdmap as $key => $skuId )
  88. {
  89. $option_specs = array();
  90. $m = explode(';', $key);
  91. foreach ($m as $v )
  92. {
  93. $mm = explode(':', $v);
  94. $option_specs[] = array('propId' => $mm[0], 'valueId' => $mm[1]);
  95. }
  96. $options[] = array('option_specs' => $option_specs, 'skuId' => $skuId, 'stock' => 0, 'marketprice' => 0, 'specs' => '');
  97. }
  98. }
  99. }
  100. $item['specs'] = $specs;
  101. $stack = $data['apiStack'][0]['value'];
  102. $value = json_decode($stack, true);
  103. $item1 = array();
  104. $data1 = $value['data'];
  105. $itemInfoModel1 = $data1['itemInfoModel'];
  106. $item['total'] = $itemInfoModel1['quantity'];
  107. $item['sales'] = $itemInfoModel1['totalSoldQuantity'];
  108. if (isset($data1['skuModel']))
  109. {
  110. $skuModel1 = $data1['skuModel'];
  111. if (isset($skuModel1['skus']))
  112. {
  113. $skus = $skuModel1['skus'];
  114. foreach ($skus as $key => $val )
  115. {
  116. $sku_id = $key;
  117. foreach ($options as &$o )
  118. {
  119. if ($o['skuId'] == $sku_id)
  120. {
  121. $o['stock'] = $val['quantity'];
  122. foreach ($val['priceUnits'] as $p )
  123. {
  124. $o['marketprice'] = $p['price'];
  125. }
  126. $item['marketprice'] = $o['marketprice'];
  127. $titles = array();
  128. foreach ($o['option_specs'] as $osp )
  129. {
  130. foreach ($specs as $sp )
  131. {
  132. if ($sp['propId'] == $osp['propId'])
  133. {
  134. foreach ($sp['items'] as $spitem )
  135. {
  136. if ($spitem['valueId'] == $osp['valueId'])
  137. {
  138. $titles[] = $spitem['title'];
  139. }
  140. }
  141. }
  142. }
  143. }
  144. $o['title'] = $titles;
  145. }
  146. }
  147. unset($o);
  148. }
  149. }
  150. else
  151. {
  152. $mprice = 0;
  153. foreach ($itemInfoModel1['priceUnits'] as $p )
  154. {
  155. $mprice = $p['price'];
  156. }
  157. $item['marketprice'] = $mprice;
  158. }
  159. }
  160. else
  161. {
  162. $mprice = 0;
  163. foreach ($itemInfoModel1['priceUnits'] as $p )
  164. {
  165. $mprice = $p['price'];
  166. }
  167. $item['marketprice'] = $mprice;
  168. }
  169. $item['options'] = $options;
  170. $item['content'] = array();
  171. $url = 'https://hws.m.taobao.com/cache/wdesc/5.0/?id=' . $itemid;
  172. $response = ihttp_get($url);
  173. $response = preg_replace('/ (?:width)=(\'|").*?\\1/', ' width="100%"', $response);
  174. $response = preg_replace('/ (?:height)=(\'|").*?\\1/', ' ', $response);
  175. $item['content'] = $response;
  176. $thumb_url = array();
  177. $pics = $item['pics'];
  178. $piclen = count($pics);
  179. if (0 < $piclen)
  180. {
  181. if (substr($pics[0], 0, 2) == '//')
  182. {
  183. $pics[0] = 'http://' . substr($pics[0], 2);
  184. }
  185. $data['thumb'] = $this->save_image($pics[0], false);
  186. if (1 < $piclen)
  187. {
  188. $i = 1;
  189. while ($i < $piclen)
  190. {
  191. if (substr($pics[$i], 0, 2) == '//')
  192. {
  193. $pics[$i] = 'http://' . substr($pics[$i], 2);
  194. }
  195. $img = $this->save_image($pics[$i], false);
  196. $thumb_url[] = $img;
  197. ++$i;
  198. }
  199. }
  200. }
  201. $image_lists = $thumb_url;
  202. $response = $item['content'];
  203. $content = $response['content'];
  204. preg_match_all('/<img.*?src=[\\\\\'| \\"](.*?(?:[\\.gif|\\.jpg]?))[\\\\\'|\\"].*?[\\/]?>/', $content, $imgs);
  205. if (isset($imgs[1]))
  206. {
  207. foreach ($imgs[1] as $img )
  208. {
  209. $catchimg = $img;
  210. if (substr($catchimg, 0, 2) == '//')
  211. {
  212. $img = 'http://' . substr($img, 2);
  213. }
  214. $im = array('catchimg' => $catchimg, 'system' => $this->save_image($img, true));
  215. $images[] = $im;
  216. }
  217. }
  218. preg_match('/tfsContent : \\\'(.*)\\\'/', $content, $html);
  219. $html = iconv('GBK', 'UTF-8', $html[1]);
  220. if (isset($images))
  221. {
  222. foreach ($images as $img )
  223. {
  224. $html = str_replace($img['catchimg'], $img['system'], $html);
  225. }
  226. }
  227. //'content' => $html $item['total']
  228. //d['marketprice'] = $minprice; ["sales"]=>
  229. //string(5) "12418"
  230. //$item['title'], 'quantity' => $item['total']
  231. $goods_data = array();
  232. $goods_data['title'] = $item['title'];
  233. $goods_data['marketprice'] = $item['marketprice'];
  234. $goods_data['sales'] = $item['sales'];
  235. $goods_data['quantity'] = $item['total'];
  236. $goods_data['image_lists'] = $image_lists;
  237. $goods_data['store_id'] = SELLERUID;
  238. $goods_data['html'] = $html;
  239. //$image_lists
  240. $model=new GoodsModel();
  241. if( empty($item['title']) )
  242. {
  243. echo json_encode( array('code' =>0,'msg' =>'采集失败') );
  244. }else{
  245. $model->add_caiji_Goods($goods_data);
  246. echo json_encode( array('code' =>1) );
  247. }
  248. die();
  249. }
  250. function caiji()
  251. {
  252. $url = I('url');
  253. preg_match('/id\\=(\\d+)/i', $url, $matches);
  254. if (isset($matches[1]))
  255. {
  256. $itemid = $matches[1];
  257. }
  258. $url = 'http://hws.m.taobao.com/cache/wdetail/5.0/?id=' . $itemid;
  259. $response = ihttp_get($url);
  260. //$response_2 = file_get_contents($url);
  261. //var_dump($response,$response_2);die();
  262. if (!(isset($response['content'])))
  263. {
  264. //return array('result' => '0', 'error' => '未从淘宝获取到商品信息!');
  265. echo json_encode( array('code' =>0,'msg' => '宝贝不存在!') );
  266. die();
  267. }
  268. $content = $response['content'];
  269. if (strexists($response['content'], 'ERRCODE_QUERY_DETAIL_FAIL'))
  270. {
  271. echo json_encode( array('code' =>0,'msg' => '宝贝不存在!请检查链接中是否有干扰参数') );
  272. die();
  273. //return array('result' => '0', 'error' => '宝贝不存在!');
  274. }
  275. $arr = json_decode($content, true);
  276. $data = $arr['data'];
  277. $itemInfoModel = $data['itemInfoModel'];
  278. $item = array();
  279. $item['id'] = $g['id'];
  280. $item['merchid'] = $merchid;
  281. if (!(empty($merchid)))
  282. {
  283. if (empty($_W['merch_user']['goodschecked']))
  284. {
  285. $item['checked'] = 1;
  286. }
  287. else
  288. {
  289. $item['checked'] = 0;
  290. }
  291. }
  292. $item['pcate'] = $pcate;
  293. $item['ccate'] = $ccate;
  294. $item['tcate'] = $tcate;
  295. $item['cates'] = $pcate . ',' . $ccate . ',' . $tcate;
  296. $item['itemId'] = $itemInfoModel['itemId'];
  297. $item['title'] = $itemInfoModel['title'];
  298. $item['pics'] = $itemInfoModel['picsPath'];
  299. $params = array();
  300. if (isset($data['props']))
  301. {
  302. $props = $data['props'];
  303. foreach ($props as $pp )
  304. {
  305. $params[] = array('title' => $pp['name'], 'value' => $pp['value']);
  306. }
  307. }
  308. $item['params'] = $params;
  309. $specs = array();
  310. $options = array();
  311. if (isset($data['skuModel']))
  312. {
  313. $skuModel = $data['skuModel'];
  314. if (isset($skuModel['skuProps']))
  315. {
  316. $skuProps = $skuModel['skuProps'];
  317. foreach ($skuProps as $prop )
  318. {
  319. $spec_items = array();
  320. foreach ($prop['values'] as $spec_item )
  321. {
  322. $spec_items[] = array('valueId' => $spec_item['valueId'], 'title' => $spec_item['name'], 'thumb' => $spec_item['imgUrl']);
  323. }
  324. $spec = array('propId' => $prop['propId'], 'title' => $prop['propName'], 'items' => $spec_items);
  325. $specs[] = $spec;
  326. }
  327. }
  328. if (isset($skuModel['ppathIdmap']))
  329. {
  330. $ppathIdmap = $skuModel['ppathIdmap'];
  331. foreach ($ppathIdmap as $key => $skuId )
  332. {
  333. $option_specs = array();
  334. $m = explode(';', $key);
  335. foreach ($m as $v )
  336. {
  337. $mm = explode(':', $v);
  338. $option_specs[] = array('propId' => $mm[0], 'valueId' => $mm[1]);
  339. }
  340. $options[] = array('option_specs' => $option_specs, 'skuId' => $skuId, 'stock' => 0, 'marketprice' => 0, 'specs' => '');
  341. }
  342. }
  343. }
  344. $item['specs'] = $specs;
  345. $stack = $data['apiStack'][0]['value'];
  346. $value = json_decode($stack, true);
  347. $item1 = array();
  348. $data1 = $value['data'];
  349. $itemInfoModel1 = $data1['itemInfoModel'];
  350. $item['total'] = $itemInfoModel1['quantity'];
  351. $item['sales'] = $itemInfoModel1['totalSoldQuantity'];
  352. if (isset($data1['skuModel']))
  353. {
  354. $skuModel1 = $data1['skuModel'];
  355. if (isset($skuModel1['skus']))
  356. {
  357. $skus = $skuModel1['skus'];
  358. foreach ($skus as $key => $val )
  359. {
  360. $sku_id = $key;
  361. foreach ($options as &$o )
  362. {
  363. if ($o['skuId'] == $sku_id)
  364. {
  365. $o['stock'] = $val['quantity'];
  366. foreach ($val['priceUnits'] as $p )
  367. {
  368. $o['marketprice'] = $p['price'];
  369. }
  370. $item['marketprice'] = $o['marketprice'];
  371. $titles = array();
  372. foreach ($o['option_specs'] as $osp )
  373. {
  374. foreach ($specs as $sp )
  375. {
  376. if ($sp['propId'] == $osp['propId'])
  377. {
  378. foreach ($sp['items'] as $spitem )
  379. {
  380. if ($spitem['valueId'] == $osp['valueId'])
  381. {
  382. $titles[] = $spitem['title'];
  383. }
  384. }
  385. }
  386. }
  387. }
  388. $o['title'] = $titles;
  389. }
  390. }
  391. unset($o);
  392. }
  393. }
  394. else
  395. {
  396. $mprice = 0;
  397. foreach ($itemInfoModel1['priceUnits'] as $p )
  398. {
  399. $mprice = $p['price'];
  400. }
  401. $item['marketprice'] = $mprice;
  402. }
  403. }
  404. else
  405. {
  406. $mprice = 0;
  407. foreach ($itemInfoModel1['priceUnits'] as $p )
  408. {
  409. $mprice = $p['price'];
  410. }
  411. $item['marketprice'] = $mprice;
  412. }
  413. $item['options'] = $options;
  414. $item['content'] = array();
  415. $url = 'http://hws.m.taobao.com/cache/wdesc/5.0/?id=' . $itemid;
  416. $response = ihttp_get($url);
  417. $response = preg_replace('/ (?:width)=(\'|").*?\\1/', ' width="100%"', $response);
  418. $response = preg_replace('/ (?:height)=(\'|").*?\\1/', ' ', $response);
  419. $item['content'] = $response;
  420. $thumb_url = array();
  421. $pics = $item['pics'];
  422. $piclen = count($pics);
  423. if (0 < $piclen)
  424. {
  425. $data['thumb'] = $this->save_image($pics[0], false);
  426. if (1 < $piclen)
  427. {
  428. $i = 1;
  429. while ($i < $piclen)
  430. {
  431. $img = $this->save_image($pics[$i], false);
  432. $thumb_url[] = $img;
  433. ++$i;
  434. }
  435. }
  436. }
  437. $image_lists = $thumb_url;
  438. $response = $item['content'];
  439. $content = $response['content'];
  440. preg_match_all('/<img.*?src=[\\\\\'| \\"](.*?(?:[\\.gif|\\.jpg]?))[\\\\\'|\\"].*?[\\/]?>/', $content, $imgs);
  441. if (isset($imgs[1]))
  442. {
  443. foreach ($imgs[1] as $img )
  444. {
  445. $catchimg = $img;
  446. if (substr($catchimg, 0, 2) == '//')
  447. {
  448. $img = 'http://' . substr($img, 2);
  449. }
  450. $im = array('catchimg' => $catchimg, 'system' => $this->save_image($img, true));
  451. $images[] = $im;
  452. }
  453. }
  454. preg_match('/tfsContent : \\\'(.*)\\\'/', $content, $html);
  455. $html = iconv('GBK', 'UTF-8', $html[1]);
  456. if (isset($images))
  457. {
  458. foreach ($images as $img )
  459. {
  460. $html = str_replace($img['catchimg'], $img['system'], $html);
  461. }
  462. }
  463. //'content' => $html $item['total']
  464. //d['marketprice'] = $minprice; ["sales"]=>
  465. //string(5) "12418"
  466. //$item['title'], 'quantity' => $item['total']
  467. $goods_data = array();
  468. $goods_data['title'] = $item['title'];
  469. $goods_data['marketprice'] = $item['marketprice'];
  470. $goods_data['sales'] = $item['sales'];
  471. $goods_data['quantity'] = $item['total'];
  472. $goods_data['image_lists'] = $image_lists;
  473. $goods_data['store_id'] = SELLERUID;
  474. $goods_data['html'] = $html;
  475. //$image_lists
  476. $model=new GoodsModel();
  477. if( empty($item['title']) )
  478. {
  479. echo json_encode( array('code' =>0) );
  480. }else{
  481. $model->add_caiji_Goods($goods_data);
  482. echo json_encode( array('code' =>1) );
  483. }
  484. die();
  485. }
  486. public function save_image($url, $iscontent = false)
  487. {
  488. $ext = strrchr($url, '.');
  489. if (($ext != '.jpeg') && ($ext != '.gif') && ($ext != '.jpg') && ($ext != '.png'))
  490. {
  491. return $url;
  492. }
  493. if (trim($url) == '')
  494. {
  495. return $url;
  496. }
  497. $filename = time().md5($url). $ext;
  498. $image_dir = ROOT_PATH.'Uploads/image/goods';
  499. $image_dir .= '/'.date('Y-m-d').'/';
  500. $file_path = '/Uploads/image/goods'.'/'.date('Y-m-d').'/';
  501. RecursiveMkdir($image_dir);
  502. $save_dir = $image_dir;
  503. $img = @file_get_contents($url);
  504. if (strlen($img) != 0)
  505. {
  506. file_put_contents($save_dir . $filename, $img);
  507. if(!$iscontent)
  508. {
  509. return 'goods'.'/'.date('Y-m-d').'/'.$filename;
  510. }else
  511. return $file_path.$filename;
  512. }
  513. return '';
  514. }
  515. }
  516. ?>