优化 currency 读取

This commit is contained in:
Edward Yang 2022-08-15 16:08:39 +08:00
parent edb860774d
commit a45406aec2
3 changed files with 13 additions and 20 deletions

View File

@ -401,7 +401,7 @@ function language_packages(): array
*/ */
function currencies() function currencies()
{ {
return CurrencyRepo::all()->where('status', true); return CurrencyRepo::listEnabled();
} }
/** /**

View File

@ -11,15 +11,16 @@
namespace Beike\Repositories; namespace Beike\Repositories;
use Beike\Models\Country;
use Beike\Models\Currency; use Beike\Models\Currency;
class CurrencyRepo class CurrencyRepo
{ {
private static $enabledCurrencies;
/** /**
* 创建一个currency记录 * 创建一个currency记录
* @param $data * @param $data
* @return int * @return mixed
*/ */
public static function create($data) public static function create($data)
{ {
@ -29,7 +30,8 @@ class CurrencyRepo
/** /**
* @param $id * @param $id
* @param $data * @param $data
* @return bool|int * @return mixed
* @throws \Exception
*/ */
public static function update($id, $data) public static function update($id, $data)
{ {
@ -43,7 +45,7 @@ class CurrencyRepo
/** /**
* @param $id * @param $id
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null * @return mixed
*/ */
public static function find($id) public static function find($id)
{ {
@ -67,9 +69,11 @@ class CurrencyRepo
return Currency::query()->get(); return Currency::query()->get();
} }
public static function enabled() public static function listEnabled()
{ {
return Currency::query()->where('status', true)->get(); if (self::$enabledCurrencies !== null) {
return self::$enabledCurrencies;
}
return self::$enabledCurrencies = Currency::query()->where('status', true)->get();
} }
} }

View File

@ -1,12 +1,4 @@
<?php <?php
/*
* @copyright 2022 opencart.cn - All Rights Reserved.
* @link https://www.guangdawangluo.com
* @Author Pu Shuo <pushuo@opencart.cn>
* @Date 2022-08-02 19:19:52
* @LastEditTime 2022-08-06 12:52:21
*/
/** /**
* CurrencyService.php * CurrencyService.php
* *
@ -19,9 +11,6 @@
namespace Beike\Services; namespace Beike\Services;
use Beike\Models\Address;
use Beike\Models\TaxRate;
use Beike\Models\TaxRule;
use Beike\Repositories\CurrencyRepo; use Beike\Repositories\CurrencyRepo;
class CurrencyService class CurrencyService
@ -30,7 +19,7 @@ class CurrencyService
private $currencies = array(); private $currencies = array();
public function __construct() { public function __construct() {
foreach (CurrencyRepo::enabled() as $result) { foreach (CurrencyRepo::listEnabled() as $result) {
$this->currencies[$result->code] = $result; $this->currencies[$result->code] = $result;
} }
} }