love_php/app/Http/Middleware/CheckBasic.php

188 lines
5.3 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Http\Middleware;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Auth\AuthenticationException;
use App\Models\ShareInfor;
use App\Models\UserGroup;
use App\Models\FormId;
use App\Models\Wechat;
use Closure;
use App\Models\Dynamic;
use App\Models\Paas;
use App\Models\PaasUser;
use App\Models\ShareSideUser;
use App\Models\User;
use App\Models\SystemBlacklist;
use App\Http\Response\ResponseJson;
use App\Jobs\AddDynamicLog;
use App\Services\UserService;
use Illuminate\Support\Facades\Cache;
/** 无需登录,如果登录,则校验用户基本信息, */
class CheckBasic
{
use ResponseJson;
/**
* The authentication factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, ...$guards)
{
$result = $this->authenticate($request, $guards);
if ($result === 0) {
} else {
$user = auth()->user();
$route_name = $request->route()->getName();
//获取路由前缀
$prefix = ($request->route()->getAction())['prefix'];
//校验 用户是否已经被封禁
$userSer = new UserService();
$userBannedState = $userSer->getUserBannedState($user->id);
if ($userBannedState) {
return $this->fail('由于您的账号涉及违规暂不支持使用。请联系客服18194063294 ', 6);
}
}
return $next($request);
}
public function checkApp($request)
{
}
public function checkAdmin($request)
{
}
public function checkOfficial($request)
{
$user = auth()->user();
$user_id = $user->id;
//平台渠道
$groupID = $request->input('openGId');
if ($groupID) {
$group = UserGroup::where(['user_id' => $user_id, 'groupID' => $groupID])->first();
if (empty($group)) {
UserGroup::create([
'user_id' => $user_id,
'groupID' => $groupID
]);
}
}
//添加型号
$systemInfo = $request->input('systemInfo');
if ($systemInfo && $systemInfo != $user->system_info) {
$user->system_info = $systemInfo;
}
$user->save();
//平台信息
$paas = $request->input('paas');
if (!empty($paas) && $paas != 'null') {
$paas_id = Paas::where('name', $paas)->value('id');
if (!empty($paas_id)) {
$paas_user = PaasUser::where('paas_id', $paas_id)->where('user_id', $user_id)->where('type', 'MINOR')->first();
if (empty($paas_user)) {
PaasUser::create([
'user_id' => $user_id,
'paas_id' => $paas_id,
'type' => 'MINOR',
]);
}
}
}
//是否关闭资料
if ($user->hidden_profile == 'ALLSEX') {
if ($request->route()->getName() == 'chage_hidden_profile' || $request->route()->getName() == 'new_message_count' || $request->route()->getName() == 'home_likers' || $request->route()->getName() == 'account_uid') {
return null;
}
return $this->fail('approve', 3, 'pages/users/optimset', '打开资料', '您的资料已关闭,是否打开?');
}
if ($user->type == 'single' && $request->input('share_user_id')) {
$share_user_id = $request->share_user_id;
$this->addShareSideUser($user, $share_user_id);
}
return null;
}
public function addShareSideUser($user, $share_user_id)
{
$share_user = User::where('id', $share_user_id)->first();
if ($share_user && $share_user->type != 'single') {
ShareSideUser::firstOrCreate([
'user_id' => $share_user_id,
'other_user_id' => $user->id,
]);
}
return;
}
/**
* Determine if the user is logged in to any of the given guards.
*
* @param array $guards
* @return void
*
* @throws \Illuminate\Auth\AuthenticationException
*/
protected function authenticate($request, array $guards)
{
if (empty($guards)) {
return $this->auth->authenticate();
}
foreach ($guards as $guard) {
if ($this->auth->guard($guard)->check()) {
return $this->auth->shouldUse($guard);
}
return 0;
}
throw new AuthenticationException('Unauthenticated.', $guards);
}
public function fail($msg, $code = 5, $path = '', $operate = '', $notice = '')
{
$result = [
'code' => $code,
'path' => $path,
'message' => $msg,
'operate' => $operate,
'notice' => $notice,
];
return Response()->json($result);
}
}