From 88f00f21c413e511de969da7e7343c9eae828268 Mon Sep 17 00:00:00 2001 From: TL Date: Wed, 31 Aug 2022 17:16:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=94=AE=E5=90=8E=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Http/Controllers/RmaReasonController.php | 4 ++- .../Admin/Http/Resources/RmaReasonDetail.php | 26 +++++++++++++++++++ beike/Repositories/RmaReasonRepo.php | 7 +++-- .../Controllers/Account/RmaController.php | 3 ++- .../Resources/Account/RmaReasonDetail.php | 26 +++++++++++++++++++ .../2022_07_29_021003_return_table.php | 6 ++--- 6 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 beike/Admin/Http/Resources/RmaReasonDetail.php create mode 100644 beike/Shop/Http/Resources/Account/RmaReasonDetail.php diff --git a/beike/Admin/Http/Controllers/RmaReasonController.php b/beike/Admin/Http/Controllers/RmaReasonController.php index 6415ef6f..f932b277 100644 --- a/beike/Admin/Http/Controllers/RmaReasonController.php +++ b/beike/Admin/Http/Controllers/RmaReasonController.php @@ -11,6 +11,7 @@ namespace Beike\Admin\Http\Controllers; +use Beike\Admin\Http\Resources\RmaReasonDetail; use Beike\Repositories\RmaReasonRepo; use Beike\Repositories\LanguageRepo; use Exception; @@ -21,9 +22,10 @@ class RmaReasonController extends Controller public function index(Request $request) { $rmaReasons = RmaReasonRepo::list($request->only('name')); + $data = [ 'languages' => LanguageRepo::all(), - 'rmaReasons' => $rmaReasons, + 'rmaReasons' => RmaReasonDetail::collection($rmaReasons)->jsonSerialize(), ]; return view('admin::pages.rma_reasons.index', $data); diff --git a/beike/Admin/Http/Resources/RmaReasonDetail.php b/beike/Admin/Http/Resources/RmaReasonDetail.php new file mode 100644 index 00000000..5ac34939 --- /dev/null +++ b/beike/Admin/Http/Resources/RmaReasonDetail.php @@ -0,0 +1,26 @@ + + * @created 2022-08-31 11:56:28 + * @modified 2022-08-31 11:56:28 + */ + +namespace Beike\Admin\Http\Resources; + +use Beike\Repositories\RmaRepo; +use Illuminate\Http\Resources\Json\JsonResource; + +class RmaReasonDetail extends JsonResource +{ + public function toArray($request): array + { + return [ + 'id' => $this->id, + 'name' => json_decode($this->name, true)[locale()] ?? '', + ]; + } +} diff --git a/beike/Repositories/RmaReasonRepo.php b/beike/Repositories/RmaReasonRepo.php index 0c9d0cbd..d4de573b 100644 --- a/beike/Repositories/RmaReasonRepo.php +++ b/beike/Repositories/RmaReasonRepo.php @@ -29,6 +29,7 @@ class RmaReasonRepo */ public static function create($data) { + $data['name'] = json_encode($data['name']); $item = RmaReason::query()->create($data); return $item; } @@ -47,6 +48,7 @@ class RmaReasonRepo if (!$reason) { throw new Exception("退换货原因id $reason 不存在"); } + $data['name'] = json_encode($data['name']); $reason->update($data); return $reason; } @@ -78,10 +80,11 @@ class RmaReasonRepo */ public static function list(array $data = []) { - $builder = RmaReason::query()->where('locale', locale()); + $builder = RmaReason::query(); if (isset($data['name'])) { - $builder->where('name', 'like', "%{$data['name']}%"); + $locale = locale(); + $builder->whereJsonContains("name->$locale", 'like', "%{$data['name']}%"); } return $builder->get(); diff --git a/beike/Shop/Http/Controllers/Account/RmaController.php b/beike/Shop/Http/Controllers/Account/RmaController.php index 3eed43f9..1905b997 100644 --- a/beike/Shop/Http/Controllers/Account/RmaController.php +++ b/beike/Shop/Http/Controllers/Account/RmaController.php @@ -12,6 +12,7 @@ namespace Beike\Shop\Http\Controllers\Account; use App\Http\Controllers\Controller; +use Beike\Admin\Http\Resources\RmaReasonDetail; use Beike\Repositories\RmaReasonRepo; use Beike\Repositories\RmaRepo; use Beike\Shop\Http\Requests\RmaRequest; @@ -54,7 +55,7 @@ class RmaController extends Controller $data = [ 'orderProduct' => OrderProductRepo::find($orderProductId), 'statuses' => RmaRepo::getStatuses(), - 'reasons' => RmaReasonRepo::list()->toArray(), + 'reasons' => RmaReasonDetail::collection(RmaReasonRepo::list())->jsonSerialize(), 'types' => RmaRepo::getTypes(), ]; diff --git a/beike/Shop/Http/Resources/Account/RmaReasonDetail.php b/beike/Shop/Http/Resources/Account/RmaReasonDetail.php new file mode 100644 index 00000000..5ac34939 --- /dev/null +++ b/beike/Shop/Http/Resources/Account/RmaReasonDetail.php @@ -0,0 +1,26 @@ + + * @created 2022-08-31 11:56:28 + * @modified 2022-08-31 11:56:28 + */ + +namespace Beike\Admin\Http\Resources; + +use Beike\Repositories\RmaRepo; +use Illuminate\Http\Resources\Json\JsonResource; + +class RmaReasonDetail extends JsonResource +{ + public function toArray($request): array + { + return [ + 'id' => $this->id, + 'name' => json_decode($this->name, true)[locale()] ?? '', + ]; + } +} diff --git a/database/migrations/2022_07_29_021003_return_table.php b/database/migrations/2022_07_29_021003_return_table.php index 5928f061..bca46bc0 100644 --- a/database/migrations/2022_07_29_021003_return_table.php +++ b/database/migrations/2022_07_29_021003_return_table.php @@ -40,9 +40,9 @@ class ReturnTable extends Migration $table->timestamps(); }); Schema::create('rma_reasons', function (Blueprint $table) { - $table->bigInteger('id'); - $table->string('locale'); - $table->string('name'); + $table->id(); + $table->json('name'); // 值示例: {"en":"cannot to use","zh_cn":"无法使用"} + $table->timestamps(); }); }