AdminMenuModel.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 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 huo\model\menu;
  12. use think\Cache;
  13. use think\Model;
  14. class AdminMenuModel extends Model {
  15. protected $name = 'admin_menu';
  16. //验证菜单是否超出三级
  17. public function checkParentId($parentId) {
  18. $find = $this->where(["id" => $parentId])->value("parent_id");
  19. if ($find) {
  20. $find2 = $this->where(["id" => $find])->value("parent_id");
  21. if ($find2) {
  22. $find3 = $this->where(["id" => $find2])->value("parent_id");
  23. if ($find3) {
  24. return false;
  25. }
  26. }
  27. }
  28. return true;
  29. }
  30. //验证action是否重复添加
  31. public function checkAction($data) {
  32. //检查是否重复添加
  33. $find = $this->where($data)->find();
  34. if ($find) {
  35. return false;
  36. }
  37. return true;
  38. }
  39. //验证action是否重复添加
  40. public function checkActionUpdate($data) {
  41. //检查是否重复添加
  42. $id = $data['id'];
  43. unset($data['id']);
  44. $find = $this->field('id')->where($data)->find();
  45. if (isset($find['id']) && $find['id'] != $id) {
  46. return false;
  47. }
  48. return true;
  49. }
  50. /**
  51. * 按父ID查找菜单子项
  52. *
  53. * @param int $parentId 父菜单ID
  54. * @param boolean $withSelf 是否包括他自己
  55. *
  56. * @return mixed
  57. */
  58. public function adminMenu($parentId, $withSelf = false) {
  59. //父节点ID
  60. $parentId = (int)$parentId;
  61. $result = $this->where(['parent_id' => $parentId, 'status' => 1])->order("list_order", "DESC")->select();
  62. if ($withSelf) {
  63. $result2[] = $this->where(['id' => $parentId])->find();
  64. $result = array_merge($result2, $result);
  65. }
  66. //权限检查
  67. if (cmf_get_current_admin_id() == 1) {
  68. //如果是超级管理员 直接通过
  69. return $result;
  70. }
  71. $array = [];
  72. foreach ($result as $v) {
  73. //方法
  74. $action = $v['action'];
  75. //public开头的通过
  76. if (preg_match('/^public_/', $action)) {
  77. $array[] = $v;
  78. } else {
  79. if (preg_match('/^ajax_([a-z]+)_/', $action, $_match)) {
  80. $action = $_match[1];
  81. }
  82. $rule_name = strtolower($v['app']."/".$v['controller']."/".$action);
  83. // print_r($rule_name);
  84. if (cmf_auth_check(cmf_get_current_admin_id(), $rule_name)) {
  85. $array[] = $v;
  86. }
  87. }
  88. }
  89. return $array;
  90. }
  91. /**
  92. * 获取菜单 头部菜单导航
  93. *
  94. * @param string $parentId 菜单id
  95. *
  96. * @return mixed|string
  97. */
  98. public function subMenu($parentId = '', $bigMenu = false) {
  99. $array = $this->adminMenu($parentId, 1);
  100. $numbers = count($array);
  101. if ($numbers == 1 && !$bigMenu) {
  102. return '';
  103. }
  104. return $array;
  105. }
  106. /**
  107. * 菜单树状结构集合
  108. */
  109. public function menuTree() {
  110. $data = $this->getTree(0);
  111. return $data;
  112. }
  113. /**
  114. * 取得树形结构的菜单
  115. *
  116. * @param $myId
  117. * @param string $parent
  118. * @param int $Level
  119. *
  120. * @return bool|null
  121. */
  122. public function getTree($myId, $parent = "", $Level = 1) {
  123. $data = $this->adminMenu($myId);
  124. $Level++;
  125. if (count($data) > 0) {
  126. $ret = null;
  127. foreach ($data as $a) {
  128. $id = $a['id'];
  129. $name = $a['app'];
  130. $controller = ucwords($a['controller']);
  131. $action = $a['action'];
  132. //附带参数
  133. $params = "";
  134. if ($a['param']) {
  135. $params = "?".htmlspecialchars_decode($a['param']);
  136. }
  137. $array = [
  138. "icon" => $a['icon'],
  139. "id" => $id.$name,
  140. "remark" => $a['remark'],
  141. "name" => lang($a['name']),
  142. "parent" => $parent,
  143. "url" => url("{$name}/{$controller}/{$action}{$params}"),
  144. 'lang' => strtoupper($name.'_'.$controller.'_'.$action)
  145. ];
  146. $ret[$id.$name] = $array;
  147. $child = $this->getTree($a['id'], $id, $Level);
  148. //由于后台管理界面只支持三层,超出的不层级的不显示
  149. if ($child && $Level <= 3) {
  150. $ret[$id.$name]['items'] = $child;
  151. }
  152. }
  153. return $ret;
  154. }
  155. return false;
  156. }
  157. /**
  158. * 更新缓存
  159. *
  160. * @param $data
  161. *
  162. * @return array
  163. */
  164. public function menuCache($data = null) {
  165. if (empty($data)) {
  166. $data = $this->order("list_order", "DESC")->column('');
  167. Cache::set('Menu', $data, 0);
  168. } else {
  169. Cache::set('Menu', $data, 0);
  170. }
  171. return $data;
  172. }
  173. /**
  174. * 后台有更新/编辑则删除缓存
  175. *
  176. * @param type $data
  177. */
  178. public function _before_write(&$data) {
  179. parent::_before_write($data);
  180. }
  181. //删除操作时删除缓存
  182. public function _after_delete($data, $options) {
  183. parent::_after_delete($data, $options);
  184. $this->_before_write($data);
  185. }
  186. public function menu($parentId, $with_self = false) {
  187. //父节点ID
  188. $parentId = (int)$parentId;
  189. $result = $this->where(['parent_id' => $parentId])->select();
  190. if ($with_self) {
  191. $result2[] = $this->where(['id' => $parentId])->find();
  192. $result = array_merge($result2, $result);
  193. }
  194. return $result;
  195. }
  196. /**
  197. * 得到某父级菜单所有子菜单,包括自己
  198. *
  199. * @param number $parentId
  200. */
  201. public function get_menu_tree($parentId = 0) {
  202. $menus = $this->where(["parent_id" => $parentId])->order(["list_order" => "DESC"])->select();
  203. if ($menus) {
  204. foreach ($menus as $key => $menu) {
  205. $children = $this->get_menu_tree($menu['id']);
  206. if (!empty($children)) {
  207. $menus[$key]['children'] = $children;
  208. }
  209. unset($menus[$key]['id']);
  210. unset($menus[$key]['parent_id']);
  211. }
  212. return $menus;
  213. } else {
  214. return $menus;
  215. }
  216. }
  217. /**
  218. * @param $criteria
  219. *
  220. * @return mixed
  221. */
  222. public function getMenuIdPpUrl($criteria) {
  223. $_rdata['url'] = url($criteria['app'].DS.$criteria['controller'].DS.$criteria['action']);
  224. $_rdata['id'] = $this->where($criteria)->value('id');
  225. $_rdata['pp_id'] = $this->getPpId($_rdata['id']);
  226. return $_rdata;
  227. }
  228. /**
  229. * 获取祖宗ID
  230. *
  231. * @param $id
  232. *
  233. * @return mixed
  234. */
  235. public function getPpId($id) {
  236. $_parent_id = $id;
  237. $_pp_id = $_parent_id;
  238. while ($_parent_id > 0) {
  239. $_pp_id = $_parent_id;
  240. $_parent_id = $this->where(["id" => $_pp_id])->value("parent_id");
  241. }
  242. return $_pp_id;
  243. }
  244. }