改进 一些可能有用的东西

This commit is contained in:
iVampireSP.com 2022-11-25 16:14:42 +08:00
parent d789e6fa56
commit 79438111ff
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
3 changed files with 49 additions and 34 deletions

View File

@ -3,9 +3,9 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\User\HostRequest;
use App\Models\Host; use App\Models\Host;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use function auth; use function auth;
use function dispatch; use function dispatch;
@ -23,7 +23,7 @@ public function index(): JsonResponse
} }
// //
public function update(Request $request, Host $host): JsonResponse public function update(HostRequest $request, Host $host): JsonResponse
{ {
$request->validate([ $request->validate([
'status' => 'required|in:running,stopped', 'status' => 'required|in:running,stopped',
@ -35,34 +35,26 @@ public function update(Request $request, Host $host): JsonResponse
return $this->error('余额不足,无法开启计费项目。'); return $this->error('余额不足,无法开启计费项目。');
} }
if ($host->user_id == $user->id) { $host->update([
$host->update([ 'status' => $request->status,
'status' => $request->status, ]);
]);
return $this->updated($host); return $this->updated($host);
} else {
return $this->error('无权操作');
}
} }
public function destroy(Host $host) public function destroy(HostRequest $host)
{ {
if ($host->user_id == auth()->id()) { if ($host->status == 'pending') {
if ($host->status == 'pending') { // 如果上次更新时间大于 5min
// 如果上次更新时间大于 5min if (time() - strtotime($host->updated_at) > 300) {
if (time() - strtotime($host->updated_at) > 300) { $host->delete();
$host->delete(); } else {
} else { return $this->error('请等待 5 分钟后再试');
return $this->error('请等待 5 分钟后再试');
}
} }
dispatch(new \App\Jobs\Module\Host($host, 'delete'));
} else {
return $this->error('无权操作');
} }
dispatch(new \App\Jobs\Module\Host($host, 'delete'));
return $this->deleted($host); return $this->deleted($host);
} }

View File

@ -0,0 +1,33 @@
<?php
namespace App\Http\Requests\User;
use Illuminate\Foundation\Http\FormRequest;
class HostRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
$host = $this->route('host');
// 检测是否是自己的主机
return $host->user_id == auth()->id();
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
//
];
}
}

View File

@ -17,17 +17,7 @@ public function authorize(): bool
$work_order = $this->route('workOrder'); $work_order = $this->route('workOrder');
// if work_order is model return $work_order->user_id == auth()->id();
if ($work_order instanceof WorkOrder) {
$work_order_id = $work_order->id;
} else {
$work_order_id = $work_order;
}
return WorkOrder::where('user_id', auth()->id())->where('id', $work_order_id)->exists();
return false;
} }
/** /**