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