Menu.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 09/03/2017
  6. * Time: 10:52
  7. */
  8. namespace app\common\models;
  9. use app\common\traits\TreeTrait;
  10. use Illuminate\Database\Eloquent\SoftDeletes;
  11. use Illuminate\Validation\Rule;
  12. class Menu extends BaseModel
  13. {
  14. use TreeTrait,SoftDeletes;
  15. public $table = 'yz_menu';
  16. //设置字段默认值
  17. public $attributes = [
  18. 'parent_id'=>0,
  19. 'name'=>'',
  20. 'item'=>'',
  21. 'url'=>'',
  22. 'url_params'=>'',
  23. 'permit'=>1,
  24. 'menu'=>1,
  25. 'icon'=>'',
  26. 'sort'=>0,
  27. 'status'=>1
  28. ];
  29. //不可填充
  30. public $guarded = [''];
  31. const STATUS_ENABLED = 1;
  32. const STATUS_DISABLED = 0;
  33. public function getDateFormat() {
  34. return 'U';
  35. }
  36. /**
  37. * 父菜单与子菜单栏目1:n关系
  38. *
  39. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  40. */
  41. public function childs()
  42. {
  43. return $this->hasMany('app\backend\models\Menu','parent_id','id');
  44. }
  45. /**
  46. * 子菜单与父菜单1:1关系
  47. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  48. */
  49. public function parent()
  50. {
  51. return $this->belongsTo('app\backend\models\Menu','parent_id','id');
  52. }
  53. /**
  54. * 获取待处理的原始节点数据
  55. *
  56. * 必须实现
  57. *
  58. * return \Illuminate\Support\Collection
  59. */
  60. public function getTreeAllNodes()
  61. {
  62. return self::orderBy('sort', 'asc')->get();
  63. }
  64. /**
  65. * 获取菜单栏目
  66. *
  67. * @param $parent_id
  68. * @param int $child_switch
  69. * @return mixed
  70. */
  71. public static function getMenuAllInfo($parent_id = 0, $child_switch = 1)
  72. {
  73. $result = self::where('parent_id', $parent_id)
  74. ->where('status', 1)
  75. ->orderBy('sort', 'asc');
  76. if ($child_switch) {
  77. $result = $result->with(['childs'=>function ($query) {
  78. return $query->where('status', self::STATUS_ENABLED)->orderBy('sort', 'asc');
  79. }]);
  80. }
  81. return $result;
  82. }
  83. /**
  84. * 生成 config 菜单数据结构
  85. *
  86. * @param int $parentId
  87. * @return array
  88. */
  89. public static function getMenuList($parentId = 0, $parent = [])
  90. {
  91. $list = [];
  92. $menuList = static::select('id','name','url','url_params','permit','menu','icon','parent_id','sort','item')
  93. ->where(['parent_id' => $parentId,'status'=>self::STATUS_ENABLED])
  94. ->with('childs')
  95. ->orderby('sort')
  96. ->get();
  97. if($menuList){
  98. foreach ($menuList as $key=>$value){
  99. $list[$value->item] = $value->toArray();
  100. $list[$value->item]['parents'] = $parent;
  101. array_forget($list[$value->item],'childs');
  102. if($value->childs->count() > 0){
  103. $list[$value->item]['child'] = self::getMenuList($value->id, array_merge($parent,(array) $value->item));
  104. }
  105. }
  106. }
  107. return $list;
  108. }
  109. /**
  110. * 获取当前菜单父级item
  111. * @param $item
  112. * @param array $menuList
  113. * @return mixed
  114. */
  115. public static function getCurrentMenuParents($item, array $menuList)
  116. {
  117. static $current = [];
  118. //dump($menuList);
  119. foreach($menuList as $key=>$value){
  120. //dump($key);
  121. if($key == $item){
  122. $current = $value['parents'];
  123. //dd(11);
  124. break;
  125. }
  126. if(isset($value['child']) && $value['child']){
  127. $current = self::getCurrentMenuParents($item,$value['child']);
  128. }
  129. }
  130. //dd($menuList);
  131. //dd($current);
  132. //exit;
  133. return $current;
  134. }
  135. /**
  136. * 获取 item from route
  137. * @param $route
  138. * @param array $menuList
  139. * @return array|int|mixed|string
  140. */
  141. public static function getCurrentItemByRoute($route, array $menuList)
  142. {
  143. static $current = null;
  144. foreach($menuList as $key=>$value){
  145. if(isset($value['url']) && $value['url'] == $route){
  146. $current = $key;
  147. break;
  148. }
  149. if(isset($value['child']) && $value['child']){
  150. $current = self::getCurrentItemByRoute($route,$value['child']);
  151. }
  152. }
  153. return $current;
  154. }
  155. public static function getItemByRoute($route)
  156. {
  157. $data = static::select('item')->where(['url'=>$route])->first();
  158. return $data ? $data->item : '';
  159. }
  160. /**
  161. * 通过ID获取菜单栏目
  162. *
  163. * @param $id
  164. * @return mixed
  165. */
  166. public static function getMenuInfoById($id)
  167. {
  168. return self::where('id', $id)->with(['childs'])->first();
  169. }
  170. /**
  171. * 重写检测提示文字
  172. * @return array
  173. */
  174. public function validationMessages()
  175. {
  176. return array_merge(parent::validationMessages(),[
  177. "different" => " 不能选择自己为上级。"
  178. ]);
  179. }
  180. /**
  181. * 定义字段名
  182. * 可使
  183. * @return array */
  184. public function atributeNames() {
  185. return [
  186. 'item'=> '标识',
  187. 'name'=> '菜单',
  188. 'url'=> 'URL',
  189. 'url_params'=> 'URL参数',
  190. 'icon'=> '图标',
  191. 'sort'=> '排序',
  192. 'permit'=> '权限控制',
  193. 'menu'=> '菜单显示',
  194. 'status'=> '状态',
  195. ];
  196. }
  197. /**
  198. * 字段规则
  199. * @return array */
  200. public function rules() {
  201. $rule = [
  202. //具体unique可看文档 https://laravel.com/docs/5.4/validation#rule-unique
  203. 'item' => ['required',Rule::unique($this->table)->ignore($this->id)],
  204. 'name' => 'required|max:45',
  205. 'url' => 'max:255',
  206. 'url_params' => 'max:255',
  207. 'icon' => 'max:45',
  208. 'sort' => 'required|integer',
  209. 'permit' => 'required|digits_between:0,1',
  210. 'menu' => 'required|digits_between:0,1',
  211. 'status' => 'required|digits_between:0,1'
  212. ];
  213. //修改时不能选择自己做为上级
  214. if((int)$this->getAttributeValue('id') > 0){
  215. $rule = array_merge(['parent_id' => 'different:id'], $rule);
  216. }
  217. return $rule;
  218. }
  219. }