CopyTaobao.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. <?php
  2. /**
  3. * Project: 快速复制 淘宝、天猫、1688、京东 商品到CRMEB系统
  4. * Author: 有一片天 <810806442@qq.com> 微信:szktor
  5. * Date: 2019-04-25
  6. */
  7. namespace app\admin\controller\store;
  8. use crmeb\services\HttpService;
  9. use crmeb\services\UploadService;
  10. use think\exception\PDOException;
  11. use app\admin\controller\AuthController;
  12. use app\admin\model\system\SystemConfig;
  13. use crmeb\traits\CurdControllerTrait;
  14. use crmeb\services\JsonService;
  15. use crmeb\services\UtilService;
  16. use app\admin\model\store\StoreCategory as CategoryModel;
  17. use app\admin\model\store\StoreProduct as ProductModel;
  18. use app\admin\model\system\SystemAttachment;
  19. use app\admin\model\system\SystemAttachmentCategory;
  20. /**
  21. * 产品管理
  22. * Class StoreProduct
  23. * @package app\admin\controller\store
  24. */
  25. class CopyTaobao extends AuthController
  26. {
  27. use CurdControllerTrait;
  28. protected $bindModel = ProductModel::class;
  29. //错误信息
  30. protected $errorInfo = true;
  31. //产品默认字段
  32. protected $productInfo = [
  33. 'cate_id' => '',
  34. 'store_name' => '',
  35. 'store_info' => '',
  36. 'unit_name' => '件',
  37. 'price' => 0,
  38. 'keyword' => '',
  39. 'ficti' => 0,
  40. 'ot_price' => 0,
  41. 'give_integral' => 0,
  42. 'postage' => 0,
  43. 'cost' => 0,
  44. 'image' => '',
  45. 'slider_image' => '',
  46. 'add_time' => 0,
  47. 'stock' => 0,
  48. 'description' => '',
  49. 'soure_link' => ''
  50. ];
  51. //抓取网站主域名
  52. protected $grabName = [
  53. 'taobao',
  54. '1688',
  55. 'tmall',
  56. 'jd'
  57. ];
  58. //远程下载附件图片分类名称
  59. protected $AttachmentCategoryName = '远程下载';
  60. /**
  61. * 显示资源
  62. * @return html
  63. */
  64. public function index()
  65. {
  66. $list = CategoryModel::getTierList(null, 1);
  67. $menus = [];
  68. foreach ($list as $menu) {
  69. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0];//,'disabled'=>$menu['pid']== 0];
  70. }
  71. $this->assign('menus', $menus);
  72. $this->assign('is_layui', 1);
  73. return $this->fetch();
  74. }
  75. /*
  76. * 设置错误信息
  77. * @param string $msg 错误信息
  78. * */
  79. public function setErrorInfo($msg = '')
  80. {
  81. $this->errorInfo = $msg;
  82. return false;
  83. }
  84. /*
  85. * 设置字符串字符集
  86. * @param string $str 需要设置字符集的字符串
  87. * @return string
  88. * */
  89. public function Utf8String($str)
  90. {
  91. $encode = mb_detect_encoding($str, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));
  92. if (strtoupper($encode) != 'UTF-8') $str = mb_convert_encoding($str, 'utf-8', $encode);
  93. return $str;
  94. }
  95. /**
  96. * 获取资源,并解析出对应的商品参数
  97. * @return json
  98. */
  99. public function get_request_contents()
  100. {
  101. list($link) = UtilService::postMore([
  102. ['link', '']
  103. ], $this->request, true);
  104. $url = $this->checkurl($link);
  105. if ($url === false) return JsonService::fail($this->errorInfo);
  106. $this->errorInfo = true;
  107. $html = $this->curl_Get($url, 60);
  108. if (!$html) return JsonService::fail('商品HTML信息获取失败');
  109. $html = $this->Utf8String($html);
  110. preg_match('/<title>([^<>]*)<\/title>/', $html, $title);
  111. //商品标题
  112. $this->productInfo['store_name'] = isset($title['1']) ? str_replace(['-淘宝网', '-tmall.com天猫', ' - 阿里巴巴', ' ', '-', '【图片价格品牌报价】京东', '京东', '【行情报价价格评测】'], '', trim($title['1'])) : '';
  113. $this->productInfo['store_info'] = $this->productInfo['store_name'];
  114. try {
  115. //获取url信息
  116. $pathinfo = pathinfo($url);
  117. if (!isset($pathinfo['dirname'])) return JsonService::fail('解析URL失败');
  118. //提取域名
  119. $parse_url = parse_url($pathinfo['dirname']);
  120. if (!isset($parse_url['host'])) return JsonService::fail('获取域名失败');
  121. //获取第一次.出现的位置
  122. $strLeng = strpos($parse_url['host'], '.') + 1;
  123. //截取域名中的真实域名不带.com后的
  124. $funsuffix = substr($parse_url['host'], $strLeng, strrpos($parse_url['host'], '.') - $strLeng);
  125. if (!in_array($funsuffix, $this->grabName)) return JsonService::fail('您输入的地址不在复制范围内!');
  126. //设拼接设置产品函数
  127. $funName = "setProductInfo" . ucfirst($funsuffix);
  128. //执行方法
  129. if (method_exists($this, $funName))
  130. $this->$funName($html);
  131. else
  132. return JsonService::fail('设置产品函数不存在');
  133. if (!$this->productInfo['slider_image']) return JsonService::fail('未能获取到商品信息,请确保商品信息有效!');
  134. return JsonService::successful($this->productInfo);
  135. } catch (\Exception $e) {
  136. return JsonService::fail('系统错误', ['line' => $e->getLine(), 'meass' => $e->getMessage()]);
  137. }
  138. }
  139. /*
  140. * 淘宝设置产品
  141. * @param string $html 网页内容
  142. * */
  143. public function setProductInfoTaobao($html)
  144. {
  145. //获取轮播图
  146. $images = $this->getTaobaoImg($html);
  147. $images = array_merge($images);
  148. $this->productInfo['slider_image'] = isset($images['gaoqing']) ? $images['gaoqing'] : (array)$images;
  149. $this->productInfo['slider_image'] = array_slice($this->productInfo['slider_image'],0,5);
  150. //获取产品详情请求链接
  151. $link = $this->getTaobaoDesc($html);
  152. //获取请求内容
  153. $desc_json = HttpService::getRequest($link);
  154. //转换字符集
  155. $desc_json = $this->Utf8String($desc_json);
  156. //截取掉多余字符
  157. $this->productInfo['test'] = $desc_json;
  158. $desc_json = str_replace('var desc=\'', '', $desc_json);
  159. $desc_json = str_replace(["\n", "\t", "\r"], '', $desc_json);
  160. $content = substr($desc_json, 0, -2);
  161. $this->productInfo['description'] = $content;
  162. //获取详情图
  163. $description_images = $this->decodedesc($this->productInfo['description']);
  164. $this->productInfo['description_images'] = is_array($description_images) ? $description_images : [];
  165. $this->productInfo['image'] = is_array($this->productInfo['slider_image']) && isset($this->productInfo['slider_image'][0]) ? $this->productInfo['slider_image'][0] : '';
  166. }
  167. /*
  168. * 天猫设置产品
  169. * @param string $html 网页内容
  170. * */
  171. public function setProductInfoTmall($html)
  172. {
  173. //获取轮播图
  174. $images = $this->getTianMaoImg($html);
  175. $images = array_merge($images);
  176. $this->productInfo['slider_image'] = $images;
  177. $this->productInfo['slider_image'] = array_slice($this->productInfo['slider_image'],0,5);
  178. $this->productInfo['image'] = is_array($this->productInfo['slider_image']) && isset($this->productInfo['slider_image'][0]) ? $this->productInfo['slider_image'][0] : '';
  179. //获取产品详情请求链接
  180. $link = $this->getTianMaoDesc($html);
  181. //获取请求内容
  182. $desc_json = HttpService::getRequest($link);
  183. //转换字符集
  184. $desc_json = $this->Utf8String($desc_json);
  185. //截取掉多余字符
  186. $desc_json = str_replace('var desc=\'', '', $desc_json);
  187. $desc_json = str_replace(["\n", "\t", "\r"], '', $desc_json);
  188. $content = substr($desc_json, 0, -2);
  189. $this->productInfo['description'] = $content;
  190. //获取详情图
  191. $description_images = $this->decodedesc($this->productInfo['description']);
  192. $this->productInfo['description_images'] = is_array($description_images) ? $description_images : [];
  193. }
  194. /*
  195. * 1688设置产品
  196. * @param string $html 网页内容
  197. * */
  198. public function setProductInfo1688($html)
  199. {
  200. //获取轮播图
  201. $images = $this->get1688Img($html);
  202. if (isset($images['gaoqing'])) {
  203. $images['gaoqing'] = array_merge($images['gaoqing']);
  204. $this->productInfo['slider_image'] = $images['gaoqing'];
  205. } else
  206. $this->productInfo['slider_image'] = $images;
  207. $this->productInfo['slider_image'] = array_slice($this->productInfo['slider_image'],0,5);
  208. $this->productInfo['image'] = is_array($this->productInfo['slider_image']) && isset($this->productInfo['slider_image'][0]) ? $this->productInfo['slider_image'][0] : '';
  209. //获取产品详情请求链接
  210. $link = $this->get1688Desc($html);
  211. //获取请求内容
  212. $desc_json = HttpService::getRequest($link);
  213. //转换字符集
  214. $desc_json = $this->Utf8String($desc_json);
  215. $this->productInfo['test'] = $desc_json;
  216. //截取掉多余字符
  217. $desc_json = str_replace('var offer_details=', '', $desc_json);
  218. $desc_json = str_replace(["\n", "\t", "\r"], '', $desc_json);
  219. $desc_json = substr($desc_json, 0, -1);
  220. $descArray = json_decode($desc_json, true);
  221. if (!isset($descArray['content'])) $descArray['content'] = '';
  222. $this->productInfo['description'] = $descArray['content'];
  223. //获取详情图
  224. $description_images = $this->decodedesc($this->productInfo['description']);
  225. $this->productInfo['description_images'] = is_array($description_images) ? $description_images : [];
  226. }
  227. /*
  228. * JD设置产品
  229. * @param string $html 网页内容
  230. * */
  231. public function setProductInfoJd($html)
  232. {
  233. //获取产品详情请求链接
  234. $desc_url = $this->getJdDesc($html);
  235. //获取请求内容
  236. $desc_json = HttpService::getRequest($desc_url);
  237. //转换字符集
  238. $desc_json = $this->Utf8String($desc_json);
  239. //截取掉多余字符
  240. if (substr($desc_json, 0, 8) == 'showdesc') $desc_json = str_replace('showdesc', '', $desc_json);
  241. $desc_json = str_replace('data-lazyload=', 'src=', $desc_json);
  242. $descArray = json_decode($desc_json, true);
  243. if (!$descArray) $descArray = ['content' => ''];
  244. //获取轮播图
  245. $images = $this->getJdImg($html);
  246. $images = array_merge($images);
  247. $this->productInfo['slider_image'] = $images;
  248. $this->productInfo['image'] = is_array($this->productInfo['slider_image']) ? $this->productInfo['slider_image'][0] : '';
  249. $this->productInfo['description'] = $descArray['content'];
  250. //获取详情图
  251. $description_images = $this->decodedesc($descArray['content']);
  252. $this->productInfo['description_images'] = is_array($description_images) ? $description_images : [];
  253. }
  254. /*
  255. * 检查淘宝,天猫,1688的商品链接
  256. * @return string
  257. */
  258. public function checkurl($link)
  259. {
  260. $link = strtolower($link);
  261. if (!$link) return $this->setErrorInfo('请输入链接地址');
  262. if (substr($link, 0, 4) != 'http') return $this->setErrorInfo('链接地址必须以http开头');
  263. $arrLine = explode('?', $link);
  264. if (!count($arrLine)) return $this->setErrorInfo('链接地址有误(ERR:1001)');
  265. if (!isset($arrLine[1])) {
  266. if (strpos($link, '1688') !== false && strpos($link, 'offer') !== false) return trim($arrLine[0]);
  267. else if (strpos($link, 'item.jd') !== false) return trim($arrLine[0]);
  268. else return $this->setErrorInfo('链接地址有误(ERR:1002)');
  269. }
  270. if (strpos($link, '1688') !== false && strpos($link, 'offer') !== false) return trim($arrLine[0]);
  271. if (strpos($link, 'item.jd') !== false) return trim($arrLine[0]);
  272. $arrLineValue = explode('&', $arrLine[1]);
  273. if (!is_array($arrLineValue)) return $this->setErrorInfo('链接地址有误(ERR:1003)');
  274. if (!strpos(trim($arrLine[0]), 'item.htm')) $this->setErrorInfo('链接地址有误(ERR:1004)');
  275. //链接参数
  276. $lastStr = '';
  277. foreach ($arrLineValue as $k => $v) {
  278. if (substr(strtolower($v), 0, 3) == 'id=') {
  279. $lastStr = trim($v);
  280. break;
  281. }
  282. }
  283. if (!$lastStr) return $this->setErrorInfo('链接地址有误(ERR:1005)');
  284. return trim($arrLine[0]) . '?' . $lastStr;
  285. }
  286. /*
  287. * 保存图片保存产品信息
  288. * */
  289. public function save_product()
  290. {
  291. $data = UtilService::postMore([
  292. ['cate_id', ''],
  293. ['store_name', ''],
  294. ['store_info', ''],
  295. ['keyword', ''],
  296. ['unit_name', ''],
  297. ['image', ''],
  298. ['slider_image', []],
  299. ['price', ''],
  300. ['ot_price', ''],
  301. ['give_integral', ''],
  302. ['postage', ''],
  303. ['sales', ''],
  304. ['ficti', ''],
  305. ['stock', ''],
  306. ['cost', ''],
  307. ['description_images', []],
  308. ['description', ''],
  309. ['is_show', 0],
  310. ['soure_link', ''],
  311. ]);
  312. if (!$data['cate_id']) return JsonService::fail('请选择分类!');
  313. if (!$data['store_name']) return JsonService::fail('请填写产品名称');
  314. if (!$data['unit_name']) return JsonService::fail('请填写产品单位');
  315. if (!$data['image']) return JsonService::fail('商品主图暂无,无法保存商品,您可选择其他链接进行复制产品');
  316. if ($data['price'] == '' || $data['price'] < 0) return JsonService::fail('请输入产品售价');
  317. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return JsonService::fail('请输入产品市场价');
  318. if ($data['stock'] == '' || $data['stock'] < 0) return JsonService::fail('请输入库存');
  319. //查询附件分类
  320. $AttachmentCategory = SystemAttachmentCategory::where('name' , $this->AttachmentCategoryName)->find();
  321. //不存在则创建
  322. if (!$AttachmentCategory) $AttachmentCategory = SystemAttachmentCategory::create(['pid' => '0', 'name' => $this->AttachmentCategoryName, 'enname' => '']);
  323. //生成附件目录
  324. try{
  325. if (make_path('attach', 3) == '') return JsonService::fail('无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS. 'attach' . DS);
  326. }catch (\Exception $e){
  327. return JsonService::fail($e->getMessage().'或无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS. 'attach' . DS);
  328. }
  329. ini_set("max_execution_time", 600);
  330. //开始图片下载处理
  331. ProductModel::beginTrans();
  332. try {
  333. //放入主图
  334. $images = [
  335. ['w' => 305, 'h' => 305, 'line' => $data['image'], 'valuename' => 'image']
  336. ];
  337. //放入轮播图
  338. foreach ($data['slider_image'] as $item) {
  339. $value = ['w' => 640, 'h' => 640, 'line' => $item, 'valuename' => 'slider_image', 'isTwoArray' => true];
  340. array_push($images, $value);
  341. }
  342. //执行下载
  343. $res = $this->uploadImage($images, false, 0, $AttachmentCategory['id']);
  344. if (!is_array($res)) return JsonService::fail($this->errorInfo ? $this->errorInfo : '保存图片失败');
  345. if (isset($res['image'])) $data['image'] = $res['image'];
  346. if (isset($res['slider_image'])) $data['slider_image'] = $res['slider_image'];
  347. $data['slider_image'] = count($data['slider_image']) ? json_encode($data['slider_image']) : '';
  348. //替换并下载详情里面的图片默认下载全部图片
  349. $data['description'] = preg_replace('#<style>.*?</style>#is', '', $data['description']);
  350. $data['description'] = $this->uploadImage($data['description_images'], $data['description'], 1, $AttachmentCategory['id']);
  351. unset($data['description_images']);
  352. $data['add_time'] = time();
  353. $cate_id = explode(',', $data['cate_id']);
  354. //产品存在
  355. if ($productInfo = ProductModel::where('soure_link', $data['soure_link'])->find()) {
  356. $productInfo->description = $data['description'];
  357. $productInfo->slider_image = $data['slider_image'];
  358. $productInfo->image = $data['image'];
  359. $productInfo->store_name = $data['store_name'];
  360. $productInfo->save();
  361. ProductModel::commitTrans();
  362. return JsonService::successful('商品存在,信息已被更新成功');
  363. } else {
  364. //不存在时新增
  365. if ($res = ProductModel::create($data)) {
  366. foreach ($cate_id as $cid) {
  367. \think\facade\Db::name('store_product_cate')->insert(['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()]);
  368. }
  369. ProductModel::commitTrans();
  370. return JsonService::successful('生成产品成功');
  371. } else {
  372. ProductModel::rollbackTrans();
  373. return JsonService::fail('生成产品失败');
  374. }
  375. }
  376. } catch (PDOException $e) {
  377. ProductModel::rollbackTrans();
  378. return JsonService::fail('插入数据库错误', ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  379. } catch (\Exception $e) {
  380. ProductModel::rollbackTrans();
  381. return JsonService::fail('系统错误', ['line' => $e->getLine(), 'messag' => $e->getMessage(),'file'=>$e->getFile()]);
  382. }
  383. }
  384. /*
  385. * 上传图片处理
  386. * @param array $image 图片路径
  387. * @param int $uploadType 上传方式 0=远程下载
  388. * */
  389. public function uploadImage(array $images = [], $html = '', $uploadType = 0, $AttachmentCategoryId = 0)
  390. {
  391. $uploadImage = [];
  392. $siteUrl = SystemConfig::getConfigValue('site_url');
  393. switch ($uploadType) {
  394. case 0:
  395. foreach ($images as $item) {
  396. //下载图片文件
  397. if ($item['w'] && $item['h'])
  398. $uploadValue = $this->downloadImage($item['line'], '', 0, 30, $item['w'], $item['h']);
  399. else
  400. $uploadValue = $this->downloadImage($item['line']);
  401. //下载成功更新数据库
  402. if (is_array($uploadValue)) {
  403. //TODO 拼接图片地址
  404. if ($uploadValue['image_type'] == 1) $imagePath = $siteUrl . $uploadValue['path'];
  405. else $imagePath = $uploadValue['path'];
  406. //写入数据库
  407. if (!$uploadValue['is_exists'] && $AttachmentCategoryId) SystemAttachment::attachmentAdd($uploadValue['name'], $uploadValue['size'], $uploadValue['mime'], $imagePath, $imagePath, $AttachmentCategoryId, $uploadValue['image_type'], time(), 1);
  408. //组装数组
  409. if (isset($item['isTwoArray']) && $item['isTwoArray'])
  410. $uploadImage[$item['valuename']][] = $imagePath;
  411. else
  412. $uploadImage[$item['valuename']] = $imagePath;
  413. }
  414. }
  415. break;
  416. case 1:
  417. preg_match_all('#<img.*?src="([^"]*)"[^>]*>#i', $html, $match);
  418. if (isset($match[1])) {
  419. foreach ($match[1] as $item) {
  420. if (is_int(strpos($item, 'http')))
  421. $arcurl = $item;
  422. else
  423. $arcurl = 'http://' . ltrim($item, '\//');
  424. $uploadValue = $this->downloadImage($arcurl);
  425. //下载成功更新数据库
  426. if (is_array($uploadValue)) {
  427. //TODO 拼接图片地址
  428. if ($uploadValue['image_type'] == 1) $imagePath = $siteUrl . $uploadValue['path'];
  429. else $imagePath = $uploadValue['path'];
  430. //写入数据库
  431. if (!$uploadValue['is_exists'] && $AttachmentCategoryId) SystemAttachment::attachmentAdd($uploadValue['name'], $uploadValue['size'], $uploadValue['mime'], $imagePath, $imagePath, $AttachmentCategoryId, $uploadValue['image_type'], time(), 1);
  432. //替换图片
  433. $html = str_replace($item, $imagePath, $html);
  434. } else {
  435. //替换掉没有下载下来的图片
  436. $html = preg_replace('#<img.*?src="' . $item . '"*>#i', '', $html);
  437. }
  438. }
  439. }
  440. return $html;
  441. break;
  442. default:
  443. return $this->setErrorInfo('上传方式错误');
  444. break;
  445. }
  446. return $uploadImage;
  447. }
  448. //提取商品描述中的所有图片
  449. public function decodedesc($desc = '')
  450. {
  451. $desc = trim($desc);
  452. if (!$desc) return '';
  453. preg_match_all('/<img[^>]*?src="([^"]*?)"[^>]*?>/i', $desc, $match);
  454. if (!isset($match[1]) || count($match[1]) <= 0) {
  455. preg_match_all('/:url(([^"]*?));/i', $desc, $match);
  456. if (!isset($match[1]) || count($match[1]) <= 0) return $desc;
  457. } else {
  458. preg_match_all('/:url(([^"]*?));/i', $desc, $newmatch);
  459. if (isset($newmatch[1]) && count($newmatch[1]) > 0) $match[1] = array_merge($match[1], $newmatch[1]);
  460. }
  461. $match[1] = array_unique($match[1]); //去掉重复
  462. foreach ($match[1] as $k => &$v) {
  463. $_tmp_img = str_replace([')', '(', ';'], '', $v);
  464. $_tmp_img = strpos($_tmp_img, 'http') ? $_tmp_img : 'http:' . $_tmp_img;
  465. if (strpos($v, '?')) {
  466. $_tarr = explode('?', $v);
  467. $_tmp_img = trim($_tarr[0]);
  468. }
  469. $_urls = str_replace(['\'', '"'], '', $_tmp_img);
  470. if ($this->_img_exists($_urls)) $v = $_urls;
  471. }
  472. return $match[1];
  473. }
  474. //获取京东商品组图
  475. public function getJdImg($html = '')
  476. {
  477. //获取图片服务器网址
  478. preg_match('/<img(.*?)id="spec-img"(.*?)data-origin=\"(.*?)\"[^>]*>/', $html, $img);
  479. if (!isset($img[3])) return '';
  480. $info = parse_url(trim($img[3]));
  481. if (!$info['host']) return '';
  482. if (!$info['path']) return '';
  483. $_tmparr = explode('/', trim($info['path']));
  484. $url = 'http://' . $info['host'] . '/' . $_tmparr[1] . '/' . str_replace(['jfs', ' '], '', trim($_tmparr[2]));
  485. preg_match('/imageList:(.*?)"],/is', $html, $img);
  486. if (!isset($img[1])) {
  487. return '';
  488. }
  489. $_arr = explode(',', $img[1]);
  490. foreach ($_arr as $k => &$v) {
  491. $_str = $url . str_replace(['"', '[', ']', ' '], '', trim($v));
  492. if (strpos($_str, '?')) {
  493. $_tarr = explode('?', $_str);
  494. $_str = trim($_tarr[0]);
  495. }
  496. if ($this->_img_exists($_str)) {
  497. $v = $_str;
  498. } else {
  499. unset($_arr[$k]);
  500. }
  501. }
  502. return array_unique($_arr);
  503. }
  504. //获取京东商品描述
  505. public function getJdDesc($html = '')
  506. {
  507. preg_match('/,(.*?)desc:([^<>]*)\',/i', $html, $descarr);
  508. if (!isset($descarr[1]) && !isset($descarr[2])) return '';
  509. $tmpArr = explode(',', $descarr[2]);
  510. if (count($tmpArr) > 0) {
  511. $descarr[2] = trim($tmpArr[0]);
  512. }
  513. $replace_arr = ['\'', '\',', ' ', ',', '/*', '*/'];
  514. if (isset($descarr[2])) {
  515. $d_url = str_replace($replace_arr, '', $descarr[2]);
  516. return $this->formatDescUrl(strpos($d_url, 'http') ? $d_url : 'http:' . $d_url);
  517. }
  518. $d_url = str_replace($replace_arr, '', $descarr[1]);
  519. $d_url = $this->formatDescUrl($d_url);
  520. $d_url = rtrim(rtrim($d_url, "?"), "&");
  521. return substr($d_url, 0, 4) == 'http' ? $d_url : 'http:' . $d_url;
  522. }
  523. //处理下京东商品描述网址
  524. public function formatDescUrl($url = '')
  525. {
  526. if (!$url) return '';
  527. $url = substr($url, 0, 4) == 'http' ? $url : 'http:' . $url;
  528. if (!strpos($url, '&')) {
  529. $_arr = explode('?', $url);
  530. if (!is_array($_arr) || count($_arr) <= 0) return $url;
  531. return trim($_arr[0]);
  532. } else {
  533. $_arr = explode('&', $url);
  534. }
  535. if (!is_array($_arr) || count($_arr) <= 0) return $url;
  536. unset($_arr[count($_arr) - 1]);
  537. $new_url = '';
  538. foreach ($_arr as $k => $v) {
  539. $new_url .= $v . '&';
  540. }
  541. return !$new_url ? $url : $new_url;
  542. }
  543. //获取1688商品组图
  544. public function get1688Img($html = '')
  545. {
  546. preg_match('/<ul class=\"nav nav-tabs fd-clr\">(.*?)<\/ul>/is', $html, $img);
  547. if (!isset($img[0])) {
  548. return '';
  549. }
  550. preg_match_all('/preview":"(.*?)\"\}\'>/is', $img[0], $arrb);
  551. if (!isset($arrb[1]) || count($arrb[1]) <= 0) {
  552. return '';
  553. }
  554. $thumb = [];
  555. $gaoqing = [];
  556. $res = ['thumb' => '', 'gaoqing' => '']; //缩略图片和高清图片
  557. foreach ($arrb[1] as $k => $v) {
  558. $_str = str_replace(['","original":"'], '*', $v);
  559. $_arr = explode('*', $_str);
  560. if (is_array($_arr) && isset($_arr[0]) && isset($_arr[1])) {
  561. if (strpos($_arr[0], '?')) {
  562. $_tarr = explode('?', $_arr[0]);
  563. $_arr[0] = trim($_tarr[0]);
  564. }
  565. if (strpos($_arr[1], '?')) {
  566. $_tarr = explode('?', $_arr[1]);
  567. $_arr[1] = trim($_tarr[0]);
  568. }
  569. if ($this->_img_exists($_arr[0])) $thumb[] = trim($_arr[0]);
  570. if ($this->_img_exists($_arr[1])) $gaoqing[] = trim($_arr[1]);
  571. }
  572. }
  573. $res = ['thumb' => array_unique($thumb), 'gaoqing' => array_unique($gaoqing)]; //缩略图片和高清图片
  574. return $res;
  575. }
  576. //获取1688商品描述
  577. public function get1688Desc($html = '')
  578. {
  579. preg_match('/data-tfs-url="([^<>]*)data-enable="true"/', $html, $descarr);
  580. if (!isset($descarr[1])) return '';
  581. return str_replace(['"', ' '], '', $descarr[1]);
  582. }
  583. //获取天猫商品组图
  584. public function getTianMaoImg($html = '')
  585. {
  586. $pic_size = '430';
  587. preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $html, $img);
  588. if (isset($img[1])) {
  589. $_arr = explode('x', $img[1]);
  590. $filename = $_arr[count($_arr) - 1];
  591. $pic_size = intval(substr($filename, 0, 3));
  592. }
  593. preg_match('|<ul id="J_UlThumb" class="tb-thumb tm-clear">(.*)</ul>|isU', $html, $match);
  594. preg_match_all('/<img src="(.*?)" \//', $match[1], $images);
  595. if (!isset($images[1])) return '';
  596. foreach ($images[1] as $k => &$v) {
  597. $tmp_v = trim($v);
  598. $_arr = explode('x', $tmp_v);
  599. $_fname = $_arr[count($_arr) - 1];
  600. $_size = intval(substr($_fname, 0, 3));
  601. if (strpos($tmp_v, '://')) {
  602. $_arr = explode(':', $tmp_v);
  603. $r_url = trim($_arr[1]);
  604. } else {
  605. $r_url = $tmp_v;
  606. }
  607. $str = str_replace($_size, $pic_size, $r_url);
  608. if (strpos($str, '?')) {
  609. $_tarr = explode('?', $str);
  610. $str = trim($_tarr[0]);
  611. }
  612. $_i_url = strpos($str, 'http') ? $str : 'http:' . $str;
  613. if ($this->_img_exists($_i_url)) {
  614. $v = $_i_url;
  615. } else {
  616. unset($images[1][$k]);
  617. }
  618. }
  619. return array_unique($images[1]);
  620. }
  621. //获取天猫商品描述
  622. public function getTianMaoDesc($html = '')
  623. {
  624. preg_match('/descUrl":"([^<>]*)","httpsDescUrl":"/', $html, $descarr);
  625. if (!isset($descarr[1])) {
  626. preg_match('/httpsDescUrl":"([^<>]*)","fetchDcUrl/', $html, $descarr);
  627. if (!isset($descarr[1])) return '';
  628. }
  629. return strpos($descarr[1], 'http') ? $descarr[1] : 'http:' . $descarr[1];
  630. }
  631. //获取淘宝商品组图
  632. public function getTaobaoImg($html = '')
  633. {
  634. preg_match('/auctionImages([^<>]*)"]/', $html, $imgarr);
  635. if (!isset($imgarr[1])) return '';
  636. $arr = explode(',', $imgarr[1]);
  637. foreach ($arr as $k => &$v) {
  638. $str = trim($v);
  639. $str = str_replace(['"', ' ', '', ':['], '', $str);
  640. if (strpos($str, '?')) {
  641. $_tarr = explode('?', $str);
  642. $str = trim($_tarr[0]);
  643. }
  644. $_i_url = strpos($str, 'http') ? $str : 'http:' . $str;
  645. if ($this->_img_exists($_i_url)) {
  646. $v = $_i_url;
  647. } else {
  648. unset($arr[$k]);
  649. }
  650. }
  651. return array_unique($arr);
  652. }
  653. //获取淘宝商品描述
  654. public function getTaobaoDesc($html = '')
  655. {
  656. preg_match('/descUrl([^<>]*)counterApi/', $html, $descarr);
  657. if (!isset($descarr[1])) return '';
  658. $arr = explode(':', $descarr[1]);
  659. $url = [];
  660. foreach ($arr as $k => $v) {
  661. if (strpos($v, '//')) {
  662. $str = str_replace(['\'', ',', ' ', '?', ':'], '', $v);
  663. $url[] = trim($str);
  664. }
  665. }
  666. if ($url) {
  667. return strpos($url[0], 'http') ? $url[0] : 'http:' . $url[0];
  668. } else {
  669. return '';
  670. }
  671. }
  672. /**
  673. * GET 请求
  674. * @param string $url
  675. */
  676. public function curl_Get($url = '', $time_out = 25)
  677. {
  678. if (!$url) return '';
  679. $ch = curl_init();
  680. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  681. if (stripos($url, "https://") !== FALSE) {
  682. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  683. }
  684. curl_setopt($ch, CURLOPT_URL, $url);
  685. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:' . $_SERVER['HTTP_USER_AGENT']));
  686. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  687. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  688. curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
  689. $response = curl_exec($ch);
  690. if ($error = curl_error($ch)) {
  691. return false;
  692. }
  693. curl_close($ch);
  694. return mb_convert_encoding($response, 'utf-8', 'GB2312');
  695. }
  696. //检测远程文件是否存在
  697. public function _img_exists($url = '')
  698. {
  699. ini_set("max_execution_time", 0);
  700. $str = @file_get_contents($url, 0, null, 0, 1);
  701. if (strlen($str) <= 0) return false;
  702. if ($str)
  703. return true;
  704. else
  705. return false;
  706. }
  707. //TODO 下载图片
  708. public function downloadImage($url = '', $name = '', $type = 0, $timeout = 30, $w = 0, $h = 0)
  709. {
  710. if (!strlen(trim($url))) return '';
  711. if (!strlen(trim($name))) {
  712. //TODO 获取要下载的文件名称
  713. $downloadImageInfo = $this->getImageExtname($url);
  714. $name = $downloadImageInfo['file_name'];
  715. if (!strlen(trim($name))) return '';
  716. }
  717. //TODO 获取远程文件所采用的方法
  718. if ($type) {
  719. $ch = curl_init();
  720. curl_setopt($ch, CURLOPT_URL, $url);
  721. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  722. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  723. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //TODO 跳过证书检查
  724. if (stripos($url, "https://") !== FALSE) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //TODO 从证书中检查SSL加密算法是否存在
  725. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:' . $_SERVER['HTTP_USER_AGENT']));
  726. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//TODO 是否采集301、302之后的页面
  727. $content = curl_exec($ch);
  728. curl_close($ch);
  729. } else {
  730. try {
  731. ob_start();
  732. readfile($url);
  733. $content = ob_get_contents();
  734. ob_end_clean();
  735. } catch (\Exception $e) {
  736. return $e->getMessage();
  737. }
  738. }
  739. $size = strlen(trim($content));
  740. if (!$content || $size <= 2) return '图片流获取失败';
  741. $date_dir = date('Y') . DS . date('m') . DS . date('d');
  742. $imageInfo = UploadService::getInstance()->setUploadPath('attach/' . $date_dir)->imageStream($name, $content);
  743. if (!is_array($imageInfo)) return $imageInfo;
  744. $date['path'] = $imageInfo['dir'];
  745. $date['name'] = $imageInfo['name'];
  746. $date['size'] = $imageInfo['size'];
  747. $date['mime'] = $imageInfo['type'];
  748. $date['image_type'] = $imageInfo['image_type'];
  749. $date['is_exists'] = false;
  750. return $date;
  751. }
  752. //获取即将要下载的图片扩展名
  753. public function getImageExtname($url = '', $ex = 'jpg')
  754. {
  755. $_empty = ['file_name' => '', 'ext_name' => $ex];
  756. if (!$url) return $_empty;
  757. if (strpos($url, '?')) {
  758. $_tarr = explode('?', $url);
  759. $url = trim($_tarr[0]);
  760. }
  761. $arr = explode('.', $url);
  762. if (!is_array($arr) || count($arr) <= 1) return $_empty;
  763. $ext_name = trim($arr[count($arr) - 1]);
  764. $ext_name = !$ext_name ? $ex : $ext_name;
  765. return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
  766. }
  767. /*
  768. $filepath = 绝对路径,末尾有斜杠 /
  769. $name = 图片文件名
  770. $maxwidth 定义生成图片的最大宽度(单位:像素)
  771. $maxheight 生成图片的最大高度(单位:像素)
  772. $filetype 最终生成的图片类型(.jpg/.png/.gif)
  773. */
  774. public function resizeImage($filepath = '', $name = '', $maxwidth = 0, $maxheight = 0)
  775. {
  776. $pic_file = $filepath . $name; //图片文件
  777. $img_info = getimagesize($pic_file); //索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,
  778. if ($img_info[2] == 1) {
  779. $im = imagecreatefromgif($pic_file); //打开图片
  780. $filetype = '.gif';
  781. } elseif ($img_info[2] == 2) {
  782. $im = imagecreatefromjpeg($pic_file); //打开图片
  783. $filetype = '.jpg';
  784. } elseif ($img_info[2] == 3) {
  785. $im = imagecreatefrompng($pic_file); //打开图片
  786. $filetype = '.png';
  787. } else {
  788. return ['path' => $filepath, 'file' => $name, 'mime' => ''];
  789. }
  790. $file_name = md5('_tmp_' . microtime() . '_' . rand(0, 10)) . $filetype;
  791. $pic_width = imagesx($im);
  792. $pic_height = imagesy($im);
  793. $resizewidth_tag = false;
  794. $resizeheight_tag = false;
  795. if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
  796. if ($maxwidth && $pic_width > $maxwidth) {
  797. $widthratio = $maxwidth / $pic_width;
  798. $resizewidth_tag = true;
  799. }
  800. if ($maxheight && $pic_height > $maxheight) {
  801. $heightratio = $maxheight / $pic_height;
  802. $resizeheight_tag = true;
  803. }
  804. if ($resizewidth_tag && $resizeheight_tag) {
  805. if ($widthratio < $heightratio)
  806. $ratio = $widthratio;
  807. else
  808. $ratio = $heightratio;
  809. }
  810. if ($resizewidth_tag && !$resizeheight_tag)
  811. $ratio = $widthratio;
  812. if ($resizeheight_tag && !$resizewidth_tag)
  813. $ratio = $heightratio;
  814. $newwidth = $pic_width * $ratio;
  815. $newheight = $pic_height * $ratio;
  816. if (function_exists("imagecopyresampled")) {
  817. $newim = imagecreatetruecolor($newwidth, $newheight);
  818. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  819. } else {
  820. $newim = imagecreate($newwidth, $newheight);
  821. imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  822. }
  823. if ($filetype == '.png') {
  824. imagepng($newim, $filepath . $file_name);
  825. } else if ($filetype == '.gif') {
  826. imagegif($newim, $filepath . $file_name);
  827. } else {
  828. imagejpeg($newim, $filepath . $file_name);
  829. }
  830. imagedestroy($newim);
  831. } else {
  832. if ($filetype == '.png') {
  833. imagepng($im, $filepath . $file_name);
  834. } else if ($filetype == '.gif') {
  835. imagegif($im, $filepath . $file_name);
  836. } else {
  837. imagejpeg($im, $filepath . $file_name);
  838. }
  839. imagedestroy($im);
  840. }
  841. @unlink($pic_file);
  842. return ['path' => $filepath, 'file' => $file_name, 'mime' => $img_info['mime']];
  843. }
  844. }