6
0
Fork 0
jiqu-library-server/app/Admin/Forms/Frozen.php

104 lines
2.7 KiB
PHP

<?php
namespace App\Admin\Forms;
use App\Models\User;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;
use Throwable;
class Frozen extends Form implements LazyRenderable
{
use LazyWidget;
/**
* @param Model|Authenticatable|HasPermissions|null $user
*
* @return bool
*/
protected function authorize($user): bool
{
return $user->can('dcat.admin.users.frozen');
}
/**
* Handle the form request.
*
* @param array $input
*
* @return mixed
*/
public function handle(array $input)
{
$id = $this->payload['id'] ?? 0;
$user = User::findOrFail($id);
try {
DB::beginTransaction();
//可提冻结
if (in_array('wallet', $input['frozen'])) {
if (is_null($user->wallet)) {
$user->wallet()->create([
'is_frozen' =>1,
]);
} else {
$user->wallet()->update([
'is_frozen' => 1,
]);
}
} else {//可提解冻
$user->wallet()->update([
'is_frozen' => 0,
]);
}
//余额冻结
if (in_array('balance', $input['frozen'])) {
if (is_null($user->balance)) {
$user->balance()->create([
'is_frozen' =>1,
]);
} else {
$user->balance()->update([
'is_frozen' => 1,
]);
}
} else {//余额解冻
$user->balance()->update([
'is_frozen' => 0,
]);
}
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()
{
$id = $this->payload['id'] ?? 0;
$user = User::findOrFail($id);
$this->checkbox('frozen', '冻结')->options([
'wallet'=>'可提', 'balance'=>'余额',
])->canCheckAll()->customFormat(function () use ($user) {
$v = [];
if ($user->wallet?->is_frozen) {
$v[] = 'wallet';
}
if ($user->balance?->is_frozen) {
$v[] = 'balance';
}
return $v;
});
}
}