Controller.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Permission;
  4. use Illuminate\Foundation\Bus\DispatchesJobs;
  5. use Illuminate\Routing\Controller as BaseController;
  6. use Illuminate\Foundation\Validation\ValidatesRequests;
  7. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  8. class Controller extends BaseController
  9. {
  10. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  11. /**
  12. * 处理权限分类
  13. */
  14. public function tree($list=[], $pk='id', $pid = 'parent_id', $child = '_child', $root = 0)
  15. {
  16. if (empty($list)){
  17. $list = Permission::get()->toArray();
  18. }
  19. // 创建Tree
  20. $tree = array();
  21. if(is_array($list)) {
  22. // 创建基于主键的数组引用
  23. $refer = array();
  24. foreach ($list as $key => $data) {
  25. $refer[$data[$pk]] =& $list[$key];
  26. }
  27. foreach ($list as $key => $data) {
  28. // 判断是否存在parent
  29. $parentId = $data[$pid];
  30. if ($root == $parentId) {
  31. $tree[] =& $list[$key];
  32. }else{
  33. if (isset($refer[$parentId])) {
  34. $parent =& $refer[$parentId];
  35. $parent[$child][] =& $list[$key];
  36. }
  37. }
  38. }
  39. }
  40. return $tree;
  41. }
  42. /**
  43. * 获取用户真是ip
  44. * @return bool|mixed
  45. */
  46. public function get_real_ip()
  47. {
  48. $ip=FALSE;
  49. //客户端IP 或 NONE
  50. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  51. $ip = $_SERVER["HTTP_CLIENT_IP"];
  52. }
  53. //多重代理服务器下的客户端真实IP地址(可能伪造),如果没有使用代理,此字段为空
  54. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  55. $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  56. if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
  57. for ($i = 0; $i < count($ips); $i++) {
  58. if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
  59. $ip = $ips[$i];
  60. break;
  61. }
  62. }
  63. }
  64. //客户端IP 或 (最后一个)代理服务器 IP
  65. return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  66. }
  67. }