添加我的银行卡接口
parent
7b6a68afff
commit
66ec472a73
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace App\Endpoint\Api\Http\Controllers;
|
||||
|
||||
use App\Endpoint\Api\Http\Resources\UserBankResource;
|
||||
use App\Models\UserBank;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UserBankController extends Controller
|
||||
{
|
||||
/**
|
||||
* 用户银行卡信息
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return void
|
||||
*/
|
||||
public function show(Request $request)
|
||||
{
|
||||
$bank = $request->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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace App\Endpoint\Api\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class UserBankResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'real_name' => (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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -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']);
|
||||
// 取消商品收藏
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用用户
|
||||
*
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class UserBank extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasDateTimeFormatter;
|
||||
|
||||
protected $casts = [
|
||||
'is_edited'=>'bool',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'real_name',
|
||||
'bank_number',
|
||||
'bank_name',
|
||||
'bank_description',
|
||||
'is_edited',
|
||||
'old_real_name',
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateUserBanksTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('user_banks', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue