MenuServer.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace app\admin\server\menu;
  3. use app\admin\model\SystemMenu;
  4. use app\admin\model\SystemRole;
  5. use support\Db;
  6. use support\Redis;
  7. class MenuServer
  8. {
  9. /**
  10. * Notes:获取菜单列表
  11. * @param string $keywords
  12. * @param int $page
  13. * @param int $limit
  14. * @return array
  15. * User: ZQ
  16. * Date: 2022/9/13
  17. */
  18. public static function getMenuList(string $keywords)
  19. {
  20. if (!empty($keywords)){
  21. $list = SystemMenu::getMenuList($keywords);
  22. }else{
  23. $list = SystemMenu::getMenus(0);
  24. if (!empty($list)){
  25. for ($i=0;$i<count($list);$i++){
  26. // if ($list[$i]['menu_is_show'] == 1){
  27. // $list[$i]['menu_is_show'] = true;
  28. // }else{
  29. // $list[$i]['menu_is_show'] = false;
  30. // }
  31. $list[$i]['children'] = SystemMenu::getMenus($list[$i]['menu_id']);
  32. if (!empty($list[$i]['children'])){
  33. for ($s=0;$s<count($list[$i]['children']);$s++){
  34. // if ($list[$i]['children'][$s]['menu_is_show'] == 1){
  35. // $list[$i]['children'][$s]['menu_is_show'] = true;
  36. // }else{
  37. // $list[$i]['children'][$s]['menu_is_show'] = false;
  38. // }
  39. $list[$i]['children'][$s]['children'] = SystemMenu::getMenus($list[$i]['children'][$s]['menu_id']);
  40. // if (!empty($list[$i]['children'][$s]['children'])){
  41. // for ($j=0;$j<count($list[$i]['children'][$s]['children']);$j++){
  42. // if ($list[$i]['children'][$s]['children'][$j]['menu_is_show'] == 1){
  43. // $list[$i]['children'][$s]['children'][$j]['menu_is_show'] = true;
  44. // }else{
  45. // $list[$i]['children'][$s]['children'][$j]['menu_is_show'] = false;
  46. // }
  47. // }
  48. // }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. return $list;
  55. }
  56. /**
  57. * Notes:修改角色
  58. * @param string $menu_name
  59. * @param int $menu_id
  60. * @param int $menu_pid
  61. * @return int
  62. * User: ZQ
  63. * Date: 2022/9/13
  64. */
  65. public static function updateMenu($menu_id, $menu_name, $menu_pid, $menu_path, $menu_icon, $menu_route, $menu_params, $menu_sort, $menu_is_menu, $menu_is_show)
  66. {
  67. SystemMenu::affairBegin();
  68. try {
  69. $where = [];
  70. $where['menu_id'] = $menu_id;
  71. $data = [];
  72. $data['menu_name'] = $menu_name;
  73. $data['level'] = 1;
  74. if (!empty($menu_pid)){
  75. $data['level'] = SystemMenu::getParentValue($menu_pid) + 1;
  76. }
  77. $data['menu_pid'] = $menu_pid;
  78. $data['menu_path'] = $menu_path;
  79. $data['menu_icon'] = $menu_icon;
  80. $data['menu_route'] = $menu_route;
  81. $data['menu_params'] = $menu_params;
  82. $data['menu_sort'] = $menu_sort;
  83. $data['menu_is_menu'] = $menu_is_menu;
  84. $data['menu_is_show'] = $menu_is_show;
  85. $result = SystemMenu::where($where)->update($data);
  86. if ($result !== false){
  87. SystemMenu::affairCommit();
  88. return true;
  89. }
  90. throw new \Exception('操作失败!');
  91. }catch (\Exception $exception){
  92. SystemMenu::affairRollback();
  93. throw new \Exception($exception->getMessage(), 500);
  94. }
  95. }
  96. /**
  97. * Notes:删除角色
  98. * @param int $menu_id
  99. * @return int
  100. * User: ZQ
  101. * Date: 2022/9/3
  102. */
  103. public static function delMenu($menu_id)
  104. {
  105. SystemMenu::affairBegin();
  106. try {
  107. $where = [];
  108. $where['menu_id'] = $menu_id;
  109. $data['menu_status'] = 2;
  110. $result = SystemMenu::where($where)->update($data);
  111. if (!empty($result)){
  112. SystemMenu::affairCommit();
  113. return true;
  114. }else{
  115. return false;
  116. }
  117. }catch (\Exception $exception){
  118. SystemMenu::affairRollback();
  119. throw new \Exception($exception->getMessage(), 500);
  120. }
  121. }
  122. /**
  123. * Notes: 添加菜单
  124. * @param string $menu_name
  125. * @param int $menu_pid
  126. * @return int
  127. * User: ZQ
  128. * Date: 2022/9/14
  129. */
  130. public static function insertMenu($menu_name, $menu_pid, $menu_path, $menu_icon, $menu_route, $menu_params, $menu_sort, $menu_is_menu, $menu_is_show)
  131. {
  132. SystemMenu::affairBegin();
  133. try {
  134. $data = [];
  135. $data['menu_name'] = $menu_name;
  136. $data['level'] = 1;
  137. if (!empty($menu_pid)){
  138. $data['level'] = SystemMenu::getParentValue($menu_pid) + 1;
  139. }
  140. $data['menu_pid'] = $menu_pid;
  141. $data['menu_path'] = $menu_path;
  142. $data['menu_icon'] = $menu_icon;
  143. $data['menu_route'] = $menu_route;
  144. $data['menu_params'] = $menu_params;
  145. $data['menu_sort'] = $menu_sort;
  146. $data['menu_is_menu'] = $menu_is_menu;
  147. $data['menu_is_show'] = $menu_is_show;
  148. $result = SystemMenu::insertGetId($data);
  149. if (!empty($result)){
  150. SystemMenu::affairCommit();
  151. return $result;
  152. }
  153. throw new \Exception('操作失败!');
  154. }catch (\Exception $exception){
  155. SystemMenu::affairRollback();
  156. throw new \Exception($exception->getMessage(), 500);
  157. }
  158. }
  159. /**
  160. * Notes:查询角色
  161. * @param int $menu_id
  162. * @return int
  163. * User: ZQ
  164. * Date: 2022/9/13
  165. */
  166. public static function menuInfo($menu_id)
  167. {
  168. $SystemMenu = new SystemMenu();
  169. $where = [];
  170. $where['menu_id'] = $menu_id;
  171. $result = SystemMenu::where($where)->first();
  172. $result['menu_is_menu'] = (int)$result['menu_is_menu'];
  173. $result['menu_pid_arr'] = $SystemMenu->getParentMenuId($result['menu_pid']);
  174. // $result['menu_is_show'] = ($result['menu_is_show'] == 1 ? true : false);
  175. return $result;
  176. }
  177. /**
  178. * Notes:获取菜单列表
  179. * @param string $keywords
  180. * @param int $page
  181. * @param int $limit
  182. * @return array
  183. * User: ZQ
  184. * Date: 2022/9/13
  185. */
  186. public static function menuLevel()
  187. {
  188. $list = SystemMenu::getLevel(0);
  189. if (!empty($list)){
  190. for ($i=0;$i<count($list);$i++){
  191. $list[$i]['children'] = SystemMenu::getLevel($list[$i]['value']);
  192. if (!empty($list[$i]['children'])){
  193. for ($s=0;$s<count($list[$i]['children']);$s++){
  194. $list[$i]['children'][$s]['children'] = SystemMenu::getLevel($list[$i]['children'][$s]['value']);
  195. }
  196. }
  197. }
  198. }
  199. return $list;
  200. }
  201. /**
  202. * Notes:获取菜单列表
  203. * @param string $keywords
  204. * @param int $page
  205. * @param int $limit
  206. * @return array
  207. * User: ZQ
  208. * Date: 2022/9/13
  209. */
  210. public static function secondLevel()
  211. {
  212. $list = SystemMenu::getLevel(0);
  213. if (!empty($list)){
  214. for ($i=0;$i<count($list);$i++){
  215. $list[$i]['children'] = SystemMenu::getLevel($list[$i]['value']);
  216. }
  217. }
  218. return $list;
  219. }
  220. /**
  221. * Notes:修改角色
  222. * @param int $menu_is_show
  223. * @param int $menu_id
  224. * @return int
  225. * User: ZQ
  226. * Date: 2022/9/3
  227. */
  228. public static function updateStatus($menu_id, $menu_is_show)
  229. {
  230. SystemMenu::affairBegin();
  231. try {
  232. $where = [];
  233. $where['menu_id'] = $menu_id;
  234. $data = [];
  235. $data['menu_is_show'] = $menu_is_show;
  236. $result = SystemMenu::where($where)->update($data);
  237. if ($result !== false){
  238. SystemMenu::affairCommit();
  239. return true;
  240. }
  241. throw new \Exception('操作失败!');
  242. }catch (\Exception $exception){
  243. SystemMenu::affairRollback();
  244. throw new \Exception($exception->getMessage(), 500);
  245. }
  246. }
  247. /**
  248. * Notes:菜单列表
  249. * @param array $adminInfo
  250. * @return array
  251. * User: yym
  252. * Date: 2022/9/21
  253. */
  254. public static function getHomeMenuList(array $adminInfo)
  255. {
  256. // $redis_key = 'roles_' . $adminInfo['admin_roles'];
  257. // if(Redis::exists($redis_key))
  258. // {
  259. // $list = json_decode(Redis::get($redis_key), true);
  260. // }else{
  261. $menu_list_ids = array();
  262. if($adminInfo['admin_is_boos'] != 1)
  263. {
  264. $rules = SystemRole::getRuleList(explode(',', $adminInfo['admin_roles']));
  265. if(!empty($rules))
  266. {
  267. foreach ($rules as $row)
  268. {
  269. $menu_list_ids = empty($menu_list_ids) ? explode(',', $row['role_rules']) : array_merge($menu_list_ids, explode(',', $row['role_rules']));
  270. }
  271. }else{
  272. $menu_list_ids = array(0);
  273. }
  274. }
  275. if(!empty($menu_list_ids))
  276. {
  277. $SystemMenu = new SystemMenu();
  278. //获取每个自己的父级
  279. foreach ($menu_list_ids as $item)
  280. {
  281. if($item <= 0){
  282. continue;
  283. }
  284. $menu_list_ids = array_merge($menu_list_ids, $SystemMenu->getParentPid($item));
  285. }
  286. $menu_list_ids = array_unique($menu_list_ids);
  287. }
  288. $list = SystemMenu::getHomeMenuList($menu_list_ids);
  289. // Redis::setEx($redis_key, 86400, json_encode($list));
  290. // }
  291. return $list;
  292. }
  293. }