AdminMenuModel.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model;
  12. use think\Model;
  13. use think\facade\Cache;
  14. class AdminMenuModel extends Model
  15. {
  16. /**
  17. * 模型名称
  18. * @var string
  19. */
  20. protected $name = 'admin_menu';
  21. /**
  22. * 按父ID查找菜单子项
  23. * @param int $parentId 父菜单ID
  24. * @param boolean $withSelf 是否包括他自己
  25. * @return array|string|\think\Collection
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. * @throws \think\exception\DbException
  29. */
  30. public function adminMenu($parentId, $withSelf = false)
  31. {
  32. //父节点ID
  33. $parentId = intval($parentId);
  34. $result = $this->where(['parent_id' => $parentId, 'status' => 1])->order("list_order", "ASC")->select();
  35. if ($withSelf) {
  36. $result2[] = $this->where('id', $parentId)->find();
  37. $result = array_merge($result2, $result);
  38. }
  39. //权限检查
  40. if (cmf_get_current_admin_id() == 1) {
  41. //如果是超级管理员 直接通过
  42. return $result;
  43. }
  44. $array = [];
  45. foreach ($result as $v) {
  46. //方法
  47. $action = $v['action'];
  48. //public开头的通过
  49. if (preg_match('/^public_/', $action)) {
  50. $array[] = $v;
  51. } else {
  52. if (preg_match('/^ajax_([a-z]+)_/', $action, $_match)) {
  53. $action = $_match[1];
  54. }
  55. $ruleName = strtolower($v['app'] . "/" . $v['controller'] . "/" . $action);
  56. // print_r($ruleName);
  57. if (cmf_auth_check(cmf_get_current_admin_id(), $ruleName)) {
  58. $array[] = $v;
  59. }
  60. }
  61. }
  62. return $array;
  63. }
  64. /**
  65. * 获取菜单 头部菜单导航
  66. * @param string $parentId 菜单id
  67. * @param bool $bigMenu
  68. * @return array|string|\think\Collection
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. * @throws \think\exception\DbException
  72. */
  73. public function subMenu($parentId = '', $bigMenu = false)
  74. {
  75. $array = $this->adminMenu($parentId, 1);
  76. $numbers = count($array);
  77. if ($numbers == 1 && !$bigMenu) {
  78. return '';
  79. }
  80. return $array;
  81. }
  82. /**
  83. * 菜单树状结构集合
  84. */
  85. public function menuTree()
  86. {
  87. $data = $this->getTree(0);
  88. return $data;
  89. }
  90. /**
  91. * 取得树形结构的菜单
  92. * @param $myId
  93. * @param string $parent
  94. * @param int $Level
  95. * @return bool|null
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @throws \think\exception\DbException
  99. */
  100. public function getTree($myId, $parent = "", $Level = 1)
  101. {
  102. $data = $this->adminMenu($myId);
  103. $Level++;
  104. if (count($data) > 0) {
  105. $ret = NULL;
  106. foreach ($data as $a) {
  107. $id = $a['id'];
  108. $app = $a['app'];
  109. $controller = ucwords($a['controller']);
  110. $action = $a['action'];
  111. //附带参数
  112. $params = "";
  113. if (!empty($a['param'])) {
  114. $params = htmlspecialchars_decode($a['param']);
  115. }
  116. if (strpos($app, 'plugin/') === 0) {
  117. $pluginName = str_replace('plugin/', '', $app);
  118. $url = cmf_plugin_url($pluginName . "://{$controller}/{$action}{$params}");
  119. } else {
  120. parse_str($params,$paramsArr);
  121. $url = url("{$app}/{$controller}/{$action}", $paramsArr);
  122. }
  123. $app = str_replace('/', '_', $app);
  124. $array = [
  125. "icon" => $a['icon'],
  126. "id" => $id . $app,
  127. "name" => $a['name'],
  128. "parent" => $parent,
  129. "url" => $url,
  130. 'lang' => strtoupper($app . '_' . $controller . '_' . $action)
  131. ];
  132. $ret[$id . $app] = $array;
  133. $child = $this->getTree($a['id'], $id, $Level);
  134. //由于后台管理界面只支持三层,超出的不层级的不显示
  135. if ($child && $Level <= 3) {
  136. $ret[$id . $app]['items'] = $child;
  137. }
  138. }
  139. return $ret;
  140. }
  141. return false;
  142. }
  143. /**
  144. * 更新缓存
  145. * @param null $data
  146. * @return array|null
  147. */
  148. public function menuCache($data = null)
  149. {
  150. if (empty($data)) {
  151. $data = $this->order("list_order", "ASC")->column('*','id');
  152. Cache::set('Menu', $data, 0);
  153. } else {
  154. Cache::set('Menu', $data, 0);
  155. }
  156. return $data;
  157. }
  158. public function menu($parentId, $with_self = false)
  159. {
  160. //父节点ID
  161. $parentId = (int)$parentId;
  162. $result = $this->where('parent_id', $parentId)->select();
  163. if ($with_self) {
  164. $result2[] = $this->where('id', $parentId)->find();
  165. $result = array_merge($result2, $result);
  166. }
  167. return $result;
  168. }
  169. /**
  170. * 得到某父级菜单所有子菜单,包括自己
  171. * @param int $parentId
  172. * @return array|string|\think\Collection
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. * @throws \think\exception\DbException
  176. */
  177. public function getMenuTree($parentId = 0)
  178. {
  179. $menus = $this->where("parent_id", $parentId)->order(["list_order" => "ASC"])->select();
  180. if ($menus) {
  181. foreach ($menus as $key => $menu) {
  182. $children = $this->getMenuTree($menu['id']);
  183. if (!empty($children)) {
  184. $menus[$key]['children'] = $children;
  185. }
  186. unset($menus[$key]['id']);
  187. unset($menus[$key]['parent_id']);
  188. }
  189. return $menus;
  190. } else {
  191. return $menus;
  192. }
  193. }
  194. }