Lae/app/Http/Controllers/Web/HostController.php

78 lines
2.3 KiB
PHP
Raw Normal View History

2023-02-12 18:27:59 +00:00
<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Models\Host;
use Illuminate\Http\RedirectResponse;
2023-02-13 09:05:28 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
2023-02-12 18:27:59 +00:00
use Illuminate\View\View;
class HostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return View
*/
public function index(): View
{
2023-02-12 19:06:27 +00:00
$hosts = (new Host)->thisUser()->with(['user', 'module'])->paginate(20);
2023-02-13 09:05:28 +00:00
$times = config('settings.billing.cycle_delete_times_every_month') - Cache::get('host_delete_times:'.auth('web')->id(), 0);
2023-02-12 18:27:59 +00:00
2023-02-13 09:05:28 +00:00
return view('hosts.index', compact('hosts', 'times'));
}
public function update(Request $request, Host $host): RedirectResponse
{
$request->validate([
'status' => 'required|in:running,stopped,suspended',
]);
$status = $host->changeStatus($request->input('status'));
return $status ? back()->with('success', '修改成功。') : back()->with('error', '修改失败。');
2023-02-12 18:27:59 +00:00
}
2023-02-12 19:09:41 +00:00
public function renew(Host $host): RedirectResponse
2023-02-12 18:27:59 +00:00
{
2023-02-12 18:42:04 +00:00
$price = $host->getRenewPrice();
if ($price > auth()->user()->balance) {
2023-02-12 19:12:26 +00:00
return back()->with('error', '余额不足,续费需要:'.$price.' 元,您还需要充值:'.($price - auth()->user()->balance).' 元');
2023-02-12 18:42:04 +00:00
}
2023-02-12 19:12:26 +00:00
if (! $host->isCycle()) {
2023-02-12 18:42:04 +00:00
return back()->with('error', '该主机不是周期性付费,无法续费。');
}
2023-02-12 18:27:59 +00:00
if ($host->renew()) {
2023-02-12 19:12:26 +00:00
return back()->with('success', '续费成功,新的到期时间为:'.$host->next_due_at.'。');
2023-02-12 18:27:59 +00:00
}
return back()->with('error', '续费失败,请检查是否有足够的余额。');
}
/**
* Remove the specified resource from storage.
*
2023-02-12 19:12:26 +00:00
* @param Host $host
2023-02-12 18:27:59 +00:00
* @return RedirectResponse
*/
public function destroy(Host $host): RedirectResponse
{
2023-02-13 09:05:28 +00:00
if ($host->isUnavailable()) {
2023-02-12 18:54:55 +00:00
return back()->with('error', '为了安全起见,此主机只能由我们自动删除。');
}
2023-02-13 09:05:28 +00:00
$status = $host->safeDelete();
2023-02-12 18:27:59 +00:00
2023-02-13 09:05:28 +00:00
if ($status) {
return redirect()->route('hosts.index')->with('success', '已添加到删除队列。');
} else {
return back()->with('error', '删除失败。');
}
2023-02-12 18:27:59 +00:00
}
}