MenuRepository.php 11 KB

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