Common.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/20
  6. * @time: 17:59
  7. */
  8. namespace app\controller\admin;
  9. use app\common\AdminBaseController;
  10. use app\services\system\admin\SystemMenusServices;
  11. use app\services\system\CityAreaServices;
  12. use Psr\SimpleCache\InvalidArgumentException;
  13. use qiniu\services\CacheService;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. class Common extends AdminBaseController
  18. {
  19. public function adminInfo(SystemMenusServices $services)
  20. {
  21. $adminInfo = $this->request->adminInfo();
  22. [$menus, $uniqueAuth] = $services->getMenusList($adminInfo['roles'], (int)$adminInfo['level']);
  23. return $this->success([
  24. 'menus' => $menus,
  25. 'unique_auth' => $uniqueAuth,
  26. 'role' => $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles'], 1, 0),
  27. 'user_info' => [
  28. 'id' => $adminInfo['id'],
  29. 'account' => $adminInfo['account'],
  30. 'head_pic' => $adminInfo['head_pic'],
  31. ]
  32. ]);
  33. }
  34. /**
  35. * 格式化菜单
  36. * @return mixed
  37. * @throws DataNotFoundException
  38. * @throws DbException
  39. * @throws ModelNotFoundException|InvalidArgumentException
  40. */
  41. public function menusList()
  42. {
  43. $cahcheKey = md5('admin_common_menu_list');
  44. $list = CacheService::redisHandler()->get($cahcheKey);
  45. if (!$list) {
  46. /** @var SystemMenusServices $menusServices */
  47. $menusServices = app()->make(SystemMenusServices::class);
  48. $menus = $menusServices->search(['is_show' => 1, 'auth_type' => 1, 'is_show_path' => 0])->where('type', 1)
  49. ->field('id,pid,menu_name,menu_path,unique_auth,sort')->order('sort DESC')->select();
  50. $counts = $menusServices->getColumn([
  51. ['is_show', '=', 1],
  52. ['auth_type', '=', 1],
  53. ['is_show_path', '=', 0],
  54. ], 'pid');
  55. $data = [];
  56. foreach ($menus as $key => $item) {
  57. $pid = $item->getData('pid');
  58. $data[$key] = json_decode($item, true);
  59. $data[$key]['pid'] = $pid;
  60. if (in_array($item->id, $counts)) {
  61. $data[$key]['type'] = 1;
  62. } else {
  63. $data[$key]['type'] = 0;
  64. }
  65. $data[$key]['menu_path'] = preg_replace('/^\/admin/', '', $item['menu_path']);
  66. }
  67. $list = sort_list_tier($data);
  68. CacheService::redisHandler()->set($cahcheKey, $list, 86400);
  69. }
  70. return app('json')->success($list);
  71. }
  72. /**
  73. * @param CityAreaServices $services
  74. * @return mixed
  75. * @throws DataNotFoundException
  76. * @throws DbException
  77. * @throws ModelNotFoundException
  78. */
  79. public function city(CityAreaServices $services)
  80. {
  81. $pid = $this->request->get('pid', 0);
  82. $type = $this->request->get('type', 0);
  83. return $this->success($services->getCityTreeList((int)$pid, $type));
  84. }
  85. }