80 lines
2.7 KiB
PHP
80 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Middleware;
|
||
|
|
|
||
|
|
use Closure;
|
||
|
|
use App\Models\UserPreviewHistory;
|
||
|
|
use App\Models\UserPreview;
|
||
|
|
class CheckRankLimitII
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* 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();
|
||
|
|
if (empty($user)) {
|
||
|
|
$user = $this->authCheck();
|
||
|
|
}
|
||
|
|
if (empty($user)) {
|
||
|
|
return $this->fail('authorization', 2, 'pages/tabBar/home', '未登录', '你的账号还没登录');
|
||
|
|
}
|
||
|
|
if ($user->type != 'single') {
|
||
|
|
return $next($request);
|
||
|
|
}
|
||
|
|
//检查自己是否是否是会员
|
||
|
|
$is_temp_member = $user->isTempMember();
|
||
|
|
$is_super_rank = $user->isSuperRank();
|
||
|
|
if (empty($is_temp_member) && empty($is_super_rank)) {
|
||
|
|
//如果是查看用户详情,普通用户可以每天查看七个
|
||
|
|
if ($request->route()->getName() === 'other_user_infor') {
|
||
|
|
$count = 7;
|
||
|
|
$other_user_id = $request->id;
|
||
|
|
$result = $this->checkPreview($user, $count, $other_user_id);
|
||
|
|
if (empty($result)) {
|
||
|
|
return $this->fail('limit', 3, 'pages/users/upgradeVIP', '获取VIP', '普通用户每天只可查看'.$count.'位用户,升级会员可查看更多!');
|
||
|
|
}else{
|
||
|
|
return $next($request);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $this->fail('limit', 3, 'pages/users/upgradeVIP', '获取VIP', '购买超级会员获得更多用户权限');
|
||
|
|
}
|
||
|
|
return $next($request);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
//如果是查看用户详情,普通用户可以每天查看七个
|
||
|
|
public function checkPreview($user, $count, $other_user_id)
|
||
|
|
{
|
||
|
|
$start_time = date('Y-m-d');
|
||
|
|
//普通用户可以查看7个用户
|
||
|
|
$history_count = UserPreview::where('preview_user_id', $user->id)->where('preview_time', '>', $start_time)->distinct('user_id')->pluck('user_id')->toArray();
|
||
|
|
$in_preview = in_array($other_user_id, $history_count);
|
||
|
|
if (count($history_count) >= $count && empty($in_preview)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function fail($msg, $code = 3, $path='', $operate='', $notice='')
|
||
|
|
{
|
||
|
|
$result = [
|
||
|
|
'code'=> $code,
|
||
|
|
'path'=> $path,
|
||
|
|
'message'=> $msg,
|
||
|
|
'operate'=> $operate,
|
||
|
|
'notice'=> $notice,
|
||
|
|
];
|
||
|
|
|
||
|
|
return Response()->json($result);
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|