Grab.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\goodsgrab\model;
  11. use app\model\BaseModel;
  12. use app\model\goods\Goods;
  13. use app\model\goods\VirtualGoods;
  14. use app\model\upload\Album;
  15. use app\model\upload\Upload;
  16. /**
  17. * 商品采集
  18. */
  19. class Grab extends BaseModel
  20. {
  21. //接口地址
  22. private $request_url = [
  23. 'taobao' => 'https://api03.6bqb.com/taobao/detail',
  24. 'tmall' => 'https://api03.6bqb.com/tmall/detail',
  25. 'jd' => 'https://api03.6bqb.com/jd/detail',
  26. '1688' => 'https://api03.6bqb.com/alibaba/pro/detail'
  27. ];
  28. //采集平台
  29. private $url_type = [
  30. 'detail.tmall.com' => ['type' => 'tmall', 'type_name' => '天猫'],
  31. 'item.taobao.com' => ['type' => 'taobao', 'type_name' => '淘宝'],
  32. 'detail.1688.com' => ['type' => '1688', 'type_name' => '1688'],
  33. 'item.jd.com' => ['type' => 'jd', 'type_name' => '京东']
  34. ];
  35. /**
  36. * 生成采集记录
  37. * @param $data
  38. * @return array
  39. */
  40. public function addGoodsGrab($data)
  41. {
  42. $data['create_time'] = time();
  43. $res = model('goods_grab')->add($data);
  44. return $this->success($res);
  45. }
  46. /**
  47. * 添加采集明细记录
  48. * @param $grab_id
  49. * @param $data
  50. * @return array
  51. */
  52. public function addGoodsGrabDetail($data)
  53. {
  54. model('goods_grab_detail')->startTrans();
  55. try {
  56. model('goods_grab_detail')->add($data);
  57. $goods_grab_info = model('goods_grab')->getInfo(['grab_id' => $data['grab_id']]);
  58. $total_num = $goods_grab_info['total_num'] + 1;
  59. $success_num = $goods_grab_info['success_num'];
  60. $error_num = $goods_grab_info['error_num'];
  61. if ($data['status'] == 1) {
  62. $success_num += 1;
  63. } else {
  64. $error_num += 1;
  65. }
  66. model('goods_grab')->update(
  67. ['total_num' => $total_num, 'success_num' => $success_num, 'error_num' => $error_num],
  68. ['grab_id' => $data['grab_id']]
  69. );
  70. model('goods_grab_detail')->commit();
  71. return $this->success();
  72. } catch (\Exception $e) {
  73. model('goods_grab_detail')->rollback();
  74. return $this->error('', $e->getMessage());
  75. }
  76. }
  77. /**
  78. * 商品采集
  79. * @param $grab_id
  80. * @param $url
  81. * @return array
  82. */
  83. public function goodsGrab($data, $url)
  84. {
  85. set_time_limit(0);
  86. $site_id = $data['site_id'];
  87. $grab_id = $data['grab_id'];
  88. if ($grab_id == 0) {
  89. return $this->error('', '缺少参数grab_id');
  90. }
  91. if ($url == '') {
  92. return $this->error('', '商品链接不能为空');
  93. }
  94. model('goods_grab')->startTrans();
  95. try {
  96. //判断链接合法性
  97. if (!is_url($url)) {
  98. $this->addGoodsGrabDetail(['site_id' => $site_id, 'grab_id' => $grab_id, 'url' => $url, 'reason' => 'url地址不正确', 'status' => 2]);
  99. model('goods_grab')->commit();
  100. return $this->error('', 'url地址不正确');
  101. }
  102. //判断url类型 及 获取id
  103. $url_data = parse_url($url);
  104. if (!array_key_exists($url_data['host'], $this->url_type)) {
  105. $this->addGoodsGrabDetail(['site_id' => $site_id, 'grab_id' => $grab_id, 'url' => $url, 'reason' => '不支持的商品链接', 'status' => 2]);
  106. model('goods_grab')->commit();
  107. return $this->error('', '不支持的商品链接');
  108. }
  109. $url_type = $this->url_type[$url_data['host']];
  110. if ($url_type['type'] == 'jd' || $url_type['type'] == '1688') {
  111. $id = preg_replace('/[^0-9]/', '', $url_data['path']);
  112. }else {
  113. $params = $this->convertUrlQuery($url_data['query']);
  114. $id = $params['id'] ?? '';
  115. }
  116. if ($id == '') {
  117. $this->addGoodsGrabDetail(['site_id' => $site_id, 'grab_id' => $grab_id, 'url' => $url, 'reason' => 'url地址不正确', 'status' => 2]);
  118. model('goods_grab')->commit();
  119. return $this->error('', 'url地址不正确');
  120. }
  121. //99api key
  122. $config_model = new GoodsGrab();
  123. $config = $config_model->getGoodsGrabConfig($site_id);
  124. $key = $config['data']['value']['key'];
  125. $request_url = $this->request_url[$url_type['type']];
  126. $request_url = $request_url . '?apikey=' . urlencode($key) . '&itemid=' . urlencode($id);
  127. //获取商品数据
  128. $goods_data = $this->httpGet($request_url);
  129. if ($goods_data['code'] < 0) {
  130. $this->addGoodsGrabDetail(array_merge($url_type, ['site_id' => $site_id, 'grab_id' => $grab_id, 'url' => $url, 'reason' => $goods_data['message'], 'status' => 2]));
  131. model('goods_grab')->commit();
  132. return $goods_data;
  133. }
  134. //处理数据
  135. switch ($url_type['type']) {
  136. //淘宝
  137. case 'taobao':
  138. $goods_data = $this->taobaoHandleData($goods_data['data']['data']['item'],$site_id);
  139. break;
  140. //天猫
  141. case 'tmall':
  142. $goods_data = $this->tmallHandleData($goods_data['data']['data']['item'],$site_id);
  143. break;
  144. //京东
  145. case 'jd':
  146. $goods_data = $this->jdHandleData($goods_data['data']['data']['item'],$site_id);
  147. break;
  148. //1688
  149. case '1688':
  150. $goods_data = $this->alibabaHandleData($goods_data['data']['data'],$site_id);
  151. break;
  152. }
  153. if($goods_data['code'] < 0){
  154. $this->addGoodsGrabDetail(array_merge($url_type, ['site_id' => $site_id, 'grab_id' => $grab_id, 'url' => $url, 'reason' => $goods_data['message'], 'status' => 2]));
  155. model('goods_grab')->commit();
  156. return $goods_data;
  157. }
  158. $goods_data = $goods_data['data'];
  159. $goods_data['category_id'] = $data['category_id'];
  160. $goods_data['category_json'] = $data['category_json'];
  161. $goods_data['is_virtual'] = $data['is_virtual'];
  162. if ($data['is_virtual']) {
  163. $goods_model = new VirtualGoods();
  164. $goods_data['is_need_verify'] = 0;
  165. $goods_data['verify_validity_type'] = 0;
  166. $goods_data['virtual_indate'] = 0;
  167. $res = $goods_model->addGoods($goods_data);
  168. } else {
  169. $goods_model = new Goods();
  170. $res = $goods_model->addGoods($goods_data);
  171. }
  172. if($res['code'] < 0){
  173. $this->addGoodsGrabDetail(array_merge($url_type, ['site_id' => $site_id, 'grab_id' => $grab_id, 'reason' => $res['message'], 'url' => $url, 'status' => 2]));
  174. }else{
  175. $this->addGoodsGrabDetail(array_merge($url_type, ['site_id' => $site_id, 'grab_id' => $grab_id, 'url' => $url, 'status' => 1]));
  176. }
  177. model('goods_grab')->commit();
  178. return $res;
  179. } catch (\Exception $e) {
  180. model('goods_grab')->rollback();
  181. return $this->error('', $e->getMessage());
  182. }
  183. }
  184. /**
  185. * 获取url中的参数
  186. * @param $query
  187. * @return array
  188. */
  189. function convertUrlQuery($query)
  190. {
  191. $queryParts = explode('&', $query);
  192. $params = array();
  193. foreach ($queryParts as $param) {
  194. $item = explode('=', $param);
  195. $params[$item[0]] = $item[1];
  196. }
  197. return $params;
  198. }
  199. /************************************************ 商品数据处理 start ********************************************************/
  200. /**
  201. * 淘宝数据处理
  202. * @param $key
  203. * @param $id
  204. */
  205. public function taobaoHandleData($params,$site_id)
  206. {
  207. try{
  208. $goods_spec_format = [];
  209. if(isset($params['props']) && !empty($params['props'])){
  210. foreach ($params['props'] as $k => $v) {
  211. $goods_spec_format[$k] = [
  212. 'spec_id' => $v['pid'],
  213. 'spec_name' => $v['name']
  214. ];
  215. foreach ($v['values'] as $key => $val) {
  216. $goods_spec_format[$k]['value'][$key] = [
  217. 'spec_id' => $v['pid'],
  218. 'spec_name' => $v['name'],
  219. 'spec_value_id' => $val['vid'],
  220. 'spec_value_name' => $val['name'],
  221. 'image' => ''
  222. ];
  223. }
  224. }
  225. }
  226. //商品sku
  227. $goods_sku_data = [];
  228. if(empty($goods_spec_format)){//单规格
  229. $goods_spec_format = '';
  230. $goods_sku_data[] = [
  231. 'spec_name' => '',
  232. 'sku_no' => "",
  233. 'sku_spec_format' => '',
  234. 'price' => '',
  235. 'market_price' => "",
  236. 'cost_price' => "",
  237. 'stock' => 0,
  238. 'stock_alarm' => 0,
  239. 'weight' => "",
  240. 'volume' => "",
  241. 'sku_image' => "",
  242. 'sku_images' => "",
  243. 'sku_images_arr' => [],
  244. 'is_default' => 0,
  245. 'propPath' => '',
  246. ];
  247. }else {//多规格
  248. foreach ($goods_spec_format as $k => $v) {
  249. $item_prop_arr = [];
  250. if(empty($goods_sku_data)){
  251. foreach ($v['value'] as $key => $val) {
  252. $item_prop_arr[] = [
  253. 'spec_name' => $val['spec_value_name'],
  254. 'sku_no' => "",
  255. 'sku_spec_format' => [$val],
  256. 'price' => "",
  257. 'market_price' => "",
  258. 'cost_price' => "",
  259. 'stock' => 0,
  260. 'stock_alarm' => 0,
  261. 'weight' => "",
  262. 'volume' => "",
  263. 'sku_image' => "",
  264. 'sku_images' => "",
  265. 'sku_images_arr' => [],
  266. 'is_default' => 0,
  267. 'propPath' => $val['spec_id'].':'.$val['spec_value_id'].';',
  268. ];
  269. }
  270. }else{
  271. foreach($goods_sku_data as $key1 => $val1){
  272. foreach($v['value'] as $key2 => $val2){
  273. $sku_spec_format = $val1['sku_spec_format'];
  274. $sku_spec_format[] = $val2;
  275. $item_prop_arr[] = [
  276. 'spec_name' => $val1['spec_name'].' '.$val2['spec_value_name'],
  277. 'sku_no' => "",
  278. 'sku_spec_format' => $sku_spec_format,
  279. 'price' => "",
  280. 'market_price' => "",
  281. 'cost_price' => "",
  282. 'stock' => 0,
  283. 'stock_alarm' => "",
  284. 'weight' => "",
  285. 'volume' => "",
  286. 'sku_image' => "",
  287. 'sku_images' => "",
  288. 'sku_images_arr' => [],
  289. 'is_default' => 0,
  290. 'propPath' => $val1['propPath'].$val2['spec_id'].':'.$val2['spec_value_id'].';',
  291. ];
  292. }
  293. }
  294. }
  295. $goods_sku_data = count($item_prop_arr) > 0 ? $item_prop_arr : $goods_sku_data;
  296. }
  297. }
  298. //获取商品价格
  299. foreach($goods_sku_data as $k=>$v){
  300. if(empty($goods_spec_format)){
  301. $goods_sku_data[$k]['price'] = $params['sku'][0]['price'];
  302. }else{
  303. $propPath = substr($v['propPath'],0,-1);
  304. $sku_info = array_filter($params['sku'], function($t) use ($propPath) { return $t['propPath'] == $propPath; });
  305. foreach($sku_info as $sku){
  306. $goods_sku_data[$k]['price'] = $sku['price'];
  307. $goods_sku_data[$k]['sku_image'] = $sku['image'] ?? '';
  308. }
  309. }
  310. unset($goods_sku_data[$k]['propPath']);
  311. }
  312. //商品属性
  313. $groupProps = $params['groupProps'][0]['基本信息'] ?? [];
  314. $goods_attr_format = [];
  315. if(!empty($groupProps)){
  316. $num = 1;
  317. foreach($groupProps as $v){
  318. foreach($v as $item_k=>$item_v){
  319. $goods_attr_format[] = [
  320. 'attr_class_id' => '-'.($num + 1),
  321. 'attr_id' => '-'.($num + 2),
  322. 'attr_name' => $item_k,
  323. 'attr_value_id' => '-'.($num + 3),
  324. 'attr_value_name' => $item_v,
  325. 'sort' => 0
  326. ];
  327. $num += 1;
  328. }
  329. }
  330. }
  331. $video_url = isset($params['videos']) ? $params['videos'][0]['url'] : '';
  332. $goods_data = [
  333. 'goods_name' => $params['title'],// 商品名称,
  334. 'goods_attr_class' => '',// 商品类型id,
  335. 'goods_attr_name' => '',// 商品类型名称,
  336. 'site_id' => $site_id,
  337. 'category_id' => '',
  338. 'category_json' => '',
  339. 'goods_image' => $params['images'],// 商品主图路径
  340. 'goods_content' => '',// 商品详情
  341. 'goods_state' => 0,// 商品状态(1.正常0下架)
  342. 'price' => $goods_sku_data[0]['price'],// 商品价格(取第一个sku)
  343. 'market_price' => '',// 市场价格(取第一个sku)
  344. 'cost_price' => '',// 成本价(取第一个sku)
  345. 'sku_no' => '',// 商品sku编码
  346. 'weight' => '',// 重量
  347. 'volume' => '',// 体积
  348. 'goods_stock' => 0,// 商品库存(总和)
  349. 'goods_stock_alarm' => '',// 库存预警
  350. 'is_free_shipping' => 1,// 是否免邮
  351. 'shipping_template' => '',// 指定运费模板
  352. 'goods_spec_format' => $goods_spec_format,// 商品规格格式
  353. 'goods_attr_format' => $goods_attr_format,// 商品属性格式
  354. 'introduction' => '',// 促销语
  355. 'keywords' => '',// 关键词
  356. 'unit' => '',// 单位
  357. 'sort' => '',// 排序,
  358. 'video_url' => $video_url,// 视频
  359. 'goods_sku_data' => $goods_sku_data,// SKU商品数据
  360. 'goods_service_ids' => '',// 商品服务id集合
  361. 'label_id' => '',// 商品分组id
  362. 'virtual_sale' => '',// 虚拟销量
  363. 'max_buy' => '',// 限购
  364. 'min_buy' => '',// 起售
  365. 'recommend_way' => '', // 推荐方式,1:新品,2:精品,3;推荐
  366. 'timer_on' => '',//定时上架
  367. 'timer_off' => '',//定时下架
  368. 'is_consume_discount' => ''//是否参与会员折扣
  369. ];
  370. //处理商品详情
  371. $desc = '';
  372. if (isset($params['desc'])) {
  373. $temp_array = explode('src="',$params['desc']);
  374. foreach($temp_array as $k => $v){
  375. if($k > 0){
  376. $position = strpos($v,'http');
  377. if($position === false || $position !== 0){
  378. $v = 'https:'.$v;
  379. }
  380. $desc .= 'src="'.$v;
  381. }else{
  382. $desc .= $v;
  383. }
  384. }
  385. }
  386. $goods_data['goods_content'] = $desc;
  387. $goods_data = $this->downloadResources($goods_data,$site_id);
  388. if($goods_data['code'] < 0){
  389. return $goods_data;
  390. }
  391. $goods_data = $goods_data['data'];
  392. $goods_data['goods_spec_format'] = empty($goods_data['goods_spec_format']) ? '' : json_encode($goods_data['goods_spec_format']);
  393. $goods_data['goods_attr_format'] = json_encode($goods_data['goods_attr_format']);
  394. $goods_data['goods_sku_data'] = json_encode($goods_data['goods_sku_data']);
  395. return $this->success($goods_data);
  396. }catch (\Exception $e){
  397. return $this->error('',$e->getMessage());
  398. }
  399. }
  400. /**
  401. * 天猫数据处理
  402. * @param $key
  403. * @param $id
  404. */
  405. public function tmallHandleData($params,$site_id)
  406. {
  407. try{
  408. $goods_spec_format = [];
  409. foreach ($params['props'] as $k => $v) {
  410. $goods_spec_format[$k] = [
  411. 'spec_id' => $v['pid'],
  412. 'spec_name' => $v['name']
  413. ];
  414. foreach ($v['values'] as $key => $val) {
  415. $goods_spec_format[$k]['value'][$key] = [
  416. 'spec_id' => $v['pid'],
  417. 'spec_name' => $v['name'],
  418. 'spec_value_id' => $val['vid'],
  419. 'spec_value_name' => $val['name'],
  420. 'image' => ''
  421. ];
  422. }
  423. }
  424. //商品sku
  425. $goods_sku_data = [];
  426. foreach ($goods_spec_format as $k => $v) {
  427. $item_prop_arr = [];
  428. if(empty($goods_sku_data)){
  429. foreach ($v['value'] as $key => $val) {
  430. $item_prop_arr[] = [
  431. 'spec_name' => $val['spec_value_name'],
  432. 'sku_no' => "",
  433. 'sku_spec_format' => [$val],
  434. 'price' => "",
  435. 'market_price' => "",
  436. 'cost_price' => "",
  437. 'stock' => 0,
  438. 'stock_alarm' => 0,
  439. 'weight' => "",
  440. 'volume' => "",
  441. 'sku_image' => "",
  442. 'sku_images' => "",
  443. 'sku_images_arr' => [],
  444. 'is_default' => 0,
  445. 'propPath' => $val['spec_id'].':'.$val['spec_value_id'].';',
  446. ];
  447. }
  448. }else{
  449. foreach($goods_sku_data as $key1 => $val1){
  450. foreach($v['value'] as $key2 => $val2){
  451. $sku_spec_format = $val1['sku_spec_format'];
  452. $sku_spec_format[] = $val2;
  453. $item_prop_arr[] = [
  454. 'spec_name' => $val1['spec_name'].' '.$val2['spec_value_name'],
  455. 'sku_no' => "",
  456. 'sku_spec_format' => $sku_spec_format,
  457. 'price' => "",
  458. 'market_price' => "",
  459. 'cost_price' => "",
  460. 'stock' => 0,
  461. 'stock_alarm' => "",
  462. 'weight' => "",
  463. 'volume' => "",
  464. 'sku_image' => "",
  465. 'sku_images' => "",
  466. 'sku_images_arr' => [],
  467. 'is_default' => 0,
  468. 'propPath' => $val1['propPath'].$val2['spec_id'].':'.$val2['spec_value_id'].';',
  469. ];
  470. }
  471. }
  472. }
  473. $goods_sku_data = count($item_prop_arr) > 0 ? $item_prop_arr : $goods_sku_data;
  474. }
  475. //获取商品价格
  476. foreach($goods_sku_data as $k=>$v){
  477. $propPath = substr($v['propPath'],0,-1);
  478. $sku_info = array_filter($params['sku'], function($t) use ($propPath) { return $t['propPath'] == $propPath; });
  479. foreach($sku_info as $sku){
  480. $goods_sku_data[$k]['price'] = $sku['price'];
  481. $goods_sku_data[$k]['sku_image'] = $sku['image'] ?? '';
  482. }
  483. unset($goods_sku_data[$k]['propPath']);
  484. }
  485. //商品属性
  486. $groupProps = $params['groupProps'][0]['基本信息'] ?? [];
  487. $goods_attr_format = [];
  488. if(!empty($groupProps)){
  489. foreach($groupProps as $v){
  490. foreach($v as $item_k=>$item_v){
  491. $goods_attr_format[] = [
  492. 'attr_class_id' => '-'.mt_rand(0,9).mt_rand(1,999),
  493. 'attr_id' => '-'.mt_rand(0,9).mt_rand(1,999),
  494. 'attr_name' => $item_k,
  495. 'attr_value_id' => '-'.mt_rand(0,9).mt_rand(1,999),
  496. 'attr_value_name' => $item_v,
  497. 'sort' => 0
  498. ];
  499. }
  500. }
  501. }
  502. $video_url = isset($params['videos']) ? $params['videos'][0]['url'] : '';
  503. $goods_data = [
  504. 'goods_name' => $params['title'],// 商品名称,
  505. 'goods_attr_class' => '',// 商品类型id,
  506. 'goods_attr_name' => '',// 商品类型名称,
  507. 'site_id' => $site_id,
  508. 'category_id' => '',
  509. 'category_json' => '',
  510. 'goods_image' => $params['images'],// 商品主图路径
  511. 'goods_content' => '',// 商品详情
  512. 'goods_state' => 0,// 商品状态(1.正常0下架)
  513. 'price' => $goods_sku_data[0]['price'],// 商品价格(取第一个sku)
  514. 'market_price' => '',// 市场价格(取第一个sku)
  515. 'cost_price' => '',// 成本价(取第一个sku)
  516. 'sku_no' => '',// 商品sku编码
  517. 'weight' => '',// 重量
  518. 'volume' => '',// 体积
  519. 'goods_stock' => 0,// 商品库存(总和)
  520. 'goods_stock_alarm' => '',// 库存预警
  521. 'is_free_shipping' => 1,// 是否免邮
  522. 'shipping_template' => '',// 指定运费模板
  523. 'goods_spec_format' => $goods_spec_format,// 商品规格格式
  524. 'goods_attr_format' => $goods_attr_format,// 商品属性格式
  525. 'introduction' => '',// 促销语
  526. 'keywords' => '',// 关键词
  527. 'unit' => '',// 单位
  528. 'sort' => '',// 排序,
  529. 'video_url' => $video_url,// 视频
  530. 'goods_sku_data' => $goods_sku_data,// SKU商品数据
  531. 'goods_service_ids' => '',// 商品服务id集合
  532. 'label_id' => '',// 商品分组id
  533. 'virtual_sale' => '',// 虚拟销量
  534. 'max_buy' => '',// 限购
  535. 'min_buy' => '',// 起售
  536. 'recommend_way' => '', // 推荐方式,1:新品,2:精品,3;推荐
  537. 'timer_on' => '',//定时上架
  538. 'timer_off' => '',//定时下架
  539. 'is_consume_discount' => ''//是否参与会员折扣
  540. ];
  541. //处理商品详情
  542. $desc = '';
  543. $temp_array = explode('src="',$params['desc']);
  544. foreach($temp_array as $k => $v){
  545. if($k > 0){
  546. $position = strpos($v,'http');
  547. if($position === false || $position !== 0){
  548. $v = 'https:'.$v;
  549. }
  550. $desc .= 'src="'.$v;
  551. }else{
  552. $desc .= $v;
  553. }
  554. }
  555. $goods_data['goods_content'] = $desc;
  556. $goods_data = $this->downloadResources($goods_data,$site_id);
  557. if($goods_data['code'] < 0){
  558. return $goods_data;
  559. }
  560. $goods_data = $goods_data['data'];
  561. $goods_data['goods_spec_format'] = json_encode($goods_data['goods_spec_format']);
  562. $goods_data['goods_attr_format'] = json_encode($goods_data['goods_attr_format']);
  563. $goods_data['goods_sku_data'] = json_encode($goods_data['goods_sku_data']);
  564. return $this->success($goods_data);
  565. }catch (\Exception $e){
  566. return $this->error('',$e->getMessage());
  567. }
  568. }
  569. /**
  570. * 京东数据处理
  571. * @param $key
  572. * @param $id
  573. */
  574. public function jdHandleData($params,$site_id)
  575. {
  576. try{
  577. $goods_spec_format = [];
  578. $num = 0;
  579. foreach ($params['saleProp'] as $k=>$v) {
  580. $sku_props = $params['skuProps'][$k];
  581. if(!empty($sku_props)){
  582. $spec_id = $k.mt_rand(1,99).mt_rand(1,999);
  583. $goods_spec_format[$num] = [
  584. 'spec_id' => $spec_id,
  585. 'spec_name' => $v,
  586. 'value' => []
  587. ];
  588. foreach($sku_props as $key => $val){
  589. if(!empty($val)){
  590. $goods_spec_format[$num]['value'][$key] = [
  591. 'spec_id' => $spec_id,
  592. 'spec_name' => $v,
  593. 'spec_value_id' => $key.mt_rand(1,999).mt_rand(1,99),
  594. 'spec_value_name' => $val,
  595. 'image' => ''
  596. ];
  597. }
  598. }
  599. }
  600. if(empty($goods_spec_format[$num]['value'])){
  601. unset($goods_spec_format[$num]);
  602. }
  603. $num ++;
  604. }
  605. //商品sku
  606. $goods_sku_data = [];
  607. if(empty($goods_spec_format)){//单规格
  608. $goods_spec_format = '';
  609. $goods_sku_data[] = [
  610. 'spec_name' => '',
  611. 'sku_no' => "",
  612. 'sku_spec_format' => '',
  613. 'price' => '',
  614. 'market_price' => "",
  615. 'cost_price' => "",
  616. 'stock' => 0,
  617. 'stock_alarm' => 0,
  618. 'weight' => "",
  619. 'volume' => "",
  620. 'sku_image' => "",
  621. 'sku_images' => "",
  622. 'sku_images_arr' => [],
  623. 'is_default' => 0,
  624. 'propPath' => '',
  625. ];
  626. }else{//多规格
  627. foreach ($goods_spec_format as $k => $v) {
  628. $item_prop_arr = [];
  629. if(empty($goods_sku_data)){
  630. foreach ($v['value'] as $key => $val) {
  631. $item_prop_arr[] = [
  632. 'spec_name' => $val['spec_value_name'],
  633. 'sku_no' => "",
  634. 'sku_spec_format' => [$val],
  635. 'price' => "",
  636. 'market_price' => "",
  637. 'cost_price' => "",
  638. 'stock' => 0,
  639. 'stock_alarm' => 0,
  640. 'weight' => "",
  641. 'volume' => "",
  642. 'sku_image' => "",
  643. 'sku_images' => "",
  644. 'sku_images_arr' => [],
  645. 'is_default' => 0,
  646. 'propPath' => $val['spec_value_name'].':',
  647. ];
  648. }
  649. }else{
  650. foreach($goods_sku_data as $key1 => $val1){
  651. foreach($v['value'] as $key2 => $val2){
  652. $sku_spec_format = $val1['sku_spec_format'];
  653. $sku_spec_format[] = $val2;
  654. $item_prop_arr[] = [
  655. 'spec_name' => $val1['spec_name'].' '.$val2['spec_value_name'],
  656. 'sku_no' => "",
  657. 'sku_spec_format' => $sku_spec_format,
  658. 'price' => "",
  659. 'market_price' => "",
  660. 'cost_price' => "",
  661. 'stock' => 0,
  662. 'stock_alarm' => "",
  663. 'weight' => "",
  664. 'volume' => "",
  665. 'sku_image' => "",
  666. 'sku_images' => "",
  667. 'sku_images_arr' => [],
  668. 'is_default' => 0,
  669. 'propPath' => $val1['propPath'].$val2['spec_value_name'].':',
  670. ];
  671. }
  672. }
  673. }
  674. $goods_sku_data = count($item_prop_arr) > 0 ? $item_prop_arr : $goods_sku_data;
  675. }
  676. }
  677. //获取商品价格
  678. foreach($goods_sku_data as $k=>$v){
  679. if($v['propPath'] == ''){
  680. $prop_path_arr = [
  681. '1' => '',
  682. '2' => '',
  683. '3' => ''
  684. ];
  685. }else{
  686. $propPath = substr($v['propPath'],0,-1);
  687. $prop_path_arr = explode(':',$propPath);
  688. $prop_path_arr = [
  689. '1' => $prop_path_arr[0] ?? '',
  690. '2' => $prop_path_arr[1] ?? '',
  691. '3' => $prop_path_arr[2] ?? '',
  692. ];
  693. }
  694. $is_exists = 0;
  695. foreach($params['sku'] as $item_k => $item_v){
  696. //判断该sku是否存在
  697. if($item_v[1] != ""){
  698. if($prop_path_arr[1] == $item_v[1] && $prop_path_arr[2] == $item_v[2] && $prop_path_arr[3] == $item_v[3]){
  699. $is_exists = 1;
  700. unset($goods_sku_data[$k]['propPath']);
  701. $goods_sku_data[$k]['price'] = $item_v['price'];
  702. break;
  703. }
  704. }
  705. if($item_v[1] == "" && $item_v[2] != ""){
  706. if($prop_path_arr[1] == $item_v[2] && $prop_path_arr[3] == $item_v[3]){
  707. $is_exists = 1;
  708. unset($goods_sku_data[$k]['propPath']);
  709. $goods_sku_data[$k]['price'] = $item_v['price'];
  710. }
  711. }
  712. if($item_v[1] == "" && $item_v[2] == ""){
  713. if($prop_path_arr[1] == $item_v[3]){
  714. $is_exists = 1;
  715. unset($goods_sku_data[$k]['propPath']);
  716. $goods_sku_data[$k]['price'] = $item_v['price'];
  717. }
  718. }
  719. }
  720. if($is_exists == 0){
  721. unset($goods_sku_data[$k]);
  722. }
  723. }
  724. $goods_sku_data = array_values($goods_sku_data);
  725. //商品属性
  726. $goods_attr_format = [];
  727. if(!empty($params['pcAttributes'])){
  728. foreach($params['pcAttributes'] as $k=>$v){
  729. $val = explode(':',$v);
  730. $goods_attr_format[] = [
  731. 'attr_class_id' => '-'. $k . mt_rand(1,99) . mt_rand(1,99),
  732. 'attr_id' => '-'. $k . mt_rand(1,99) . mt_rand(1,99),
  733. 'attr_name' => $val[0],
  734. 'attr_value_id' => '-'. $k . mt_rand(1,99) . mt_rand(1,99),
  735. 'attr_value_name' => $val[1],
  736. 'sort' => 0
  737. ];
  738. }
  739. }
  740. $video_url = "";
  741. if(isset($params['videoInfo'])){
  742. if(isset($params['videoInfo']['videoUrl'])){
  743. $video_url = $params['videoInfo']['videoInfo'] ? $params['videoInfo']['videoUrl'] : '';
  744. $video_url = is_array($video_url) ? $video_url['android'] : $video_url;
  745. }
  746. if(isset($params['videoInfo']['data'])){
  747. $video_url = is_array($params['videoInfo']['data']) ? $params['videoInfo']['data'][2]['main_url'] : '';
  748. }
  749. }
  750. $goods_data = [
  751. 'goods_name' => $params['name'],// 商品名称,
  752. 'goods_attr_class' => '',// 商品类型id,
  753. 'goods_attr_name' => '',// 商品类型名称,
  754. 'site_id' => $site_id,
  755. 'category_id' => '',
  756. 'category_json' => '',
  757. 'goods_image' => $params['images'],// 商品主图路径
  758. 'goods_content' => '',// 商品详情
  759. 'goods_state' => 0,// 商品状态(1.正常0下架)
  760. 'price' => !empty($goods_sku_data[0]['price'])?$goods_sku_data[0]['price']:'',// 商品价格(取第一个sku)
  761. 'market_price' => '',// 市场价格(取第一个sku)
  762. 'cost_price' => '',// 成本价(取第一个sku)
  763. 'sku_no' => '',// 商品sku编码
  764. 'weight' => '',// 重量
  765. 'volume' => '',// 体积
  766. 'goods_stock' => 0,// 商品库存(总和)
  767. 'goods_stock_alarm' => '',// 库存预警
  768. 'is_free_shipping' => 1,// 是否免邮
  769. 'shipping_template' => '',// 指定运费模板
  770. 'goods_spec_format' => $goods_spec_format,// 商品规格格式
  771. 'goods_attr_format' => $goods_attr_format,// 商品属性格式
  772. 'introduction' => '',// 促销语
  773. 'keywords' => '',// 关键词
  774. 'unit' => '',// 单位
  775. 'sort' => '',// 排序,
  776. 'video_url' => $video_url,// 视频
  777. 'goods_sku_data' => $goods_sku_data,// SKU商品数据
  778. 'goods_service_ids' => '',// 商品服务id集合
  779. 'label_id' => '',// 商品分组id
  780. 'virtual_sale' => '',// 虚拟销量
  781. 'max_buy' => '',// 限购
  782. 'min_buy' => '',// 起售
  783. 'recommend_way' => '', // 推荐方式,1:新品,2:精品,3;推荐
  784. 'timer_on' => '',//定时上架
  785. 'timer_off' => '',//定时下架
  786. 'is_consume_discount' => ''//是否参与会员折扣
  787. ];
  788. //处理商品详情
  789. if(!empty($params['desc'])){
  790. $old_desc = preg_replace('/(<img.+?data-lazyload=")(.*?)/','<img src="$2', $params['desc']);
  791. $temp_array = explode('src="',$old_desc);
  792. $desc = '';
  793. foreach($temp_array as $k => $v){
  794. if($k > 0){
  795. $position = strpos($v,'http');
  796. if($position === false || $position !== 0){
  797. $v = 'https:'.$v;
  798. }
  799. $desc .= 'src="'.$v;
  800. }else{
  801. $desc .= $v;
  802. }
  803. }
  804. $goods_data['goods_content'] = $desc;
  805. }
  806. $goods_data = $this->downloadResources($goods_data,$site_id);
  807. if($goods_data['code'] < 0){
  808. return $goods_data;
  809. }
  810. $goods_data = $goods_data['data'];
  811. if($goods_data['goods_spec_format']){
  812. sort($goods_data['goods_spec_format']);//排序
  813. }
  814. $goods_data['goods_spec_format'] = empty($goods_data['goods_spec_format']) ? '' : json_encode($goods_data['goods_spec_format']);
  815. $goods_data['goods_attr_format'] = json_encode($goods_data['goods_attr_format']);
  816. $goods_data['goods_sku_data'] = json_encode($goods_data['goods_sku_data']);
  817. return $this->success($goods_data);
  818. }catch (\Exception $e){
  819. return $this->error('',$e->getMessage());
  820. }
  821. }
  822. /**
  823. * 1688数据处理
  824. * @param $key
  825. * @param $id
  826. */
  827. public function alibabaHandleData($params,$site_id)
  828. {
  829. try{
  830. $goods_spec_format = [];
  831. if(isset($params['skuProps'])){
  832. foreach ($params['skuProps'] as $k => $v) {
  833. $spec_id = $k.mt_rand(1,99).mt_rand(1,999);
  834. $goods_spec_format[$k] = [
  835. 'spec_id' => $spec_id,
  836. 'spec_name' => $v['prop']
  837. ];
  838. foreach ($v['value'] as $key => $val) {
  839. $goods_spec_format[$k]['value'][$key] = [
  840. 'spec_id' => $spec_id,
  841. 'spec_name' => $v['prop'],
  842. 'spec_value_id' => $key.mt_rand(1,999).mt_rand(1,99),
  843. 'spec_value_name' => $val['name'],
  844. 'image' => ''
  845. ];
  846. }
  847. }
  848. }
  849. //商品sku
  850. $goods_sku_data = [];
  851. if(empty($goods_spec_format)){//单规格
  852. $goods_spec_format = '';
  853. $price = explode('-',$params['displayPrice']);
  854. $goods_sku_data[] = [
  855. 'spec_name' => '',
  856. 'sku_no' => "",
  857. 'sku_spec_format' => '',
  858. 'price' => str_replace('¥','',$price[0]),
  859. 'market_price' => "",
  860. 'cost_price' => "",
  861. 'stock' => 0,
  862. 'stock_alarm' => 0,
  863. 'weight' => "",
  864. 'volume' => "",
  865. 'sku_image' => "",
  866. 'sku_images' => "",
  867. 'sku_images_arr' => [],
  868. 'is_default' => 0,
  869. 'propPath' => '',
  870. ];
  871. }else{//多规格
  872. foreach ($goods_spec_format as $k => $v) {
  873. $item_prop_arr = [];
  874. if(empty($goods_sku_data)){
  875. foreach ($v['value'] as $key => $val) {
  876. $item_prop_arr[] = [
  877. 'spec_name' => $val['spec_value_name'],
  878. 'sku_no' => "",
  879. 'sku_spec_format' => [$val],
  880. 'price' => "",
  881. 'market_price' => "",
  882. 'cost_price' => "",
  883. 'stock' => 0,
  884. 'stock_alarm' => 0,
  885. 'weight' => "",
  886. 'volume' => "",
  887. 'sku_image' => "",
  888. 'sku_images' => "",
  889. 'sku_images_arr' => [],
  890. 'is_default' => 0,
  891. 'propPath' => $val['spec_value_name'].'>',
  892. ];
  893. }
  894. }else{
  895. foreach($goods_sku_data as $key1 => $val1){
  896. foreach($v['value'] as $key2 => $val2){
  897. $sku_spec_format = $val1['sku_spec_format'];
  898. $sku_spec_format[] = $val2;
  899. $item_prop_arr[] = [
  900. 'spec_name' => $val1['spec_name'].' '.$val2['spec_value_name'],
  901. 'sku_no' => "",
  902. 'sku_spec_format' => $sku_spec_format,
  903. 'price' => "",
  904. 'market_price' => "",
  905. 'cost_price' => "",
  906. 'stock' => 0,
  907. 'stock_alarm' => "",
  908. 'weight' => "",
  909. 'volume' => "",
  910. 'sku_image' => "",
  911. 'sku_images' => "",
  912. 'sku_images_arr' => [],
  913. 'is_default' => 0,
  914. 'propPath' => $val1['spec_name'].'>'.$val2['spec_value_name'].'>',
  915. ];
  916. }
  917. }
  918. }
  919. $goods_sku_data = count($item_prop_arr) > 0 ? $item_prop_arr : $goods_sku_data;
  920. }
  921. }
  922. //获取商品价格
  923. foreach($goods_sku_data as $k=>$v){
  924. if(isset($params['skuMap'])){
  925. if($params['skuMap']){
  926. $propPath = substr($v['propPath'],0,-1);
  927. $sku_info = $params['skuMap'][htmlspecialchars($propPath)];
  928. if(isset($sku_info['discountPrice'])){
  929. $price = $sku_info['discountPrice'];
  930. }else{
  931. $price = explode('-',$params['displayPrice']);
  932. $price = str_replace('¥','',$price[0]);
  933. }
  934. }else{
  935. $price = explode('-',$params['displayPrice']);
  936. $price = str_replace('¥','',$price[0]);
  937. }
  938. }else{
  939. $price = explode('-',$params['displayPrice']);
  940. $price = str_replace('¥','',$price[0]);
  941. }
  942. $goods_sku_data[$k]['price'] = $price;
  943. //$goods_sku_data[$k]['sku_image'] = $sku['image'] ?? '';
  944. unset($goods_sku_data[$k]['propPath']);
  945. }
  946. //商品属性
  947. $goods_attr_format = [];
  948. if(!empty($params['attributes'])){
  949. foreach($params['attributes'] as $k=>$v){
  950. $val = explode(':',$v);
  951. $goods_attr_format[] = [
  952. 'attr_class_id' => '-'. $k . mt_rand(1,99) . mt_rand(1,99),
  953. 'attr_id' => '-'. $k . mt_rand(1,99) . mt_rand(1,99),
  954. 'attr_name' => $val[0],
  955. 'attr_value_id' => '-'. $k . mt_rand(1,99) . mt_rand(1,99),
  956. 'attr_value_name' => $val[1],
  957. 'sort' => 0
  958. ];
  959. }
  960. }
  961. $video_url = '';
  962. if(isset($params['videoInfo']) && isset($params['videoInfo']['videoUrl'])){
  963. $video_url = $params['videoInfo']['videoUrl'];
  964. $video_url = is_array($video_url) ? $video_url['android'] : $video_url;
  965. }
  966. $goods_data = [
  967. 'goods_name' => $params['title'],// 商品名称,
  968. 'goods_attr_class' => '',// 商品类型id,
  969. 'goods_attr_name' => '',// 商品类型名称,
  970. 'site_id' => $site_id,
  971. 'category_id' => '',
  972. 'category_json' => '',
  973. 'goods_image' => $params['images'],// 商品主图路径
  974. 'goods_content' => '',// 商品详情
  975. 'goods_state' => 0,// 商品状态(1.正常0下架)
  976. 'price' => $goods_sku_data[0]['price'],// 商品价格(取第一个sku)
  977. 'market_price' => '',// 市场价格(取第一个sku)
  978. 'cost_price' => '',// 成本价(取第一个sku)
  979. 'sku_no' => '',// 商品sku编码
  980. 'weight' => '',// 重量
  981. 'volume' => '',// 体积
  982. 'goods_stock' => 0,// 商品库存(总和)
  983. 'goods_stock_alarm' => '',// 库存预警
  984. 'is_free_shipping' => 1,// 是否免邮
  985. 'shipping_template' => '',// 指定运费模板
  986. 'goods_spec_format' => $goods_spec_format,// 商品规格格式
  987. 'goods_attr_format' => $goods_attr_format,// 商品属性格式
  988. 'introduction' => '',// 促销语
  989. 'keywords' => '',// 关键词
  990. 'unit' => $params['unit'] ?? '',// 单位
  991. 'sort' => '',// 排序,
  992. 'video_url' => $video_url,// 视频
  993. 'goods_sku_data' => $goods_sku_data,// SKU商品数据
  994. 'goods_service_ids' => '',// 商品服务id集合
  995. 'label_id' => '',// 商品分组id
  996. 'virtual_sale' => '',// 虚拟销量
  997. 'max_buy' => '',// 限购
  998. 'min_buy' => '',// 起售
  999. 'recommend_way' => '', // 推荐方式,1:新品,2:精品,3;推荐
  1000. 'timer_on' => '',//定时上架
  1001. 'timer_off' => '',//定时下架
  1002. 'is_consume_discount' => ''//是否参与会员折扣
  1003. ];
  1004. //处理商品详情
  1005. $desc = '';
  1006. $temp_array = explode('src="',$params['desc']);
  1007. foreach($temp_array as $k => $v){
  1008. if($k > 0){
  1009. $position = strpos($v,'http');
  1010. if($position === false || $position !== 0){
  1011. $v = 'https:'.$v;
  1012. }
  1013. $desc .= 'src="'.$v;
  1014. }else{
  1015. $desc .= $v;
  1016. }
  1017. }
  1018. $goods_data['goods_content'] = $desc;
  1019. $goods_data = $this->downloadResources($goods_data,$site_id);
  1020. if($goods_data['code'] < 0){
  1021. return $goods_data;
  1022. }
  1023. $goods_data = $goods_data['data'];
  1024. $goods_data['goods_spec_format'] = empty($goods_data['goods_spec_format']) ? '' : json_encode($goods_data['goods_spec_format']);
  1025. $goods_data['goods_attr_format'] = json_encode($goods_data['goods_attr_format']);
  1026. $goods_data['goods_sku_data'] = json_encode($goods_data['goods_sku_data']);
  1027. return $this->success($goods_data);
  1028. }catch (\Exception $e){
  1029. return $this->error('',$e->getMessage());
  1030. }
  1031. }
  1032. /**
  1033. * 下载资源(商品图片 + 商品视屏)
  1034. * @param $goods_data
  1035. */
  1036. public function downloadResources($goods_data,$site_id)
  1037. {
  1038. try{
  1039. //查询相册默认值
  1040. $album_model = new Album();
  1041. $album_info = $album_model->getAlbumInfo([['site_id','=',$site_id],['is_default','=',1]]);
  1042. $album_info = $album_info['data'];
  1043. $upload_model = new Upload($site_id);
  1044. $path = "common/goods_grab/images/" . date("Ymd") . '/';
  1045. //下载商品图片
  1046. $goods_image = [];
  1047. if(!empty($goods_data['goods_image'])){
  1048. foreach($goods_data['goods_image'] as $v){
  1049. if(strpos($v,'http') !== false){
  1050. $goods_image_url = $v;
  1051. }else{
  1052. $goods_image_url = 'http:'.$v;
  1053. }
  1054. $goods_image_result = @$upload_model->setPath($path)->remoteGoodsPullToLocal(['img' => $goods_image_url, "thumb_type" => [ "BIG", "MID", "SMALL" ], 'album_id' => $album_info['album_id']]);
  1055. if($goods_image_result['code'] > 0){
  1056. $goods_image[] = $goods_image_result['data']['pic_path'];
  1057. }
  1058. }
  1059. $goods_data['goods_image'] = implode(',',$goods_image);
  1060. }
  1061. //下载sku图片
  1062. $goods_sku_data = $goods_data['goods_sku_data'];
  1063. foreach($goods_sku_data as $k=>$v){
  1064. if($v['sku_image'] != ''){
  1065. if(strpos($v['sku_image'],'http') !== false){
  1066. $sku_image = $v['sku_image'];
  1067. }else{
  1068. $sku_image = 'http:'.$v['sku_image'];
  1069. }
  1070. $sku_image_result = @$upload_model->setPath($path)->remoteGoodsPullToLocal(['img' => $sku_image, "thumb_type" => [ "BIG", "MID", "SMALL" ], 'album_id' => $album_info['album_id']]);
  1071. if($sku_image_result['code'] > 0){
  1072. $goods_sku_data[$k]['sku_image'] = $sku_image_result['data']['pic_path'];
  1073. $goods_sku_data[$k]['sku_images'] = $sku_image_result['data']['pic_path'];
  1074. }
  1075. }
  1076. }
  1077. $goods_data['goods_sku_data'] = $goods_sku_data;
  1078. return $this->success($goods_data);
  1079. }catch (\Exception $e){
  1080. return $this->error('',$e->getMessage());
  1081. }
  1082. }
  1083. /**
  1084. * 商品数据采集
  1085. * @param $url
  1086. * @return array
  1087. */
  1088. public function httpGet($url)
  1089. {
  1090. // 模拟提交数据函数
  1091. $curl = curl_init(); // 启动一个CURL会话
  1092. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
  1093. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  1094. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  1095. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  1096. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  1097. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  1098. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
  1099. curl_setopt($curl, CURLOPT_HEADER, false); //开启header
  1100. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  1101. 'Content-Type: application/json; charset=utf-8',
  1102. )); //类型为json
  1103. //类型为json
  1104. $result = curl_exec($curl); // 执行操作
  1105. if (curl_errno($curl)) {
  1106. return $this->error('', '系统错误,请联系平台进行处理');
  1107. }
  1108. curl_close($curl); // 关键CURL会话
  1109. $result = json_decode($result, true);
  1110. if ($result['retcode'] != '0000') {
  1111. return $this->error('', $result['message']);
  1112. }
  1113. return $this->success($result); // 返回数据
  1114. }
  1115. /************************************************ 商品数据处理 end ********************************************************/
  1116. }