CopyTaobao.php 42 KB

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