v2board/app/Http/Controllers/User/PlanController.php

44 lines
1.3 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
2020-01-29 16:08:50 +08:00
namespace App\Http\Controllers\User;
2019-10-29 15:33:36 +08:00
use App\Http\Controllers\Controller;
2022-04-19 04:10:02 +08:00
use App\Models\User;
2022-07-19 03:11:36 +08:00
use App\Services\PlanService;
2020-01-29 16:08:50 +08:00
use Illuminate\Http\Request;
2019-10-29 15:33:36 +08:00
use App\Models\Plan;
2022-07-08 02:36:33 +08:00
use Illuminate\Support\Facades\DB;
2019-10-29 15:33:36 +08:00
class PlanController extends Controller
{
2020-01-11 13:36:52 +08:00
public function fetch(Request $request)
{
2022-07-19 03:11:36 +08:00
$user = User::find($request->user['id']);
2020-01-03 21:04:44 +08:00
if ($request->input('id')) {
2022-04-19 04:10:02 +08:00
$plan = Plan::where('id', $request->input('id'))->first();
2020-01-03 21:04:44 +08:00
if (!$plan) {
2021-06-12 00:56:39 +08:00
abort(500, __('Subscription plan does not exist'));
2020-01-03 21:04:44 +08:00
}
2022-04-19 04:10:02 +08:00
if ((!$plan->show && !$plan->renew) || (!$plan->show && $user->plan_id !== $plan->id)) {
abort(500, __('Subscription plan does not exist'));
}
2020-01-03 21:04:44 +08:00
return response([
'data' => $plan
]);
2019-10-29 15:33:36 +08:00
}
2022-07-19 03:11:36 +08:00
$counts = PlanService::countActiveUsers();
2022-07-08 02:36:33 +08:00
$plans = Plan::where('show', 1)
2020-04-21 00:22:05 +08:00
->orderBy('sort', 'ASC')
2020-01-03 21:04:44 +08:00
->get();
2022-07-19 03:11:36 +08:00
foreach ($plans as $k => $v) {
if ($plans[$k]->capacity_limit === NULL) continue;
if (!isset($counts[$plans[$k]->id])) continue;
$plans[$k]->capacity_limit = $plans[$k]->capacity_limit - $counts[$plans[$k]->id]->count;
2022-07-08 02:36:33 +08:00
}
2019-10-29 15:33:36 +08:00
return response([
2022-07-08 02:36:33 +08:00
'data' => $plans
2019-10-29 15:33:36 +08:00
]);
}
}