MenusController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace App\Http\Controllers\Menu;
  3. use App\Http\Controllers\AdminBaseController;
  4. use App\Http\Requests\MenuCreateRequest;
  5. use App\Http\Requests\MenuUpdateRequest;
  6. use App\Models\Menu;
  7. use App\Repositories\Eloquent\MenuRepositoryEloquent;
  8. use App\Validators\MenuValidator;
  9. /**
  10. * Class MenusController.
  11. *
  12. * @package namespace App\Http\Controllers;
  13. */
  14. class MenusController extends AdminBaseController
  15. {
  16. /**
  17. * @var MenuRepositoryEloquent
  18. */
  19. protected $repository;
  20. /**
  21. * @var MenuValidator
  22. */
  23. protected $validator;
  24. /**
  25. * MenusController constructor.
  26. *
  27. * @param MenuRepositoryEloquent $repository
  28. * @param MenuValidator $validator
  29. */
  30. public function __construct(MenuRepositoryEloquent $repository, MenuValidator $validator)
  31. {
  32. parent::__construct($repository, $validator);
  33. }
  34. /**
  35. * 数据检索
  36. */
  37. public function _indexScopeQuery()
  38. {
  39. $where = [];
  40. $search = explode(";", request()->input('search', ""));
  41. $start = $end = null;
  42. $fieldSearchable = $this->repository->getFieldsSearchable();
  43. $p_id = request()->input('p_id', '0');
  44. $where [] = ['is_del', 0];
  45. $where [] = ['p_id', $p_id];
  46. $where [] = ['version', 1];//获取未修改之前版本的菜单
  47. foreach ($search as $value) {
  48. if (!empty($value)) {
  49. list($one, $tow) = explode(":", $value);
  50. if (!empty($fieldSearchable[$one])) {
  51. if ($fieldSearchable[$one] == 'like') $tow = "%{$tow}%";
  52. $where[] = [$one, $fieldSearchable[$one], $tow];
  53. } elseif ($one == 'start') {
  54. $start = $tow . " 00:00:00";
  55. } elseif ($one == 'end') {
  56. $end = $tow . " 23:59:59";
  57. } else {
  58. continue;
  59. }
  60. }
  61. }
  62. return function ($query) use ($where, $start, $end) {
  63. if ($start) $where[] = ['created_at', '>=', $start];
  64. if ($end) $where[] = ['created_at', '<=', $end];
  65. if ($where) {
  66. $query->where($where);
  67. }
  68. };
  69. }
  70. /**
  71. * 数据更新
  72. */
  73. protected function _indexPost($datum)
  74. {
  75. foreach ($datum as $item) {
  76. if ($item->level <= 1) {
  77. $item->skip = '';
  78. } else {
  79. $item->skip = 'skip';
  80. }
  81. $item->{'level'} = $item->{'level'} == 0 ? '一级菜单' : ($item->{'level'} == 2 ? '三级菜单' : '二级菜单');
  82. }
  83. return $datum;
  84. }
  85. /**
  86. * 创建数据组建
  87. * @return array
  88. */
  89. function _storeGet()
  90. {
  91. $p_id = request()->input('p_id');
  92. $level = request()->input('level', '0');
  93. $level_list = $this->getLevelList($level);
  94. $one_id = $two_id = 0;
  95. if ($level == 2) {
  96. $two_id = $p_id;
  97. $one_id = Menu::where('id', $p_id)->value('p_id');
  98. } elseif ($level == 1) {
  99. $one_id = $p_id;
  100. }
  101. return ['p_id' => $p_id, 'level_list' => json_encode($level_list, JSON_UNESCAPED_SLASHES), 'level' => $level, 'two_id' => $two_id, 'one_id' => $one_id];
  102. }
  103. function _storePost()
  104. {
  105. $menu_name = request()->input('menu_name', '');
  106. if (empty($menu_name)) {
  107. $this->errorMsg = "请输入菜单名称";
  108. return false;
  109. }
  110. $level = request()->input('level', '0');
  111. $p_id = 0;
  112. $url_name = $label_name = $p_ids = '';
  113. if ($level == 0) {
  114. $label_name = request()->input('label_name', '');
  115. if (empty($label_name)) {
  116. $this->errorMsg = "请选择菜单标签";
  117. return false;
  118. }
  119. } elseif ($level == 1) {
  120. $url_name = request()->input('url_name', '');
  121. if (empty($url_name)) {
  122. $this->errorMsg = "请输入访问地址";
  123. return false;
  124. }
  125. $one_name = request()->input('one_name', '0');
  126. if (empty($one_name)) {
  127. $this->errorMsg = "请选择父级菜单";
  128. return false;
  129. }
  130. $p_ids = $p_id = $one_name;
  131. } else {
  132. $one_name = request()->input('one_name', '0');
  133. $two_name = request()->input('two_name', '0');
  134. if (empty($two_name)) {
  135. $this->errorMsg = "请选择父级菜单";
  136. return false;
  137. }
  138. $p_ids = $one_name . ',' . $two_name;
  139. $p_id = $two_name;
  140. }
  141. $data = compact('p_id', 'p_ids', 'url_name', 'label_name', 'level', 'menu_name');
  142. return Menu::create($data);
  143. }
  144. /**
  145. * 修改数据组建
  146. * @param $id
  147. * @return array
  148. */
  149. function _editGet($id)
  150. {
  151. $model = $this->repository->find($id);
  152. $one_id = $two_id = 0;
  153. if ($model->{'level'} == 2) {
  154. $two_id = $model->{'p_id'};
  155. $one_id = Menu::where('id', $model->{'p_id'})->value('p_id');
  156. } elseif ($model->{'level'} == 1) {
  157. $one_id = $model->{'p_id'};
  158. }
  159. $level_list = $this->getLevelList($model->{'level'});
  160. return ['model' => $model, 'p_id' => $model->{'p_id'}, 'level_list' => json_encode($level_list, JSON_UNESCAPED_SLASHES), 'level' => $model->{'level'}, 'two_id' => $two_id, 'one_id' => $one_id];
  161. }
  162. function _updatePost($request, $id)
  163. {
  164. $menu_name = request()->input('menu_name', '');
  165. if (empty($menu_name)) {
  166. $this->errorMsg = "请输入菜单名称";
  167. return false;
  168. }
  169. $level = request()->input('level', '0');
  170. $p_id = 0;
  171. $url_name = $label_name = $p_ids = '';
  172. if ($level == 0) {
  173. $label_name = request()->input('label_name', '');
  174. if (empty($label_name)) {
  175. $this->errorMsg = "请选择菜单标签";
  176. return false;
  177. }
  178. } elseif ($level == 1) {
  179. $url_name = request()->input('url_name', '');
  180. if (empty($url_name)) {
  181. $this->errorMsg = "请输入访问地址";
  182. return false;
  183. }
  184. $one_name = request()->input('one_name', '0');
  185. if (empty($one_name)) {
  186. $this->errorMsg = "请选择父级菜单";
  187. return false;
  188. }
  189. $p_ids = $p_id = $one_name;
  190. } else {
  191. $one_name = request()->input('one_name', '0');
  192. $two_name = request()->input('two_name', '0');
  193. if (empty($two_name)) {
  194. $this->errorMsg = "请选择父级菜单";
  195. return false;
  196. }
  197. $p_ids = $one_name . ',' . $two_name;
  198. $p_id = $two_name;
  199. }
  200. $data = compact('p_id', 'p_ids', 'url_name', 'label_name', 'level', 'menu_name');
  201. return Menu::where('id', $id)->update($data);
  202. }
  203. /**
  204. * 获取层级菜单组件
  205. * @return array
  206. */
  207. private function getLevelList($level = '0')
  208. {
  209. $option = [
  210. 'checked' => $level . '',
  211. 'attr' => [
  212. [
  213. 'value' => '0',
  214. 'label' => "一级菜单",
  215. 'disabled' => false,
  216. 'notice' => '*'
  217. ],
  218. [
  219. 'value' => '1',
  220. 'label' => "二级菜单",
  221. 'disabled' => false,
  222. 'notice' => '*'
  223. ],
  224. [
  225. 'value' => '2',
  226. 'label' => "三级菜单",
  227. 'disabled' => false,
  228. 'notice' => '*'
  229. ],
  230. ]
  231. ];
  232. return $option;
  233. }
  234. /**
  235. * 获取父级菜单组件信息
  236. * @return array
  237. */
  238. private function getMenuList($p_id = '0')
  239. {
  240. $option = [
  241. 'checked' => $p_id . '',
  242. 'attr' => [
  243. [
  244. 'value' => '0',
  245. 'label' => "请选择菜单",
  246. 'disabled' => false,
  247. 'notice' => '*'
  248. ]
  249. ]
  250. ];
  251. $menu_list = Menu::where('p_id', 0)->where('version',1)->select(['id', 'menu_name'])->get();
  252. foreach ($menu_list as $menu) {
  253. $option['attr'][] = [
  254. 'value' => $menu->{'id'},
  255. 'label' => $menu->{'menu_name'},
  256. 'disabled' => false,
  257. 'notice' => '*'
  258. ];
  259. }
  260. return $option;
  261. }
  262. /**
  263. * 获取父级数据
  264. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
  265. */
  266. function getParentMenuList()
  267. {
  268. $p_id = request()->input('p_id', '0');
  269. $option = [
  270. [
  271. 'value' => '0',
  272. 'label' => "请选择菜单",
  273. 'disabled' => false,
  274. 'notice' => '*'
  275. ]
  276. ];
  277. $menu_list = Menu::where('p_id', $p_id)->where('version',1)->select(['id', 'menu_name'])->get();
  278. foreach ($menu_list as $menu) {
  279. $option[] = [
  280. 'value' => $menu->{'id'},
  281. 'label' => $menu->{'menu_name'},
  282. 'disabled' => false,
  283. 'notice' => '*'
  284. ];
  285. }
  286. return $this->response('获取数据成功', 'success', '', '', $option);
  287. }
  288. }