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

76 lines
2.0 KiB
PHP

<?php
namespace App\Admin\Forms;
use App\Enums\PointLogAction;
use App\Models\User;
use App\Services\PointService;
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 PointChange extends Form implements LazyRenderable
{
use LazyWidget;
protected function authorize($user): bool
{
return $user->can('dcat.admin.users.change_points');
}
public function handle(array $input)
{
$changePoints = (int) $input['change_points'];
if ($changePoints < 1) {
return $this->response()->error('积分必须大于1');
}
$action = PointLogAction::from($input['action']);
if ($action === PointLogAction::Deduction) {
$changePoints = -$changePoints;
}
$user = User::findOrFail($this->payload['id']);
try {
DB::beginTransaction();
(new PointService())->change($user, $changePoints, $action, $input['remark'], null, Admin::user());
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->radio('action', '操作')
->options([
PointLogAction::Recharge->value => '充值',
PointLogAction::Deduction->value => '扣减',
])
->default(PointLogAction::Recharge->value)
->required();
$this->number('change_points', '积分')->min(1)->required();
$this->textarea('remark', '备注')->required();
$this->confirm('是否确认变更?', '提交后该动作无法逆转');
}
}