CopyTaobao.php 41 KB

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