123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- namespace App\Http\Controllers\Menu;
- use App\Http\Controllers\AdminBaseController;
- use App\Http\Requests\MenuCreateRequest;
- use App\Http\Requests\MenuUpdateRequest;
- use App\Models\Menu;
- use App\Repositories\Eloquent\MenuRepositoryEloquent;
- use App\Validators\MenuValidator;
- /**
- * Class MenusController.
- *
- * @package namespace App\Http\Controllers;
- */
- class MenusController extends AdminBaseController
- {
- /**
- * @var MenuRepositoryEloquent
- */
- protected $repository;
- /**
- * @var MenuValidator
- */
- protected $validator;
- /**
- * MenusController constructor.
- *
- * @param MenuRepositoryEloquent $repository
- * @param MenuValidator $validator
- */
- public function __construct(MenuRepositoryEloquent $repository, MenuValidator $validator)
- {
- parent::__construct($repository, $validator);
- }
- /**
- * 数据检索
- */
- public function _indexScopeQuery()
- {
- $where = [];
- $search = explode(";", request()->input('search', ""));
- $start = $end = null;
- $fieldSearchable = $this->repository->getFieldsSearchable();
- $p_id = request()->input('p_id', '0');
- $where [] = ['is_del', 0];
- $where [] = ['p_id', $p_id];
- $where [] = ['version', 1];//获取未修改之前版本的菜单
- 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 _indexPost($datum)
- {
- foreach ($datum as $item) {
- if ($item->level <= 1) {
- $item->skip = '';
- } else {
- $item->skip = 'skip';
- }
- $item->{'level'} = $item->{'level'} == 0 ? '一级菜单' : ($item->{'level'} == 2 ? '三级菜单' : '二级菜单');
- }
- return $datum;
- }
- /**
- * 创建数据组建
- * @return array
- */
- function _storeGet()
- {
- $p_id = request()->input('p_id');
- $level = request()->input('level', '0');
- $level_list = $this->getLevelList($level);
- $one_id = $two_id = 0;
- if ($level == 2) {
- $two_id = $p_id;
- $one_id = Menu::where('id', $p_id)->value('p_id');
- } elseif ($level == 1) {
- $one_id = $p_id;
- }
- return ['p_id' => $p_id, 'level_list' => json_encode($level_list, JSON_UNESCAPED_SLASHES), 'level' => $level, 'two_id' => $two_id, 'one_id' => $one_id];
- }
- function _storePost()
- {
- $menu_name = request()->input('menu_name', '');
- if (empty($menu_name)) {
- $this->errorMsg = "请输入菜单名称";
- return false;
- }
- $level = request()->input('level', '0');
- $p_id = 0;
- $url_name = $label_name = $p_ids = '';
- if ($level == 0) {
- $label_name = request()->input('label_name', '');
- if (empty($label_name)) {
- $this->errorMsg = "请选择菜单标签";
- return false;
- }
- } elseif ($level == 1) {
- $url_name = request()->input('url_name', '');
- if (empty($url_name)) {
- $this->errorMsg = "请输入访问地址";
- return false;
- }
- $one_name = request()->input('one_name', '0');
- if (empty($one_name)) {
- $this->errorMsg = "请选择父级菜单";
- return false;
- }
- $p_ids = $p_id = $one_name;
- } else {
- $one_name = request()->input('one_name', '0');
- $two_name = request()->input('two_name', '0');
- if (empty($two_name)) {
- $this->errorMsg = "请选择父级菜单";
- return false;
- }
- $p_ids = $one_name . ',' . $two_name;
- $p_id = $two_name;
- }
- $data = compact('p_id', 'p_ids', 'url_name', 'label_name', 'level', 'menu_name');
- return Menu::create($data);
- }
- /**
- * 修改数据组建
- * @param $id
- * @return array
- */
- function _editGet($id)
- {
- $model = $this->repository->find($id);
- $one_id = $two_id = 0;
- if ($model->{'level'} == 2) {
- $two_id = $model->{'p_id'};
- $one_id = Menu::where('id', $model->{'p_id'})->value('p_id');
- } elseif ($model->{'level'} == 1) {
- $one_id = $model->{'p_id'};
- }
- $level_list = $this->getLevelList($model->{'level'});
- 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];
- }
- function _updatePost($request, $id)
- {
- $menu_name = request()->input('menu_name', '');
- if (empty($menu_name)) {
- $this->errorMsg = "请输入菜单名称";
- return false;
- }
- $level = request()->input('level', '0');
- $p_id = 0;
- $url_name = $label_name = $p_ids = '';
- if ($level == 0) {
- $label_name = request()->input('label_name', '');
- if (empty($label_name)) {
- $this->errorMsg = "请选择菜单标签";
- return false;
- }
- } elseif ($level == 1) {
- $url_name = request()->input('url_name', '');
- if (empty($url_name)) {
- $this->errorMsg = "请输入访问地址";
- return false;
- }
- $one_name = request()->input('one_name', '0');
- if (empty($one_name)) {
- $this->errorMsg = "请选择父级菜单";
- return false;
- }
- $p_ids = $p_id = $one_name;
- } else {
- $one_name = request()->input('one_name', '0');
- $two_name = request()->input('two_name', '0');
- if (empty($two_name)) {
- $this->errorMsg = "请选择父级菜单";
- return false;
- }
- $p_ids = $one_name . ',' . $two_name;
- $p_id = $two_name;
- }
- $data = compact('p_id', 'p_ids', 'url_name', 'label_name', 'level', 'menu_name');
- return Menu::where('id', $id)->update($data);
- }
- /**
- * 获取层级菜单组件
- * @return array
- */
- private function getLevelList($level = '0')
- {
- $option = [
- 'checked' => $level . '',
- 'attr' => [
- [
- 'value' => '0',
- 'label' => "一级菜单",
- 'disabled' => false,
- 'notice' => '*'
- ],
- [
- 'value' => '1',
- 'label' => "二级菜单",
- 'disabled' => false,
- 'notice' => '*'
- ],
- [
- 'value' => '2',
- 'label' => "三级菜单",
- 'disabled' => false,
- 'notice' => '*'
- ],
- ]
- ];
- return $option;
- }
- /**
- * 获取父级菜单组件信息
- * @return array
- */
- private function getMenuList($p_id = '0')
- {
- $option = [
- 'checked' => $p_id . '',
- 'attr' => [
- [
- 'value' => '0',
- 'label' => "请选择菜单",
- 'disabled' => false,
- 'notice' => '*'
- ]
- ]
- ];
- $menu_list = Menu::where('p_id', 0)->where('version',1)->select(['id', 'menu_name'])->get();
- foreach ($menu_list as $menu) {
- $option['attr'][] = [
- 'value' => $menu->{'id'},
- 'label' => $menu->{'menu_name'},
- 'disabled' => false,
- 'notice' => '*'
- ];
- }
- return $option;
- }
- /**
- * 获取父级数据
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
- */
- function getParentMenuList()
- {
- $p_id = request()->input('p_id', '0');
- $option = [
- [
- 'value' => '0',
- 'label' => "请选择菜单",
- 'disabled' => false,
- 'notice' => '*'
- ]
- ];
- $menu_list = Menu::where('p_id', $p_id)->where('version',1)->select(['id', 'menu_name'])->get();
- foreach ($menu_list as $menu) {
- $option[] = [
- 'value' => $menu->{'id'},
- 'label' => $menu->{'menu_name'},
- 'disabled' => false,
- 'notice' => '*'
- ];
- }
- return $this->response('获取数据成功', 'success', '', '', $option);
- }
- }
|