123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- namespace app\controller\admin;
- use app\service\admin\admin\UserServiceFacade;
- use laytp\controller\Backend;
- use laytp\library\CommonFun;
- use laytp\library\Tree;
- use think\facade\Config;
- /**
- * 菜单控制器
- */
- class Menu extends Backend
- {
- public $noNeedAuth = ['getMenuTree', 'getTree'];
- public $model;
- public $orderRule = ['sort' => 'desc', 'id' => 'asc'];
- public function _initialize()
- {
- $this->model = new \app\model\admin\Menu();
- }
- public function index()
- {
- global $_W;
- $where = $this->buildSearchParams(false);
- // $where[] = ['uniacid','=',$_W['uniacid']];
- $order = $this->buildOrder();
- $sourceData = $this->model->where($where)->order($order);
- $isTree = $this->request->param('is_tree');
- if ($isTree) {
- $menuTreeObj = Tree::instance();
- $menuTreeObj->init($sourceData->select()->toArray());
- $data = $menuTreeObj->getRootTrees();
- } else {
- $paging = $this->request->param('paging', false);
- if ($paging) {
- $limit = $this->request->param('limit', Config::get('paginate.limit'));
- $data = $sourceData->paginate($limit)->toArray();
- $data['data'] = $this->getSelectedData($data['data']);
- } else {
- $data = $sourceData->select()->toArray();
- }
- }
- return $this->success('数据获取成功', $data);
- }
- //获取当前登录者的权限列表,返回树形数据,角色管理赋予权限时用到
- public function getTree()
- {
- $user = UserServiceFacade::getUser();
- if ($user->is_super_manager === 1) {
- $sourceData = $this->model->order($this->orderRule)->select()->toArray();
- } else {
- $roleIds = \app\model\admin\role\User::where('admin_user_id', '=', $user->id)
- ->column('admin_role_id');
- $menuIds = \app\model\admin\menu\Role::where('admin_role_id', 'in', $roleIds)
- ->column('admin_menu_id');
- $where[] = ['id', 'in', $menuIds];
- $sourceData = $this->model->order($this->orderRule)->where($where)->select()->toArray();
- }
- $menuTreeObj = Tree::instance();
- $menuTreeObj->init($sourceData);
- //由列表数据转化成树形结构数据
- $data = $menuTreeObj->getRootTrees();
- return $this->success('获取成功', $data);
- }
- //获取当前登录者的菜单列表,返回树形数据,仅返回is_menu=1的列表,后台菜单列表展示使用
- public function getMenuTree()
- {
- $user = UserServiceFacade::getUser();
- $where[] = ['is_show', '=', 1];
- $where[] = ['is_menu', '=', 1];
- if ($user->is_super_manager === 1) {
- $sourceData = $this->model->order($this->orderRule)->where($where)->select()->toArray();
- } else {
- $roleIds = \app\model\admin\role\User::where('admin_user_id', '=', $user->id)
- ->column('admin_role_id');
- $menuIds = \app\model\admin\menu\Role::where('admin_role_id', 'in', $roleIds)
- ->column('admin_menu_id');
- $where[] = ['id', 'in', $menuIds];
- $sourceData = $this->model->order($this->orderRule)->where($where)->select()->toArray();
- }
- $menuTreeObj = Tree::instance();
- $menuTreeObj->init($sourceData);
- //由列表数据转化成树形结构数据
- $data = $menuTreeObj->getRootTrees();
- return $this->success('获取成功', $data);
- }
- //添加
- public function add()
- {
- $post = CommonFun::filterPostData($this->request->post());
- if ($post['rule'] && substr($post['rule'], 0, 1) != '/') {
- $post['rule'] = '/' . $post['rule'];
- }
- if ($this->model->create($post)) {
- return $this->success('添加成功', $post);
- } else {
- return $this->error('操作失败');
- }
- }
- //编辑
- public function edit()
- {
- $id = $this->request->param('id');
- $info = $this->model->find($id);
- $post = CommonFun::filterPostData($this->request->post());
- if ($post['rule'] && substr($post['rule'], 0, 1) != '/') {
- $post['rule'] = '/' . $post['rule'];
- }
- if ($id == $post['pid']) {
- return $this->error('不能将上级改成自己');
- }
- foreach ($post as $k => $v) {
- $info->$k = $v;
- }
- $update_res = $info->save();
- if ($update_res) {
- return $this->success('操作成功');
- } else if ($update_res === 0) {
- return $this->success('未做修改');
- } else {
- return $this->error('操作失败');
- }
- }
- //删除
- public function del()
- {
- $ids = $this->request->post('ids');
- if (!$ids) {
- return $this->error('参数ids不能为空');
- }
- $sourceData = $this->model->select()->toArray();
- $treeLib = Tree::instance();
- $treeLib->init($sourceData);
- $childIds = $treeLib->getChildIds($ids);
- if ($this->model->destroy($childIds)) {
- return $this->success('数据删除成功', $childIds);
- } else {
- return $this->error('数据删除失败');
- }
- }
- //设置排序
- public function setSort()
- {
- $id = $this->request->post('id');
- $fieldVal = $this->request->post('field_val');
- $isRecycle = $this->request->post('is_recycle');
- $update['sort'] = $fieldVal;
- try {
- if ($isRecycle) {
- $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
- } else {
- $updateRes = $this->model->where('id', '=', $id)->update($update);
- }
- if ($updateRes) {
- return $this->success('操作成功');
- } else if ($updateRes === 0) {
- return $this->success('未作修改');
- } else {
- return $this->error('操作失败');
- }
- } catch (\Exception $e) {
- return $this->exceptionError($e);
- }
- }
- //设置是否为菜单
- public function setIsMenu()
- {
- $id = $this->request->post('id');
- $fieldVal = $this->request->post('field_val');
- $isRecycle = $this->request->post('is_recycle');
- $update['is_menu'] = $fieldVal;
- try {
- if ($isRecycle) {
- $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
- } else {
- $updateRes = $this->model->where('id', '=', $id)->update($update);
- }
- if ($updateRes) {
- return $this->success('操作成功');
- } else if ($updateRes === 0) {
- return $this->success('未作修改');
- } else {
- return $this->error('操作失败');
- }
- } catch (\Exception $e) {
- return $this->exceptionError($e);
- }
- }
- //设置是否显示
- public function setIsShow()
- {
- $id = $this->request->post('id');
- $fieldVal = $this->request->post('field_val');
- $isRecycle = $this->request->post('is_recycle');
- $update['is_show'] = $fieldVal;
- try {
- if ($isRecycle) {
- $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
- } else {
- $updateRes = $this->model->where('id', '=', $id)->update($update);
- }
- if ($updateRes) {
- return $this->success('操作成功');
- } else if ($updateRes === 0) {
- return $this->success('未作修改');
- } else {
- return $this->error('操作失败');
- }
- } catch (\Exception $e) {
- return $this->exceptionError($e);
- }
- }
- // 复制菜单
- public function copy()
- {
- $pid = (int)$this->request->post('pid');
- $ids = $this->request->post('ids');
- if (!$ids) {
- return $this->error('参数ids不能为空');
- }
- $data = \app\model\admin\Menu::where('id', 'in', $ids)
- ->withoutField('id')->select()
- ->each(function ($item) use ($pid) {
- $item->pid = $pid;
- })->toArray();
- $insert = $this->model->insertAll($data);
- if ($insert) {
- return $this->success('复制成功');
- } else {
- return $this->error('复制失败');
- }
- }
- // 移动菜单
- public function move()
- {
- $pid = (int)$this->request->post('pid');
- $ids = $this->request->post('ids');
- if (!$ids) {
- return $this->error('参数ids不能为空');
- }
- $save = \app\model\admin\Menu::where('id', 'in', $ids)
- ->save(['pid' => $pid]);
- if ($save) {
- return $this->success('移动成功');
- } else {
- return $this->error('移动失败');
- }
- }
- public function recycle()
- {
- global $_W;
- $where = $this->buildSearchParams(false);
- $order = $this->buildOrder();
- $data = $this->model->onlyTrashed()->where($where)->order($order);
- $limit = $this->request->param('limit', Config::get('paginate.limit'));
- $data = $data->paginate($limit)->toArray();
- $data['data'] = $this->getSelectedData($data['data']);
- return $this->success('回收站数据获取成功', $data);
- }
- }
|