自动更新汇率, 并显示在后台汇率列表

This commit is contained in:
nilsir 2023-02-22 11:04:28 +08:00 committed by Edward Yang
parent bb85406c62
commit b4586bd452
16 changed files with 86 additions and 0 deletions

View File

@ -60,3 +60,6 @@ MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
# 前端热更新, PROXY: vhost or path, HOST: 多设备实时
MIX_PROXY=beikeshop.test
MIX_HOST=192.168.0.122
# 自动获取最新汇率API KEY
RATE_KEY=

View File

@ -28,6 +28,7 @@ use Beike\Console\Commands\GenerateDatabaseDict;
use Beike\Console\Commands\GenerateSitemap;
use Beike\Console\Commands\MakeRootAdminUser;
use Beike\Console\Commands\MigrateFromOpenCart;
use Beike\Console\Commands\FetchCurrencyRate;
use Beike\Models\AdminUser;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
@ -90,6 +91,7 @@ class AdminServiceProvider extends ServiceProvider
GenerateSitemap::class,
MakeRootAdminUser::class,
MigrateFromOpenCart::class,
FetchCurrencyRate::class,
]);
}
}

View File

@ -16,4 +16,5 @@ return [
'admin_name' => env('ADMIN_NAME'),
'force_url_https' => env('APP_FORCE_HTTPS', false),
'rate_key' => env('RATE_KEY')
];

View File

@ -0,0 +1,37 @@
<?php
namespace Beike\Console\Commands;
use Beike\Services\CurrencyService;
use Illuminate\Console\Command;
class FetchCurrencyRate extends Command
{
protected $signature = 'fetch:currency-rate';
protected $description = '更新货币汇率';
public function handle()
{
$today = date('Y-m-d');
$this->info(sprintf('获取 %s 汇率数据开始', $today));
try {
$tableRows = [];
$data = CurrencyService::getInstance()->getRatesFromApi($today);
foreach ($data as $key => $val) {
$tableRows[] = [$key, $val];
}
$this->table(['货币', '汇率'], $tableRows);
$this->info(sprintf('获取 %s 汇率数据完成', $today));
}catch (\Exception) {
$this->error(sprintf('获取 %s 汇率数据失败', $today));
}
}
}

View File

@ -11,6 +11,7 @@
namespace Beike\Models;
use Beike\Services\CurrencyService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Currency extends Base
@ -18,4 +19,20 @@ class Currency extends Base
use HasFactory;
protected $fillable = ['name', 'code', 'symbol_left', 'symbol_right', 'decimal_place', 'value', 'status'];
protected $appends = ['latest_value'];
public function getLatestValueAttribute()
{
try {
$today = date('Y-m-d');
$data = CurrencyService::getInstance()->getRatesFromApi($today);
return $data[$this->code] ?? $this->value;
} catch (\Exception) {
return $this->value;
}
}
}

View File

@ -12,6 +12,7 @@
namespace Beike\Services;
use Beike\Repositories\CurrencyRepo;
use Illuminate\Support\Facades\Http;
class CurrencyService
{
@ -91,4 +92,18 @@ class CurrencyService
return $value * ($to / $from);
}
public function getRatesFromApi(string $date)
{
$cacheKey = 'currency:rates:' . $date;
if ($rates = cache()->get($cacheKey)) {
return $rates;
}
$data = Http::get(sprintf('https://v6.exchangerate-api.com/v6/%s/latest/USD', config('beike.rate_key')))
->json();
$rates = $data['conversion_rates'] ?? [];
cache()->set($cacheKey, $rates);
return $rates;
}
}

View File

@ -19,6 +19,7 @@
<th>{{ __('currency.symbol_right') }}</th>
<th>{{ __('currency.decimal_place') }}</th>
<th>{{ __('currency.value') }}</th>
<th>{{ __('currency.latest_value') }}</th>
<th>{{ __('common.status') }}</th>
<th class="text-end">{{ __('common.action') }}</th>
</tr>
@ -32,6 +33,7 @@
<td>@{{ language.symbol_right }}</td>
<td>@{{ language.decimal_place }}</td>
<td>@{{ language.value }}</td>
<td>@{{ language.latest_value }}</td>
<td>
<span v-if="language.status" class="text-success">{{ __('common.enable') }}</span>
<span v-else class="text-secondary">{{ __('common.disable') }}</span>

View File

@ -19,4 +19,5 @@ return [
'symbol_right' => 'rechtes Symbol',
'value' => 'Wechselkurswert',
'decimal_place' => 'Nachkommastellen',
'latest_value' => 'letzten Wechselkurswert',
];

View File

@ -18,4 +18,5 @@ return [
'symbol_right' => 'Symbol Right',
'value' => 'Value',
'decimal_place' => 'Decimal Place',
'latest_value' => 'Latest Value',
];

View File

@ -19,4 +19,5 @@ return [
'symbol_right' => 'signo correcto',
'value' => 'valor del tipo de cambio',
'decimal_place' => 'lugares decimales',
'latest_value' => 'último valor del tipo de cambio',
];

View File

@ -19,4 +19,5 @@ return [
'symbol_right' => 'bon signe',
'value' => 'valeur du taux de change',
'decimal_place' => 'décimales',
'latest_value' => 'dernière valeur du taux de change',
];

View File

@ -19,4 +19,5 @@ return [
'symbol_right' => 'segno giusto',
'value' => 'valore del tasso di cambio',
'decimal_place' => 'decimali',
'latest_value' => 'ultimo valore del tasso di cambio',
];

View File

@ -19,4 +19,5 @@ return [
'symbol_right' => '右記号',
'value' => '為替レート値',
'decimal_place' => '小数位',
'latest_value' => '最新の為替レート値',
];

View File

@ -19,4 +19,5 @@ return [
'symbol_right' => 'правильный символ',
'value' => 'Стоимость обменного курса',
'decimal_place' => 'Десятичные разряды',
'latest_value' => 'последнее значение обменного курса',
];

View File

@ -19,4 +19,5 @@ return [
'symbol_right' => '右符号',
'value' => '汇率值',
'decimal_place' => '小数位数',
'latest_value' => '最新汇率值',
];

View File

@ -19,4 +19,5 @@ return [
'symbol_right' => '右符號',
'value' => '匯率值',
'decimal_place' => '小數位數',
'latest_value' => '最新匯率值',
];