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

79 lines
1.9 KiB
PHP

<?php
namespace App\Admin\Forms;
use App\Models\{User, Vip};
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;
use Throwable;
class UserEditVip 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.edit_vip');
}
/**
* Handle the form request.
*
* @param array $input
*
* @return mixed
*/
public function handle(array $input)
{
$id = $this->payload['id'] ?? 0;
$user = User::findOrFail($id);
$vip_id = $input['vip'];
try {
DB::beginTransaction();
if ($user->userVip) {
if ($vip_id) {
$user->userVip->update([
'vip_id' => $vip_id
]);
} else {
$user->userVip->delete();
}
} else {
if ($vip_id) {
$user->userVip()->create([
'vip_id' => $vip_id
]);
}
}
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);
$vips = Vip::orderBy('sort')->get();
$this->text('user_vip', '当前代理')->value(data_get($user, 'userVip.vip.name', ''))->disable();
$this->select('vip')->options($vips->pluck('name', 'id'));
}
}