Menu.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-03-24
  7. *
  8. *
  9. */
  10. namespace app\controller\admin\system\auth;
  11. use crmeb\basic\BaseController;
  12. use app\common\repositories\system\auth\MenuRepository;
  13. use app\validate\admin\MenuValidate;
  14. use FormBuilder\Exception\FormBuilderException;
  15. use think\App;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\Exception;
  20. use think\facade\Db;
  21. /**
  22. * Class Menu
  23. * @package app\controller\admin\system\menu
  24. * @author xaboy
  25. * @day 2020-04-08
  26. */
  27. class Menu extends BaseController
  28. {
  29. /**
  30. * @var int
  31. */
  32. protected $merchant;
  33. /**
  34. * @var MenuRepository
  35. */
  36. protected $repository;
  37. /**
  38. * Menu constructor.
  39. * @param App $app
  40. * @param MenuRepository $repository
  41. */
  42. public function __construct(App $app, MenuRepository $repository)
  43. {
  44. parent::__construct($app);
  45. $this->repository = $repository;
  46. $this->merchant = $this->request->param('merchant', 0) == 0 ? 0 : 1;
  47. }
  48. /**
  49. * @return mixed
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. * @author xaboy
  54. * @day 2020-04-08
  55. */
  56. public function getList()
  57. {
  58. $data = $this->repository->getList([], $this->merchant);
  59. $data['list'] = formatCategory($data['list'], 'menu_id');
  60. return app('json')->success($data);
  61. }
  62. /**
  63. * 创建
  64. * @param MenuValidate $validate
  65. * @return mixed
  66. * @author 张先生
  67. * @date 2020-03-30
  68. */
  69. public function create(MenuValidate $validate)
  70. {
  71. $data = $this->checkParam($validate);
  72. if (!$data['pid']) $data['pid'] = 0;
  73. if ($data['pid'] && !$this->repository->merExists($data['pid'], $this->merchant))
  74. return app('json')->fail('父级分类不存在');
  75. $data['is_mer'] = $this->merchant;
  76. $this->repository->create($data);
  77. return app('json')->success('添加成功');
  78. }
  79. /**
  80. * 更新
  81. * @param int $id id
  82. * @param MenuValidate $validate
  83. * @return mixed
  84. * @throws DbException
  85. * @author 张先生
  86. * @date 2020-03-30
  87. */
  88. public function update($id, MenuValidate $validate)
  89. {
  90. $data = $this->checkParam($validate);
  91. if (!$data['pid']) $data['pid'] = 0;
  92. if (!$this->repository->merExists($id, $this->merchant))
  93. return app('json')->fail('数据不存在');
  94. if ($data['pid'] && !$this->repository->merExists($data['pid'], $this->merchant))
  95. return app('json')->fail('父级分类不存在');
  96. $this->repository->update($id, $data);
  97. return app('json')->success('编辑成功');
  98. }
  99. /**
  100. * @return mixed
  101. * @throws FormBuilderException
  102. * @author xaboy
  103. * @day 2020-04-08
  104. */
  105. public function createForm()
  106. {
  107. return app('json')->success(formToData($this->repository->menuForm($this->merchant)));
  108. }
  109. /**
  110. * @param int $id
  111. * @return mixed
  112. * @throws DataNotFoundException
  113. * @throws DbException
  114. * @throws ModelNotFoundException
  115. * @throws FormBuilderException
  116. * @author xaboy
  117. * @day 2020-04-08
  118. */
  119. public function updateForm($id)
  120. {
  121. if (!$this->repository->merExists($id, $this->merchant))
  122. return app('json')->fail('数据不存在');
  123. return app('json')->success(formToData($this->repository->updateMenuForm($id, $this->merchant)));
  124. }
  125. /**
  126. *
  127. * @param MenuValidate $validate 验证规则
  128. * @return mixed
  129. * @author 张先生
  130. * @date 2020-03-30
  131. */
  132. private function checkParam(MenuValidate $validate)
  133. {
  134. $data = $this->request->params([['pid', 0], 'icon', 'menu_name', 'route', ['params', '[]'], 'sort', ['is_show', 0], ['is_menu', 0]]);
  135. if ($data['is_menu'] != 1) $validate->isAuth();
  136. $validate->check($data);
  137. return $data;
  138. }
  139. /**
  140. * @param int $id
  141. * @return mixed
  142. * @throws DbException
  143. * @author xaboy
  144. * @day 2020-04-08
  145. */
  146. public function delete($id)
  147. {
  148. if ($this->repository->pidExists($id))
  149. return app('json')->fail('存在下级,无法删除');
  150. $this->repository->delete($id, $this->merchant);
  151. return app('json')->success('删除成功');
  152. }
  153. /**
  154. * @return mixed
  155. * @author xaboy
  156. * @day 2020-04-10
  157. */
  158. public function menus()
  159. {
  160. $pre = '/' . config('admin.' . ($this->merchant ? 'merchant' : 'admin') . '_prefix');
  161. $rule=$this->request->adminRule();
  162. if($this->merchant){
  163. $arr=[];
  164. foreach ($rule as $v){
  165. $pid=Db::name('system_menu')->where('is_show',0)->where('is_mer',1)->where('menu_id',$v)->value('pid');
  166. $pid2=Db::name('system_menu')->where('is_show',0)->where('is_mer',1)->where('menu_id',$pid)->value('pid');
  167. $pid3=Db::name('system_menu')->where('is_show',0)->where('is_mer',1)->where('menu_id',$pid2)->value('pid');
  168. $pid4=Db::name('system_menu')->where('is_show',0)->where('is_mer',1)->where('menu_id',$pid3)->value('pid');
  169. $pid5=Db::name('system_menu')->where('is_show',0)->where('is_mer',1)->where('menu_id',$pid4)->value('pid');
  170. $arr[]=$pid;
  171. $arr[]=$pid2;
  172. $arr[]=$pid3;
  173. $arr[]=$pid4;
  174. $arr[]=$pid5;
  175. }
  176. foreach ($arr as $v){
  177. $rule[]=$v;
  178. }
  179. }
  180. $menus = $this->request->adminInfo()->level
  181. ? $this->repository->ruleByMenuList($rule, $this->merchant)
  182. : $this->repository->getValidMenuList($this->merchant);
  183. // var_dump($menus);
  184. foreach ($menus as $k => $menu) {
  185. $menu['route'] = $pre . $menu['route'];
  186. $menus[$k] = $menu;
  187. }
  188. if($this->merchant){
  189. foreach($menus as $key => $value){
  190. if($value['menu_id'] == '1112' && $this->request->merId() != 439){
  191. unset($menus[$key]);
  192. }
  193. if($value['menu_id'] == '1127' && $this->request->merId() != 439){
  194. unset($menus[$key]);
  195. }
  196. if($value['menu_id'] == '1103'){
  197. unset($menus[$key]);
  198. }
  199. }
  200. }
  201. return app('json')->success(array_values(array_filter(formatCategory($menus, 'menu_id'), function ($v) {
  202. return 0 == $v['pid'];
  203. })));
  204. }
  205. }