CopyForm.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\ShareLevel;
  9. use yii\base\Model;
  10. use yii\helpers\Json;
  11. class CopyForm extends Model
  12. {
  13. public $url;
  14. public $level = 2;
  15. private $taobaoApi = 'https://api03.6bqb.com/taobao/detail?apikey=3749F9A9867E7B463FF92F2029CB5663';
  16. private $tmallApi = 'https://api03.6bqb.com/tmall/detail?apikey=3749F9A9867E7B463FF92F2029CB5663';
  17. //private $jdApi = 'https://api03.6bqb.com/jd/detail?apikey=3749F9A9867E7B463FF92F2029CB5663';
  18. private $t1688Api = 'https://api-gw.onebound.cn/1688/item_get/?api_name=item_get&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326';
  19. private $jdApi = 'https://api-gw.onebound.cn/jd/item_get?api_name=item_get&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326';
  20. public function rules()
  21. {
  22. return [
  23. [['url'], 'trim'],
  24. [['url'], 'string']
  25. ];
  26. }
  27. public function copy()
  28. {
  29. if (!$this->validate()) {
  30. return [
  31. 'code' => 1,
  32. 'msg' => $this->getErrorSummary(false)[0]
  33. ];
  34. }
  35. try {
  36. if (strpos($this->url, 'item.jd')) {
  37. $id = $this->str_substr('item.jd.com/', '.html', $this->url);
  38. $id = $this->pregSubstr('/item.jd.[a-z]+\//', '/.html/', $this->url);
  39. if (empty($id[0])) {
  40. return [
  41. 'code' => 1,
  42. 'msg' => '链接错误,请检查链接'
  43. ];
  44. }
  45. return $this->j_copy($id[0]);
  46. }
  47. //淘宝连接检查URL的合法性
  48. $arr = explode('?', $this->url);
  49. if (count($arr) < 2) {
  50. return [
  51. 'code' => 1,
  52. 'msg' => '链接错误,请检查链接'
  53. ];
  54. }
  55. if (strpos($arr[0], 'taobao')) {
  56. $id = $this->pregSubstr('/(\?id=|&id=)/', '/&/', $this->url);
  57. if (empty($id[0])) {
  58. return [
  59. 'code' => 1,
  60. 'msg' => '链接错误,请检查链接'
  61. ];
  62. }
  63. $data = $this->taobao($id[0]);
  64. return $data;
  65. }
  66. if (strpos($arr[0], 'tmall')) {
  67. $id = $this->pregSubstr('/(\?id=|&id=)/', '/&/', $this->url);
  68. if (empty($id[0])) {
  69. return [
  70. 'code' => 1,
  71. 'msg' => '链接错误,请检查链接'
  72. ];
  73. }
  74. $data = $this->tmall($id[0]);
  75. return $data;
  76. }
  77. if (strpos($arr[0], '1688')) {
  78. $id = $this->pregSubstr('/(\/offer\/)/', '/\.html/', $this->url);
  79. if (empty($id[0])) {
  80. return [
  81. 'code' => 1,
  82. 'msg' => '链接错误,请检查链接'
  83. ];
  84. }
  85. return $this->t1688($id[0]);
  86. }
  87. return [
  88. 'code' => 1,
  89. 'msg' => '链接错误,请检查链接'
  90. ];
  91. } catch (\Exception $e) {
  92. \Yii::error([__METHOD__, $e->getTrace()]);
  93. return [
  94. 'code' => 1,
  95. 'msg' => $e->getMessage()
  96. ];
  97. }
  98. }
  99. private function taobao($id)
  100. {
  101. $res = http_get($this->taobaoApi . '&itemid=' . $id);
  102. \Yii::error([__METHOD__, $res, $id, $this->taobaoApi]);
  103. if ($res->getStatusCode() != 200) {
  104. return [
  105. 'code' => 1,
  106. 'msg' => '请求出错',
  107. ];
  108. }
  109. $res = json_decode((string)$res->getBody(), true);
  110. \Yii::error([__METHOD__, $res, $id, $this->taobaoApi]);
  111. if (!empty($res['message']) || $res['retcode'] != '0000') {
  112. return [
  113. 'code' => 1,
  114. 'msg' => !empty($res['message']) ? $res['message'] : $res['data'],
  115. ];
  116. }
  117. // 售价
  118. $price = null;
  119. if (!empty($res['data']['item']['priceRange'])) {
  120. $priceEx = explode('-', $res['data']['item']['priceRange']);
  121. $price = $priceEx[0];
  122. }
  123. // 市场价
  124. $marketPrice = null;
  125. if (!empty($res['data']['item']['marketPriceRange'])) {
  126. $marketrPriceEx = explode('-', $res['data']['item']['marketPriceRange']);
  127. $marketPrice = $marketrPriceEx[0];
  128. }
  129. // 总库存
  130. $quantity = 0;
  131. if (!empty($res['data']['item']['sku'][0])) {
  132. $quantity = $res['data']['item']['sku'][0]['quantity'];
  133. }
  134. // 详情
  135. $desc = null;
  136. if (!empty($res['data']['item']['desc'])) {
  137. $desc = $res['data']['item']['desc'];
  138. } else {
  139. $desc = '<p>';
  140. if (!empty($res['data']['item']['descImgs'])) {
  141. foreach ($res['data']['item']['descImgs'] as $key => $value) {
  142. $desc .= '<img src="' . $value . '">';
  143. }
  144. }
  145. $desc .= '</p>';
  146. }
  147. // 视频
  148. $video = null;
  149. if (!empty($res['data']['item']['videos'])) {
  150. $video = $res['data']['item']['videos'][0]['url'];
  151. }
  152. //虚拟销量
  153. $vagueSellCount = 0;
  154. if (!empty($res['data']['item']['vagueSellCount'])) {
  155. $vagueSellCount = (int)$res['data']['item']['vagueSellCount'];
  156. }
  157. $images = $res['data']['item']['images'];
  158. foreach ($images as &$item) {
  159. $index = strstr($item, 'http');
  160. if (!$index) {
  161. $item = 'https://' . $item;
  162. }
  163. $item = \str_replace('http://', 'https://', $item);
  164. $item = \str_replace('https:////', 'https://', $item);
  165. }
  166. $data = [
  167. 'title' => $res['data']['item']['title'],
  168. 'pics' => $images,
  169. 'price' => $price,
  170. 'marketPrice' => $marketPrice,
  171. 'vagueSellCount' => $vagueSellCount,
  172. 'quantity' => $quantity,
  173. 'desc' => $desc,
  174. 'video' => $video,
  175. 'attr' => $this->exeAttr($res['data']['item']['sku']),
  176. 'attrGroup' => $this->exeAttrGroup($res['data']['item']['props']),
  177. ];
  178. return [
  179. 'code' => 0,
  180. 'msg' => 'success',
  181. 'data' => $data,
  182. ];
  183. }
  184. private function tmall($id)
  185. {
  186. $res = http_get($this->tmallApi . '&itemid=' . $id);
  187. if ($res->getStatusCode() != 200) {
  188. return [
  189. 'code' => 1,
  190. 'msg' => '请求出错',
  191. ];
  192. }
  193. $res = json_decode((string)$res->getBody(), true);
  194. if (!empty($res['message']) || $res['retcode'] != '0000') {
  195. return [
  196. 'code' => 1,
  197. 'msg' => !empty($res['message']) ? $res['message'] : $res['data'],
  198. ];
  199. }
  200. // 售价
  201. $price = null;
  202. if (!empty($res['data']['item']['priceRange'])) {
  203. $priceEx = explode('-', $res['data']['item']['priceRange']);
  204. $price = $priceEx[0];
  205. }
  206. // 市场价
  207. $marketPrice = null;
  208. if (!empty($res['data']['item']['marketPriceRange'])) {
  209. $marketrPriceEx = explode('-', $res['data']['item']['marketPriceRange']);
  210. $marketPrice = $marketrPriceEx[0];
  211. }
  212. // 总库存
  213. $quantity = 0;
  214. if (!empty($res['data']['item']['sku'][0])) {
  215. $quantity = $res['data']['item']['sku'][0]['quantity'];
  216. }
  217. // 详情
  218. $desc = null;
  219. if (!empty($res['data']['item']['desc'])) {
  220. $desc = $res['data']['item']['desc'];
  221. } else {
  222. $desc = '<p>';
  223. if ($res['data']['item']['descImgs']) {
  224. foreach ($res['data']['item']['descImgs'] as $key => $value) {
  225. $desc .= '<img src="' . $value . '">';
  226. }
  227. }
  228. $desc .= '</p>';
  229. }
  230. // 视频
  231. $video = null;
  232. if (!empty($res['data']['item']['videos'])) {
  233. $video = $res['data']['item']['videos'][0]['url'];
  234. }
  235. //虚拟销量
  236. $vagueSellCount = 0;
  237. if (!empty($res['data']['item']['vagueSellCount'])) {
  238. $vagueSellCount = (int)$res['data']['item']['vagueSellCount'];
  239. }
  240. $images = $res['data']['item']['images'];
  241. foreach ($images as &$item) {
  242. $index = strstr($item, 'http');
  243. if (!$index) {
  244. $item = 'https://' . $item;
  245. }
  246. $item = \str_replace('http://', 'https://', $item);
  247. $item = \str_replace('https:////', 'https://', $item);
  248. }
  249. $data = [
  250. 'title' => $res['data']['item']['title'],
  251. 'pics' => $images,
  252. 'price' => $price,
  253. 'marketPrice' => $marketPrice,
  254. 'vagueSellCount' => $vagueSellCount,
  255. 'quantity' => $quantity,
  256. 'desc' => $desc,
  257. 'video' => $video,
  258. 'attr' => $this->exeAttr($res['data']['item']['sku']),
  259. 'attrGroup' => $this->exeAttrGroup($res['data']['item']['props']),
  260. ];
  261. return [
  262. 'code' => 0,
  263. 'msg' => 'success',
  264. 'data' => $data,
  265. ];
  266. }
  267. private function t1688($id) {
  268. $res = http_get($this->t1688Api . '&num_iid=' . $id);
  269. if ($res->getStatusCode() != 200) {
  270. return [
  271. 'code' => 1,
  272. 'msg' => '请求出错',
  273. ];
  274. }
  275. $res = json_decode((string)$res->getBody(), true);
  276. if (!empty($res['message']) || $res['error_code'] != '0000') {
  277. return [
  278. 'code' => 1,
  279. 'msg' => !empty($res['message']) ? $res['message'] : $res['data'],
  280. ];
  281. }
  282. $title = $res['item']['title'];
  283. $price = $res['item']['price'];
  284. $marketPrice = $res['item']['orginal_price'];
  285. $quantity = $res['item']['num'];
  286. // 详情
  287. if (!empty($res['item']['desc'])) {
  288. $desc = $res['item']['desc'];
  289. } else {
  290. $desc = '<p>';
  291. foreach ($res['item']['item_imgs'] as $key => $value) {
  292. $desc .= '<img src="' . $value['url'] . '">';
  293. }
  294. $desc .= '</p>';
  295. }
  296. // 视频
  297. $video = null;
  298. if (!empty($res['item']['video'])) {
  299. $video = $res['item']['video'][0]['url'] ?? $res['item']['video'];
  300. }
  301. //虚拟销量
  302. $vagueSellCount = 0;
  303. // if (!empty($res['data']['item']['vagueSellCount'])) {
  304. // $vagueSellCount = (int)$res['data']['item']['vagueSellCount'];
  305. // }
  306. $images = array_map(function($item){
  307. $index = strstr($item['url'], 'http');
  308. if (!$index) {
  309. $item['url'] = 'https://' . $item['url'];
  310. }
  311. $url = \str_replace('http://', 'https://', $item['url']);
  312. //兼容本来有两个斜杠问题
  313. return \str_replace('https:////', 'https://', $url);
  314. }, $res['item']['item_imgs']);
  315. //兼容商城规格组数据格式
  316. //兼容商品规格
  317. $prop_imgs_arr = array();
  318. if ($res['item']['prop_imgs']['prop_img']) {
  319. foreach ($res['item']['prop_imgs']['prop_img'] as $prop_img) {
  320. $prop_imgs_arr[$prop_img['propPath']] = $prop_img['url'];
  321. }
  322. }
  323. //兼容商城规格组数据格式
  324. $attr_group_list_array = array();
  325. if ($res['item']['props_list']) {
  326. foreach ($res['item']['props_list'] as $index => $item) {
  327. $prop_group = explode(':', $index);
  328. $prop_item = explode(':', $item);
  329. $attr_group_list_array[$prop_group[0]]['attr_group_name'] = $prop_item[0];
  330. $attr_group_list_array[$prop_group[0]]['attr_list'][$prop_group[1]]['attr_name'] = $prop_item[1];
  331. }
  332. }
  333. //兼容商城规格数据格式
  334. $attr_list_array = array();
  335. if ($res['item']['skus']['sku']) {
  336. foreach ($res['item']['skus']['sku'] as $index => $item) {
  337. $attr = [
  338. 'num' => $item['quantity'],
  339. 'price' => $item['price'],
  340. 'cost_price' => $item['price'],
  341. 'no' => '',
  342. 'pic' => $res['item']['prop_imgs']['prop_img'][$index]['url'] ?: $res['item']['pic_url'],
  343. 'share_commission_first' => '',
  344. 'wholesale_price' => $item['price'],
  345. 'original_price' => $item['price'],
  346. 'weight' => 0,
  347. 'share_commission_second' => '',
  348. 'share_commission_third' => '',
  349. ];
  350. $properties = explode(';', $item['properties_name']);
  351. $attrs = [];
  352. foreach ($properties as $properties_item) {
  353. $properties_item = explode(":", $properties_item);
  354. unset($properties_item[0], $properties_item[1]);
  355. $properties_item = array_values($properties_item);
  356. $attrs[] = [
  357. 'attr_name' => $properties_item[1],
  358. 'attr_group_name' => $properties_item[0],
  359. ];
  360. }
  361. $attr['attr_list'] = $attrs;
  362. $attr_list_array[] = $attr;
  363. }
  364. }
  365. $data = [
  366. 'title' => $title,
  367. 'pics' => $images,
  368. 'price' => $price,
  369. 'marketPrice' => $marketPrice,
  370. 'vagueSellCount' => $vagueSellCount,
  371. 'quantity' => $quantity,
  372. 'desc' => $desc,
  373. 'video' => $video,
  374. 'attr' => $attr_list_array,
  375. 'attrGroup' => $attr_group_list_array,
  376. ];
  377. return [
  378. 'code' => 0,
  379. 'msg' => 'success',
  380. 'data' => $data,
  381. ];
  382. }
  383. /**
  384. * 处理规格
  385. */
  386. protected function exeAttr($sku)
  387. {
  388. unset($sku[0]);
  389. $data = [];
  390. foreach ($sku as $key => $value) {
  391. $attr = [
  392. 'num' => $value['quantity'],
  393. 'price' => $value['price'],
  394. 'cost_price' => $value['price'],
  395. 'wholesale_price' => $value['price'],
  396. 'original_price' => $value['price'],
  397. 'weight' => 0,
  398. 'no' => '',
  399. 'pic' => isset($value['image']) && !empty($value['image']) ? $value['image'] : '',
  400. 'share_commission_first' => '',
  401. 'share_commission_second' => '',
  402. 'share_commission_third' => '',
  403. ];
  404. $attrEx = explode(';', $value['skuName']);
  405. $attrs = [];
  406. foreach ($attrEx as $k => $v) {
  407. $attrItemEx = explode('--', $v);
  408. $attrs[] = [
  409. 'attr_name' => $attrItemEx[1],
  410. 'attr_group_name' => $attrItemEx[0],
  411. ];
  412. }
  413. $attr['attr_list'] = $attrs;
  414. $data[] = $attr;
  415. }
  416. return $data;
  417. }
  418. // 处理规格组
  419. protected function exeAttrGroup($props)
  420. {
  421. $data = [];
  422. if (is_array($props) && !empty($props)) {
  423. foreach ($props as $key => $value) {
  424. $group = [
  425. 'attr_group_name' => $value['name'],
  426. ];
  427. $groupValues = [];
  428. foreach ($value['values'] as $k => $v) {
  429. $groupValues[] = [
  430. 'attr_name' => $v['name'],
  431. ];
  432. }
  433. $group['attr_list'] = $groupValues;
  434. $data[] = $group;
  435. }
  436. }
  437. return $data;
  438. }
  439. public function copy1()
  440. {
  441. if (!$this->validate()) {
  442. return [
  443. 'code' => 1,
  444. 'msg' => $this->getErrorSummary(false)[0]
  445. ];
  446. }
  447. if (strpos($this->url, 'item.jd')) {
  448. $id = $this->preg_substr('/item.jd.[a-z]+\//', '/.html/', $this->url);
  449. $data = $this->j_copy($id[0]);
  450. if ($data['code'] == 1) {
  451. return $data;
  452. }
  453. return [
  454. 'code' => 0,
  455. 'msg' => 'success',
  456. 'data' => $data
  457. ];
  458. }
  459. //淘宝连接检查URL的合法性
  460. $arr = explode('?', $this->url);
  461. if (count($arr) < 2) {
  462. return [
  463. 'code' => 1,
  464. 'msg' => '链接错误,请检查链接'
  465. ];
  466. }
  467. if (strpos($arr[0], 'taobao') || strpos($arr[0], 'tmall')) {
  468. $id = $this->preg_substr('/(\?id=|&id=)/', '/&/', $this->url);
  469. $data = $this->t_copy($id[0]);
  470. if ($data['code'] == 1) {
  471. return $data;
  472. }
  473. return [
  474. 'code' => 0,
  475. 'msg' => 'success',
  476. 'data' => $data
  477. ];
  478. }
  479. return [
  480. 'code' => 1,
  481. 'msg' => '链接错误,请检查链接'
  482. ];
  483. }
  484. public function t_copy($num_iid){
  485. //图文详情
  486. //商品基本信息
  487. $title = '';
  488. $goods_price_sale = '';
  489. $goods_price = '';
  490. $sale_count = '';
  491. $quantity = '';
  492. $attr_group_list = [];
  493. $checked_attr_list = [];
  494. $picsPath = [];
  495. $detail_info = "";
  496. try {
  497. // $detail_api = "http://api.onebound.cn/taobao/api_call.php?api_name=item_get&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326";
  498. // $html = $this->getHTTPS($detail_api);
  499. // $detail_json = json_decode($html, true);
  500. // \Yii::error($detail_json);
  501. // // 转化商品详情的编码
  502. // if ($detail_json['item']['desc']) {
  503. // $detail_info = $detail_json['item']['desc'];
  504. // } else {
  505. //
  506. // }
  507. //临时处理办法,兼容淘宝详情采集为空的问题
  508. $detail_api = "https://api-gw.onebound.cn/taobao/item_get/?key=tel18538854912&&num_iid={$num_iid}&is_promotion=1&&lang=zh-CN&secret=20200326";
  509. $html = $this->getHTTPS($detail_api);
  510. $detail_json = json_decode($html, true);
  511. $detail_info = $detail_json['item']['desc'];
  512. $coding = mb_detect_encoding($detail_info, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5', 'ISO-8859-1'));
  513. $detail_info = mb_convert_encoding($detail_info, 'utf-8', $coding);
  514. $title = $detail_json['item']['title'];
  515. $goods_price_sale = $detail_json['item']['price'];
  516. $goods_price = $detail_json['item']['orginal_price'];
  517. $sale_count = $detail_json['item']['sales'];
  518. $video = $detail_json['item']['video'];
  519. $pic_url = $detail_json['item']['pic_url'];
  520. $quantity = "";
  521. //兼容商品图片的数组格式
  522. $picsPath_array=array();
  523. if (!empty($detail_json['item']['item_imgs'])) {
  524. foreach($detail_json['item']['item_imgs'] as $key => $value){
  525. if (strpos($value['url'],'http') === false) {
  526. $value['url'] = 'http:'. trim($value['url']);
  527. }
  528. $picsPath_array[$key]=$value['url'];
  529. }
  530. }
  531. $picsPath = $picsPath_array;
  532. //兼容商品规格
  533. $prop_imgs_arr = array();
  534. if ($detail_json['item']['prop_imgs']) {
  535. $prop_imgs = json_decode(json_encode($detail_json['item']['prop_imgs']),true);
  536. if ($prop_imgs['prop_img']) {
  537. foreach($prop_imgs['prop_img'] as $prop_img){
  538. $prop_imgs_arr[$prop_img['properties']] = $prop_img['url'];
  539. }
  540. }
  541. }
  542. $prop_array=array();
  543. foreach ($detail_json['item']['props_list'] as $dk=>$val) {
  544. $dks = explode(':',$dk);
  545. $vals = explode(':',$val);
  546. $prop_img = !empty($prop_imgs_arr[$dks[0].':'.$dks[1]]) ? $prop_imgs_arr[$dks[0] . ':' . $dks[1]] : '';
  547. $prop_array[$dks[0]][$dks[1]]=array(
  548. 'prop_key' => $dks[0],
  549. 'prop_val' => $dks[1],
  550. 'name' => $vals[0],
  551. 'value' => $vals[1],
  552. 'pic_url' => $prop_img,
  553. );
  554. }
  555. $mypropids = array_keys($prop_array);
  556. $mypropids = implode('|', $mypropids);
  557. //兼容商城规格组数据格式
  558. $attr_group_list_array = array();
  559. if (!empty($prop_array)) {
  560. $i = 0;
  561. foreach ($prop_array as $key=>$value) {
  562. $name = current($value);
  563. $attr_group_list_array[$i] = [
  564. 'attr_group_name' => $name['name']
  565. ];
  566. $j = 0;
  567. foreach ($value as $key2 => $value) {
  568. $attr_group_list_array[$i]['attr_list'][$j]['attr_name']=$value['value'];
  569. $j++;
  570. }
  571. $i++;
  572. }
  573. }
  574. $attr_group_list = $attr_group_list_array;
  575. //兼容货品\库存\价格数据格式
  576. $sku = $detail_json['item']['skus']['sku'];
  577. $checked_attr_list = array();
  578. foreach ($sku as $key1 => $value1) {
  579. $attr_list = explode(';',$value1['properties_name']);
  580. foreach ($attr_list as $key2=>$value2) {
  581. $value2_arr = explode(':',$value2);
  582. $attr_name = $value2_arr[3];
  583. $attr_group_name = $value2_arr[2];
  584. $checked_attr_list[$key1]['attr_list'][$key2] = array("attr_id"=>null,'attr_name' => $attr_name, 'attr_group_name' => $attr_group_name);
  585. }
  586. $checked_attr_list[$key1]['price'] = $value1['price'];
  587. $checked_attr_list[$key1]['num'] = $value1['quantity'];
  588. }
  589. } catch (\Exception $e) {}
  590. return [
  591. 'title' => $title,//标题
  592. 'pic_url' => $pic_url,
  593. 'picsPath' => $picsPath,//缩略图
  594. 'price' => $goods_price,//原价
  595. 'sale_price' => $goods_price_sale,//售价
  596. 'sale_count' => $sale_count,//销量
  597. 'quantity' => $quantity,//库存
  598. 'detail_info' => $detail_info,//图文详情
  599. 'video' => $video,//图文详情
  600. 'attr_group_list' => Json::encode($attr_group_list),
  601. 'checked_attr_list' => Json::encode($checked_attr_list),
  602. ];
  603. }
  604. private function j_copy_detail($num_iid, $sku_id)
  605. {
  606. $html = $this->getHTTPS("https://api-gw.onebound.cn/jd/item_get_desc?api_name=item_get_desc&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326&main_sku_id={$sku_id}");
  607. $detail_json = json_decode($html, true);
  608. $detail_info = $detail_json['item']['desc'];
  609. return $detail_info;
  610. }
  611. /**
  612. * @param $id
  613. * @return array
  614. * 京东抓取
  615. */
  616. public function j_copy($num_iid)
  617. {
  618. //图文详情
  619. //商品基本信息
  620. $title = '';
  621. $goods_price_sale = '';
  622. $goods_price = '';
  623. $sale_count = '';
  624. $quantity = '';
  625. $attr_group_list = [];
  626. $checked_attr_list = [];
  627. $picsPath = [];
  628. $detail_info = "";
  629. try {
  630. $html = $this->getHTTPS("https://api-gw.onebound.cn/jd/item_get?api_name=item_get&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326");
  631. //$html = http_get("https://api-gw.onebound.cn/jd/item_get?api_name=item_get&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326");
  632. $detail_json = json_decode($html, true);
  633. //转化商品详情的编码
  634. $detail_info = $detail_json['item']['desc'];
  635. if (empty($detail_info)) {
  636. $detail_info = $this->j_copy_detail($num_iid, $detail_json['item']['main_sku_id']);
  637. }
  638. // $coding = mb_detect_encoding($detail_info, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5', 'ISO-8859-1'));
  639. // $detail_info = mb_convert_encoding($detail_info, 'utf-8', $coding);
  640. $detail_info = str_replace('data-lazyload', 'src', $detail_info);
  641. $end_index = stripos($detail_info, '</style>');
  642. if ($end_index !== false) {
  643. $detail_style_info = substr($detail_info, 0, ($end_index + 8));
  644. $arr = $this->setDetail($detail_style_info);
  645. $img_str = '';
  646. $detail_info = substr($end_index, ($end_index + 8));
  647. foreach ($arr as $item) {
  648. $img_str .= '<img src="'. $item .'">';
  649. }
  650. $detail_info = $img_str .$detail_info;
  651. }
  652. $title =$detail_json['item']['title'];
  653. $goods_price_sale = $detail_json['item']['price'];
  654. $goods_price = $detail_json['item']['orginal_price'];
  655. $sale_count = $detail_json['item']['sales'];
  656. $video=$detail_json['item']['video'];
  657. $quantity = "";
  658. //兼容商品图片的数组格式
  659. $picsPath_array=array();
  660. if(!empty($detail_json['item']['item_imgs'])){
  661. foreach($detail_json['item']['item_imgs'] as $key=>$value){
  662. $index = strstr($value['url'], 'http');
  663. if (!$index) {
  664. $value['url'] = 'https://' . $value['url'];
  665. }
  666. $picsPath_array[$key]= \str_replace('http://', 'https://', $value['url']);
  667. //兼容本来有两个斜杠问题
  668. $picsPath_array[$key]= \str_replace('https:////', 'https://', $picsPath_array[$key]);
  669. }
  670. }
  671. $picsPath = $picsPath_array;
  672. //兼容商品规格
  673. $prop_imgs_arr = array();
  674. if($detail_json['item']['prop_imgs']){
  675. $prop_imgs = json_decode(json_encode($detail_json['item']['prop_imgs']),true);
  676. if($prop_imgs['prop_img']){
  677. foreach($prop_imgs['prop_img'] as $prop_img){
  678. $prop_imgs_arr[$prop_img['properties']] = $prop_img['url'];
  679. }
  680. }
  681. }
  682. $prop_array=array();
  683. foreach($detail_json['item']['props_list'] as $dk=>$val){
  684. $dks=explode(':',$dk);
  685. $vals=explode(':',$val);
  686. $prop_img = !empty($prop_imgs_arr[$dks[0].':'.$dks[1]])?$prop_imgs_arr[$dks[0].':'.$dks[1]]:'';
  687. $prop_array[$dks[0]][$dks[1]]=array(
  688. 'prop_key'=>$dks[0],
  689. 'prop_val'=>$dks[1],
  690. 'name'=>$vals[0],
  691. 'value'=>$vals[1],
  692. 'pic_url'=>$prop_img,
  693. );
  694. }
  695. $mypropids= array_keys($prop_array);
  696. $mypropids= implode('|',$mypropids);
  697. //兼容商城规格组数据格式
  698. $attr_group_list_array=array();
  699. if(!empty($prop_array)){
  700. $i=0;
  701. foreach($prop_array as $key=>$value){
  702. $name=current($value);
  703. $attr_group_list_array[$i] = [
  704. 'attr_group_name' => $name['name']
  705. ];
  706. $j=0;
  707. foreach($value as $key2=>$value){
  708. $attr_group_list_array[$i]['attr_list'][$j]['attr_name']=$value['value'];
  709. $j++;
  710. }
  711. $i++;
  712. }
  713. }
  714. $attr_group_list =$attr_group_list_array;
  715. //兼容货品\库存\价格数据格式
  716. $sku = $detail_json['item']['skus']['sku'];
  717. $checked_attr_list=array();
  718. $share_level = ShareLevel::find()->where(['store_id' => get_store_id(), 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON])
  719. ->select('level id, name')->orderBy('level ASC')->asArray()->all();
  720. $share_level = array_merge([[
  721. 'id' => 0,
  722. 'name' => '默认等级'
  723. ]], $share_level);
  724. foreach($sku as $key1=>$value1){
  725. $attr_list=explode(';',$value1['properties_name']);
  726. foreach($attr_list as $key2=>$value2){
  727. $value2_arr=explode(':',$value2);
  728. $attr_name=$value2_arr[3];
  729. $attr_group_name=$value2_arr[2];
  730. $checked_attr_list[$key1]['attr_list'][$key2]=array("attr_id"=>null,'attr_name'=>$attr_name,'attr_group_name'=>$attr_group_name);
  731. }
  732. $checked_attr_list[$key1]['price']=$value1['price'];
  733. $checked_attr_list[$key1]['cost_price']=$value1['price'];
  734. $checked_attr_list[$key1]['num']=$value1['quantity'];
  735. $checked_attr_list[$key1]['wholesale_price'] = $value1['price'];
  736. $checked_attr_list[$key1]['original_price'] = $value1['price'];
  737. $checked_attr_list[$key1]['weight'] = 0;
  738. $checked_attr_list[$key1]['share_commission_new_first'] = null;
  739. $checked_attr_list[$key1]['share_commission_new_second'] = null;
  740. $checked_attr_list[$key1]['share_commission_new_third'] = null;
  741. foreach ($share_level as $share_level_index => $share_level_) {
  742. $checked_attr_list[$key1]['share_commission_new_first'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
  743. $checked_attr_list[$key1]['share_commission_new_first'][$share_level_index]['value'] = 0;
  744. $checked_attr_list[$key1]['share_commission_new_second'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
  745. $checked_attr_list[$key1]['share_commission_new_second'][$share_level_index]['value'] = 0;
  746. $checked_attr_list[$key1]['share_commission_new_third'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
  747. $checked_attr_list[$key1]['share_commission_new_third'][$share_level_index]['value'] = 0;
  748. }
  749. //$checked_attr_list[$key1]['pic']=$value1['quantity'];
  750. }
  751. } catch (\Exception $e) {}
  752. return [
  753. 'code' => 0,
  754. 'data' => [
  755. 'title' => $title,//标题
  756. 'pics' => $picsPath,//缩略图
  757. 'price' => $goods_price,//原价
  758. 'sale_price' => $goods_price_sale,//售价
  759. 'sale_count' => $sale_count,//销量
  760. 'quantity' => $quantity, //库存
  761. 'desc' => $detail_info,//图文详情
  762. 'video' => $video,//图文详情
  763. 'attrGroup' => $attr_group_list,
  764. 'attr' => $checked_attr_list,
  765. ]
  766. ];
  767. }
  768. public function setDetail($detail_info, $arr = [], $index = 0) {
  769. $start = stripos($detail_info, 'background-image:url(');
  770. $end = stripos($detail_info, '.jpg');
  771. if ($end !== false && $start !== false) {
  772. $arr[$index] = substr($detail_info, ($start), (($end + 4) - $start));
  773. $arr[$index] = substr($arr[$index], 21);
  774. if (!stripos($arr[$index], 'http') && !stripos($arr[$index], 'https')) {
  775. $arr[$index] = 'https:' . $arr[$index];
  776. }
  777. $new_detail_info = substr($detail_info, ($end + 4));
  778. if (stripos($new_detail_info, 'background-image:url(')) {
  779. ++$index;
  780. return $this->setDetail($new_detail_info, $arr, $index);
  781. }
  782. }
  783. return $arr;
  784. }
  785. /**
  786. * @param $list //规格列表
  787. * @param $level //规格列表下标
  788. * @param array $attr_list
  789. * @param string $n
  790. * @param array $new_list
  791. * @return array
  792. * 规格列表数组重组
  793. */
  794. public function get_attr_list($list, $level, $attr_list = array(), $n = '', $new_list = array())
  795. {
  796. foreach ($list[$level]['values'] as $key => $item) {
  797. $attr_list_new = $attr_list;
  798. $n_n = $n;
  799. $a = [];
  800. $n_n .= $list[$level]['propId'] . ':' . $item['valueId'];
  801. $a['attr_group_name'] = $list[$level]['propName'];
  802. $a['attr_id'] = null;
  803. $a['attr_name'] = $item['name'];
  804. array_push($attr_list_new, $a);
  805. if ($level < count($list) - 1 && $level < $this->level) {
  806. $new_list = $this->get_attr_list($list, $level + 1, $attr_list_new, $n_n . ';', $new_list);
  807. } else {
  808. $new_list_new['attr_list'] = $attr_list_new;
  809. $new_list_new['dataId'] = $n_n;
  810. $new_list[] = $new_list_new;
  811. }
  812. }
  813. return $new_list;
  814. }
  815. /**
  816. * @param $list //规格列表
  817. * @param $level //规格列表下标
  818. * @param array $attr_list
  819. * @param string $n
  820. * @param array $new_list
  821. * @return array
  822. * 规格列表数组重组 for jd by tianxin100
  823. */
  824. public function get_attr_list_for_jd($list, $level, $attr_list = array(), $n = '', $new_list = array())
  825. {
  826. foreach ($list[$level]['attr_list'] as $key => $item) {
  827. $attr_list_new = $attr_list;
  828. //$n_n = $n;
  829. $a = [];
  830. // $n_n .= $list[$level]['propId'] . ':' . $item['valueId'];
  831. $a['attr_group_name'] = $list[$level]['attr_group_name'];
  832. $a['attr_id'] = null;
  833. $a['attr_name'] = $item['attr_name'];
  834. array_push($attr_list_new, $a);
  835. if ($level < count($list) - 1 && $level < $this->level) {
  836. $new_list[] = $this->get_attr_list_for_jd($list, $level + 1, $attr_list_new, $new_list);
  837. } else {
  838. $new_list_new['attr_list'] = $attr_list_new;
  839. //$new_list_new['dataId'] = $n_n;
  840. $new_list[] = $new_list_new;
  841. }
  842. }
  843. return $new_list;
  844. }
  845. /**
  846. * @param $start
  847. * @param $end
  848. * @param $str
  849. * @return array
  850. * 正则截取函数
  851. */
  852. public function pregSubstr($start, $end, $str) // 正则截取函数
  853. {
  854. try {
  855. $temp = preg_split($start, $str);
  856. } catch (\Exception $e) {
  857. var_dump($str);
  858. exit();
  859. }
  860. $result = [];
  861. foreach ($temp as $index => $value) {
  862. if ($index == 0) {
  863. continue;
  864. }
  865. $content = preg_split($end, $value);
  866. array_push($result, $content[0]);
  867. }
  868. return $result;
  869. }
  870. /**
  871. * @param $start
  872. * @param $end
  873. * @param $str
  874. * @return array
  875. * 字符串截取函数
  876. */
  877. function str_substr($start, $end, $str) // 字符串截取函数
  878. {
  879. $temp = explode($start, $str);
  880. try {
  881. $result = [];
  882. foreach ($temp as $index => $value) {
  883. if ($index == 0) {
  884. continue;
  885. }
  886. $content = explode($end, $value);
  887. array_push($result, $content[0]);
  888. }
  889. return $result;
  890. } catch (\Exception $e) {
  891. var_dump($temp);
  892. return [];
  893. }
  894. }
  895. /**
  896. * @param $url
  897. * @return mixed
  898. * curl访问https
  899. */
  900. public function getHTTPS($url)
  901. {
  902. $ch = curl_init();
  903. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  904. curl_setopt($ch, CURLOPT_HEADER, false);
  905. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
  906. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  907. }
  908. curl_setopt($ch, CURLOPT_URL, $url);
  909. curl_setopt($ch, CURLOPT_REFERER, $url);
  910. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  911. $result = curl_exec($ch);
  912. curl_close($ch);
  913. return $result;
  914. }
  915. /**
  916. * @param $url
  917. * @return mixed
  918. * curl访问http
  919. */
  920. public function normal_curl($url)
  921. {
  922. $curl = curl_init();
  923. curl_setopt($curl, CURLOPT_URL, $url);
  924. curl_setopt($curl, CURLOPT_HEADER, 0);
  925. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  926. $data = curl_exec($curl);
  927. //错误提示
  928. if (curl_exec($curl) === false) {
  929. die(curl_error($curl));
  930. }
  931. // 检查是否有错误发生
  932. if (curl_errno($curl)) {
  933. echo 'Curl error: ' . curl_error($curl);
  934. }
  935. curl_close($curl);
  936. return $data;
  937. }
  938. //判断数组是几维数组
  939. public function arrayLevel($arr){
  940. $al = array(0);
  941. function aL($arr,&$al,$level=0){
  942. if(is_array($arr)){
  943. $level++;
  944. $al[] = $level;
  945. foreach($arr as $v){
  946. aL($v,$al,$level);
  947. }
  948. }
  949. }
  950. aL($arr,$al);
  951. return max($al);
  952. }
  953. }