224 lines
6.6 KiB
PHP
224 lines
6.6 KiB
PHP
<?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 Illuminate\Support\Facades\Cache;
|
|
|
|
class h5
|
|
{
|
|
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
|
|
* @param string[] ...$guards
|
|
* @return mixed
|
|
*
|
|
* @throws \Illuminate\Auth\AuthenticationException
|
|
*/
|
|
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'];
|
|
$date = date('Y-m-d H:i:s');
|
|
$blacklist = SystemBlacklist::where('user_id', $user->id)->first();
|
|
if (!empty($blacklist)) {
|
|
if($blacklist->end_time >= $date && $blacklist->start_time <= $date){
|
|
return $this->fail('由于您的账号涉及违规,暂无法使用福恋平台', 1);
|
|
}
|
|
}
|
|
//活跃度缓存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->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);
|
|
|
|
}
|
|
|
|
}
|