MenuRepository.php 11 KB

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