SystemRouteServices.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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('修改的路由不存在');
  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. return get_tree_children($list);
  88. }
  89. /**
  90. * 获取某个应用下的所有路由权限
  91. * @param string $app
  92. * @return array
  93. * @author 等风来
  94. * @email 136327134@qq.com
  95. * @date 2023/4/6
  96. */
  97. public function getRouteListAll(string $app = 'adminapi')
  98. {
  99. //获取所有的路由
  100. $this->app = app();
  101. $this->app->route->setTestMode(true);
  102. $this->app->route->clear();
  103. $path = $this->app->getRootPath() . 'app' . DS . $app . DS . 'route' . DS;
  104. $files = is_dir($path) ? scandir($path) : [];
  105. foreach ($files as $file) {
  106. if (strpos($file, '.php')) {
  107. include $path . $file;
  108. }
  109. }
  110. $route = $this->app->route->getRuleList();
  111. $action_arr = ['index', 'read', 'create', 'save', 'edit', 'update', 'delete'];
  112. foreach ($route as $key => $item) {
  113. $real_name = $item['option']['real_name'] ?? '';
  114. if (is_array($real_name)) {
  115. foreach ($action_arr as $a) {
  116. if (Str::contains($item['route'], $a)) {
  117. $real_name = $real_name[$a] ?? '';
  118. }
  119. }
  120. }
  121. $item['option']['real_name'] = $real_name;
  122. $route[$key] = $item;
  123. $except = $item['option']['except'] ?? [];
  124. $router = is_string($item['route']) ? explode('/', $item['route']) : [];
  125. $action = $router[count($router) - 1] ?? null;
  126. //去除不需要的路由
  127. if ($except && $action && in_array($action, $except)) {
  128. unset($route[$key]);
  129. }
  130. $only = $item['option']['only'] ?? [];
  131. if ($only && $action && !in_array($action, $only)) {
  132. unset($route[$key]);
  133. }
  134. }
  135. return $route;
  136. }
  137. /**
  138. * 获取顶级id
  139. * @param string $app
  140. * @return mixed
  141. * @author 等风来
  142. * @email 136327134@qq.com
  143. * @date 2023/4/11
  144. */
  145. public function topCateId(string $app)
  146. {
  147. $id = app()->make(SystemRouteCateServices::class)->value(['app_name' => $app, 'name' => '全部权限', 'pid' => 0], 'id');
  148. if (!$id) {
  149. $res = app()->make(SystemRouteCateServices::class)->save([
  150. 'app_name' => $app,
  151. 'name' => '全部权限',
  152. 'pid' => 0,
  153. 'add_time' => time(),
  154. ]);
  155. $id = $res->id;
  156. }
  157. return $id;
  158. }
  159. /**
  160. * 同步路由
  161. * @author 等风来
  162. * @email 136327134@qq.com
  163. * @date 2023/4/6
  164. */
  165. public function syncRoute(string $app = 'adminapi')
  166. {
  167. $id = $this->topCateId($app);
  168. $listAll = $this->getRouteListAll($app);
  169. //保持新增的权限路由
  170. $data = $this->dao->selectList(['app_name' => $app], 'path,method')->toArray();
  171. $save = [];
  172. foreach ($listAll as $item) {
  173. if (!$this->diffRoute($data, $item['rule'], $item['method']) && strstr($item['rule'], '<MISS>') === false) {
  174. $save[] = [
  175. 'name' => $item['option']['real_name'] ?? $item['name'],
  176. 'path' => $item['rule'],
  177. 'cate_id' => $id,
  178. 'app_name' => $app,
  179. 'type' => isset($item['option']['is_common']) && $item['option']['is_common'] ? 1 : 0,
  180. 'method' => $item['method'],
  181. 'add_time' => date('Y-m-d H:i:s'),
  182. ];
  183. }
  184. }
  185. if ($save) {
  186. $this->dao->saveAll($save);
  187. }
  188. //删除不存在的权限路由
  189. $data = $this->dao->selectList(['app_name' => $app], 'path,method,id')->toArray();
  190. $delete = [];
  191. foreach ($data as $item) {
  192. if (!$this->diffRoute($listAll, $item['path'], $item['method'], 'rule') && $item['path'] !== '<MISS>') {
  193. $delete[] = $item['id'];
  194. }
  195. }
  196. if ($delete) {
  197. $this->dao->delete([['id', 'in', $delete]]);
  198. }
  199. }
  200. /**
  201. * 对比路由
  202. * @param array $data
  203. * @param string $path
  204. * @param string $method
  205. * @return bool
  206. * @author 等风来
  207. * @email 136327134@qq.com
  208. * @date 2023/4/6
  209. */
  210. protected function diffRoute(array $data, string $path, string $method, string $key = 'path')
  211. {
  212. $res = false;
  213. foreach ($data as $item) {
  214. if (strtolower($item['method']) == strtolower($method) && strtolower($item[$key]) == strtolower($path)) {
  215. $res = true;
  216. break;
  217. } else if ($method === '*' && strtolower($item[$key]) == strtolower($path)) {
  218. $res = true;
  219. break;
  220. }
  221. }
  222. return $res;
  223. }
  224. /**
  225. * 添加和修改路由
  226. * @param int $id
  227. * @return array
  228. * @author 等风来
  229. * @email 136327134@qq.com
  230. * @date 2023/4/7
  231. */
  232. public function getFrom(int $id = 0, string $appName = 'adminapi')
  233. {
  234. $cateList = app()->make(SystemRouteCateServices::class)->getAllList($appName, 'name as label,path as value');
  235. $url = '/system/route';
  236. $routeInfo = [];
  237. if ($id) {
  238. $routeInfo = $this->dao->get($id);
  239. $routeInfo = $routeInfo ? $routeInfo->toArray() : [];
  240. $url .= '/' . $id;
  241. }
  242. $rule = [
  243. FormBuilder::cascader('cate_id', '分类', $routeInfo['cate_id'] ?? 0)->data($cateList),
  244. FormBuilder::input('name', '路由名称', $routeInfo['name'] ?? '')->required(),
  245. FormBuilder::input('path', '路由路径', $routeInfo['path'] ?? '')->required(),
  246. FormBuilder::select('method', '请求方式', $routeInfo['method'] ?? '')->options([
  247. ['value' => 'POST', 'label' => 'POST'],
  248. ['value' => 'GET', 'label' => 'GET'],
  249. ['value' => 'DELETE', 'label' => 'DELETE'],
  250. ['value' => 'PUT', 'label' => 'PUT'],
  251. ['value' => '*', 'label' => '*'],
  252. ])->required(),
  253. FormBuilder::radio('type', '类型', $routeInfo['type'] ?? 0)->options([
  254. ['value' => 0, 'lable' => '普通路由'],
  255. ['value' => 1, 'lable' => '公共路由'],
  256. ]),
  257. FormBuilder::hidden('app_name', $appName),
  258. ];
  259. return create_form($id ? '修改路由' : '添加路由', $rule, $url, $id ? 'PUT' : 'POST');
  260. }
  261. }