CopyTaobao.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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,true) === '')
  326. return JsonService::fail('无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS. 'attach' . DS);
  327. }catch (\Exception $e){
  328. return JsonService::fail($e->getMessage().'或无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS. 'attach' . DS);
  329. }
  330. ini_set("max_execution_time", 600);
  331. //开始图片下载处理
  332. ProductModel::beginTrans();
  333. try {
  334. //放入主图
  335. $images = [
  336. ['w' => 305, 'h' => 305, 'line' => $data['image'], 'valuename' => 'image']
  337. ];
  338. //放入轮播图
  339. foreach ($data['slider_image'] as $item) {
  340. $value = ['w' => 640, 'h' => 640, 'line' => $item, 'valuename' => 'slider_image', 'isTwoArray' => true];
  341. array_push($images, $value);
  342. }
  343. //执行下载
  344. $res = $this->uploadImage($images, false, 0, $AttachmentCategory['id']);
  345. if (!is_array($res)) return JsonService::fail($this->errorInfo ? $this->errorInfo : '保存图片失败');
  346. if (isset($res['image'])) $data['image'] = $res['image'];
  347. if (isset($res['slider_image'])) $data['slider_image'] = $res['slider_image'];
  348. $data['slider_image'] = count($data['slider_image']) ? json_encode($data['slider_image']) : '';
  349. //替换并下载详情里面的图片默认下载全部图片
  350. $data['description'] = preg_replace('#<style>.*?</style>#is', '', $data['description']);
  351. $data['description'] = $this->uploadImage($data['description_images'], $data['description'], 1, $AttachmentCategory['id']);
  352. unset($data['description_images']);
  353. $data['add_time'] = time();
  354. $cate_id = explode(',', $data['cate_id']);
  355. //产品存在
  356. if ($productInfo = ProductModel::where('soure_link', $data['soure_link'])->find()) {
  357. $productInfo->description = $data['description'];
  358. $productInfo->slider_image = $data['slider_image'];
  359. $productInfo->image = $data['image'];
  360. $productInfo->store_name = $data['store_name'];
  361. $productInfo->save();
  362. ProductModel::commitTrans();
  363. return JsonService::successful('商品存在,信息已被更新成功');
  364. } else {
  365. //不存在时新增
  366. if ($res = ProductModel::create($data)) {
  367. foreach ($cate_id as $cid) {
  368. \think\facade\Db::name('store_product_cate')->insert(['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()]);
  369. }
  370. ProductModel::commitTrans();
  371. return JsonService::successful('生成产品成功');
  372. } else {
  373. ProductModel::rollbackTrans();
  374. return JsonService::fail('生成产品失败');
  375. }
  376. }
  377. } catch (PDOException $e) {
  378. ProductModel::rollbackTrans();
  379. return JsonService::fail('插入数据库错误', ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  380. } catch (\Exception $e) {
  381. ProductModel::rollbackTrans();
  382. return JsonService::fail('系统错误', ['line' => $e->getLine(), 'messag' => $e->getMessage(),'file'=>$e->getFile()]);
  383. }
  384. }
  385. /*
  386. * 上传图片处理
  387. * @param array $image 图片路径
  388. * @param int $uploadType 上传方式 0=远程下载
  389. * */
  390. public function uploadImage(array $images = [], $html = '', $uploadType = 0, $AttachmentCategoryId = 0)
  391. {
  392. $uploadImage = [];
  393. $siteUrl = SystemConfig::getConfigValue('site_url');
  394. switch ($uploadType) {
  395. case 0:
  396. foreach ($images as $item) {
  397. //下载图片文件
  398. if ($item['w'] && $item['h'])
  399. $uploadValue = $this->downloadImage($item['line'], '', 0, 30, $item['w'], $item['h']);
  400. else
  401. $uploadValue = $this->downloadImage($item['line']);
  402. //下载成功更新数据库
  403. if (is_array($uploadValue)) {
  404. //TODO 拼接图片地址
  405. if ($uploadValue['image_type'] == 1) $imagePath = $siteUrl . $uploadValue['path'];
  406. else $imagePath = $uploadValue['path'];
  407. //写入数据库
  408. if (!$uploadValue['is_exists'] && $AttachmentCategoryId) SystemAttachment::attachmentAdd($uploadValue['name'], $uploadValue['size'], $uploadValue['mime'], $imagePath, $imagePath, $AttachmentCategoryId, $uploadValue['image_type'], time(), 1);
  409. //组装数组
  410. if (isset($item['isTwoArray']) && $item['isTwoArray'])
  411. $uploadImage[$item['valuename']][] = $imagePath;
  412. else
  413. $uploadImage[$item['valuename']] = $imagePath;
  414. }
  415. }
  416. break;
  417. case 1:
  418. preg_match_all('#<img.*?src="([^"]*)"[^>]*>#i', $html, $match);
  419. if (isset($match[1])) {
  420. foreach ($match[1] as $item) {
  421. if (is_int(strpos($item, 'http')))
  422. $arcurl = $item;
  423. else
  424. $arcurl = 'http://' . ltrim($item, '\//');
  425. $uploadValue = $this->downloadImage($arcurl);
  426. //下载成功更新数据库
  427. if (is_array($uploadValue)) {
  428. //TODO 拼接图片地址
  429. if ($uploadValue['image_type'] == 1) $imagePath = $siteUrl . $uploadValue['path'];
  430. else $imagePath = $uploadValue['path'];
  431. //写入数据库
  432. if (!$uploadValue['is_exists'] && $AttachmentCategoryId) SystemAttachment::attachmentAdd($uploadValue['name'], $uploadValue['size'], $uploadValue['mime'], $imagePath, $imagePath, $AttachmentCategoryId, $uploadValue['image_type'], time(), 1);
  433. //替换图片
  434. $html = str_replace($item, $imagePath, $html);
  435. } else {
  436. //替换掉没有下载下来的图片
  437. $html = preg_replace('#<img.*?src="' . $item . '"*>#i', '', $html);
  438. }
  439. }
  440. }
  441. return $html;
  442. break;
  443. default:
  444. return $this->setErrorInfo('上传方式错误');
  445. break;
  446. }
  447. return $uploadImage;
  448. }
  449. //提取商品描述中的所有图片
  450. public function decodedesc($desc = '')
  451. {
  452. $desc = trim($desc);
  453. if (!$desc) return '';
  454. preg_match_all('/<img[^>]*?src="([^"]*?)"[^>]*?>/i', $desc, $match);
  455. if (!isset($match[1]) || count($match[1]) <= 0) {
  456. preg_match_all('/:url(([^"]*?));/i', $desc, $match);
  457. if (!isset($match[1]) || count($match[1]) <= 0) return $desc;
  458. } else {
  459. preg_match_all('/:url(([^"]*?));/i', $desc, $newmatch);
  460. if (isset($newmatch[1]) && count($newmatch[1]) > 0) $match[1] = array_merge($match[1], $newmatch[1]);
  461. }
  462. $match[1] = array_unique($match[1]); //去掉重复
  463. foreach ($match[1] as $k => &$v) {
  464. $_tmp_img = str_replace([')', '(', ';'], '', $v);
  465. $_tmp_img = strpos($_tmp_img, 'http') ? $_tmp_img : 'http:' . $_tmp_img;
  466. if (strpos($v, '?')) {
  467. $_tarr = explode('?', $v);
  468. $_tmp_img = trim($_tarr[0]);
  469. }
  470. $_urls = str_replace(['\'', '"'], '', $_tmp_img);
  471. if ($this->_img_exists($_urls)) $v = $_urls;
  472. }
  473. return $match[1];
  474. }
  475. //获取京东商品组图
  476. public function getJdImg($html = '')
  477. {
  478. //获取图片服务器网址
  479. preg_match('/<img(.*?)id="spec-img"(.*?)data-origin=\"(.*?)\"[^>]*>/', $html, $img);
  480. if (!isset($img[3])) return '';
  481. $info = parse_url(trim($img[3]));
  482. if (!$info['host']) return '';
  483. if (!$info['path']) return '';
  484. $_tmparr = explode('/', trim($info['path']));
  485. $url = 'http://' . $info['host'] . '/' . $_tmparr[1] . '/' . str_replace(['jfs', ' '], '', trim($_tmparr[2]));
  486. preg_match('/imageList:(.*?)"],/is', $html, $img);
  487. if (!isset($img[1])) {
  488. return '';
  489. }
  490. $_arr = explode(',', $img[1]);
  491. foreach ($_arr as $k => &$v) {
  492. $_str = $url . str_replace(['"', '[', ']', ' '], '', trim($v));
  493. if (strpos($_str, '?')) {
  494. $_tarr = explode('?', $_str);
  495. $_str = trim($_tarr[0]);
  496. }
  497. if ($this->_img_exists($_str)) {
  498. $v = $_str;
  499. } else {
  500. unset($_arr[$k]);
  501. }
  502. }
  503. return array_unique($_arr);
  504. }
  505. //获取京东商品描述
  506. public function getJdDesc($html = '')
  507. {
  508. preg_match('/,(.*?)desc:([^<>]*)\',/i', $html, $descarr);
  509. if (!isset($descarr[1]) && !isset($descarr[2])) return '';
  510. $tmpArr = explode(',', $descarr[2]);
  511. if (count($tmpArr) > 0) {
  512. $descarr[2] = trim($tmpArr[0]);
  513. }
  514. $replace_arr = ['\'', '\',', ' ', ',', '/*', '*/'];
  515. if (isset($descarr[2])) {
  516. $d_url = str_replace($replace_arr, '', $descarr[2]);
  517. return $this->formatDescUrl(strpos($d_url, 'http') ? $d_url : 'http:' . $d_url);
  518. }
  519. $d_url = str_replace($replace_arr, '', $descarr[1]);
  520. $d_url = $this->formatDescUrl($d_url);
  521. $d_url = rtrim(rtrim($d_url, "?"), "&");
  522. return substr($d_url, 0, 4) == 'http' ? $d_url : 'http:' . $d_url;
  523. }
  524. //处理下京东商品描述网址
  525. public function formatDescUrl($url = '')
  526. {
  527. if (!$url) return '';
  528. $url = substr($url, 0, 4) == 'http' ? $url : 'http:' . $url;
  529. if (!strpos($url, '&')) {
  530. $_arr = explode('?', $url);
  531. if (!is_array($_arr) || count($_arr) <= 0) return $url;
  532. return trim($_arr[0]);
  533. } else {
  534. $_arr = explode('&', $url);
  535. }
  536. if (!is_array($_arr) || count($_arr) <= 0) return $url;
  537. unset($_arr[count($_arr) - 1]);
  538. $new_url = '';
  539. foreach ($_arr as $k => $v) {
  540. $new_url .= $v . '&';
  541. }
  542. return !$new_url ? $url : $new_url;
  543. }
  544. //获取1688商品组图
  545. public function get1688Img($html = '')
  546. {
  547. preg_match('/<ul class=\"nav nav-tabs fd-clr\">(.*?)<\/ul>/is', $html, $img);
  548. if (!isset($img[0])) {
  549. return '';
  550. }
  551. preg_match_all('/preview":"(.*?)\"\}\'>/is', $img[0], $arrb);
  552. if (!isset($arrb[1]) || count($arrb[1]) <= 0) {
  553. return '';
  554. }
  555. $thumb = [];
  556. $gaoqing = [];
  557. $res = ['thumb' => '', 'gaoqing' => '']; //缩略图片和高清图片
  558. foreach ($arrb[1] as $k => $v) {
  559. $_str = str_replace(['","original":"'], '*', $v);
  560. $_arr = explode('*', $_str);
  561. if (is_array($_arr) && isset($_arr[0]) && isset($_arr[1])) {
  562. if (strpos($_arr[0], '?')) {
  563. $_tarr = explode('?', $_arr[0]);
  564. $_arr[0] = trim($_tarr[0]);
  565. }
  566. if (strpos($_arr[1], '?')) {
  567. $_tarr = explode('?', $_arr[1]);
  568. $_arr[1] = trim($_tarr[0]);
  569. }
  570. if ($this->_img_exists($_arr[0])) $thumb[] = trim($_arr[0]);
  571. if ($this->_img_exists($_arr[1])) $gaoqing[] = trim($_arr[1]);
  572. }
  573. }
  574. $res = ['thumb' => array_unique($thumb), 'gaoqing' => array_unique($gaoqing)]; //缩略图片和高清图片
  575. return $res;
  576. }
  577. //获取1688商品描述
  578. public function get1688Desc($html = '')
  579. {
  580. preg_match('/data-tfs-url="([^<>]*)data-enable="true"/', $html, $descarr);
  581. if (!isset($descarr[1])) return '';
  582. return str_replace(['"', ' '], '', $descarr[1]);
  583. }
  584. //获取天猫商品组图
  585. public function getTianMaoImg($html = '')
  586. {
  587. $pic_size = '430';
  588. preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $html, $img);
  589. if (isset($img[1])) {
  590. $_arr = explode('x', $img[1]);
  591. $filename = $_arr[count($_arr) - 1];
  592. $pic_size = intval(substr($filename, 0, 3));
  593. }
  594. preg_match('|<ul id="J_UlThumb" class="tb-thumb tm-clear">(.*)</ul>|isU', $html, $match);
  595. preg_match_all('/<img src="(.*?)" \//', $match[1], $images);
  596. if (!isset($images[1])) return '';
  597. foreach ($images[1] as $k => &$v) {
  598. $tmp_v = trim($v);
  599. $_arr = explode('x', $tmp_v);
  600. $_fname = $_arr[count($_arr) - 1];
  601. $_size = intval(substr($_fname, 0, 3));
  602. if (strpos($tmp_v, '://')) {
  603. $_arr = explode(':', $tmp_v);
  604. $r_url = trim($_arr[1]);
  605. } else {
  606. $r_url = $tmp_v;
  607. }
  608. $str = str_replace($_size, $pic_size, $r_url);
  609. if (strpos($str, '?')) {
  610. $_tarr = explode('?', $str);
  611. $str = trim($_tarr[0]);
  612. }
  613. $_i_url = strpos($str, 'http') ? $str : 'http:' . $str;
  614. if ($this->_img_exists($_i_url)) {
  615. $v = $_i_url;
  616. } else {
  617. unset($images[1][$k]);
  618. }
  619. }
  620. return array_unique($images[1]);
  621. }
  622. //获取天猫商品描述
  623. public function getTianMaoDesc($html = '')
  624. {
  625. preg_match('/descUrl":"([^<>]*)","httpsDescUrl":"/', $html, $descarr);
  626. if (!isset($descarr[1])) {
  627. preg_match('/httpsDescUrl":"([^<>]*)","fetchDcUrl/', $html, $descarr);
  628. if (!isset($descarr[1])) return '';
  629. }
  630. return strpos($descarr[1], 'http') ? $descarr[1] : 'http:' . $descarr[1];
  631. }
  632. //获取淘宝商品组图
  633. public function getTaobaoImg($html = '')
  634. {
  635. preg_match('/auctionImages([^<>]*)"]/', $html, $imgarr);
  636. if (!isset($imgarr[1])) return '';
  637. $arr = explode(',', $imgarr[1]);
  638. foreach ($arr as $k => &$v) {
  639. $str = trim($v);
  640. $str = str_replace(['"', ' ', '', ':['], '', $str);
  641. if (strpos($str, '?')) {
  642. $_tarr = explode('?', $str);
  643. $str = trim($_tarr[0]);
  644. }
  645. $_i_url = strpos($str, 'http') ? $str : 'http:' . $str;
  646. if ($this->_img_exists($_i_url)) {
  647. $v = $_i_url;
  648. } else {
  649. unset($arr[$k]);
  650. }
  651. }
  652. return array_unique($arr);
  653. }
  654. //获取淘宝商品描述
  655. public function getTaobaoDesc($html = '')
  656. {
  657. preg_match('/descUrl([^<>]*)counterApi/', $html, $descarr);
  658. if (!isset($descarr[1])) return '';
  659. $arr = explode(':', $descarr[1]);
  660. $url = [];
  661. foreach ($arr as $k => $v) {
  662. if (strpos($v, '//')) {
  663. $str = str_replace(['\'', ',', ' ', '?', ':'], '', $v);
  664. $url[] = trim($str);
  665. }
  666. }
  667. if ($url) {
  668. return strpos($url[0], 'http') ? $url[0] : 'http:' . $url[0];
  669. } else {
  670. return '';
  671. }
  672. }
  673. /**
  674. * GET 请求
  675. * @param string $url
  676. */
  677. public function curl_Get($url = '', $time_out = 25)
  678. {
  679. if (!$url) return '';
  680. $ch = curl_init();
  681. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  682. if (stripos($url, "https://") !== FALSE) {
  683. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  684. }
  685. curl_setopt($ch, CURLOPT_URL, $url);
  686. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:' . $_SERVER['HTTP_USER_AGENT']));
  687. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  688. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  689. curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
  690. $response = curl_exec($ch);
  691. if ($error = curl_error($ch)) {
  692. return false;
  693. }
  694. curl_close($ch);
  695. return mb_convert_encoding($response, 'utf-8', 'GB2312');
  696. }
  697. //检测远程文件是否存在
  698. public function _img_exists($url = '')
  699. {
  700. ini_set("max_execution_time", 0);
  701. $str = @file_get_contents($url, 0, null, 0, 1);
  702. if (strlen($str) <= 0) return false;
  703. if ($str)
  704. return true;
  705. else
  706. return false;
  707. }
  708. //TODO 下载图片
  709. public function downloadImage($url = '', $name = '', $type = 0, $timeout = 30, $w = 0, $h = 0)
  710. {
  711. if (!strlen(trim($url))) return '';
  712. if (!strlen(trim($name))) {
  713. //TODO 获取要下载的文件名称
  714. $downloadImageInfo = $this->getImageExtname($url);
  715. $name = $downloadImageInfo['file_name'];
  716. if (!strlen(trim($name))) return '';
  717. }
  718. //TODO 获取远程文件所采用的方法
  719. if ($type) {
  720. $ch = curl_init();
  721. curl_setopt($ch, CURLOPT_URL, $url);
  722. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  723. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  724. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //TODO 跳过证书检查
  725. if (stripos($url, "https://") !== FALSE) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //TODO 从证书中检查SSL加密算法是否存在
  726. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:' . $_SERVER['HTTP_USER_AGENT']));
  727. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//TODO 是否采集301、302之后的页面
  728. $content = curl_exec($ch);
  729. curl_close($ch);
  730. } else {
  731. try {
  732. ob_start();
  733. readfile($url);
  734. $content = ob_get_contents();
  735. ob_end_clean();
  736. } catch (\Exception $e) {
  737. return $e->getMessage();
  738. }
  739. }
  740. $size = strlen(trim($content));
  741. if (!$content || $size <= 2) return '图片流获取失败';
  742. $date_dir = date('Y') . DS . date('m') . DS . date('d');
  743. $imageInfo = UploadService::instance()->setUploadPath('attach/' . $date_dir)->imageStream($name, $content);
  744. if (!is_array($imageInfo)) return $imageInfo;
  745. $date['path'] = $imageInfo['dir'];
  746. $date['name'] = $imageInfo['name'];
  747. $date['size'] = $imageInfo['size'];
  748. $date['mime'] = $imageInfo['type'];
  749. $date['image_type'] = $imageInfo['image_type'];
  750. $date['is_exists'] = false;
  751. return $date;
  752. }
  753. //获取即将要下载的图片扩展名
  754. public function getImageExtname($url = '', $ex = 'jpg')
  755. {
  756. $_empty = ['file_name' => '', 'ext_name' => $ex];
  757. if (!$url) return $_empty;
  758. if (strpos($url, '?')) {
  759. $_tarr = explode('?', $url);
  760. $url = trim($_tarr[0]);
  761. }
  762. $arr = explode('.', $url);
  763. if (!is_array($arr) || count($arr) <= 1) return $_empty;
  764. $ext_name = trim($arr[count($arr) - 1]);
  765. $ext_name = !$ext_name ? $ex : $ext_name;
  766. return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
  767. }
  768. /*
  769. $filepath = 绝对路径,末尾有斜杠 /
  770. $name = 图片文件名
  771. $maxwidth 定义生成图片的最大宽度(单位:像素)
  772. $maxheight 生成图片的最大高度(单位:像素)
  773. $filetype 最终生成的图片类型(.jpg/.png/.gif)
  774. */
  775. public function resizeImage($filepath = '', $name = '', $maxwidth = 0, $maxheight = 0)
  776. {
  777. $pic_file = $filepath . $name; //图片文件
  778. $img_info = getimagesize($pic_file); //索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,
  779. if ($img_info[2] == 1) {
  780. $im = imagecreatefromgif($pic_file); //打开图片
  781. $filetype = '.gif';
  782. } elseif ($img_info[2] == 2) {
  783. $im = imagecreatefromjpeg($pic_file); //打开图片
  784. $filetype = '.jpg';
  785. } elseif ($img_info[2] == 3) {
  786. $im = imagecreatefrompng($pic_file); //打开图片
  787. $filetype = '.png';
  788. } else {
  789. return ['path' => $filepath, 'file' => $name, 'mime' => ''];
  790. }
  791. $file_name = md5('_tmp_' . microtime() . '_' . rand(0, 10)) . $filetype;
  792. $pic_width = imagesx($im);
  793. $pic_height = imagesy($im);
  794. $resizewidth_tag = false;
  795. $resizeheight_tag = false;
  796. if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
  797. if ($maxwidth && $pic_width > $maxwidth) {
  798. $widthratio = $maxwidth / $pic_width;
  799. $resizewidth_tag = true;
  800. }
  801. if ($maxheight && $pic_height > $maxheight) {
  802. $heightratio = $maxheight / $pic_height;
  803. $resizeheight_tag = true;
  804. }
  805. if ($resizewidth_tag && $resizeheight_tag) {
  806. if ($widthratio < $heightratio)
  807. $ratio = $widthratio;
  808. else
  809. $ratio = $heightratio;
  810. }
  811. if ($resizewidth_tag && !$resizeheight_tag)
  812. $ratio = $widthratio;
  813. if ($resizeheight_tag && !$resizewidth_tag)
  814. $ratio = $heightratio;
  815. $newwidth = $pic_width * $ratio;
  816. $newheight = $pic_height * $ratio;
  817. if (function_exists("imagecopyresampled")) {
  818. $newim = imagecreatetruecolor($newwidth, $newheight);
  819. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  820. } else {
  821. $newim = imagecreate($newwidth, $newheight);
  822. imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  823. }
  824. if ($filetype == '.png') {
  825. imagepng($newim, $filepath . $file_name);
  826. } else if ($filetype == '.gif') {
  827. imagegif($newim, $filepath . $file_name);
  828. } else {
  829. imagejpeg($newim, $filepath . $file_name);
  830. }
  831. imagedestroy($newim);
  832. } else {
  833. if ($filetype == '.png') {
  834. imagepng($im, $filepath . $file_name);
  835. } else if ($filetype == '.gif') {
  836. imagegif($im, $filepath . $file_name);
  837. } else {
  838. imagejpeg($im, $filepath . $file_name);
  839. }
  840. imagedestroy($im);
  841. }
  842. @unlink($pic_file);
  843. return ['path' => $filepath, 'file' => $file_name, 'mime' => $img_info['mime']];
  844. }
  845. }