love_php/app/Http/Middleware/Wechartauth.php
2026-04-02 09:20:51 +08:00

255 lines
7.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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 App\Models\WrongInfoHistories;
use Illuminate\Support\Facades\Cache;
/**微信小程序 登录校验 */
class Wechartauth
{
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) {
return response()->json(['code' => 2, 'message' => '请登录后访问.'], 200);
}
$user = auth()->user();
$route_name = $request->route()->getName();
//获取路由前缀
$prefix = ($request->route()->getAction())['prefix'];
//是否关闭资料
if ($user->hidden_profile == 'ALLSEX' && $route_name != 'chage_hidden_profile' && $prefix == 'api') {
return $this->fail('关闭资料', 3, 'pages/users/setTing', '打开资料', '您的资料已关闭,是否打开?');
}
//校验 用户是否已经被封禁
$userSer = new UserService();
$userBannedState = $userSer->getUserBannedState($user->id);
if ($userBannedState) {
return $this->fail('由于您的账号涉及违规暂不支持使用。请联系客服18194063294 ', 6);
}
$info = WrongInfoHistories::where('user_id', $user->id)->where('type', 'frozen')->first();
if ($info) {
$frozen_state = 1;//未超过3天
$date = date('Y-m-d H:i:s');
$frozen_time = $info->created_at;
$expire_time = date('Y-m-d H:i:s', strtotime('+3 days', strtotime($frozen_time)));
if ($date > $expire_time) {
$frozen_state = 2; //超过3天
return $this->fail('用户资料异常已被平台冻结。如有疑问请联系客服18194063294', 7);
}
}
//活跃度缓存key
$rd_liveness_key = User::RDLIVENESSKEY . $user->id;
if (Cache::has($rd_liveness_key)) {
Cache::increment($rd_liveness_key);
} else {
Cache::forever($rd_liveness_key, $user->liveness);
Cache::increment($rd_liveness_key);
}
//最近访问时间缓存key
$rd_last_time_key = User::RDLASTTIMEKEY . $user->id;
Cache::forever($rd_last_time_key, date('Y-m-d H:i:s'));
$pa = json_encode($request->route()->parameters());
$params = [
'user_id' => $user->id,
'content' => json_encode($request->all(), JSON_UNESCAPED_UNICODE),
'route' => $request->route()->uri() . "?" . $pa,
];
AddDynamicLog::dispatch($params)->onQueue('dynamic');
if ($prefix === 'api/app') {
//修改在线状态
$user->app_online = 1;
$client_os = $request->header('client-os');
if ($client_os == 'IOS') {
$device_idfa = $request->header('device-idfa');
if ($device_idfa && $user->idfa != $device_idfa) {
$user->idfa = $device_idfa;
}
} else {
$device_imei = $request->header('device-imei');
if ($device_imei && $user->imei != $device_imei) {
$user->imei = $device_imei;
}
}
$user->save();
$this->checkApp($request);
} elseif ($prefix == 'api/admin') {
$this->checkAdmin($request);
} elseif ($prefix == 'api/official') {
$result = $this->checkOfficial($request);
if ($result) {
return $result;
}
}
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 = 3, $path = '', $operate = '', $notice = '')
{
$result = [
'code' => $code,
'path' => $path,
'message' => $msg,
'operate' => $operate,
'notice' => $notice,
];
return Response()->json($result);
}
}