MenuApi.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\common\api;
  3. use app\admin\model\Role;
  4. use app\admin\model\SysRelation;
  5. use app\admin\model\Menu;
  6. /**
  7. * Description of MenuApi
  8. *
  9. * @author sgq
  10. */
  11. class MenuApi {
  12. public static function getMenuListByRoleid($roleid) {
  13. $role = Role::find($roleid);
  14. $menu_ids = $role->sysRelation->column("menuid");
  15. $menus = Menu::where("id", "in", $menu_ids)->where("ismenu", 1)->where("status", 1)->order("levels asc,num asc")->select()->toArray();
  16. return self::buildMenu($menus);
  17. }
  18. public static function getPrivilagesByRoleid($roleid) {
  19. $role = Role::find($roleid);
  20. $menu_ids = $role->sysRelation->column("menuid");
  21. $menus = Menu::where("status", 1)->order("levels asc,num asc")->select()->toArray();
  22. $codeToPid = [];
  23. foreach ($menus as &$menu) {
  24. $codeToPid[$menu["code"]] = $menu["id"];
  25. $menu["checked"] = in_array($menu["id"], $menu_ids) ? true : false;
  26. $menu["pid"] = isset($codeToPid[$menu["pcode"]]) ? $codeToPid[$menu["pcode"]] : 0;
  27. }unset($menu);
  28. return $menus;
  29. }
  30. /**
  31. * 递归获取多级菜单集合
  32. * @param type $menus
  33. * @param type $pcode
  34. * @return type
  35. */
  36. private static function buildMenu($menus, $pcode = "0") {
  37. $result = [];
  38. foreach ($menus as $key => $menu) {
  39. if ($pcode == $menu["pcode"]) {
  40. unset($menus[$key]);
  41. $menu["children"] = self::buildMenu($menus, $menu["code"]);
  42. if ($menu["pcode"] == 0) {
  43. $result[] = $menu;
  44. }
  45. }
  46. }
  47. return $result;
  48. }
  49. /**
  50. * 权限检查
  51. * @param type $url 新权限url
  52. * @param type $old_url 旧系统权限url
  53. * @return boolean
  54. */
  55. public static function chkPermission($url, $old_url) {
  56. $url = strtolower($url);
  57. $old_url = strtolower($old_url);
  58. $menu = Menu::whereRaw("`status`=1 and (new_url=:new_url or `url`=:url)", ["new_url" => $url, "url" => $old_url])->findOrEmpty()->toArray();
  59. if (!$menu)
  60. return true;
  61. $menuid = $menu["id"];
  62. $roleid = session("user")["roleid"];
  63. $role = Role::find($roleid);
  64. $menu_ids = $role->sysRelation->column("menuid");
  65. return in_array($menuid, $menu_ids);
  66. }
  67. public static function getEnterpriseMenuByType($type) {
  68. $menus = [];
  69. $menus[] = ["type" => [1, 2, 3], "code" => "qyzx", "pcode" => "0", "name" => "企业用户中心", "url" => "", "icon" => "fa-user"];
  70. $menus[] = ["type" => [1, 2], "code" => "yhfk", "pcode" => "0", "name" => "用户反馈", "url" => "", "icon" => "fa-bug"];
  71. $menus[] = ["type" => [1, 2], "code" => "rcrd", "pcode" => "0", "name" => "人才认定", "url" => "#", "icon" => "fa-thumbs-o-up"];
  72. $menus[] = ["type" => [1, 2], "code" => "rcrdsb", "pcode" => "rcrd", "name" => "人才认定申报", "url" => "/enterprise/talent", "icon" => "fa-thumbs-up"];
  73. $menus[] = ["type" => [1, 2], "code" => "yxrck", "pcode" => "rcrd", "name" => "优秀人才库", "url" => "", "icon" => "fa-thumbs-up"];
  74. $menus[] = ["type" => [1, 2], "code" => "lzsb", "pcode" => "rcrd", "name" => "离职申报", "url" => "", "icon" => "fa-thumbs-up"];
  75. $menus[] = ["type" => [1, 2], "code" => "gzdwbg", "pcode" => "rcrd", "name" => "工作单位变更", "url" => "", "icon" => "fa-thumbs-up"];
  76. $menus[] = ["type" => [1, 2], "code" => "yhzhbg", "pcode" => "rcrd", "name" => "银行账号变更", "url" => "", "icon" => "fa-thumbs-up"];
  77. $menus[] = ["type" => [1, 2], "code" => "rcccbg", "pcode" => "rcrd", "name" => "人才层次变更", "url" => "", "icon" => "fa-thumbs-up"];
  78. $menus[] = ["type" => [1, 2], "code" => "jcxxbg", "pcode" => "rcrd", "name" => "基础信息变更", "url" => "", "icon" => "fa-thumbs-up"];
  79. $menus[] = ["type" => [1, 2], "code" => "jbtmanage", "pcode" => "0", "name" => "津补贴申报", "url" => "", "icon" => "fa-money"];
  80. $menus[] = ["type" => [1, 2], "code" => "jbtsb", "pcode" => "jbtmanage", "name" => "津补贴申报", "url" => "", "icon" => "fa-thumbs-up"];
  81. $menus[] = ["type" => [1, 2], "code" => "gfbt", "pcode" => "0", "name" => "购房补贴", "url" => "", "icon" => "fa-university"];
  82. $menus[] = ["type" => [1, 2], "code" => "gfbtsb", "pcode" => "gfbt", "name" => "购房补贴申报", "url" => "", "icon" => "fa-thumbs-up"];
  83. $menus[] = ["type" => [2], "code" => "pxbtmanage", "pcode" => "0", "name" => "培训补贴", "url" => "", "icon" => "fa-building"];
  84. $menus[] = ["type" => [2], "code" => "pxbt", "pcode" => "pxbtmanage", "name" => "培训补贴", "url" => "", "icon" => "fa-thumbs-up"];
  85. $menus[] = ["type" => [2], "code" => "zdzxmanage", "pcode" => "0", "name" => "重大专项", "url" => "", "icon" => "fa-life-ring"];
  86. $menus[] = ["type" => [2], "code" => "zdzx", "pcode" => "zdzxmanage", "name" => "重大专项", "url" => "", "icon" => "fa-thumbs-up"];
  87. $menus[] = ["type" => [2], "code" => "znjy", "pcode" => "0", "name" => "子女教育", "url" => "", "icon" => "fa-graduation-cap"];
  88. $menus[] = ["type" => [2], "code" => "xzfbz", "pcode" => "znjy", "name" => "学杂费补助", "url" => "", "icon" => "fa-thumbs-up"];
  89. $menus[] = ["type" => [2], "code" => "ylbzmanage", "pcode" => "0", "name" => "医疗补助", "url" => "", "icon" => "fa-hospital-o"];
  90. $menus[] = ["type" => [2], "code" => "ylbz", "pcode" => "ylbzmanage", "name" => "医疗补助", "url" => "", "icon" => "fa-thumbs-up"];
  91. $menus[] = ["type" => [2], "code" => "zfbz", "pcode" => "0", "name" => "住房保障", "url" => "", "icon" => "fa-home"];
  92. $menus[] = ["type" => [2], "code" => "zfsq", "pcode" => "zfbz", "name" => "租房申请", "url" => "", "icon" => "fa-home"];
  93. $menus[] = ["type" => [2], "code" => "fzbz", "pcode" => "zfbz", "name" => "房租补助", "url" => "", "icon" => "fa-money"];
  94. $menus[] = ["type" => [2], "code" => "gfsq", "pcode" => "zfbz", "name" => "购房申请", "url" => "", "icon" => "fa-envira"];
  95. $menus[] = ["type" => [2], "code" => "gfbz", "pcode" => "zfbz", "name" => "购房补助", "url" => "", "icon" => "fa-envira"];
  96. $menus[] = ["type" => [1], "code" => "sbrc", "pcode" => "0", "name" => "硕博人才", "url" => "", "icon" => "fa-graduation-cap"];
  97. $menus[] = ["type" => [1], "code" => "shbt", "pcode" => "sbrc", "name" => "生活补贴", "url" => "", "icon" => "fa-graduation-cap"];
  98. $menus[] = ["type" => [3], "code" => "hxjh", "pcode" => "0", "name" => "海峡计划", "url" => "", "icon" => "fa-plane"];
  99. $menus[] = ["type" => [3], "code" => "hxjhsb", "pcode" => "hxjh", "name" => "海峡计划申报", "url" => "", "icon" => "fa-pencil"];
  100. foreach ($menus as $key => $menu) {
  101. if (!in_array($type, $menu["type"]))
  102. unset($menus[$key]);
  103. }
  104. return self::buildMenu($menus);
  105. }
  106. }