Commit 2869aea0 authored by hj's avatar hj

更新提交

parent ddb46597
<?php
namespace App\Console\Commands;
use App\Jobs\WuxiLabJob;
use App\Services\ThirdPlatform\ChemSrcService;
use App\Services\ThirdPlatform\WuxiLabNewService;
use App\Services\ThirdPlatform\WuxiLabService;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
class ChemSrcCommand extends Command
{
use ConfirmableTrait;
/**
* 命令行的名称及用法。
*
* @var string
*/
protected $signature = 'chemsrc:job
{--action_type= : 任务操作类型}
{--params= : 任务参数}';
/**
* 命令行的概述。
*
* @var string
*/
protected $description = '化源网任务命令行';
/**
* 创建新的命令实例。
*
* @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(ChemSrcService::class);
$service->notWuhuaCasList();
}
}
public function help()
{
$helpStr = "参数帮助说明";
$this->comment($this->setHelp($helpStr)->getProcessedHelp());
$this->line("action_type: 具体动作参数\n\n initProductToExcel => 初始化批量商品 \n batchUpdateProduct => 批量更新商品\n\nparams : 操作需要传入的参数.非必填项");
}
}
......@@ -79,7 +79,8 @@ class AppServiceProvider extends ServiceProvider
\App\Console\Commands\TestJobCommand::class,
\App\Console\Commands\RhawnToolsJobCommand::class,
\App\Console\Commands\InvoiceJobCommand::class,
\App\Console\Commands\WuxiLabNewJobCommand::class
\App\Console\Commands\WuxiLabNewJobCommand::class,
\App\Console\Commands\ChemSrcCommand::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 ChemSrcApiService extends PlatformAbstractService
{
use HttpClientHelpers;
public function __construct(ThirdApiPlatformRepository $thirdApiPlatformRepository)
{
parent::__construct($thirdApiPlatformRepository);
$this->setPlatformName('chemsrc');
$this->getPlatformInfo();
}
public function getCasWebInfo($cas)
{
try {
if ($this->checkPlatformStatus()) {
$cas = urlencode(urlencode(iconv("gbk", "UTF-8", $cas)));
return $this->clientRequest('get', $this->platformInfo['platform_url'] . '/searchResult/'.$cas.'/', []);
}
}catch(\Exception $e){
$this->requestError($e);
}
}
protected function requestError($response)
{
SimpleLogs::writeLog($response->getMessage(), 'WuxiApi error', 'error');
throw $response;
}
}
<?php
namespace App\Services\ThirdPlatform;
use App\Repositories\Contracts\ThirdApiPlatformRepository;
use App\Rhawn\Services\RhawnProductService;
use App\Services\ThirdPlatform\Api\ChemSrcApiService;
use App\Support\Facades\SimpleLogs;
use App\Support\Traits\Helpers;
use Illuminate\Support\Facades\DB;
class ChemSrcService
{
use Helpers;
public function __construct()
{
$this->apiService = (new ChemSrcApiService(app(ThirdApiPlatformRepository::class)));
$this->rhawnChemicalsService = app(RhawnProductService::class);
}
public function batchUpdateCas($updateData)
{
if(empty($updateData['cas'])){
return false;
}
$cas = $updateData['cas'];
$molId = $updateData['mol_id'];
try{
$responseHtml = $this->apiService->getCasWebInfo($cas);
if($responseHtml){
$wuhuaList = [];
$wuhuaDivRegx = '/id=[\\\'|\"]wuHuaDiv[\\\'|\"]>(?<wuhuaHtml>[\s\S]+?)<div\s{0,}class=[\\\'|\"]cata-div row[\\\'|\"]/i';
preg_match($wuhuaDivRegx, $responseHtml,$wuhuaHtml);
if(!empty($wuhuaHtml)) {
$wuhuaDetailRegx = '/<th[\s\S]*?>(?<title>[\s\S]*?)<\/th>[\s\S]*?<td[\s\S]*?>(?<content>[\s\S]*?)<\/td>/i';
preg_match_all($wuhuaDetailRegx, $wuhuaHtml['wuhuaHtml'],$wuhuaDetailList);
if(!empty($wuhuaDetailList)){
foreach($wuhuaDetailList['title'] as $key => $title){
$wuhuaList[$title] = str_replace( " ", "", str_replace( "\r\n", "", $wuhuaDetailList['content'][$key]));
}
}
}
if(!empty($wuhuaList)){
$baikeMysql = DB::connection('baike_mysql');
$baikeMysql->table('tp_mol_wuhua')
->updateOrInsert([
'cas' => $cas
], [
'mol_id' => $molId,
'content' => json_encode([$wuhuaList], JSON_UNESCAPED_UNICODE)
]);
}
return true;
}
}catch(\Throwable $exception){
SimpleLogs::writeLog($exception->getMessage(), __CLASS__.':batchUpdateCas', 'error');
}
}
public function notWuhuaCasList()
{
try{
$status = $this->apiService->checkPlatformStatus();
if($status){
$limit = 100;
$page = 0;
ini_set('memory_limit','3072M');
while(true) {
$baikeMysql = DB::connection('baike_mysql');
$casList = $baikeMysql->table('tp_mol_data')
->offset($page * $limit)
->limit($limit)
->get()->toArray();
$waitSearchCasList = array_unique(array_column($casList, 'cas'));
$waitSearchCasListSort = array_column($casList, null, 'cas');
$wuhuaList = $baikeMysql->table('tp_mol_wuhua')
->whereIn('cas', $waitSearchCasList)
->get()->toArray();
$wuhuaCasList = array_unique(array_column($wuhuaList, 'cas'));
$waitGetCasWuhuaList = array_diff($waitSearchCasList, $wuhuaCasList);
if(!empty($waitGetCasWuhuaList)) {
foreach ($waitGetCasWuhuaList as $cas) {
if (!empty($waitSearchCasListSort[$cas])) {
$this->apiService->pushQueue([
'params' => ['cas' => $cas, 'mol_id' => $waitSearchCasListSort[$cas]->id],
'consumer' => __CLASS__,
'method' => 'batchUpdateCas'
], 'chemsrc_wuhua_crawl');
}
}
}
$page ++;
}
}
}catch(\Throwable $exception){
SimpleLogs::writeLog($exception->getMessage(), __CLASS__.':notWuhuaCasList', 'error');
}
}
}
......@@ -164,4 +164,12 @@ trait Helpers
return $mixed;
}
function stripHtml($str): string
{
$str = str_replace("<br>", "", $str);
//$str = htmlspecialchars($str);
$str = htmlspecialchars_decode($str);
return strip_tags($str);
}
}
......@@ -86,6 +86,17 @@ class ThridApiPlatformSeeder extends Seeder
'platform_status' => '1',
'platform_token' => '',
],
[
'platform_name' => 'chemsrc',
'platform_title' => '化源网',
'platform_url' => 'https://www.chemsrc.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