MenuUrlsController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Http\Controllers\MenuUrl;
  3. use App\Http\Controllers\AdminBaseController;
  4. use App\Http\Requests\MenuUrlCreateRequest;
  5. use App\Http\Requests\MenuUrlUpdateRequest;
  6. use App\Models\Menu;
  7. use App\Models\MenuUrl;
  8. use App\Repositories\Eloquent\MenuUrlRepositoryEloquent;
  9. use App\Validators\MenuUrlValidator;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * Class MenuUrlsController.
  13. *
  14. * @package namespace App\Http\Controllers;
  15. */
  16. class MenuUrlsController extends AdminBaseController
  17. {
  18. /**
  19. * @var MenuUrlRepositoryEloquent
  20. */
  21. protected $repository;
  22. /**
  23. * @var MenuUrlValidator
  24. */
  25. protected $validator;
  26. /**
  27. * MenuUrlsController constructor.
  28. *
  29. * @param MenuUrlRepositoryEloquent $repository
  30. * @param MenuUrlValidator $validator
  31. */
  32. public function __construct(MenuUrlRepositoryEloquent $repository, MenuUrlValidator $validator)
  33. {
  34. parent::__construct($repository, $validator);
  35. }
  36. /**
  37. * 数据检索
  38. */
  39. public function _indexScopeQuery()
  40. {
  41. $where = [];
  42. $search = explode(";", request()->input('search', ""));
  43. $start = $end = null;
  44. $fieldSearchable = $this->repository->getFieldsSearchable();
  45. foreach ($search as $value) {
  46. if (!empty($value)) {
  47. list($one, $tow) = explode(":", $value);
  48. if (!empty($fieldSearchable[$one])) {
  49. if ($fieldSearchable[$one] == 'like') $tow = "%{$tow}%";
  50. $where[] = [$one, $fieldSearchable[$one], $tow];
  51. } elseif ($one == 'start') {
  52. $start = $tow . " 00:00:00";
  53. } elseif ($one == 'end') {
  54. $end = $tow . " 23:59:59";
  55. } else {
  56. continue;
  57. }
  58. }
  59. }
  60. return function ($query) use ($where, $start, $end) {
  61. if ($start) $where[] = ['created_at', '>=', $start];
  62. if ($end) $where[] = ['created_at', '<=', $end];
  63. if ($where) {
  64. $query->where($where);
  65. }
  66. };
  67. }
  68. protected function _indexJoin()
  69. {
  70. return MenuUrl::from('menu_urls as u')->leftJoin('menus as m', 'u.menu_id', '=', 'm.id');
  71. }
  72. protected function _indexSelect()
  73. {
  74. return ['u.url_name', 'u.id', 'u.url', 'u.created_at', 'u.updated_at', 'm.menu_name'];
  75. }
  76. /**
  77. * 修改数据组建
  78. * @param $id
  79. * @return array
  80. */
  81. function _editGet($id)
  82. {
  83. $model = $this->repository->find($id);
  84. $option = [
  85. [
  86. 'value' => '0',
  87. 'label' => "请选择菜单",
  88. 'disabled' => false,
  89. 'notice' => '*'
  90. ]
  91. ];
  92. $menu_list = Menu::where('p_id', 0)->select(['id', 'menu_name'])->get();
  93. foreach ($menu_list as $menu) {
  94. $option[] = [
  95. 'value' => $menu->{'id'},
  96. 'label' => $menu->{'menu_name'},
  97. 'disabled' => false,
  98. 'notice' => '*'
  99. ];
  100. }
  101. return ['model' => $model, 'one_list' => json_encode($option, JSON_UNESCAPED_SLASHES)];
  102. }
  103. /**
  104. * 更新路由菜单信息
  105. * @return bool
  106. * @throws \Exception
  107. */
  108. function _updatePost()
  109. {
  110. $data = request()->input();
  111. if (empty($data['url_name'])) {
  112. $this->errorMsg = '请输入地址名称';
  113. return false;
  114. }
  115. if (empty($data['child_id'])) {
  116. $this->errorMsg = '请选择对应菜单';
  117. return false;
  118. }
  119. DB::beginTransaction();
  120. //删除二级信息
  121. $this->delAuth($data['id'],'1');
  122. //删除三级信息
  123. $this->delAuth($data['id'],'2');
  124. //添加二级信息
  125. $this->addAuth($data['menu_id'],$data['id']);
  126. //添加三级信息
  127. $this->addAuth($data['child_id'],$data['id']);
  128. MenuUrl::where('id', $data['id'])->update($data);
  129. DB::commit();
  130. return true;
  131. }
  132. /**
  133. * 删除授权ID
  134. * @param $del_id
  135. * @param string $type
  136. */
  137. private function delAuth($del_id,$type='1'){
  138. $old_menu_id = MenuUrl::where('id', $del_id)->value($type=='1'?'menu_id':'child_id');
  139. if (!empty($old_menu_id)) {
  140. $old_url_ids = Menu::where('id', $old_menu_id)->value('url_ids');
  141. $old_url_ids = explode(',', $old_url_ids);
  142. $old_url_ids = array_diff($old_url_ids, [$del_id]);
  143. Menu::where('id', $old_menu_id)->update(['url_ids' => implode(',', $old_url_ids)]);
  144. }
  145. }
  146. /**
  147. * 添加授权ID
  148. * @param $menu_id
  149. * @param $add_id
  150. */
  151. private function addAuth($menu_id,$add_id){
  152. $menu_url_ids = Menu::where('id', $menu_id)->value('url_ids');
  153. $menu_url_ids = explode(',', $menu_url_ids);
  154. $menu_url_ids[] = $add_id;
  155. Menu::where('id', $menu_id)->update(['url_ids' => implode(',', $menu_url_ids)]);
  156. }
  157. }