MenuRepository.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace app\common\repositories\system\auth;
  3. //附件
  4. use app\common\dao\BaseDao;
  5. use app\common\dao\system\menu\MenuDao;
  6. use app\common\repositories\BaseRepository;
  7. use FormBuilder\Exception\FormBuilderException;
  8. use FormBuilder\Factory\Elm;
  9. use FormBuilder\Form;
  10. use think\db\exception\DataNotFoundException;
  11. use think\db\exception\DbException;
  12. use think\db\exception\ModelNotFoundException;
  13. use think\facade\Db;
  14. use think\facade\Route;
  15. use think\Model;
  16. /**
  17. * Class BaseRepository
  18. * @package common\repositories
  19. * @mixin MenuDao
  20. */
  21. class MenuRepository extends BaseRepository
  22. {
  23. /**
  24. * MenuRepository constructor.
  25. * @param MenuDao $dao
  26. */
  27. public function __construct(MenuDao $dao)
  28. {
  29. /**
  30. * @var MenuDao
  31. */
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * @param array $where
  36. * @param int $merId
  37. * @return array
  38. * @throws DataNotFoundException
  39. * @throws DbException
  40. * @throws ModelNotFoundException
  41. * @author xaboy
  42. * @day 2020-04-16
  43. */
  44. public function getList(array $where, $merId = 0)
  45. {
  46. $query = $this->dao->search($where, $merId);
  47. $count = $query->count();
  48. $list = $query->hidden(['update_time', 'path'])->select()->toArray();
  49. return compact('count', 'list');
  50. }
  51. /**
  52. * @param array $data
  53. * @return BaseDao|Model
  54. * @author xaboy
  55. * @day 2020-04-09
  56. */
  57. public function create(array $data)
  58. {
  59. $data['path'] = '/';
  60. if ($data['pid']) {
  61. $data['path'] = $this->getPath($data['pid']) . $data['pid'] . '/';
  62. }
  63. return $this->dao->create($data);
  64. }
  65. /**
  66. * @param int $id
  67. * @param array $data
  68. * @return int
  69. * @throws DbException
  70. * @author xaboy
  71. * @day 2020-04-09
  72. */
  73. public function update(int $id, array $data)
  74. {
  75. $menu = $this->dao->get($id);
  76. if ($menu->pid != $data['pid']) {
  77. Db::transaction(function () use ($menu, $data) {
  78. $data['path'] = '/';
  79. if ($data['pid']) {
  80. $data['path'] = $this->getPath($data['pid']) . $data['pid'] . '/';
  81. }
  82. $this->dao->updatePath($menu->path . $menu->menu_id . '/', $data['path'] . $menu->menu_id . '/');
  83. $menu->save($data);
  84. });
  85. } else {
  86. unset($data['path']);
  87. $this->dao->update($id, $data);
  88. }
  89. }
  90. /**
  91. * @param bool $is_mer
  92. * @return array
  93. * @author xaboy
  94. * @day 2020-04-18
  95. */
  96. public function getTree($is_mer = false)
  97. {
  98. $options = $this->dao->getAllOptions($is_mer);
  99. return formatTree($options, 'menu_name');
  100. }
  101. /**
  102. * @param int $isMer
  103. * @param int|null $id
  104. * @param array $formData
  105. * @return Form
  106. * @throws FormBuilderException
  107. * @author xaboy
  108. * @day 2020-04-16
  109. */
  110. public function menuForm(int $isMer = 0, ?int $id = null, array $formData = []): Form
  111. {
  112. $action = $isMer == 0 ? (is_null($id) ? Route::buildUrl('systemMenuCreate')->build() : Route::buildUrl('systemMenuUpdate', ['id' => $id])->build())
  113. : (is_null($id) ? Route::buildUrl('systemMerchantMenuCreate')->build() : Route::buildUrl('systemMerchantMenuUpdate', ['id' => $id])->build());
  114. $form = Elm::createForm($action);
  115. $form->setRule([
  116. Elm::cascader('pid', '父级分类')->options(function () use ($id, $isMer) {
  117. $menus = $this->dao->getAllOptions($isMer, true);
  118. if ($id && isset($menus[$id])) unset($menus[$id]);
  119. $menus = formatCascaderData($menus, 'menu_name');
  120. array_unshift($menus, ['label' => '顶级分类', 'value' => 0]);
  121. return $menus;
  122. })->props(['props' => ['checkStrictly' => true, 'emitPath' => false]]),
  123. Elm::select('is_menu', '权限类型', 1)->options([
  124. ['value' => 1, 'label' => '菜单'],
  125. ['value' => 0, 'label' => '权限'],
  126. ])->control([
  127. [
  128. 'value' => 0,
  129. 'rule' => [
  130. Elm::input('menu_name', '路由名称')->required(),
  131. Elm::textarea('params', '参数')->placeholder("路由参数:\r\nkey1:value1\r\nkey2:value2"),
  132. ]
  133. ], [
  134. 'value' => 1,
  135. 'rule' => [
  136. Elm::switches('is_show', '是否显示', 1)->inactiveValue(0)->activeValue(1)->inactiveText('关闭')->activeText('开启'),
  137. Elm::frameInput('icon', '菜单图标', '/' . config('admin.admin_prefix') . '/setting/icons?field=icon')->icon('el-icon-circle-plus-outline')->height('338px')->width('700px')->modal(['modal' => false]),
  138. Elm::input('menu_name', '菜单名称')->required(),
  139. ]
  140. ]
  141. ]),
  142. Elm::input('route', '路由'),
  143. Elm::number('sort', '排序', 0)
  144. ]);
  145. return $form->setTitle(is_null($id) ? '添加菜单' : '编辑菜单')->formData($formData);
  146. }
  147. /**
  148. * @param int $id
  149. * @param int $merId
  150. * @return Form
  151. * @throws DataNotFoundException
  152. * @throws DbException
  153. * @throws FormBuilderException
  154. * @throws ModelNotFoundException
  155. * @author xaboy
  156. * @day 2020-04-16
  157. */
  158. public function updateMenuForm(int $id, $merId = 0)
  159. {
  160. return $this->menuForm($merId, $id, $this->dao->get($id)->toArray());
  161. }
  162. /**
  163. * @param string $params
  164. * @return array
  165. * @author xaboy
  166. * @day 2020-04-22
  167. */
  168. public function tidyParams(?string $params)
  169. {
  170. return $params ? array_reduce(explode('|', $params), function ($initial, $val) {
  171. $data = explode(':', $val, 2);
  172. if (count($data) != 2) return $initial;
  173. $initial[$data[0]] = $data[1];
  174. return $initial;
  175. }, []) : [];
  176. }
  177. /**
  178. * @param array $params
  179. * @param array $routeParams
  180. * @return bool
  181. * @author xaboy
  182. * @day 2020-04-23
  183. */
  184. public function checkParams(array $params, array $routeParams)
  185. {
  186. foreach ($routeParams as $k => $param) {
  187. if (isset($params[$k]) && $params[$k] != $param)
  188. return false;
  189. }
  190. return true;
  191. }
  192. /**
  193. * @Author:Qinii
  194. * @Date: 2020/5/26
  195. * @param array $data
  196. */
  197. public function commandCreate(array $data)
  198. {
  199. $sys = $this->chekcAndCreate($data['sys'],0,'sys');
  200. $mer = $this->chekcAndCreate($data['mer'],1,'mer');
  201. return $sys+$mer;
  202. }
  203. /**
  204. * 入库操作
  205. * @Author:Qinii
  206. * @Date: 2020/9/8
  207. * @param array $data
  208. * @param int $isMer
  209. * @param string $name
  210. * @param $authName
  211. */
  212. public function chekcAndCreate(array $data, int $isMer, string $group)
  213. {
  214. $slit = [];
  215. foreach ($data as $key => $v)
  216. {
  217. $result = $this->dao->getMenuPid($v);
  218. if(!empty($result)){
  219. $res = $this->createSlit($isMer,$result[0]['menu_id'],$result[0]['path'],$v);
  220. }else{
  221. $auth = $this->dao->findOrCreate([
  222. 'menu_name' => $group.'/'.$key,
  223. 'path' => '/',
  224. 'is_mer' => $isMer,
  225. 'is_menu' => 0
  226. ]);
  227. $res = $this->createSlit($isMer,$auth['menu_id'],$auth['path'],$v);
  228. }
  229. if(!empty($res)) $slit = array_merge($slit,$res);
  230. }
  231. $count = count($slit);
  232. if (!empty($slit)) $this->dao->insertAll($slit);
  233. return $count;
  234. }
  235. /**
  236. * 新增权限数据整理
  237. * @Author:Qinii
  238. * @Date: 2020/9/8
  239. * @param int $isMer
  240. * @param int $menuId
  241. * @param string $path
  242. * @param array $data
  243. * @return array
  244. */
  245. public function createSlit(int $isMer,int $menuId, string $path,array $data)
  246. {
  247. $arr = [];
  248. foreach ($data as $item){
  249. $result = $this->dao->getFieldExists('route', $item);
  250. if (!$result) {
  251. $arr[] = [
  252. 'pid' => $menuId,
  253. 'path' => $path . $menuId . '/',
  254. 'menu_name' => $this->getMenuName($item),
  255. 'route' => $item,
  256. 'is_mer' => $isMer,
  257. 'is_menu' => 0
  258. ];
  259. echo ( '新增权限:'.$item);
  260. echo PHP_EOL;
  261. }
  262. }
  263. return $arr;
  264. }
  265. public function getMenuName($item)
  266. {
  267. if (strpos($item, 'CreateForm') != false) return '添加表单';
  268. if (strpos($item, 'UpdateForm') != false) return '编辑表单';
  269. if (strpos($item, 'Create') != false) return '添加';
  270. if (strpos($item, 'Update') != false) return '编辑';
  271. if (strpos($item, 'Detail') != false) return '详情';
  272. if (strpos($item, 'Status') != false) return '编辑状态';
  273. if (strpos($item, 'Lst') != false) return '列表';
  274. if (strpos($item, 'Delete') != false) return '删除';
  275. return $item;
  276. }
  277. public function formatPath($is_mer = 0)
  278. {
  279. $options = $this->getAll($is_mer);
  280. $options = formatCategory($options, 'menu_id');
  281. Db::transaction(function () use ($options) {
  282. foreach ($options as $option) {
  283. $this->_formatPath($option);
  284. }
  285. });
  286. }
  287. protected function _formatPath($parent, $path = '/')
  288. {
  289. $this->dao->update($parent['menu_id'], ['path' => $path]);
  290. foreach ($parent['children'] ?? [] as $item) {
  291. $itemPath = $path . $item['pid'] . '/';
  292. $this->_formatPath($item, $itemPath);
  293. }
  294. }
  295. }