add api token modify table
This commit is contained in:
parent
ebb87f09df
commit
4284066120
@ -3,9 +3,9 @@ APP_ENV=local
|
||||
APP_KEY=
|
||||
APP_DEBUG=true
|
||||
APP_URL=http://localhost
|
||||
API_TOKEN=
|
||||
NOTICE_URL=
|
||||
GOODS_URL=
|
||||
APP_ID=
|
||||
APP_KEYX=
|
||||
API_DOMAIN=
|
||||
LOG_CHANNEL=stack
|
||||
LOG_DEPRECATIONS_CHANNEL=null
|
||||
LOG_LEVEL=debug
|
||||
|
@ -7,6 +7,7 @@
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Contracts\Console\Isolatable;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class HandleNotice extends Command implements Isolatable
|
||||
{
|
||||
@ -44,10 +45,7 @@ public function handle(): int
|
||||
{
|
||||
$chunk = $this->argument('chunk');
|
||||
$this->line('开始消息队列');
|
||||
$url = env('NOTICE_URL');
|
||||
$res = apiRequest::request($url, [
|
||||
'token' => env('API_TOKEN'),
|
||||
]);
|
||||
$res = apiRequest::api('/Notic/getNotics', []);
|
||||
if (!$res['result'] || $res['errCode'] != '0000') {
|
||||
$this->error('request err: ' . $res['desc'] ?? '');
|
||||
return 1;
|
||||
@ -92,6 +90,7 @@ public function handle(): int
|
||||
}
|
||||
foreach ($queue as $type => $item) {
|
||||
\App\Jobs\notice::dispatch($type, $item);
|
||||
Log::info('添加 type' . $type . '的队列', $item);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
@ -3,7 +3,6 @@
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\helpers\apiRequest;
|
||||
use App\helpers\FormType;
|
||||
use App\Models\Good;
|
||||
use App\Models\GoodsItem;
|
||||
use Exception;
|
||||
@ -15,6 +14,7 @@
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class notice implements ShouldQueue
|
||||
@ -28,7 +28,7 @@ class notice implements ShouldQueue
|
||||
*/
|
||||
public function __construct(public int $type, public mixed $data)
|
||||
{
|
||||
$this->goodsUrl = env('GOODS_URL');
|
||||
$this->goodsUrl = '/Product/getGoodInfo';
|
||||
}
|
||||
|
||||
public string $goodsUrl;
|
||||
@ -40,6 +40,7 @@ public function __construct(public int $type, public mixed $data)
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
Log::info('开始执行type' . $this->type . ' 的队列');
|
||||
switch ($this->type) {
|
||||
case 1:
|
||||
$this->addGoods();
|
||||
@ -54,6 +55,7 @@ public function handle(): void
|
||||
$this->changePrice();
|
||||
break;
|
||||
}
|
||||
Log::info('执行完成');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,19 +71,24 @@ public function addGoods(): void
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$good = new Good();
|
||||
$goodsItem = new GoodsItem();
|
||||
$goodsIds = array_column(array_values($this->data), 'itemId');
|
||||
|
||||
$goodsIds = array_map(fn($item) => $item['result']['itemId'], $this->data);
|
||||
$existedIds = Good::query()->whereIn('itemid', $goodsIds)->select('itemid')->get()->toArray();
|
||||
$newIds = array_diff($goodsIds, $existedIds);
|
||||
$newIds = array_unique($newIds);
|
||||
if (!$newIds) {
|
||||
Log::info('没有新的商品添加');
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($newIds as $id) {
|
||||
$item = $this->data[$id];
|
||||
$product = apiRequest::requests($this->client, $this->goodsUrl, [
|
||||
$newIdss = array_flip($newIds);
|
||||
foreach ($this->data as $item) {
|
||||
if (!isset($newIdss[$item['result']['itemId']])) {
|
||||
continue;
|
||||
}
|
||||
$product = apiRequest::apis($this->goodsUrl, $this->client, [
|
||||
'token' => $apiToken,
|
||||
'itemId' => $item['result']['itemId'],
|
||||
], FormType::json);
|
||||
]);
|
||||
if ($product['errCode'] != '0000') {
|
||||
throw new Exception('request item ' . $item['item_id'] . ' err:' . $product['desc'] ?? '');
|
||||
}
|
||||
@ -110,6 +117,7 @@ public function addGoods(): void
|
||||
} catch (Throwable $throwable) {
|
||||
\App\Models\Notice::query()->whereIn('notice_id', $noticeIds)
|
||||
->update(['err_message' => $throwable->getMessage()]);
|
||||
Log::error('添加商品失败', [$throwable->getMessage(), $throwable->getFile(), $throwable->getTrace()]);
|
||||
throw $throwable;
|
||||
}
|
||||
}
|
||||
|
@ -5,11 +5,12 @@
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class apiRequest
|
||||
{
|
||||
|
||||
public static Client|null $client=null;
|
||||
public static Client|null $client = null;
|
||||
|
||||
public static function getClient(): Client
|
||||
{
|
||||
@ -22,9 +23,9 @@ public static function getClient(): Client
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public static function request(string $url, mixed $param=[], FormType $type = FormType::urlencoded, bool $isJson = true, int $timeout = 5, array $extern = []):mixed
|
||||
public static function request(string $url, mixed $param = [], FormType $type = FormType::urlencoded, bool $isJson = true, int $timeout = 5, array $extern = []): mixed
|
||||
{
|
||||
return self::requests(self::getClient(),$url,$param,$type,$isJson,$timeout,$extern);
|
||||
return self::requests(self::getClient(), $url, $param, $type, $isJson, $timeout, $extern);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -40,7 +41,7 @@ public static function request(string $url, mixed $param=[], FormType $type = Fo
|
||||
* @throws GuzzleException
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function requests(Client $client,string $url, mixed $param=[], FormType $type = FormType::urlencoded, bool $isJson = true, int $timeout = 5, array $extern = []): mixed
|
||||
public static function requests(Client $client, string $url, mixed $param = [], FormType $type = FormType::urlencoded, bool $isJson = true, int $timeout = 5, array $extern = []): mixed
|
||||
{
|
||||
$params = [
|
||||
'timeout' => $timeout
|
||||
@ -78,4 +79,55 @@ public static function requests(Client $client,string $url, mixed $param=[], For
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function GetToken(): string
|
||||
{
|
||||
$token = Cache::get('token');
|
||||
if ($token) {
|
||||
return $token;
|
||||
}
|
||||
$url = rtrim(env('API_DOMAIN'), '/') . '/Index/getToken';
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$appId = env('APP_ID');
|
||||
$appKey = env('APP_KEYX');
|
||||
$sign = sprintf("app_id=%s:app_key=%s:tamptimes=%s", $appId, $appKey, $now);
|
||||
$sign = strtoupper(md5($sign));
|
||||
$form = [
|
||||
'app_id' => $appId,
|
||||
'app_key' => $appKey,
|
||||
'tamptimes' => $now,
|
||||
'sign' => $sign
|
||||
];
|
||||
$res = self::request($url, $form, FormType::json);
|
||||
if (!$res['data']['token']) {
|
||||
throw new Exception('获取token失败:' . $res['desc'] ?? '');
|
||||
}
|
||||
$token = $res['data']['token'];
|
||||
Cache::set('token', $token, strtotime($res['data']['deadline']) - time());
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public static function api($path, mixed $param = [], int $timeout = 5, array $extern = []): mixed
|
||||
{
|
||||
|
||||
return self::apis($path, self::getClient(), $param, $timeout, $extern);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public static function apis($path, Client $client, mixed $param = [], int $timeout = 5, array $extern = []): mixed
|
||||
{
|
||||
$url = rtrim(env('API_DOMAIN'), '/') . $path;
|
||||
$token = self::GetToken();
|
||||
$param['token'] = $token;
|
||||
return self::requests($client, $url, $param, FormType::json, true, $timeout, $extern);
|
||||
}
|
||||
}
|
||||
|
@ -70,7 +70,7 @@
|
||||
|
|
||||
*/
|
||||
|
||||
'timezone' => 'UTC',
|
||||
'timezone' => 'Asia/Shanghai',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
@ -22,10 +22,10 @@ public function up()
|
||||
$table->string('specifications')->comment('规格');
|
||||
$table->string('shop_url')->comment('链接');
|
||||
$table->string('product_images')->default('')->comment('商品主图');
|
||||
$table->string('images')->default('')->comment('主图列表');
|
||||
$table->text('images')->comment('主图列表');
|
||||
$table->text('product_infos')->comment('图文信息');
|
||||
$table->text('app_introduce')->comment('app图文信息');
|
||||
$table->string('applet_introduce')->default('详情图片地址');
|
||||
$table->text('applet_introduce')->comment('详情图片地址');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user