Menus.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\store\service;
  3. use think\Config;
  4. /**
  5. * 商家后台菜单业务
  6. * Class Menus
  7. * @package app\store\service
  8. */
  9. class Menus
  10. {
  11. /** @var self $instance 存放实例 */
  12. static public $instance;
  13. /** @var Auth $auth 商家后台权限 */
  14. private $auth;
  15. /**
  16. * 公有化获取实例方法
  17. * @return Menus
  18. * @throws \think\exception\DbException
  19. */
  20. public static function getInstance()
  21. {
  22. if (!(self::$instance instanceof Auth)) {
  23. self::$instance = new self;
  24. }
  25. return self::$instance;
  26. }
  27. /**
  28. * 私有化构造方法
  29. * Menus constructor.
  30. * @throws \think\exception\DbException
  31. */
  32. private function __construct()
  33. {
  34. $this->auth = Auth::getInstance();
  35. }
  36. /**
  37. * 私有化克隆方法
  38. */
  39. private function __clone()
  40. {
  41. }
  42. /**
  43. * 后台菜单配置
  44. * @param $routeUri
  45. * @param $group
  46. * @return array|mixed
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. public function getMenus($routeUri, $group)
  52. {
  53. // 菜单列表数据
  54. $menus = Config::get('menus');
  55. $this->first($menus, $routeUri, $group);
  56. // pre($menus);
  57. return $menus;
  58. }
  59. /**
  60. * 一级菜单
  61. * @param $menus
  62. * @param $routeUri
  63. * @param $group
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. */
  68. private function first(&$menus, $routeUri, $group)
  69. {
  70. foreach ($menus as $key => &$first) :
  71. // 一级菜单索引url
  72. $indexData = $this->getMenusIndexUrls($first, 1);
  73. // 权限验证
  74. $first['index'] = $this->getAuthUrl($indexData);
  75. if ($first['index'] === false) {
  76. unset($menus[$key]);
  77. continue;
  78. }
  79. // 菜单聚焦
  80. $first['active'] = $key === $group;
  81. // 遍历:二级菜单
  82. if (isset($first['submenu'])) {
  83. $this->second($first['submenu'], $routeUri);
  84. }
  85. endforeach;
  86. }
  87. /**
  88. * 二级菜单
  89. * @param array $menus
  90. * @param $routeUri
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. * @throws \think\exception\DbException
  94. */
  95. public function second(&$menus, $routeUri)
  96. {
  97. foreach ($menus as $key => &$second) :
  98. // 二级菜单索引url
  99. $indexData = $this->getMenusIndexUrls($second, 2);
  100. // 权限验证
  101. $second['index'] = $this->getAuthUrl($indexData);
  102. if ($second['index'] === false) {
  103. unset($menus[$key]);
  104. continue;
  105. }
  106. // 二级菜单所有uri
  107. $secondUris = [];
  108. // 遍历:三级菜单
  109. if (isset($second['submenu'])) {
  110. $this->third($second['submenu'], $routeUri, $secondUris);
  111. } else {
  112. if (isset($second['uris']))
  113. $secondUris = array_merge($secondUris, $second['uris']);
  114. else
  115. $secondUris[] = $second['index'];
  116. }
  117. // 二级菜单:active
  118. !isset($second['active']) && $second['active'] = in_array($routeUri, $secondUris);
  119. endforeach;
  120. // 删除空数组
  121. $menus = array_filter($menus);
  122. }
  123. /**
  124. * 三级菜单
  125. * @param array $menus
  126. * @param $routeUri
  127. * @param $secondUris
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. * @throws \think\exception\DbException
  131. */
  132. private function third(&$menus, $routeUri, &$secondUris)
  133. {
  134. foreach ($menus as $key => &$third):
  135. // 三级菜单索引url
  136. $indexData = $this->getMenusIndexUrls($third, 3);
  137. // 权限验证
  138. $third['index'] = $this->getAuthUrl($indexData);
  139. if ($third['index'] === false) {
  140. unset($menus[$key]);
  141. continue;
  142. }
  143. // 三级菜单所有uri
  144. $thirdUris = [];
  145. if (isset($third['uris'])) {
  146. $secondUris = array_merge($secondUris, $third['uris']);
  147. $thirdUris = array_merge($thirdUris, $third['uris']);
  148. } else {
  149. $secondUris[] = $third['index'];
  150. $thirdUris[] = $third['index'];
  151. }
  152. $third['active'] = in_array($routeUri, $thirdUris);
  153. endforeach;
  154. }
  155. /**
  156. * 获取指定菜单下的所有索引url
  157. * @param array $menus
  158. * @param int $level
  159. * @return array|null
  160. */
  161. private function getMenusIndexUrls(&$menus, $level = 1)
  162. {
  163. // // 三级
  164. // if ($level === 3) {
  165. // return isset($menus['index']) ? [$menus['index']] : null;
  166. // }
  167. // 判断是否存在url
  168. if (!isset($menus['index']) && !isset($menus['submenu'])) {
  169. return null;
  170. }
  171. $data = [];
  172. if (isset($menus['index']) && !empty($menus['index'])) {
  173. $data[] = $menus['index'];
  174. }
  175. if (isset($menus['submenu']) && !empty($menus['submenu'])) {
  176. foreach ($menus['submenu'] as $submenu) {
  177. $submenuIndex = $this->getMenusIndexUrls($submenu, ++$level);
  178. !is_null($submenuIndex) && $data = array_merge($data, $submenuIndex);
  179. }
  180. }
  181. return array_unique($data);
  182. }
  183. /**
  184. * 取出通过权限验证urk作为index
  185. * @param $urls
  186. * @return bool
  187. * @throws \think\db\exception\DataNotFoundException
  188. * @throws \think\db\exception\ModelNotFoundException
  189. * @throws \think\exception\DbException
  190. */
  191. private function getAuthUrl($urls)
  192. {
  193. // 取出通过权限验证urk作为index
  194. foreach ($urls as $url) {
  195. if ($this->auth->checkPrivilege($url)) return $url;
  196. }
  197. return false;
  198. }
  199. }