Menus.php 6.2 KB

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