73 lines
2.1 KiB
PHP
73 lines
2.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 文件功能:前台会员中心控制器
|
||
|
|
* 负责展示会员等级、权益说明、当前会员状态以及用户自己的购买记录
|
||
|
|
*/
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Models\Sysparam;
|
||
|
|
use App\Models\VipLevel;
|
||
|
|
use App\Models\VipPaymentOrder;
|
||
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\View\View;
|
||
|
|
|
||
|
|
class VipCenterController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* 显示会员中心页面
|
||
|
|
*
|
||
|
|
* @param Request $request 当前请求对象
|
||
|
|
*/
|
||
|
|
public function index(Request $request): View
|
||
|
|
{
|
||
|
|
$user = $request->user();
|
||
|
|
|
||
|
|
// 会员等级按后台排序字段展示,方便用户对比不同档位权益。
|
||
|
|
$vipLevels = VipLevel::query()
|
||
|
|
->withCount('users')
|
||
|
|
->orderByDesc('sort_order')
|
||
|
|
->orderBy('id')
|
||
|
|
->get();
|
||
|
|
|
||
|
|
// 仅展示当前用户自己的购买记录,避免泄露其他会员订单信息。
|
||
|
|
$paymentLogs = $this->buildPaymentLogs($user->id);
|
||
|
|
|
||
|
|
$paidOrders = VipPaymentOrder::query()
|
||
|
|
->where('user_id', $user->id)
|
||
|
|
->where('status', 'paid')
|
||
|
|
->count();
|
||
|
|
|
||
|
|
$totalAmount = (float) VipPaymentOrder::query()
|
||
|
|
->where('user_id', $user->id)
|
||
|
|
->where('status', 'paid')
|
||
|
|
->sum('amount');
|
||
|
|
|
||
|
|
return view('vip.center', [
|
||
|
|
'user' => $user,
|
||
|
|
'vipLevels' => $vipLevels,
|
||
|
|
'paymentLogs' => $paymentLogs,
|
||
|
|
'vipPaymentEnabled' => Sysparam::getValue('vip_payment_enabled', '0') === '1',
|
||
|
|
'paidOrders' => $paidOrders,
|
||
|
|
'totalAmount' => $totalAmount,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 构建当前用户的购买记录分页数据
|
||
|
|
*
|
||
|
|
* @param int $userId 当前登录用户 ID
|
||
|
|
*/
|
||
|
|
private function buildPaymentLogs(int $userId): LengthAwarePaginator
|
||
|
|
{
|
||
|
|
return VipPaymentOrder::query()
|
||
|
|
->with('vipLevel:id,name,color,icon')
|
||
|
|
->where('user_id', $userId)
|
||
|
|
->latest('id')
|
||
|
|
->paginate(10)
|
||
|
|
->withQueryString();
|
||
|
|
}
|
||
|
|
}
|