70 lines
1.9 KiB
PHP
70 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Models\QuotaV1Log;
|
|
use App\Models\User;
|
|
use App\Services\QuotaV1Service;
|
|
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 QuotaV1Recharge extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
|
|
/**
|
|
* @param Model|Authenticatable|HasPermissions|null $user
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function authorize($user): bool
|
|
{
|
|
return $user->can('dcat.admin.quota_v1_send_jobs.recharge');
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
$quotaV1Service = new QuotaV1Service();
|
|
$quotaV1Service->changeBalance($user, $input['change_balance'] ?? 0, QuotaV1Log::ACTION_ADMIN_RECHARGE, '后台增加', $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', '增加配额')->digits(3)->required();
|
|
$this->confirm('是否确认增加老配额?', '提交后该动作无法逆转');
|
|
}
|
|
}
|