78 lines
2.1 KiB
PHP
78 lines
2.1 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 < 0) {
|
|
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->currency('change_points', '积分')->symbol('¥')->saving(function ($value) {
|
|
return bcmul($value, 100);
|
|
})->required();
|
|
$this->textarea('remark', '备注')->required();
|
|
$this->confirm('是否确认变更?', '提交后该动作无法逆转');
|
|
}
|
|
}
|