Commit 533d1c59 authored by hj's avatar hj

更新提交

parent acdea22f
<?php
namespace App\Console\Commands;
use App\Jobs\WuxiLabJob;
use App\Services\ThirdPlatform\CheMenuService;
use App\Services\ThirdPlatform\ChemSrcService;
use App\Services\ThirdPlatform\WuxiLabNewService;
use App\Services\ThirdPlatform\WuxiLabService;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
class CheMenuCommand extends Command
{
use ConfirmableTrait;
/**
* 命令行的名称及用法。
*
* @var string
*/
protected $signature = 'chemenu:job
{--action_type= : 任务操作类型}
{--params= : 任务参数}';
/**
* 命令行的概述。
*
* @var string
*/
protected $description = 'chemenu任务命令行';
/**
* 创建新的命令实例。
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* 运行命令。
* @throws \Exception
*/
public function handle()
{
if (! $this->confirmToProceed()) {
return 1;
}
if($this->hasArgument('help')){
$this->help();
}else{
$action_type = $this->option('action_type');
$params = $this->option('params');
if(empty($action_type)){
$this->error('缺少命令参数,请输入具体的参数命令.如需帮助请输入 --help');
exit;
}
$service = app(CheMenuService::class);
switch($action_type){
case 'category' :
$service->allCategory();
break;
case 'product':
$service->productPage($params);
default:
}
}
}
public function help()
{
$helpStr = "参数帮助说明";
$this->comment($this->setHelp($helpStr)->getProcessedHelp());
$this->line("action_type: 具体动作参数\n\n initProductToExcel => 初始化批量商品 \n batchUpdateProduct => 批量更新商品\n\nparams : 操作需要传入的参数.非必填项");
}
}
......@@ -80,7 +80,8 @@ class AppServiceProvider extends ServiceProvider
\App\Console\Commands\RhawnToolsJobCommand::class,
\App\Console\Commands\InvoiceJobCommand::class,
\App\Console\Commands\WuxiLabNewJobCommand::class,
\App\Console\Commands\ChemSrcCommand::class
\App\Console\Commands\ChemSrcCommand::class,
\App\Console\Commands\CheMenuCommand::class
]);
}
......
<?php
namespace App\Services\ThirdPlatform\Api;
use App\Repositories\Contracts\ThirdApiPlatformRepository;
use App\Services\ThirdPlatform\PlatformAbstractService;
use App\Support\Facades\SimpleLogs;
use App\Support\Traits\HttpClientHelpers;
class CheMenuApiService extends PlatformAbstractService
{
use HttpClientHelpers;
private $yybaoApiUrl = 'http://localhost:8216';
public function __construct(ThirdApiPlatformRepository $thirdApiPlatformRepository)
{
parent::__construct($thirdApiPlatformRepository);
$this->setPlatformName('chemenu');
$this->getPlatformInfo();
}
public function getAllCatetgory()
{
try {
if ($this->checkPlatformStatus()) {
return $this->clientRequest('get', $this->getDomain() . '/category/New.html', [], false);
}
}catch(\Exception $e){
$this->requestError($e);
}
}
public function saveCategory($categoryName, $parentCategoryName, $webPath)
{
try{
$response = $this->getPostClient($this->yybaoApiUrl.'/webpage/chemenu/saveCategory',
[
'categoryName' => $categoryName,
'parentName' => $parentCategoryName,
'webPath' => $webPath
], []);
return $this->returnResponse($response);
}catch(\Throwable $exception){
if(preg_match('/\{(?<response>[\s\S]*?)\}/i', $exception->getMessage(), $response)){
$response = json_decode('{'.$response['response'].'}', true);
if($response && $response['status'] == 429){
sleep(60);
$this->saveCategory($categoryName, $parentCategoryName, $webPath);
}
}else{
dd(123);
}
$this->requestError($exception);
}
}
public function returnResponse($apiResponse): bool
{
$response = json_decode($apiResponse, true);
if($response){
if($response['status'] == 'success' && $response['code'] == 200){
return true;
}
}
return false;
}
protected function requestError($response)
{
SimpleLogs::writeLog($response->getMessage(), 'chemenu error', 'error');
throw $response;
}
}
<?php
namespace App\Services\ThirdPlatform;
use App\Repositories\Contracts\ThirdApiPlatformRepository;
use App\Services\ThirdPlatform\Api\CheMenuApiService;
use App\Support\Traits\Helpers;
class CheMenuService
{
use Helpers;
public function __construct()
{
$this->apiService = (new CheMenuApiService(app(ThirdApiPlatformRepository::class)));
}
public function allCategory()
{
$responseHtml = $this->apiService->getAllCatetgory();
if($responseHtml){
$categoryDivRegx = '/<dt><span[\s]*?style=[\\\'|\"][\s\S]+?[\\\'|\"]>(?<categoryH1>[\s\S]+?)<\/span>[\s\S]*?<dd>(?<category>[\s\S]+?)<\/dd>/i';
preg_match_all($categoryDivRegx, $responseHtml,$categoryHtml);
if($categoryHtml){
foreach($categoryHtml['categoryH1'] as $key => $h1){
if(!empty($categoryHtml['category'][$key])){
preg_match_all('/<a[\s]*?href=[\\\'|\"](?<href>[\s\S]+?)[\\\'|\"]>(?<categoryName>[\s\S]+?)<\/a>/i', $categoryHtml['category'][$key],$categoryList);
if(!empty($categoryList)){
foreach($categoryList['categoryName'] as $k => $category){
$this->apiService->saveCategory($category, $h1, $categoryList['href'][$k]);
$jobContent = [
'params' => ['url' => $categoryList['href'][$k]],
'consumer' => __CLASS__,
'method' => 'categoryPage'
];
$this->apiService->pushQueue($jobContent,'chemenu_category_page');
}
}
}
sleep(60);
}
}
return true;
}
}
public function categoryPage($url)
{
try{
$pageUrl = $url['url'];
while(true){
$responseHtml = $this->apiService->getPage($pageUrl);
//file_put_contents('./test2.txt', $responseHtml);
if($responseHtml){
$titleDivRegx = '/<div[\s]*?class=[\\\'|\"]cate_desc[\\\'|\"]>[\S\s]*<h2[\s]*?class=[\\\'|\"]desc_title[\\\'|\"]>(?<title>[\s\S]+?)<\/h2>[\s\S]*?<p[\s]*?class=[\\\'|\"]desc_content[\\\'|\"]>(?<content>[\s\S]*?)<\/p>/i';
preg_match($titleDivRegx, $responseHtml,$titleHtml);
$productDivRegx = '/<div[\s]*?class=[\\\'|\"]col-lg-3 col-sm-6 col-md-4 col-xs-6 products_rows_div[\\\'|\"]>[\S\s]*?<div[\s]*?class=[\\\'|\"]cas_img[\\\'|\"]>[\s]*?<a[\s]*href=[\\\'|\"](?<href>[\S\s]+?)[\\\'|\"][\s\S]*?>[\s\S]+?<\/div>/i';
preg_match_all($productDivRegx, $responseHtml, $productList);
if($productList){
foreach($productList['href'] as $href){
$productJobContent = [
'params' => ['url' => $href],
'consumer' => __CLASS__,
'method' => 'productPage'
];
$this->apiService->pushQueue($productJobContent,'chemenu_product_page');
}
//分页处理
if(preg_match('/<nav[\s]*?class=[\\\'|\"]paging[\\\'|\"]>/i', $responseHtml)){
if(preg_match('/<li[\s]*?id=[\\\'|\"]pagination_next[\\\'|\"][\s]*?class=[\\\'|\"]disabled[\\\'|\"]>/i', $responseHtml)){
break;
}
preg_match('/<li[\s]*?id=[\\\'|\"]pagination_next[\\\'|\"]>[\s]*?<a[\S\s]*?href=[\\\'|\"](?<nextHref>[\s\S]+?)[\\\'|\"]/i', $responseHtml, $nextHref);
$pageUrl = str_replace($this->apiService->getDomain(), '', $nextHref['nextHref']);
}
}
}
}
}catch(\Throwable $exception){
throw $exception;
}
}
public function productPage($url)
{
$pageUrl = $url['url'];
$responseHtml = $this->apiService->getPage($pageUrl);
//file_put_contents('./test3.html', $responseHtml);
if($responseHtml){
$productPageContent = [];
if(preg_match('/<div[\s]*?class=[\\\'|\"]products-content[\\\'|\"]>/i', $responseHtml)){
$imageDivRegx = '/<div[\s]*?class=[\\\'|\"]product-image[\\\'|\"]>[\s\S]*?<img[\s]*?class=[\\\'|\"]big_img[\\\'|\"][\s]*?src[\s\S]*?[\\\'|\"](?<image>[\s\S]+?)[\\\'|\"]/i';
preg_match($imageDivRegx, $responseHtml, $image);
if($image && $image['image']){
$productPageContent['image'] = $image['image'];
}
$productNameDivRegx = '/<div[\s]*?class=[\\\'|\"]col-md-12 col-sm-12[\\\'|\"]>[\s]*?<p[\s]*?class=[\\\'|\"]padding-tb-15[\\\'|\"]>[\s]*?<b>(?<name>[\s\S]+?)<\/b>(?<nameContent>[\s\S]+?)<\/p>/i';
preg_match_all($productNameDivRegx, $responseHtml, $productName);
if($productName){
foreach($productName['nameContent'] as $key => $name){
$strName = $this->stripHtml($name);
if(substr($strName, 0, 1) == ':'){
$strName = str_replace(':', '', $strName);
switch($key){
case 0:
$productPageContent['productName'] = $strName;break;
case 1:
$productPageContent['IUPAC'] = $strName;break;
}
}
}
}
$chemicalInfoDivRegx = '/<div[\s]*?class=[\\\'|\"]col-md-6 col-sm-12[\\\'|\"]>(?<chemicalInfoDiv>[\s\S]+?)<\/div>/i';
preg_match_all($chemicalInfoDivRegx, $responseHtml, $chemicalInfoDiv);
if($chemicalInfoDiv){
foreach($chemicalInfoDiv['chemicalInfoDiv'] as $div){
$div = $this->stripHtml($div);
$infoList = explode("\r\n",$div);
foreach($infoList as $info){
$info = trim($info);
if(empty($info)) continue;
try{
list($name, $content) = explode(':', $info);
}catch(\Throwable $exception){
list($name, $content) = explode(':', $info);
}
$productPageContent['casInfo'][] = [$name =>$content];
}
}
}
$productPageContent['casInfo'] = array_unique($productPageContent['casInfo'],SORT_REGULAR);
$productListDivRegx = '/<div[\s]*?class=[\\\'|\"]col-md-12 col-lg-12 col-sm-12 product-details-size[\\\'|\"]>[\s\S]*?<tbody>(?<productList>[\s\S]*?)<\/tbody>/i';
if(preg_match_all($productListDivRegx, $responseHtml, $productList)){
if(!empty($productList['productList'])){
foreach($productList['productList'] as $product){
$product = $this->stripTabTag($product);
preg_match_all('/<td[\s\S]*?>(?<content>[\s\S]*?)<\/td>/i', $product, $productContentTd);
$productPageContent['productList'] = [];
if($productContentTd['content']){
$productContent = $productContentTd['content'];
$productContent = array_chunk($productContent, 5);
foreach($productContent as $content){
if(empty($content[0])) continue;
$productPageContent['productList'][] = ['productUnit' => $content[0], 'stock' => $content[1]];
}
}
}
}
}
dd($productPageContent);
}
}
}
}
......@@ -66,6 +66,26 @@ abstract class PlatformAbstractService
{
}
public function getDomain()
{
return $this->platformInfo['platform_url'];
}
public function getPage($uri)
{
try {
if ($this->checkPlatformStatus()) {
if(substr($uri, 0, 1) != '/'){
$uri = '/' . $uri;
}
return $this->clientRequest('get', $this->getDomain() . $uri, [], false);
}
}catch(\Exception $e){
$this->requestError($e);
}
}
/**
* @对平台状态进行检查
* @return bool
......
......@@ -173,4 +173,11 @@ trait Helpers
return strip_tags($str);
}
function stripTabTag($str)
{
$str = str_replace("\t", "", $str);
$str = str_replace("\r\n", "", $str);
return $str;
}
}
......@@ -97,6 +97,17 @@ class ThridApiPlatformSeeder extends Seeder
'platform_status' => '1',
'platform_token' => '',
],
[
'platform_name' => 'chemenu',
'platform_title' => 'chemenu',
'platform_url' => 'https://www.chemenu.com',
'platform_desc' => '',
'platform_icon' => '',
'platform_params' => '',
'platform_type' => 'third',
'platform_status' => '1',
'platform_token' => '',
],
];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment