From 66ec472a732bce1b9ae50b2a5a108e7f2a1bff10 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Fri, 24 Dec 2021 14:22:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=88=91=E7=9A=84=E9=93=B6?= =?UTF-8?q?=E8=A1=8C=E5=8D=A1=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Http/Controllers/UserBankController.php | 49 +++++++++++++++++++ .../Api/Http/Resources/UserBankResource.php | 25 ++++++++++ app/Endpoint/Api/routes.php | 5 ++ app/Models/User.php | 8 +++ app/Models/UserBank.php | 27 ++++++++++ ...1_12_24_131826_create_user_banks_table.php | 38 ++++++++++++++ 6 files changed, 152 insertions(+) create mode 100644 app/Endpoint/Api/Http/Controllers/UserBankController.php create mode 100644 app/Endpoint/Api/Http/Resources/UserBankResource.php create mode 100644 app/Models/UserBank.php create mode 100644 database/migrations/2021_12_24_131826_create_user_banks_table.php diff --git a/app/Endpoint/Api/Http/Controllers/UserBankController.php b/app/Endpoint/Api/Http/Controllers/UserBankController.php new file mode 100644 index 00000000..3b711c4c --- /dev/null +++ b/app/Endpoint/Api/Http/Controllers/UserBankController.php @@ -0,0 +1,49 @@ +user()->bank; + return $bank ? UserBankResource::make($bank) : response()->json([]); + } + + public function update(Request $request) + { + $input = $request->validate([ + 'real_name' => ['bail', 'required', 'string', 'max:50'], + 'bank_number' => ['bail', 'required', 'string', 'max:30'], + 'bank_name' => ['bail', 'required', 'string', 'max:100'], + 'bank_description' => ['bail', 'required', 'string', 'max:200'], + ]); + + $bank = $request->user()->bank; + + if ($bank && $bank->real_name != $input['real_name']) { + $input['is_edited'] = 1; + $input['old_real_name'] = $bank->real_name; + } + + if ($bank?->is_edited) { + unset($input['real_name']); + } + + $bank = UserBank::updateOrCreate([ + 'user_id' => $request->user()->id, + ], $input); + + return UserBankResource::make($bank); + } +} diff --git a/app/Endpoint/Api/Http/Resources/UserBankResource.php b/app/Endpoint/Api/Http/Resources/UserBankResource.php new file mode 100644 index 00000000..0cdef986 --- /dev/null +++ b/app/Endpoint/Api/Http/Resources/UserBankResource.php @@ -0,0 +1,25 @@ + (string) $this->real_name, + 'bank_number' => (string) $this->bank_number, + 'bank_name' => (string) $this->bank_name, + 'bank_description' => (string) $this->bank_description, + 'is_edited' => (bool) $this->is_edited, + ]; + } +} diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index 5cba2301..99855250 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -28,6 +28,7 @@ use App\Endpoint\Api\Http\Controllers\ShareBgController; use App\Endpoint\Api\Http\Controllers\ShippingAddressController; use App\Endpoint\Api\Http\Controllers\ShoppingCartItemController; use App\Endpoint\Api\Http\Controllers\SmsCodeController; +use App\Endpoint\Api\Http\Controllers\UserBankController; use App\Endpoint\Api\Http\Controllers\UserCouponController; use App\Endpoint\Api\Http\Controllers\ZoneController; use Illuminate\Support\Facades\Route; @@ -82,6 +83,10 @@ Route::group([ // 修改密码 Route::post('change-password', ChangePasswordController::class); + //银行卡 + Route::get('user-bank', [UserBankController::class, 'show']); + Route::put('user-bank', [UserBankController::class, 'update']); + // 收藏商品 Route::post('product/products/{product}/collect', [ProductSkuController::class, 'collect']); // 取消商品收藏 diff --git a/app/Models/User.php b/app/Models/User.php index 830627f3..cbc8fdca 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -172,6 +172,14 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac return $this->hasMany(ClickLog::class); } + /** + * 用户的银行卡 + */ + public function bank() + { + return $this->hasOne(UserBank::class, 'user_id'); + } + /** * 禁用用户 * diff --git a/app/Models/UserBank.php b/app/Models/UserBank.php new file mode 100644 index 00000000..631d9817 --- /dev/null +++ b/app/Models/UserBank.php @@ -0,0 +1,27 @@ +'bool', + ]; + + protected $fillable = [ + 'user_id', + 'real_name', + 'bank_number', + 'bank_name', + 'bank_description', + 'is_edited', + 'old_real_name', + ]; +} diff --git a/database/migrations/2021_12_24_131826_create_user_banks_table.php b/database/migrations/2021_12_24_131826_create_user_banks_table.php new file mode 100644 index 00000000..0611367c --- /dev/null +++ b/database/migrations/2021_12_24_131826_create_user_banks_table.php @@ -0,0 +1,38 @@ +id(); + $table->unsignedBigInteger('user_id')->unique()->comment('用户ID'); + $table->string('real_name')->comment('真实姓名'); + $table->string('bank_number')->comment('银行卡号'); + $table->string('bank_name')->comment('银行名称'); + $table->string('bank_description')->comment('开户行'); + $table->unsignedTinyInteger('is_edited')->default(0)->comment('是否修改过姓名'); + $table->string('old_real_name')->nullable()->comment('原真实姓名'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('user_banks'); + } +}