72 lines
2.0 KiB
PHP
72 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Models\User;
|
|
use App\Models\WalletLog;
|
|
use App\Services\WalletService;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class WalletDeduction extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
|
|
/**
|
|
* @param Model|Authenticatable|HasPermissions|null $user
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function authorize($user): bool
|
|
{
|
|
return $user->can('dcat.admin.wallet_logs.deduction');
|
|
}
|
|
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
if (($input['change_balance']??0) <=0) {
|
|
return $this->response()->error('扣减金额必须大于0');
|
|
}
|
|
try {
|
|
DB::beginTransaction();
|
|
//获取当前操作人;
|
|
$adminUser = Admin::user();
|
|
$user = User::findOrFail($input['user_id']??0);
|
|
$walletService = new WalletService();
|
|
$walletService->changeBalance($user, -($input['change_balance']??0), WalletLog::ACTION_ADMIN_DEDUCTION, '后台扣减', $adminUser);
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
return $this->response()->error('操作失败:'.$th->getMessage());
|
|
}
|
|
|
|
return $this->response()
|
|
->success(__('admin.update_succeeded'))
|
|
->refresh();
|
|
}
|
|
|
|
/**
|
|
* Build a form here.
|
|
*/
|
|
public function form()
|
|
{
|
|
$this->select('user_id', '用户手机号')->ajax(admin_route('api.users'))->required();
|
|
$this->currency('change_balance', '扣减金额')->symbol('¥')->saving(function ($value) {
|
|
return bcmul($value, 100);
|
|
})->required();
|
|
$this->confirm('是否确认扣减可提?', '提交后该动作无法逆转');
|
|
}
|
|
}
|