SystemRouteServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace app\services\system;
  14. use app\dao\system\SystemRouteDao;
  15. use app\services\BaseServices;
  16. use crmeb\services\FormBuilder;
  17. use think\exception\ValidateException;
  18. use think\helper\Str;
  19. /**
  20. * Class SystemRouteServices
  21. * @author 等风来
  22. * @email 136327134@qq.com
  23. * @date 2023/4/6
  24. * @package app\services\system
  25. */
  26. class SystemRouteServices extends BaseServices
  27. {
  28. /**
  29. * SystemRouteServices constructor.
  30. * @param SystemRouteDao $dao
  31. */
  32. public function __construct(SystemRouteDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * @param array $where
  38. * @return array
  39. * @author 等风来
  40. * @email 136327134@qq.com
  41. * @date 2023/4/7
  42. */
  43. public function getList(array $where)
  44. {
  45. [$page, $limit] = $this->getPageValue();
  46. $list = $this->dao->selectList($where, 'name,path,method', $page, $limit)->toArray();
  47. $count = $this->dao->count($where);
  48. return compact('list', 'count');
  49. }
  50. /**
  51. * @param int $id
  52. * @return array
  53. * @author 等风来
  54. * @email 136327134@qq.com
  55. * @date 2023/4/10
  56. */
  57. public function getInfo(int $id)
  58. {
  59. $routeInfo = $this->dao->get($id);
  60. if (!$routeInfo) {
  61. throw new ValidateException(500036);
  62. }
  63. $routeInfo = $routeInfo->toArray();
  64. $routeInfo['cate_tree'] = app()->make(SystemRouteCateServices::class)->getAllList($routeInfo['app_name'], '*', 'id asc,sort desc');
  65. return $routeInfo;
  66. }
  67. /**
  68. * 获取tree数据
  69. * @param string $appName
  70. * @return array
  71. * @author 等风来
  72. * @email 136327134@qq.com
  73. * @date 2023/4/7
  74. */
  75. public function getTreeList(array $where, string $appName = 'adminapi')
  76. {
  77. $list = app()->make(SystemRouteCateServices::class)
  78. ->selectList(['app_name' => $appName], '*', 0, 0, 'id asc,sort desc', [
  79. 'children' => function ($query) use ($where) {
  80. $query->where('app_name', $where['app_name'])
  81. ->when('' !== $where['name_like'], function ($q) use ($where) {
  82. $q->where('name|path', 'LIKE', '%' . $where['name_like'] . '%');
  83. });
  84. }
  85. ])
  86. ->toArray();
  87. foreach ($list as $key => $item) {
  88. if (!empty($item['children'])) {
  89. foreach ($item['children'] as $k => $v) {
  90. if (isset($v['cate_id']) && isset($v['method']) && $v['method'] === 'DELETE') {
  91. $v['method'] = 'DEL';
  92. $list[$key]['children'][$k] = $v;
  93. }
  94. }
  95. }
  96. }
  97. return get_tree_children($list);
  98. }
  99. /**
  100. * 获取某个应用下的所有路由权限
  101. * @param string $app
  102. * @return array
  103. * @author 等风来
  104. * @email 136327134@qq.com
  105. * @date 2023/4/6
  106. */
  107. public function getRouteListAll(string $app = 'adminapi')
  108. {
  109. //获取所有的路由
  110. $this->app = app();
  111. $this->app->route->setTestMode(true);
  112. $this->app->route->clear();
  113. $path = $this->app->getRootPath() . 'app' . DS . $app . DS . 'route' . DS;
  114. $files = is_dir($path) ? scandir($path) : [];
  115. foreach ($files as $file) {
  116. if (strpos($file, '.php')) {
  117. include $path . $file;
  118. }
  119. }
  120. $route = $this->app->route->getRuleList();
  121. $action_arr = ['index', 'read', 'create', 'save', 'edit', 'update', 'delete'];
  122. foreach ($route as $key => $item) {
  123. $real_name = $item['option']['real_name'] ?? '';
  124. if (is_array($real_name)) {
  125. foreach ($action_arr as $a) {
  126. if (Str::contains($item['route'], $a)) {
  127. $real_name = $real_name[$a] ?? '';
  128. }
  129. }
  130. }
  131. $item['option']['real_name'] = $real_name;
  132. $route[$key] = $item;
  133. $except = $item['option']['except'] ?? [];
  134. $router = is_string($item['route']) ? explode('/', $item['route']) : [];
  135. $action = $router[count($router) - 1] ?? null;
  136. //去除不需要的路由
  137. if ($except && $action && in_array($action, $except)) {
  138. unset($route[$key]);
  139. }
  140. $only = $item['option']['only'] ?? [];
  141. if ($only && $action && !in_array($action, $only)) {
  142. unset($route[$key]);
  143. }
  144. }
  145. return $route;
  146. }
  147. /**
  148. * 获取顶级id
  149. * @param string $app
  150. * @return mixed
  151. * @author 等风来
  152. * @email 136327134@qq.com
  153. * @date 2023/4/11
  154. */
  155. public function topCateId(string $app, string $name = '全部权限')
  156. {
  157. $id = app()->make(SystemRouteCateServices::class)->value(['app_name' => $app, 'name' => $name, 'pid' => 0], 'id');
  158. if (!$id) {
  159. $res = app()->make(SystemRouteCateServices::class)->save([
  160. 'app_name' => $app,
  161. 'name' => $name,
  162. 'pid' => 0,
  163. 'add_time' => time(),
  164. ]);
  165. $id = $res->id;
  166. }
  167. return $id;
  168. }
  169. /**
  170. * 同步路由
  171. * @author 等风来
  172. * @email 136327134@qq.com
  173. * @date 2023/4/6
  174. */
  175. public function syncRoute(string $app = 'adminapi')
  176. {
  177. $id = $this->topCateId($app);
  178. $commmonId = $this->topCateId($app, '公共权限');
  179. $listAll = $this->getRouteListAll($app);
  180. $list = [];
  181. foreach ($listAll as $item) {
  182. if (isset($item['option']['is_common']) && $item['option']['is_common']) {
  183. $cateId = $commmonId;
  184. } else {
  185. if (!isset($item['option']['cate_name'])) {
  186. $cateId = $id;
  187. } else {
  188. $cateId = $this->topCateId($app, $item['option']['cate_name']);
  189. }
  190. }
  191. $list[$cateId][] = $item;
  192. }
  193. //保持新增的权限路由
  194. $data = $this->dao->selectList(['app_name' => $app], 'path,method')->toArray();
  195. $save = [];
  196. foreach ($list as $key => $value) {
  197. foreach ($value as $item) {
  198. if (!$this->diffRoute($data, $item['rule'], $item['method']) && strstr($item['rule'], '<MISS>') === false) {
  199. $save[] = [
  200. 'name' => $item['option']['real_name'] ?? $item['name'],
  201. 'path' => $item['rule'],
  202. 'cate_id' => $key,
  203. 'app_name' => $app,
  204. 'type' => isset($item['option']['is_common']) && $item['option']['is_common'] ? 1 : 0,
  205. 'method' => $item['method'],
  206. 'add_time' => date('Y-m-d H:i:s'),
  207. ];
  208. }
  209. }
  210. }
  211. if ($save) {
  212. $this->dao->saveAll($save);
  213. }
  214. //删除不存在的权限路由
  215. $data = $this->dao->selectList(['app_name' => $app], 'path,method,id')->toArray();
  216. $delete = [];
  217. $deleteData = [];
  218. foreach ($data as $item) {
  219. if (!$this->diffRoute($listAll, $item['path'], $item['method'], 'rule') && $item['path'] !== '<MISS>') {
  220. $delete[] = $item['id'];
  221. $deleteData[] = [
  222. 'path' => $item['path'],
  223. 'methods' => $item['method']
  224. ];
  225. }
  226. }
  227. //删除不存在的路由
  228. if ($delete) {
  229. $this->dao->delete([['id', 'in', $delete]]);
  230. }
  231. //删除不存在的权限
  232. if ($deleteData) {
  233. foreach ($deleteData as $item) {
  234. app()->make(SystemMenusServices::class)->deleteMenu($item['path'], $item['method']);
  235. }
  236. }
  237. }
  238. /**
  239. * 对比路由
  240. * @param array $data
  241. * @param string $path
  242. * @param string $method
  243. * @return bool
  244. * @author 等风来
  245. * @email 136327134@qq.com
  246. * @date 2023/4/6
  247. */
  248. protected function diffRoute(array $data, string $path, string $method, string $key = 'path')
  249. {
  250. $res = false;
  251. foreach ($data as $item) {
  252. if (strtolower($item['method']) == strtolower($method) && strtolower($item[$key]) == strtolower($path)) {
  253. $res = true;
  254. break;
  255. } else if ($method === '*' && strtolower($item[$key]) == strtolower($path)) {
  256. $res = true;
  257. break;
  258. }
  259. }
  260. return $res;
  261. }
  262. /**
  263. * 添加和修改路由
  264. * @param int $id
  265. * @return array
  266. * @author 等风来
  267. * @email 136327134@qq.com
  268. * @date 2023/4/7
  269. */
  270. public function getFrom(int $id = 0, string $appName = 'adminapi')
  271. {
  272. $cateList = app()->make(SystemRouteCateServices::class)->getAllList($appName, 'name as label,path as value');
  273. $url = '/system/route';
  274. $routeInfo = [];
  275. if ($id) {
  276. $routeInfo = $this->dao->get($id);
  277. $routeInfo = $routeInfo ? $routeInfo->toArray() : [];
  278. $url .= '/' . $id;
  279. }
  280. $rule = [
  281. FormBuilder::cascader('cate_id', '分类', $routeInfo['cate_id'] ?? 0)->data($cateList),
  282. FormBuilder::input('name', '路由名称', $routeInfo['name'] ?? '')->required(),
  283. FormBuilder::input('path', '路由路径', $routeInfo['path'] ?? '')->required(),
  284. FormBuilder::select('method', '请求方式', $routeInfo['method'] ?? '')->options([
  285. ['value' => 'POST', 'label' => 'POST'],
  286. ['value' => 'GET', 'label' => 'GET'],
  287. ['value' => 'DELETE', 'label' => 'DELETE'],
  288. ['value' => 'PUT', 'label' => 'PUT'],
  289. ['value' => '*', 'label' => '*'],
  290. ])->required(),
  291. FormBuilder::radio('type', '类型', $routeInfo['type'] ?? 0)->options([
  292. ['value' => 0, 'lable' => '普通路由'],
  293. ['value' => 1, 'lable' => '公共路由'],
  294. ]),
  295. FormBuilder::hidden('app_name', $appName),
  296. ];
  297. return create_form($id ? '修改路由' : '添加路由', $rule, $url, $id ? 'PUT' : 'POST');
  298. }
  299. }