SystemMenu.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace app\admin\model;
  3. use support\Db;
  4. use support\Model;
  5. /**
  6. * 角色模型
  7. * Class Users
  8. * @package app\admin\model
  9. */
  10. class SystemMenu extends Model
  11. {
  12. const STATUS_DEL_NO = '1';
  13. const STATUS_DEL_YES = '2';
  14. const STATUS_DEL = [
  15. self::STATUS_DEL_NO => '正常',
  16. self::STATUS_DEL_YES => '删除'
  17. ];
  18. const MENU_MENU = '1';
  19. const MENU_RULE = '2';
  20. const MENU_TYPE = [
  21. self::MENU_MENU => '菜单',
  22. self::MENU_RULE => '权限'
  23. ];
  24. const MENU_SHOW_NO = '0';
  25. const MENU_SHOW_YES = '1';
  26. const MENU_SHOW = [
  27. self::MENU_SHOW_NO => '隐藏',
  28. self::MENU_SHOW_YES => '显示'
  29. ];
  30. const MENU_LEVEL_ONE = '1';
  31. const MENU_LEVEL_TWO = '2';
  32. const MENU_LEVEL_THE = '3';
  33. /**
  34. * The table associated with the model.
  35. *
  36. * @var string
  37. */
  38. protected $table = 'system_menu';
  39. public $timestamps = false;
  40. /**
  41. * Notes:父级模型关联
  42. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  43. * User: yym
  44. * Date: 2022/9/21
  45. */
  46. public function children()
  47. {
  48. $where = array(
  49. 'menu_status' => static::STATUS_DEL_NO,
  50. 'menu_is_show' => static::MENU_SHOW_YES,
  51. 'menu_is_menu' => static::MENU_MENU
  52. );
  53. return $this->hasMany(static::class, 'menu_pid', 'menu_id')->where($where)->select(['menu_id', 'menu_pid', 'menu_path', 'menu_icon', 'menu_name', 'menu_route'])->orderBy('menu_sort', 'desc');
  54. }
  55. /**
  56. * Notes:获取菜单列表
  57. * @param string $keywords
  58. * @param int $page
  59. * @param int $limit
  60. * @return array
  61. * User: ZQ
  62. * Date: 2022/9/14
  63. */
  64. public static function getMenuList(string $keywords)
  65. {
  66. $list = static::select('*')->where(['menu_status'=>static::STATUS_DEL_NO])
  67. ->when($keywords != '', function ($query) use ($keywords){
  68. $query->where('menu_name', 'like', '%' . $keywords . '%');
  69. })
  70. ->orderBy('menu_create_time','DESC')
  71. ->get();
  72. $count = static::where(['menu_status'=>static::STATUS_DEL_NO])
  73. ->when($keywords != '', function ($query) use ($keywords){
  74. $query->where('menu_name', 'like', '%' . $keywords . '%');
  75. })
  76. ->count();
  77. return $list;
  78. }
  79. /**
  80. * Notes:获取菜单
  81. * @param int $menu_pid
  82. * @return array
  83. * User: ZQ
  84. * Date: 2022/9/13
  85. */
  86. public static function getMenus(int $menu_pid)
  87. {
  88. $list = static::select('*')
  89. ->where(['menu_pid' => $menu_pid, 'menu_status' => static::STATUS_DEL_NO])
  90. ->orderBy('menu_sort','DESC')
  91. ->get();
  92. return $list;
  93. }
  94. /**
  95. * Notes:三级菜单
  96. * @param int $menu_pid
  97. * @return array
  98. * User: ZQ
  99. * Date: 2022/9/15
  100. */
  101. public static function getLevel($menu_pid)
  102. {
  103. $list = static::select(['menu_id as value','menu_name as label'])
  104. ->where(['menu_pid' => $menu_pid, 'menu_status' => static::STATUS_DEL_NO])
  105. ->orderBy('menu_sort','DESC')
  106. ->get();
  107. return $list;
  108. }
  109. /**
  110. * Notes:获取菜单
  111. * @param int $menus
  112. * @return array
  113. * User: ZQ
  114. * Date: 2022/9/13
  115. */
  116. public static function menusMation($menus)
  117. {
  118. $list = static::where(['menu_status'=>static::STATUS_DEL_NO])
  119. ->when($menus != '', function ($query) use ($menus){
  120. $query->whereIn('menu_id', $menus);
  121. })
  122. ->get();
  123. return $list;
  124. }
  125. /**
  126. * Notes:获取角色对应权限菜单
  127. * @param array $menu_ids
  128. * @return array
  129. * User: yym
  130. * Date: 2022/9/21
  131. */
  132. public static function getMenuListRule(array $menu_ids)
  133. {
  134. return static::where(['menu_status' => static::STATUS_DEL_NO])
  135. ->whereIn('menu_id', $menu_ids)
  136. ->get()
  137. ->toArray();
  138. }
  139. /**
  140. * Notes:菜单列表
  141. * @param array $menu_list_ids
  142. * @return array
  143. * User: yym
  144. * Date: 2022/9/21
  145. */
  146. public static function getHomeMenuList(array $menu_list_ids)
  147. {
  148. $where = array(
  149. 'menu_status' => static::STATUS_DEL_NO,
  150. 'menu_is_show' => static::MENU_SHOW_YES,
  151. 'menu_is_menu' => static::MENU_MENU,
  152. 'level' => STATIC::MENU_LEVEL_ONE
  153. );
  154. return static::where($where)
  155. ->select(['menu_id', 'menu_pid', 'menu_path', 'menu_icon', 'menu_name', 'menu_route'])
  156. ->when(!empty($menu_list_ids), function ($query) use ($menu_list_ids){
  157. $query->whereIn('menu_id', $menu_list_ids);
  158. })
  159. ->with(['children' => function($query) use ($menu_list_ids){
  160. $query->with('children')->when(!empty($menu_list_ids), function ($query1) use ($menu_list_ids){
  161. $query1->whereIn('menu_id', $menu_list_ids);
  162. });
  163. }])
  164. ->orderBy('menu_sort', 'desc')
  165. ->get()
  166. ->toArray();
  167. }
  168. /**
  169. * Notes:查询指定条件下的某个字段内容
  170. * @param int $menu_id
  171. * @param string $field
  172. * User: yym
  173. * Date: 2022/9/27
  174. */
  175. public static function getParentValue(int $menu_id, string $field = 'level')
  176. {
  177. return static::where(['menu_id' => $menu_id])->value($field);
  178. }
  179. /**
  180. * Notes:获取父级菜单编号 处理成数组形式
  181. * @param int $menu_pid
  182. * @return array|false|mixed
  183. * User: yym
  184. * Date: 2022/9/26
  185. */
  186. public function getParentMenuId(int $menu_pid)
  187. {
  188. $parent = array();
  189. $parent = $this->getParentPid($menu_pid);
  190. if(!empty($parent))
  191. {
  192. $parent = array_merge([$menu_pid], $parent);
  193. array_pop($parent);
  194. }
  195. return array_reverse($parent);
  196. }
  197. /**
  198. * Notes:递归查询父级ID
  199. * @param $menu_id
  200. * @param array $date
  201. * @return array|false|mixed
  202. * User: yym
  203. * Date: 2022/9/26
  204. */
  205. public function getParentPid($menu_id, $date=[]){
  206. if(empty($menu_id) && empty($date))
  207. {
  208. return array();
  209. }
  210. if(empty($menu_id) && !empty($date)){
  211. return $date;
  212. }
  213. $parentIds = array();
  214. $parentIds = SystemMenu::select('menu_pid')->where(['menu_id' => $menu_id, 'menu_status' => SystemMenu::STATUS_DEL_NO])->get()->toArray();
  215. $subIds = array();
  216. $subIds = array_column($parentIds, 'menu_pid');
  217. $subIds = array_unique($subIds);
  218. if(count($subIds)>0)
  219. {
  220. foreach($subIds as $val)
  221. {
  222. $date[] = $val;
  223. $date = $this->getParentPid($val, $date); //注意写$date 返回给上级
  224. }
  225. }
  226. if(count($date)>0)
  227. {
  228. return $date;
  229. }else{
  230. return false;
  231. }
  232. }
  233. }