| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- /**
- * @package merchant
- *
- * @author xaboy
- * @day 2020-03-24
- *
- *
- */
- namespace app\controller\admin\system\auth;
- use crmeb\basic\BaseController;
- use app\common\repositories\system\auth\MenuRepository;
- use app\validate\admin\MenuValidate;
- use FormBuilder\Exception\FormBuilderException;
- use think\App;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- use think\facade\Db;
- /**
- * Class Menu
- * @package app\controller\admin\system\menu
- * @author xaboy
- * @day 2020-04-08
- */
- class Menu extends BaseController
- {
- /**
- * @var int
- */
- protected $merchant;
- /**
- * @var MenuRepository
- */
- protected $repository;
- /**
- * Menu constructor.
- * @param App $app
- * @param MenuRepository $repository
- */
- public function __construct(App $app, MenuRepository $repository)
- {
- parent::__construct($app);
- $this->repository = $repository;
- $this->merchant = $this->request->param('merchant', 0) == 0 ? 0 : 1;
- }
- /**
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @author xaboy
- * @day 2020-04-08
- */
- public function getList()
- {
- $data = $this->repository->getList([], $this->merchant);
- $data['list'] = formatCategory($data['list'], 'menu_id');
- return app('json')->success($data);
- }
- /**
- * 创建
- * @param MenuValidate $validate
- * @return mixed
- * @author 张先生
- * @date 2020-03-30
- */
- public function create(MenuValidate $validate)
- {
- $data = $this->checkParam($validate);
- if (!$data['pid']) $data['pid'] = 0;
- if ($data['pid'] && !$this->repository->merExists($data['pid'], $this->merchant))
- return app('json')->fail('父级分类不存在');
- $data['is_mer'] = $this->merchant;
- $this->repository->create($data);
- return app('json')->success('添加成功');
- }
- /**
- * 更新
- * @param int $id id
- * @param MenuValidate $validate
- * @return mixed
- * @throws DbException
- * @author 张先生
- * @date 2020-03-30
- */
- public function update($id, MenuValidate $validate)
- {
- $data = $this->checkParam($validate);
- if (!$data['pid']) $data['pid'] = 0;
- if (!$this->repository->merExists($id, $this->merchant))
- return app('json')->fail('数据不存在');
- if ($data['pid'] && !$this->repository->merExists($data['pid'], $this->merchant))
- return app('json')->fail('父级分类不存在');
- $this->repository->update($id, $data);
- return app('json')->success('编辑成功');
- }
- /**
- * @return mixed
- * @throws FormBuilderException
- * @author xaboy
- * @day 2020-04-08
- */
- public function createForm()
- {
- return app('json')->success(formToData($this->repository->menuForm($this->merchant)));
- }
- /**
- * @param int $id
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws FormBuilderException
- * @author xaboy
- * @day 2020-04-08
- */
- public function updateForm($id)
- {
- if (!$this->repository->merExists($id, $this->merchant))
- return app('json')->fail('数据不存在');
- return app('json')->success(formToData($this->repository->updateMenuForm($id, $this->merchant)));
- }
- /**
- *
- * @param MenuValidate $validate 验证规则
- * @return mixed
- * @author 张先生
- * @date 2020-03-30
- */
- private function checkParam(MenuValidate $validate)
- {
- $data = $this->request->params([['pid', 0], 'icon', 'menu_name', 'route', ['params', '[]'], 'sort', ['is_show', 0], ['is_menu', 0]]);
- if ($data['is_menu'] != 1) $validate->isAuth();
- $validate->check($data);
- return $data;
- }
- /**
- * @param int $id
- * @return mixed
- * @throws DbException
- * @author xaboy
- * @day 2020-04-08
- */
- public function delete($id)
- {
- if ($this->repository->pidExists($id))
- return app('json')->fail('存在下级,无法删除');
- $this->repository->delete($id, $this->merchant);
- return app('json')->success('删除成功');
- }
- /**
- * @return mixed
- * @author xaboy
- * @day 2020-04-10
- */
- public function menus()
- {
- $pre = '/' . config('admin.' . ($this->merchant ? 'merchant' : 'admin') . '_prefix');
- $rule=$this->request->adminRule();
- if($this->merchant){
- $arr=[];
- foreach ($rule as $v){
- $pid=Db::name('system_menu')->where('is_show',0)->where('is_mer',1)->where('menu_id',$v)->value('pid');
- $pid2=Db::name('system_menu')->where('is_show',0)->where('is_mer',1)->where('menu_id',$pid)->value('pid');
- $pid3=Db::name('system_menu')->where('is_show',0)->where('is_mer',1)->where('menu_id',$pid2)->value('pid');
- $pid4=Db::name('system_menu')->where('is_show',0)->where('is_mer',1)->where('menu_id',$pid3)->value('pid');
- $pid5=Db::name('system_menu')->where('is_show',0)->where('is_mer',1)->where('menu_id',$pid4)->value('pid');
- $arr[]=$pid;
- $arr[]=$pid2;
- $arr[]=$pid3;
- $arr[]=$pid4;
- $arr[]=$pid5;
- }
- foreach ($arr as $v){
- $rule[]=$v;
- }
- }
- $menus = $this->request->adminInfo()->level
- ? $this->repository->ruleByMenuList($rule, $this->merchant)
- : $this->repository->getValidMenuList($this->merchant);
- // var_dump($menus);
- foreach ($menus as $k => $menu) {
- $menu['route'] = $pre . $menu['route'];
- $menus[$k] = $menu;
- }
- if ($this->merchant) {
- // 获取是否拥有阿里巴巴选品库权限
- $show = 0;
- $isShowAlibaba = Db::name('merchant')->where('mer_id', $this->request->merId())->value('is_show_alibaba');
- if ($isShowAlibaba) $show = 1;
- foreach ($menus as $key => $value) {
- if ($value['menu_id'] == '1112' && $this->request->merId() != 439) {
- unset($menus[$key]);
- }
- if ($value['menu_id'] == '1127' && $this->request->merId() != 439) {
- unset($menus[$key]);
- }
- if ($value['menu_id'] == '1103') {
- unset($menus[$key]);
- }
- if ($value['route'] == '/merchant/product/alibaba') {
- if ($show == 0) {
- unset($menus[$key]);
- }
- }
- }
- }
- return app('json')->success(array_values(array_filter(formatCategory($menus, 'menu_id'), function ($v) {
- return 0 == $v['pid'];
- })));
- }
- }
|