MenuModel.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. /* *
  8. * 菜单
  9. */
  10. namespace Common\Model;
  11. use Common\Model\CommonModel;
  12. class MenuModel extends CommonModel {
  13. //自动验证
  14. protected $_validate = array(
  15. //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
  16. array('name', 'require', '菜单名称不能为空!', 1, 'regex', CommonModel:: MODEL_BOTH ),
  17. array('app', 'require', '应用不能为空!', 1, 'regex', CommonModel:: MODEL_BOTH ),
  18. array('model', 'require', '模块名称不能为空!', 1, 'regex', CommonModel:: MODEL_BOTH ),
  19. array('action', 'require', '方法名称不能为空!', 1, 'regex', CommonModel:: MODEL_BOTH ),
  20. array('app,model,action', 'checkAction', '同样的记录已经存在!', 1, 'callback', CommonModel:: MODEL_INSERT ),
  21. array('id,app,model,action', 'checkActionUpdate', '同样的记录已经存在!', 1, 'callback', CommonModel:: MODEL_UPDATE ),
  22. array('parentid', 'checkParentid', '菜单只支持四级!', 1, 'callback', 1),
  23. );
  24. //自动完成
  25. protected $_auto = array(
  26. //array(填充字段,填充内容,填充条件,附加规则)
  27. );
  28. //验证菜单是否超出三级
  29. public function checkParentid($parentid) {
  30. $find = $this->where(array("id" => $parentid))->getField("parentid");
  31. if ($find) {
  32. $find2 = $this->where(array("id" => $find))->getField("parentid");
  33. if ($find2) {
  34. $find3 = $this->where(array("id" => $find2))->getField("parentid");
  35. if ($find3) {
  36. return false;
  37. }
  38. }
  39. }
  40. return true;
  41. }
  42. //验证action是否重复添加
  43. public function checkAction($data) {
  44. //检查是否重复添加
  45. $find = $this->where($data)->find();
  46. if ($find) {
  47. return false;
  48. }
  49. return true;
  50. }
  51. //验证action是否重复添加
  52. public function checkActionUpdate($data) {
  53. //检查是否重复添加
  54. $id=$data['id'];
  55. unset($data['id']);
  56. $find = $this->field('id')->where($data)->find();
  57. if (isset($find['id']) && $find['id']!=$id) {
  58. return false;
  59. }
  60. return true;
  61. }
  62. /**
  63. * 按父ID查找菜单子项
  64. * @param integer $parentid 父菜单ID
  65. * @param integer $with_self 是否包括他自己
  66. */
  67. public function admin_menu($parentid, $with_self = false) {
  68. //父节点ID
  69. $parentid = (int) $parentid;
  70. $result = $this->where(array('parentid' => $parentid, 'status' => 1))->order(array("listorder" => "ASC"))->select();
  71. if ($with_self) {
  72. $result2[] = $this->where(array('id' => $parentid))->find();
  73. $result = array_merge($result2, $result);
  74. }
  75. //权限检查
  76. if (sp_get_current_admin_id() == 1) {
  77. //如果是超级管理员 直接通过
  78. return $result;
  79. }
  80. $array = array();
  81. foreach ($result as $v) {
  82. //方法
  83. $action = $v['action'];
  84. //public开头的通过
  85. if (preg_match('/^public_/', $action)) {
  86. $array[] = $v;
  87. } else {
  88. if (preg_match('/^ajax_([a-z]+)_/', $action, $_match)){
  89. $action = $_match[1];
  90. }
  91. $rule_name=strtolower($v['app']."/".$v['model']."/".$action);
  92. if ( sp_auth_check(sp_get_current_admin_id(),$rule_name)){
  93. $array[] = $v;
  94. }
  95. }
  96. }
  97. return $array;
  98. }
  99. /**
  100. * 获取菜单 头部菜单导航
  101. * @param $parentid 菜单id
  102. */
  103. public function submenu($parentid = '', $big_menu = false) {
  104. $array = $this->admin_menu($parentid, 1);
  105. $numbers = count($array);
  106. if ($numbers == 1 && !$big_menu) {
  107. return '';
  108. }
  109. return $array;
  110. }
  111. /**
  112. * 菜单树状结构集合
  113. */
  114. public function menu_json() {
  115. $data = $this->get_tree(0);
  116. return $data;
  117. }
  118. //取得树形结构的菜单
  119. public function get_tree($myid, $parent = "", $Level = 1) {
  120. $data = $this->admin_menu($myid);
  121. $Level++;
  122. if (is_array($data)) {
  123. foreach ($data as $a) {
  124. $id = $a['id'];
  125. $name = ucwords($a['app']);
  126. $model = ucwords($a['model']);
  127. $action = $a['action'];
  128. //附带参数
  129. $fu = "";
  130. if ($a['data']) {
  131. $fu = "?" . htmlspecialchars_decode($a['data']);
  132. }
  133. $array = array(
  134. "icon" => $a['icon'],
  135. "id" => $id . $name,
  136. "name" => $a['name'],
  137. "parent" => $parent,
  138. "url" => U("{$name}/{$model}/{$action}{$fu}", array("menuid" => $id)),
  139. 'lang'=> strtoupper($name.'_'.$model.'_'.$action)
  140. );
  141. $ret[$id . $name] = $array;
  142. $child = $this->get_tree($a['id'], $id, $Level);
  143. //由于后台管理界面只支持三层,超出的不层级的不显示
  144. if ($child && $Level <= 3) {
  145. $ret[$id . $name]['items'] = $child;
  146. }
  147. }
  148. return $ret;
  149. }
  150. return false;
  151. }
  152. /**
  153. * 更新缓存
  154. * @param type $data
  155. * @return type
  156. */
  157. public function menu_cache($data = null) {
  158. if (empty($data)) {
  159. $data = $this->select();
  160. F("Menu", $data);
  161. } else {
  162. F("Menu", $data);
  163. }
  164. return $data;
  165. }
  166. /**
  167. * 后台有更新/编辑则删除缓存
  168. * @param type $data
  169. */
  170. public function _before_write(&$data) {
  171. parent::_before_write($data);
  172. F("Menu", NULL);
  173. }
  174. //删除操作时删除缓存
  175. public function _after_delete($data, $options) {
  176. parent::_after_delete($data, $options);
  177. $this->_before_write($data);
  178. }
  179. public function menu($parentid, $with_self = false){
  180. //父节点ID
  181. $parentid = (int) $parentid;
  182. $result = $this->where(array('parentid' => $parentid))->select();
  183. if ($with_self) {
  184. $result2[] = $this->where(array('id' => $parentid))->find();
  185. $result = array_merge($result2, $result);
  186. }
  187. return $result;
  188. }
  189. /**
  190. * 得到某父级菜单所有子菜单,包括自己
  191. * @param number $parentid
  192. */
  193. public function get_menu_tree($parentid=0){
  194. $menus=$this->where(array("parentid"=>$parentid))->order(array("listorder"=>"ASC"))->select();
  195. if($menus){
  196. foreach ($menus as $key=>$menu){
  197. $children=$this->get_menu_tree($menu['id']);
  198. if(!empty($children)){
  199. $menus[$key]['children']=$children;
  200. }
  201. unset($menus[$key]['id']);
  202. unset($menus[$key]['parentid']);
  203. }
  204. return $menus;
  205. }else{
  206. return $menus;
  207. }
  208. }
  209. }