CopyTaobao.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 $html
  157. */
  158. public function setProductInfoTaobao($html)
  159. {
  160. $this->webnname = 'taobao';
  161. //获取轮播图
  162. $images = $this->getTaobaoImg($html);
  163. $images = array_merge(is_array($images) ? $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 $html
  186. */
  187. public function setProductInfoTmall($html)
  188. {
  189. $this->webnname = 'tmall';
  190. //获取轮播图
  191. $images = $this->getTianMaoImg($html);
  192. $images = array_merge(is_array($images) ? $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 $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. if (!is_array($this->productInfo['slider_image'])) {
  226. $this->productInfo['slider_image'] = [];
  227. }
  228. $this->productInfo['slider_image'] = array_slice($this->productInfo['slider_image'], 0, 5);
  229. $this->productInfo['image'] = is_array($this->productInfo['slider_image']) && isset($this->productInfo['slider_image'][0]) ? $this->productInfo['slider_image'][0] : '';
  230. //获取产品详情请求链接
  231. $link = $this->get1688Desc($html);
  232. //获取请求内容
  233. $desc_json = HttpService::getRequest($link);
  234. //转换字符集
  235. $desc_json = $this->Utf8String($desc_json);
  236. $this->productInfo['test'] = $desc_json;
  237. //截取掉多余字符
  238. $desc_json = str_replace('var offer_details=', '', $desc_json);
  239. $desc_json = str_replace(["\n", "\t", "\r"], '', $desc_json);
  240. $desc_json = substr($desc_json, 0, -1);
  241. $descArray = json_decode($desc_json, true);
  242. if (!isset($descArray['content'])) $descArray['content'] = '';
  243. $this->productInfo['description'] = $descArray['content'];
  244. //获取详情图
  245. $description_images = $this->decodedesc($this->productInfo['description']);
  246. $this->productInfo['description_images'] = is_array($description_images) ? $description_images : [];
  247. }
  248. /**
  249. * JD设置产品
  250. * @param string $html 网页内容
  251. * */
  252. public function setProductInfoJd($html)
  253. {
  254. $this->webnname = 'jd';
  255. //获取产品详情请求链接
  256. $desc_url = $this->getJdDesc($html);
  257. //获取请求内容
  258. $desc_json = HttpService::getRequest($desc_url);
  259. //转换字符集
  260. $desc_json = $this->Utf8String($desc_json);
  261. //截取掉多余字符
  262. if (substr($desc_json, 0, 8) == 'showdesc') $desc_json = str_replace('showdesc', '', $desc_json);
  263. $desc_json = str_replace('data-lazyload=', 'src=', $desc_json);
  264. $descArray = json_decode($desc_json, true);
  265. if (!$descArray) $descArray = ['content' => ''];
  266. //获取轮播图
  267. $images = $this->getJdImg($html);
  268. $images = array_merge(is_array($images) ? $images : []);
  269. $this->productInfo['slider_image'] = $images;
  270. $this->productInfo['image'] = is_array($this->productInfo['slider_image']) ? ($this->productInfo['slider_image'][0] ?? '') : '';
  271. $this->productInfo['description'] = $descArray['content'];
  272. //获取详情图
  273. $description_images = $this->decodedesc($descArray['content']);
  274. $this->productInfo['description_images'] = is_array($description_images) ? $description_images : [];
  275. }
  276. /**
  277. * 检查淘宝,天猫,1688的商品链接
  278. * @param $link
  279. * @return bool|string
  280. */
  281. public function checkurl($link)
  282. {
  283. $link = strtolower(htmlspecialchars_decode($link));
  284. if (!$link) return $this->setErrorInfo('请输入链接地址');
  285. if (substr($link, 0, 4) != 'http') return $this->setErrorInfo('链接地址必须以http开头');
  286. $arrLine = explode('?', $link);
  287. if (!count($arrLine)) return $this->setErrorInfo('链接地址有误(ERR:1001)');
  288. if (!isset($arrLine[1])) {
  289. if (strpos($link, '1688') !== false && strpos($link, 'offer') !== false) return trim($arrLine[0]);
  290. else if (strpos($link, 'item.jd') !== false) return trim($arrLine[0]);
  291. else return $this->setErrorInfo('链接地址有误(ERR:1002)');
  292. }
  293. if (strpos($link, '1688') !== false && strpos($link, 'offer') !== false) return trim($arrLine[0]);
  294. if (strpos($link, 'item.jd') !== false) return trim($arrLine[0]);
  295. $arrLineValue = explode('&', $arrLine[1]);
  296. if (!is_array($arrLineValue)) return $this->setErrorInfo('链接地址有误(ERR:1003)');
  297. if (!strpos(trim($arrLine[0]), 'item.htm')) $this->setErrorInfo('链接地址有误(ERR:1004)');
  298. //链接参数
  299. $lastStr = '';
  300. foreach ($arrLineValue as $k => $v) {
  301. if (substr(strtolower($v), 0, 3) == 'id=') {
  302. $lastStr = trim($v);
  303. break;
  304. }
  305. }
  306. if (!$lastStr) return $this->setErrorInfo('链接地址有误(ERR:1005)');
  307. return trim($arrLine[0]) . '?' . $lastStr;
  308. }
  309. /*
  310. * 保存图片保存产品信息
  311. * */
  312. public function save_product()
  313. {
  314. $data = UtilService::postMore([
  315. ['cate_id', ''],
  316. ['store_name', ''],
  317. ['store_info', ''],
  318. ['keyword', ''],
  319. ['unit_name', ''],
  320. ['image', ''],
  321. ['slider_image', []],
  322. ['price', ''],
  323. ['ot_price', ''],
  324. ['give_integral', ''],
  325. ['postage', ''],
  326. ['sales', ''],
  327. ['ficti', ''],
  328. ['stock', ''],
  329. ['cost', ''],
  330. ['description_images', []],
  331. ['description', ''],
  332. ['is_show', 0],
  333. ['soure_link', ''],
  334. ['temp_id', 0],
  335. ]);
  336. if (!$data['cate_id']) return JsonService::fail('请选择分类!');
  337. if (!$data['store_name']) return JsonService::fail('请填写产品名称');
  338. if (!$data['unit_name']) return JsonService::fail('请填写产品单位');
  339. if (!$data['image']) return JsonService::fail('商品主图暂无,无法保存商品,您可选择其他链接进行复制产品');
  340. if ($data['price'] == '' || $data['price'] < 0) return JsonService::fail('请输入产品售价');
  341. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return JsonService::fail('请输入产品市场价');
  342. if ($data['stock'] == '' || $data['stock'] < 0) return JsonService::fail('请输入库存');
  343. if (!$data['temp_id']) return JsonService::fail('请选择运费模板');
  344. //查询附件分类
  345. $AttachmentCategory = SystemAttachmentCategory::where('name', $this->AttachmentCategoryName)->find();
  346. //不存在则创建
  347. if (!$AttachmentCategory) $AttachmentCategory = SystemAttachmentCategory::create(['pid' => '0', 'name' => $this->AttachmentCategoryName, 'enname' => '']);
  348. //生成附件目录
  349. try {
  350. if (make_path('attach', 3, true) === '')
  351. return JsonService::fail('无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS);
  352. } catch (\Exception $e) {
  353. return JsonService::fail($e->getMessage() . '或无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS);
  354. }
  355. ini_set("max_execution_time", 600);
  356. //开始图片下载处理
  357. ProductModel::beginTrans();
  358. try {
  359. //放入主图
  360. $images = [
  361. ['w' => 305, 'h' => 305, 'line' => $data['image'], 'valuename' => 'image']
  362. ];
  363. //放入轮播图
  364. foreach ($data['slider_image'] as $item) {
  365. $value = ['w' => 640, 'h' => 640, 'line' => $item, 'valuename' => 'slider_image', 'isTwoArray' => true];
  366. array_push($images, $value);
  367. }
  368. //执行下载
  369. $res = $this->uploadImage($images, false, 0, $AttachmentCategory['id']);
  370. if (!is_array($res)) return JsonService::fail($this->errorInfo ? $this->errorInfo : '保存图片失败');
  371. if (isset($res['image'])) $data['image'] = $res['image'];
  372. if (isset($res['slider_image'])) $data['slider_image'] = $res['slider_image'];
  373. $data['slider_image'] = count($data['slider_image']) ? json_encode($data['slider_image']) : '';
  374. //替换并下载详情里面的图片默认下载全部图片
  375. $data['description'] = preg_replace('#<style>.*?</style>#is', '', $data['description']);
  376. $data['description'] = $this->uploadImage($data['description_images'], $data['description'], 1, $AttachmentCategory['id']);
  377. unset($data['description_images']);
  378. $description = $data['description'];
  379. unset($data['description']);
  380. $data['add_time'] = time();
  381. $cate_id = explode(',', $data['cate_id']);
  382. //产品存在
  383. if ($productInfo = ProductModel::where('soure_link', $data['soure_link'])->find()) {
  384. $productInfo->slider_image = $data['slider_image'];
  385. $productInfo->image = $data['image'];
  386. $productInfo->store_name = $data['store_name'];
  387. StoreDescription::saveDescription($description, $productInfo->id);
  388. $productInfo->save();
  389. ProductModel::commitTrans();
  390. return JsonService::successful('商品存在,信息已被更新成功');
  391. } else {
  392. //不存在时新增
  393. if ($productId = ProductModel::insertGetId($data)) {
  394. $cateList = [];
  395. foreach ($cate_id as $cid) {
  396. $cateList [] = ['product_id' => $productId, 'cate_id' => $cid, 'add_time' => time()];
  397. }
  398. StoreProductCate::insertAll($cateList);
  399. $attr = [
  400. [
  401. 'value' => '规格',
  402. 'detailValue' => '',
  403. 'attrHidden' => '',
  404. 'detail' => ['默认']
  405. ]
  406. ];
  407. $detail[0]['value1'] = '规格';
  408. $detail[0]['detail'] = ['规格' => '默认'];
  409. $detail[0]['price'] = $data['price'];
  410. $detail[0]['stock'] = $data['stock'];
  411. $detail[0]['cost'] = $data['cost'];
  412. $detail[0]['pic'] = $data['image'];
  413. $detail[0]['ot_price'] = $data['price'];
  414. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $productId);
  415. if ($attr_res) {
  416. StoreDescription::saveDescription($description, $productId);
  417. ProductModel::commitTrans();
  418. return JsonService::successful('生成产品成功');
  419. } else {
  420. ProductModel::rollbackTrans();
  421. return JsonService::fail(StoreProductAttr::getErrorInfo('生成产品失败'));
  422. }
  423. } else {
  424. ProductModel::rollbackTrans();
  425. return JsonService::fail('生成产品失败');
  426. }
  427. }
  428. } catch (PDOException $e) {
  429. ProductModel::rollbackTrans();
  430. return JsonService::fail('插入数据库错误', ['line' => $e->getLine(), 'messag' => $e->getMessage()]);
  431. } catch (\Exception $e) {
  432. ProductModel::rollbackTrans();
  433. return JsonService::fail('系统错误', ['line' => $e->getLine(), 'messag' => $e->getMessage(), 'file' => $e->getFile()]);
  434. }
  435. }
  436. /*
  437. * 上传图片处理
  438. * @param array $image 图片路径
  439. * @param int $uploadType 上传方式 0=远程下载
  440. * */
  441. public function uploadImage(array $images = [], $html = '', $uploadType = 0, $AttachmentCategoryId = 0)
  442. {
  443. $uploadImage = [];
  444. $siteUrl = sys_config('site_url');
  445. switch ($uploadType) {
  446. case 0:
  447. foreach ($images as $item) {
  448. //下载图片文件
  449. if ($item['w'] && $item['h'])
  450. $uploadValue = $this->downloadImage($item['line'], '', 0, 30, $item['w'], $item['h']);
  451. else
  452. $uploadValue = $this->downloadImage($item['line']);
  453. //下载成功更新数据库
  454. if (is_array($uploadValue)) {
  455. //TODO 拼接图片地址
  456. if ($uploadValue['image_type'] == 1) $imagePath = $siteUrl . $uploadValue['path'];
  457. else $imagePath = $uploadValue['path'];
  458. //写入数据库
  459. if (!$uploadValue['is_exists'] && $AttachmentCategoryId) SystemAttachment::attachmentAdd($uploadValue['name'], $uploadValue['size'], $uploadValue['mime'], $imagePath, $imagePath, $AttachmentCategoryId, $uploadValue['image_type'], time(), 1);
  460. //组装数组
  461. if (isset($item['isTwoArray']) && $item['isTwoArray'])
  462. $uploadImage[$item['valuename']][] = $imagePath;
  463. else
  464. $uploadImage[$item['valuename']] = $imagePath;
  465. }
  466. }
  467. break;
  468. case 1:
  469. preg_match_all('#<img.*?src="([^"]*)"[^>]*>#i', $html, $match);
  470. if (isset($match[1])) {
  471. foreach ($match[1] as $item) {
  472. if (is_int(strpos($item, 'http')))
  473. $arcurl = $item;
  474. else
  475. $arcurl = 'http://' . ltrim($item, '\//');
  476. $uploadValue = $this->downloadImage($arcurl);
  477. //下载成功更新数据库
  478. if (is_array($uploadValue)) {
  479. //TODO 拼接图片地址
  480. if ($uploadValue['image_type'] == 1) $imagePath = $siteUrl . $uploadValue['path'];
  481. else $imagePath = $uploadValue['path'];
  482. //写入数据库
  483. if (!$uploadValue['is_exists'] && $AttachmentCategoryId) SystemAttachment::attachmentAdd($uploadValue['name'], $uploadValue['size'], $uploadValue['mime'], $imagePath, $imagePath, $AttachmentCategoryId, $uploadValue['image_type'], time(), 1);
  484. //替换图片
  485. $html = str_replace($item, $imagePath, $html);
  486. } else {
  487. //替换掉没有下载下来的图片
  488. $html = preg_replace('#<img.*?src="' . $item . '"*>#i', '', $html);
  489. }
  490. }
  491. }
  492. return $html;
  493. break;
  494. default:
  495. return $this->setErrorInfo('上传方式错误');
  496. break;
  497. }
  498. return $uploadImage;
  499. }
  500. //提取商品描述中的所有图片
  501. public function decodedesc($desc = '')
  502. {
  503. $desc = trim($desc);
  504. if (!$desc) return '';
  505. preg_match_all('/<img[^>]*?src="([^"]*?)"[^>]*?>/i', $desc, $match);
  506. if (!isset($match[1]) || count($match[1]) <= 0) {
  507. preg_match_all('/:url(([^"]*?));/i', $desc, $match);
  508. if (!isset($match[1]) || count($match[1]) <= 0) return $desc;
  509. } else {
  510. preg_match_all('/:url(([^"]*?));/i', $desc, $newmatch);
  511. if (isset($newmatch[1]) && count($newmatch[1]) > 0) $match[1] = array_merge($match[1], $newmatch[1]);
  512. }
  513. $match[1] = array_unique($match[1]); //去掉重复
  514. foreach ($match[1] as $k => &$v) {
  515. $_tmp_img = str_replace([')', '(', ';'], '', $v);
  516. $_tmp_img = strpos($_tmp_img, 'http') ? $_tmp_img : 'http:' . $_tmp_img;
  517. if (strpos($v, '?')) {
  518. $_tarr = explode('?', $v);
  519. $_tmp_img = trim($_tarr[0]);
  520. }
  521. $_urls = str_replace(['\'', '"'], '', $_tmp_img);
  522. if ($this->_img_exists($_urls)) $v = $_urls;
  523. }
  524. return $match[1];
  525. }
  526. //获取京东商品组图
  527. public function getJdImg($html = '')
  528. {
  529. //获取图片服务器网址
  530. preg_match('/<img(.*?)id="spec-img"(.*?)data-origin=\"(.*?)\"[^>]*>/', $html, $img);
  531. if (!isset($img[3])) return '';
  532. $info = parse_url(trim($img[3]));
  533. if (!$info['host']) return '';
  534. if (!$info['path']) return '';
  535. $_tmparr = explode('/', trim($info['path']));
  536. $url = 'http://' . $info['host'] . '/' . $_tmparr[1] . '/' . str_replace(['jfs', ' '], '', trim($_tmparr[2]));
  537. preg_match('/imageList:(.*?)"],/is', $html, $img);
  538. if (!isset($img[1])) {
  539. return '';
  540. }
  541. $_arr = explode(',', $img[1]);
  542. foreach ($_arr as $k => &$v) {
  543. $_str = $url . str_replace(['"', '[', ']', ' '], '', trim($v));
  544. if (strpos($_str, '?')) {
  545. $_tarr = explode('?', $_str);
  546. $_str = trim($_tarr[0]);
  547. }
  548. if ($this->_img_exists($_str)) {
  549. $v = $_str;
  550. } else {
  551. unset($_arr[$k]);
  552. }
  553. }
  554. return array_unique($_arr);
  555. }
  556. //获取京东商品描述
  557. public function getJdDesc($html = '')
  558. {
  559. preg_match('/,(.*?)desc:([^<>]*)\',/i', $html, $descarr);
  560. if (!isset($descarr[1]) && !isset($descarr[2])) return '';
  561. $tmpArr = explode(',', $descarr[2]);
  562. if (count($tmpArr) > 0) {
  563. $descarr[2] = trim($tmpArr[0]);
  564. }
  565. $replace_arr = ['\'', '\',', ' ', ',', '/*', '*/'];
  566. if (isset($descarr[2])) {
  567. $d_url = str_replace($replace_arr, '', $descarr[2]);
  568. return $this->formatDescUrl(strpos($d_url, 'http') ? $d_url : 'http:' . $d_url);
  569. }
  570. $d_url = str_replace($replace_arr, '', $descarr[1]);
  571. $d_url = $this->formatDescUrl($d_url);
  572. $d_url = rtrim(rtrim($d_url, "?"), "&");
  573. return substr($d_url, 0, 4) == 'http' ? $d_url : 'http:' . $d_url;
  574. }
  575. //处理下京东商品描述网址
  576. public function formatDescUrl($url = '')
  577. {
  578. if (!$url) return '';
  579. $url = substr($url, 0, 4) == 'http' ? $url : 'http:' . $url;
  580. if (!strpos($url, '&')) {
  581. $_arr = explode('?', $url);
  582. if (!is_array($_arr) || count($_arr) <= 0) return $url;
  583. return trim($_arr[0]);
  584. } else {
  585. $_arr = explode('&', $url);
  586. }
  587. if (!is_array($_arr) || count($_arr) <= 0) return $url;
  588. unset($_arr[count($_arr) - 1]);
  589. $new_url = '';
  590. foreach ($_arr as $k => $v) {
  591. $new_url .= $v . '&';
  592. }
  593. return !$new_url ? $url : $new_url;
  594. }
  595. //获取1688商品组图
  596. public function get1688Img($html = '')
  597. {
  598. preg_match('/<ul class=\"nav nav-tabs fd-clr\">(.*?)<\/ul>/is', $html, $img);
  599. if (!isset($img[0])) {
  600. return '';
  601. }
  602. preg_match_all('/preview":"(.*?)\"\}\'>/is', $img[0], $arrb);
  603. if (!isset($arrb[1]) || count($arrb[1]) <= 0) {
  604. return '';
  605. }
  606. $thumb = [];
  607. $gaoqing = [];
  608. $res = ['thumb' => '', 'gaoqing' => '']; //缩略图片和高清图片
  609. foreach ($arrb[1] as $k => $v) {
  610. $_str = str_replace(['","original":"'], '*', $v);
  611. $_arr = explode('*', $_str);
  612. if (is_array($_arr) && isset($_arr[0]) && isset($_arr[1])) {
  613. if (strpos($_arr[0], '?')) {
  614. $_tarr = explode('?', $_arr[0]);
  615. $_arr[0] = trim($_tarr[0]);
  616. }
  617. if (strpos($_arr[1], '?')) {
  618. $_tarr = explode('?', $_arr[1]);
  619. $_arr[1] = trim($_tarr[0]);
  620. }
  621. if ($this->_img_exists($_arr[0])) $thumb[] = trim($_arr[0]);
  622. if ($this->_img_exists($_arr[1])) $gaoqing[] = trim($_arr[1]);
  623. }
  624. }
  625. $res = ['thumb' => array_unique($thumb), 'gaoqing' => array_unique($gaoqing)]; //缩略图片和高清图片
  626. return $res;
  627. }
  628. //获取1688商品描述
  629. public function get1688Desc($html = '')
  630. {
  631. preg_match('/data-tfs-url="([^<>]*)data-enable="true"/', $html, $descarr);
  632. if (!isset($descarr[1])) return '';
  633. return str_replace(['"', ' '], '', $descarr[1]);
  634. }
  635. //获取天猫商品组图
  636. public function getTianMaoImg($html = '')
  637. {
  638. $pic_size = '430';
  639. preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $html, $img);
  640. if (isset($img[1])) {
  641. $_arr = explode('x', $img[1]);
  642. $filename = $_arr[count($_arr) - 1];
  643. $pic_size = intval(substr($filename, 0, 3));
  644. }
  645. preg_match('|<ul id="J_UlThumb" class="tb-thumb tm-clear">(.*)</ul>|isU', $html, $match);
  646. preg_match_all('/<img src="(.*?)" \//', $match[1], $images);
  647. if (!isset($images[1])) return '';
  648. foreach ($images[1] as $k => &$v) {
  649. $tmp_v = trim($v);
  650. $_arr = explode('x', $tmp_v);
  651. $_fname = $_arr[count($_arr) - 1];
  652. $_size = intval(substr($_fname, 0, 3));
  653. if (strpos($tmp_v, '://')) {
  654. $_arr = explode(':', $tmp_v);
  655. $r_url = trim($_arr[1]);
  656. } else {
  657. $r_url = $tmp_v;
  658. }
  659. $str = str_replace($_size, $pic_size, $r_url);
  660. if (strpos($str, '?')) {
  661. $_tarr = explode('?', $str);
  662. $str = trim($_tarr[0]);
  663. }
  664. $_i_url = strpos($str, 'http') ? $str : 'http:' . $str;
  665. if ($this->_img_exists($_i_url)) {
  666. $v = $_i_url;
  667. } else {
  668. unset($images[1][$k]);
  669. }
  670. }
  671. return array_unique($images[1]);
  672. }
  673. //获取天猫商品描述
  674. public function getTianMaoDesc($html = '')
  675. {
  676. preg_match('/descUrl":"([^<>]*)","httpsDescUrl":"/', $html, $descarr);
  677. if (!isset($descarr[1])) {
  678. preg_match('/httpsDescUrl":"([^<>]*)","fetchDcUrl/', $html, $descarr);
  679. if (!isset($descarr[1])) return '';
  680. }
  681. return strpos($descarr[1], 'http') ? $descarr[1] : 'http:' . $descarr[1];
  682. }
  683. //获取淘宝商品组图
  684. public function getTaobaoImg($html = '')
  685. {
  686. preg_match('/auctionImages([^<>]*)"]/', $html, $imgarr);
  687. if (!isset($imgarr[1])) return '';
  688. $arr = explode(',', $imgarr[1]);
  689. foreach ($arr as $k => &$v) {
  690. $str = trim($v);
  691. $str = str_replace(['"', ' ', '', ':['], '', $str);
  692. if (strpos($str, '?')) {
  693. $_tarr = explode('?', $str);
  694. $str = trim($_tarr[0]);
  695. }
  696. $_i_url = strpos($str, 'http') ? $str : 'http:' . $str;
  697. if ($this->_img_exists($_i_url)) {
  698. $v = $_i_url;
  699. } else {
  700. unset($arr[$k]);
  701. }
  702. }
  703. return array_unique($arr);
  704. }
  705. //获取淘宝商品描述
  706. public function getTaobaoDesc($html = '')
  707. {
  708. preg_match('/descUrl([^<>]*)counterApi/', $html, $descarr);
  709. if (!isset($descarr[1])) return '';
  710. $arr = explode(':', $descarr[1]);
  711. $url = [];
  712. foreach ($arr as $k => $v) {
  713. if (strpos($v, '//')) {
  714. $str = str_replace(['\'', ',', ' ', '?', ':'], '', $v);
  715. $url[] = trim($str);
  716. }
  717. }
  718. if ($url) {
  719. return strpos($url[0], 'http') ? $url[0] : 'http:' . $url[0];
  720. } else {
  721. return '';
  722. }
  723. }
  724. /**
  725. * GET 请求
  726. * @param string $url
  727. */
  728. public function curl_Get($url = '', $time_out = 25)
  729. {
  730. if (!$url) return '';
  731. $ch = curl_init();
  732. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  733. if (stripos($url, "https://") !== FALSE) {
  734. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  735. }
  736. $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'];
  737. if ($this->webnname) {
  738. $headers[] = $this->webcookie["$this->webnname"];
  739. }
  740. curl_setopt($ch, CURLOPT_URL, $url);
  741. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  742. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  743. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  744. curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
  745. $response = curl_exec($ch);
  746. if ($error = curl_error($ch)) {
  747. return false;
  748. }
  749. curl_close($ch);
  750. return mb_convert_encoding($response, 'utf-8', 'GB2312');
  751. }
  752. //检测远程文件是否存在
  753. public function _img_exists($url = '')
  754. {
  755. ini_set("max_execution_time", 0);
  756. $str = @file_get_contents($url, 0, null, 0, 1);
  757. if (strlen($str) <= 0) return false;
  758. if ($str)
  759. return true;
  760. else
  761. return false;
  762. }
  763. //TODO 下载图片
  764. public function downloadImage($url = '', $name = '', $type = 0, $timeout = 30, $w = 0, $h = 0)
  765. {
  766. if (!strlen(trim($url))) return '';
  767. if (!strlen(trim($name))) {
  768. //TODO 获取要下载的文件名称
  769. $downloadImageInfo = $this->getImageExtname($url);
  770. $name = $downloadImageInfo['file_name'];
  771. if (!strlen(trim($name))) return '';
  772. }
  773. //TODO 获取远程文件所采用的方法
  774. if ($type) {
  775. $ch = curl_init();
  776. curl_setopt($ch, CURLOPT_URL, $url);
  777. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  778. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  779. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //TODO 跳过证书检查
  780. if (stripos($url, "https://") !== FALSE) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //TODO 从证书中检查SSL加密算法是否存在
  781. curl_setopt($ch, CURLOPT_HTTPHEADER, array('user-agent:' . $_SERVER['HTTP_USER_AGENT']));
  782. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//TODO 是否采集301、302之后的页面
  783. $content = curl_exec($ch);
  784. curl_close($ch);
  785. } else {
  786. try {
  787. ob_start();
  788. readfile($url);
  789. $content = ob_get_contents();
  790. ob_end_clean();
  791. } catch (\Exception $e) {
  792. return $e->getMessage();
  793. }
  794. }
  795. $size = strlen(trim($content));
  796. if (!$content || $size <= 2) return '图片流获取失败';
  797. $date_dir = date('Y') . DS . date('m') . DS . date('d');
  798. $upload_type = sys_config('upload_type', 1);
  799. $upload = new Upload((int)$upload_type, [
  800. 'accessKey' => sys_config('accessKey'),
  801. 'secretKey' => sys_config('secretKey'),
  802. 'uploadUrl' => sys_config('uploadUrl'),
  803. 'storageName' => sys_config('storage_name'),
  804. 'storageRegion' => sys_config('storage_region'),
  805. ]);
  806. $info = $upload->to('attach/' . $date_dir)->validate()->stream($content, $name);
  807. if ($info === false) {
  808. return $upload->getError();
  809. }
  810. $imageInfo = $upload->getUploadInfo();
  811. $date['path'] = $imageInfo['dir'];
  812. $date['name'] = $imageInfo['name'];
  813. $date['size'] = $imageInfo['size'];
  814. $date['mime'] = $imageInfo['type'];
  815. $date['image_type'] = $upload_type;
  816. $date['is_exists'] = false;
  817. return $date;
  818. }
  819. //获取即将要下载的图片扩展名
  820. public function getImageExtname($url = '', $ex = 'jpg')
  821. {
  822. $_empty = ['file_name' => '', 'ext_name' => $ex];
  823. if (!$url) return $_empty;
  824. if (strpos($url, '?')) {
  825. $_tarr = explode('?', $url);
  826. $url = trim($_tarr[0]);
  827. }
  828. $arr = explode('.', $url);
  829. if (!is_array($arr) || count($arr) <= 1) return $_empty;
  830. $ext_name = trim($arr[count($arr) - 1]);
  831. $ext_name = !$ext_name ? $ex : $ext_name;
  832. return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
  833. }
  834. /*
  835. $filepath = 绝对路径,末尾有斜杠 /
  836. $name = 图片文件名
  837. $maxwidth 定义生成图片的最大宽度(单位:像素)
  838. $maxheight 生成图片的最大高度(单位:像素)
  839. $filetype 最终生成的图片类型(.jpg/.png/.gif)
  840. */
  841. public function resizeImage($filepath = '', $name = '', $maxwidth = 0, $maxheight = 0)
  842. {
  843. $pic_file = $filepath . $name; //图片文件
  844. $img_info = getimagesize($pic_file); //索引 2 是图像类型的标记:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,
  845. if ($img_info[2] == 1) {
  846. $im = imagecreatefromgif($pic_file); //打开图片
  847. $filetype = '.gif';
  848. } elseif ($img_info[2] == 2) {
  849. $im = imagecreatefromjpeg($pic_file); //打开图片
  850. $filetype = '.jpg';
  851. } elseif ($img_info[2] == 3) {
  852. $im = imagecreatefrompng($pic_file); //打开图片
  853. $filetype = '.png';
  854. } else {
  855. return ['path' => $filepath, 'file' => $name, 'mime' => ''];
  856. }
  857. $file_name = md5('_tmp_' . microtime() . '_' . rand(0, 10)) . $filetype;
  858. $pic_width = imagesx($im);
  859. $pic_height = imagesy($im);
  860. $resizewidth_tag = false;
  861. $resizeheight_tag = false;
  862. if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
  863. if ($maxwidth && $pic_width > $maxwidth) {
  864. $widthratio = $maxwidth / $pic_width;
  865. $resizewidth_tag = true;
  866. }
  867. if ($maxheight && $pic_height > $maxheight) {
  868. $heightratio = $maxheight / $pic_height;
  869. $resizeheight_tag = true;
  870. }
  871. if ($resizewidth_tag && $resizeheight_tag) {
  872. if ($widthratio < $heightratio)
  873. $ratio = $widthratio;
  874. else
  875. $ratio = $heightratio;
  876. }
  877. if ($resizewidth_tag && !$resizeheight_tag)
  878. $ratio = $widthratio;
  879. if ($resizeheight_tag && !$resizewidth_tag)
  880. $ratio = $heightratio;
  881. $newwidth = $pic_width * $ratio;
  882. $newheight = $pic_height * $ratio;
  883. if (function_exists("imagecopyresampled")) {
  884. $newim = imagecreatetruecolor($newwidth, $newheight);
  885. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  886. } else {
  887. $newim = imagecreate($newwidth, $newheight);
  888. imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  889. }
  890. if ($filetype == '.png') {
  891. imagepng($newim, $filepath . $file_name);
  892. } else if ($filetype == '.gif') {
  893. imagegif($newim, $filepath . $file_name);
  894. } else {
  895. imagejpeg($newim, $filepath . $file_name);
  896. }
  897. imagedestroy($newim);
  898. } else {
  899. if ($filetype == '.png') {
  900. imagepng($im, $filepath . $file_name);
  901. } else if ($filetype == '.gif') {
  902. imagegif($im, $filepath . $file_name);
  903. } else {
  904. imagejpeg($im, $filepath . $file_name);
  905. }
  906. imagedestroy($im);
  907. }
  908. @unlink($pic_file);
  909. return ['path' => $filepath, 'file' => $file_name, 'mime' => $img_info['mime']];
  910. }
  911. }