From 6db222628675254bec2f33cd5c6a7426b9430ed2 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Fri, 24 Dec 2021 16:02:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=89=E5=85=A8=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Account/WalletPasswordController.php | 38 +++++++++++++++++++ app/Endpoint/Api/routes.php | 3 ++ app/Models/User.php | 8 ++++ app/Models/Wallet.php | 30 +++++++++++++++ ...4_144345_add_password_to_wallets_table.php | 34 +++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 app/Endpoint/Api/Http/Controllers/Account/WalletPasswordController.php create mode 100644 database/migrations/2021_12_24_144345_add_password_to_wallets_table.php diff --git a/app/Endpoint/Api/Http/Controllers/Account/WalletPasswordController.php b/app/Endpoint/Api/Http/Controllers/Account/WalletPasswordController.php new file mode 100644 index 00000000..31443222 --- /dev/null +++ b/app/Endpoint/Api/Http/Controllers/Account/WalletPasswordController.php @@ -0,0 +1,38 @@ +validate([ + 'old_password' => ['bail', 'filled', 'string', 'min:6', 'max:6'], + 'new_password' => ['bail', 'required', 'string', 'min:6', 'max:6'], + ]); + + $wallet = $request->user()->wallet; + + //如果已经设置安全密码则比对 + if ($wallet && $wallet->password && (!$wallet->verifyPassword(Arr::get($input, 'old_password', '')))) { + throw new BizException('安全密码不正确'); + } + + Wallet::updateOrCreate( + ['user_id'=> $request->user()->id], + ['password' => $input['new_password']], + ); + + return response()->noContent(); + } +} diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index 7308d16b..eccd4561 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -2,6 +2,7 @@ use App\Endpoint\Api\Http\Controllers\Account\ChangePasswordController; use App\Endpoint\Api\Http\Controllers\Account\UserController; +use App\Endpoint\Api\Http\Controllers\Account\WalletPasswordController; use App\Endpoint\Api\Http\Controllers\AdController; use App\Endpoint\Api\Http\Controllers\AfterSaleController; use App\Endpoint\Api\Http\Controllers\AliOssController; @@ -82,6 +83,8 @@ Route::group([ Route::put('me', [UserController::class, 'update']); // 修改密码 Route::post('change-password', ChangePasswordController::class); + //安全密码 + Route::post('wallet-password/reset', [WalletPasswordController::class, 'reset']);//重置或设置安全密码 //银行卡 Route::get('user-bank', [UserBankController::class, 'show']); diff --git a/app/Models/User.php b/app/Models/User.php index cbc8fdca..712fb473 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -180,6 +180,14 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac return $this->hasOne(UserBank::class, 'user_id'); } + /** + * 用户的资产 + */ + public function wallet() + { + return $this->hasOne(Wallet::class, 'user_id'); + } + /** * 禁用用户 * diff --git a/app/Models/Wallet.php b/app/Models/Wallet.php index 9d897dee..fda2f692 100644 --- a/app/Models/Wallet.php +++ b/app/Models/Wallet.php @@ -27,10 +27,12 @@ class Wallet extends Model * @var array */ protected $fillable = [ + 'user_id', 'balance', 'total_expenses', 'total_revenue', 'withdrawable', + 'password', //安全密码 ]; /** @@ -39,4 +41,32 @@ class Wallet extends Model protected $casts = [ 'withdrawable' => 'bool', ]; + + /** + * 设置此用户的安全密码 + * + * @param string $value + * @return void + */ + public function setPasswordAttribute($value): void + { + if ((string) $value === '') { + $value = null; + } else { + $value = md5($value); + } + + $this->attributes['password'] = $value; + } + + /** + * 确认给定的密码是否正确 + * + * @param string $password + * @return bool + */ + public function verifyPassword(string $password): bool + { + return $this->password && md5($password) === $this->password; + } } diff --git a/database/migrations/2021_12_24_144345_add_password_to_wallets_table.php b/database/migrations/2021_12_24_144345_add_password_to_wallets_table.php new file mode 100644 index 00000000..1d27e0bc --- /dev/null +++ b/database/migrations/2021_12_24_144345_add_password_to_wallets_table.php @@ -0,0 +1,34 @@ +string('password')->nullable()->comment('密码'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('wallets', function (Blueprint $table) { + // + $table->dropColumn('password'); + }); + } +}