Permission.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Http\Middleware\Permission;
  3. use App\Servers\AdminLogsServer;
  4. use App\Servers\PermissionServer;
  5. use Closure;
  6. class Permission
  7. {
  8. /**
  9. * Handle an incoming request.
  10. *
  11. * @param \Illuminate\Http\Request $request
  12. * @param \Closure $next
  13. * @return mixed
  14. *
  15. * 后台权限限制
  16. */
  17. public function handle($request, Closure $next)
  18. {
  19. // 获取当前会员信息
  20. $user = app('auth')->user();
  21. AdminLogsServer::write(empty($user->id)?0:$user->id,'后台访问日志记录');
  22. // // 判断当前会员是否具有权限
  23. $clientRoute = request()->route()->getName();
  24. // $p_num=\App\Models\Permission::where('routes',$clientRoute)->count();
  25. // if($p_num<=0){
  26. // \App\Models\Permission::create(['routes'=>$clientRoute,'name'=>$clientRoute,'sort'=>1,'p_id'=>1,'type'=>'3']);
  27. // }
  28. if(in_array($clientRoute,['login','admin.index.ajax','admin.not.permission','admin.index','admin.ajaxIndex','admin.switch','admin.u_sort']) || $user->{"id"}==1){
  29. return $next($request);
  30. }
  31. $date_time=date('Hi');
  32. // if( $date_time<800){
  33. // echo 'end';exit;
  34. // return redirect()->route('admin.not.permission');
  35. // }
  36. // dd($clientRoute);
  37. $ret=PermissionServer::verifyPermission($user->{'id'},$clientRoute);
  38. if ( empty($ret) ) {
  39. $is_ajax=request()->ajax();
  40. if($is_ajax){
  41. return response()->json([
  42. 'status' => 'error',
  43. 'message' => '暂无权限',
  44. 'redirect' => '',
  45. 'callback' => '',
  46. 'data' => []
  47. ]);
  48. }else{
  49. // 当前会员没有权限
  50. return redirect()->route('admin.not.permission');
  51. }
  52. }
  53. return $next($request);
  54. }
  55. }