123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace App\Http\Controllers\MenuUrl;
- use App\Http\Controllers\AdminBaseController;
- use App\Http\Requests\MenuUrlCreateRequest;
- use App\Http\Requests\MenuUrlUpdateRequest;
- use App\Models\Menu;
- use App\Models\MenuUrl;
- use App\Repositories\Eloquent\MenuUrlRepositoryEloquent;
- use App\Validators\MenuUrlValidator;
- use Illuminate\Support\Facades\DB;
- /**
- * Class MenuUrlsController.
- *
- * @package namespace App\Http\Controllers;
- */
- class MenuUrlsController extends AdminBaseController
- {
- /**
- * @var MenuUrlRepositoryEloquent
- */
- protected $repository;
- /**
- * @var MenuUrlValidator
- */
- protected $validator;
- /**
- * MenuUrlsController constructor.
- *
- * @param MenuUrlRepositoryEloquent $repository
- * @param MenuUrlValidator $validator
- */
- public function __construct(MenuUrlRepositoryEloquent $repository, MenuUrlValidator $validator)
- {
- parent::__construct($repository, $validator);
- }
- /**
- * 数据检索
- */
- public function _indexScopeQuery()
- {
- $where = [];
- $search = explode(";", request()->input('search', ""));
- $start = $end = null;
- $fieldSearchable = $this->repository->getFieldsSearchable();
- foreach ($search as $value) {
- if (!empty($value)) {
- list($one, $tow) = explode(":", $value);
- if (!empty($fieldSearchable[$one])) {
- if ($fieldSearchable[$one] == 'like') $tow = "%{$tow}%";
- $where[] = [$one, $fieldSearchable[$one], $tow];
- } elseif ($one == 'start') {
- $start = $tow . " 00:00:00";
- } elseif ($one == 'end') {
- $end = $tow . " 23:59:59";
- } else {
- continue;
- }
- }
- }
- return function ($query) use ($where, $start, $end) {
- if ($start) $where[] = ['created_at', '>=', $start];
- if ($end) $where[] = ['created_at', '<=', $end];
- if ($where) {
- $query->where($where);
- }
- };
- }
- protected function _indexJoin()
- {
- return MenuUrl::from('menu_urls as u')->leftJoin('menus as m', 'u.menu_id', '=', 'm.id');
- }
- protected function _indexSelect()
- {
- return ['u.url_name', 'u.id', 'u.url', 'u.created_at', 'u.updated_at', 'm.menu_name'];
- }
- /**
- * 修改数据组建
- * @param $id
- * @return array
- */
- function _editGet($id)
- {
- $model = $this->repository->find($id);
- $option = [
- [
- 'value' => '0',
- 'label' => "请选择菜单",
- 'disabled' => false,
- 'notice' => '*'
- ]
- ];
- $menu_list = Menu::where('p_id', 0)->select(['id', 'menu_name'])->get();
- foreach ($menu_list as $menu) {
- $option[] = [
- 'value' => $menu->{'id'},
- 'label' => $menu->{'menu_name'},
- 'disabled' => false,
- 'notice' => '*'
- ];
- }
- return ['model' => $model, 'one_list' => json_encode($option, JSON_UNESCAPED_SLASHES)];
- }
- /**
- * 更新路由菜单信息
- * @return bool
- * @throws \Exception
- */
- function _updatePost()
- {
- $data = request()->input();
- if (empty($data['url_name'])) {
- $this->errorMsg = '请输入地址名称';
- return false;
- }
- if (empty($data['child_id'])) {
- $this->errorMsg = '请选择对应菜单';
- return false;
- }
- DB::beginTransaction();
- //删除二级信息
- $this->delAuth($data['id'],'1');
- //删除三级信息
- $this->delAuth($data['id'],'2');
- //添加二级信息
- $this->addAuth($data['menu_id'],$data['id']);
- //添加三级信息
- $this->addAuth($data['child_id'],$data['id']);
- MenuUrl::where('id', $data['id'])->update($data);
- DB::commit();
- return true;
- }
- /**
- * 删除授权ID
- * @param $del_id
- * @param string $type
- */
- private function delAuth($del_id,$type='1'){
- $old_menu_id = MenuUrl::where('id', $del_id)->value($type=='1'?'menu_id':'child_id');
- if (!empty($old_menu_id)) {
- $old_url_ids = Menu::where('id', $old_menu_id)->value('url_ids');
- $old_url_ids = explode(',', $old_url_ids);
- $old_url_ids = array_diff($old_url_ids, [$del_id]);
- Menu::where('id', $old_menu_id)->update(['url_ids' => implode(',', $old_url_ids)]);
- }
- }
- /**
- * 添加授权ID
- * @param $menu_id
- * @param $add_id
- */
- private function addAuth($menu_id,$add_id){
- $menu_url_ids = Menu::where('id', $menu_id)->value('url_ids');
- $menu_url_ids = explode(',', $menu_url_ids);
- $menu_url_ids[] = $add_id;
- Menu::where('id', $menu_id)->update(['url_ids' => implode(',', $menu_url_ids)]);
- }
- }
|