love_php/app/Http/Middleware/CheckRankLimit.php

125 lines
5.1 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Http\Middleware;
use Closure;
use App\Models\Wechat;
use App\Models\User;
use App\Models\RankHistory;
use App\Models\Linking;
use App\Models\UserPreviewHistory;
class CheckRankLimit
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->input('versions') == config('app.versions')) {
return $next($request);
}
$user = auth()->user();
$user_id = $user->id;
if (empty($user)) {
$user = $this->authCheck();
}
if ($user->type != 'single') {
return $next($request);
}
$other_user_id = $request->id;
//是否是好友
if ($other_user_id) {
$link = Linking::where(function($sql) use($user_id, $other_user_id){
$sql->where(['user_id'=>$user_id, 'user_linking_id'=>$other_user_id]);
})->orWhere(function($sql) use($user_id, $other_user_id){
$sql->where(['user_id'=>$other_user_id, 'user_linking_id'=>$user_id]);
})->first();
if ($link) {//是好友
return $next($request);
}
}
if ($user->temp_member) {
return $next($request);
}elseif ($user->rank_id > 1) {
//这个月邀请的人
$history = RankHistory::where('user_id', $user->id)->where('rank_id', $user->rank_id)->whereNotNull('deadline')->orderBY('id', 'desc')->first();
if (empty($history)) {
return $this->fail('limit', 3, 'pages/users/upgradeVIP', '获取VIP', '请先升级VIP');
}else{
//判断是否到期
$end_time = strtotime($history->deadline);
if (time() >= $end_time) {
return $this->fail('limit', 3, 'pages/users/upgradeVIP', '获取VIP', '请先升级VIP');
}
}
}elseif ($user->rank_id == 1) {
$history = RankHistory::where('user_id', $user->id)->where('rank_id', 1)->orderBY('id', 'desc')->first();
if (empty($history)) {
return $this->fail('limit', 3, 'pages/users/upgradeVIP', '获取VIP', '请先升级VIP');
}
$end_time = strtotime($history->deadline);
if ($history->deadline && time() >= $end_time) {
return $this->fail('limit', 3, 'pages/users/shareVip', '获取VIP', '会员已到期,邀请好友可延长期限!');
}
//不是会员
if (empty($history->deadline) && $request->route()->getName() === 'other_user_infor') {
$start_time = date('Y-m-d', time());
// $history_count = UserPreviewHistory::where('user_id', $user->id)->where('created_at', '>', $start_time)->count();
$history_count = UserPreviewHistory::where('user_id', $user->id)->where('created_at', '>', $start_time)->distinct('preview_user_id')->pluck('preview_user_id')->toArray();
//普通用户只能查看12个个人信息
if (count($history_count) <= 7) {
return $next($request);
}else{
// return $this->fail('当日可查看12个人信息次数用尽请充值会员查看更多用户信息', $user, 1);
return $this->fail('limit', 3, 'pages/users/upgradeVIP', '获取VIP', '普通用户每天只可能查看7位用户升级会员可查看更多');
}
}
}else{
$start_time = date('Y-m-d', time());
// $history_count = UserPreviewHistory::where('user_id', $user->id)->where('created_at', '>', $start_time)->count();
$history_count = UserPreviewHistory::where('user_id', $user->id)->where('created_at', '>', $start_time)->distinct('preview_user_id')->pluck('preview_user_id')->toArray();
//普通用户只能查看12个个人信息
if (count($history_count) <= 7) {
return $next($request);
}else{
// return $this->fail('当日可查看12个人信息次数用尽请充值会员查看更多用户信息', $user, 1);
return $this->fail('limit', 3, 'pages/users/upgradeVIP', '获取VIP', '普通用户每天只可能查看7位用户升级会员可查看更多');
}
}
return $next($request);
}
public function fail($msg, $code = 3, $path='', $operate='', $notice='')
{
$result = [
'code'=> $code,
'path'=> $path,
'message'=> $msg,
'operate'=> $operate,
'notice'=> $notice,
];
return Response()->json($result);
}
public function authCheck()
{
$guards = config('auth.guards');
$result = false;
foreach($guards as $key=>$guard){
if(\Auth::guard($key)->check()){
$result = \Auth::guard($key)->user();
continue;
}
}
return $result;
}
}