SystemRouteServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. * @param string $name
  71. * @return mixed
  72. * @author 吴汐
  73. * @email 442384644@qq.com
  74. * @date 2023/05/06
  75. */
  76. public function getTreeList(string $appName = 'adminapi', string $name = '')
  77. {
  78. return $this->cacheDriver()->remember('ROUTE_LIST' . strtoupper($appName), function () use ($name, $appName) {
  79. $list = app()->make(SystemRouteCateServices::class)
  80. ->selectList(['app_name' => $appName], '*', 0, 0, 'id asc,sort desc', [
  81. 'children' => function ($query) use ($name, $appName) {
  82. $query->where('app_name', $appName)
  83. ->when('' !== $name, function ($q) use ($name) {
  84. $q->where('name|path', 'LIKE', '%' . $name . '%');
  85. });
  86. }
  87. ])
  88. ->toArray();
  89. foreach ($list as $key => $item) {
  90. if (!empty($item['children'])) {
  91. foreach ($item['children'] as $k => $v) {
  92. if (isset($v['cate_id']) && isset($v['method'])) {
  93. if ($v['method'] === 'DELETE') {
  94. $v['method'] = 'DEL';
  95. }
  96. $v['pid'] = $v['cate_id'];
  97. $list[$key]['children'][$k] = $v;
  98. }
  99. }
  100. }
  101. }
  102. return get_tree_children($list);
  103. }, 600);
  104. }
  105. /**
  106. * @param array $importData
  107. * @return bool
  108. * @author 等风来
  109. * @email 136327134@qq.com
  110. * @date 2023/4/26
  111. */
  112. public function importData(array $importData)
  113. {
  114. foreach ($importData as $item) {
  115. $id = $this->dao->value(['method' => $item['method'], 'path' => $item['path']], 'id');
  116. if ($id) {
  117. $this->dao->update($id, [
  118. 'request' => $item['request'],
  119. 'response' => $item['response'],
  120. 'request_type' => $item['request_type'],
  121. ]);
  122. }
  123. }
  124. return true;
  125. }
  126. /**
  127. * 获取某个应用下的所有路由权限
  128. * @param string $app
  129. * @return array
  130. * @author 等风来
  131. * @email 136327134@qq.com
  132. * @date 2023/4/6
  133. */
  134. public function getRouteListAll(string $app = 'adminapi')
  135. {
  136. //获取所有的路由
  137. $this->app = app();
  138. $this->app->route->setTestMode(true);
  139. $this->app->route->clear();
  140. $path = $this->app->getRootPath() . 'app' . DS . $app . DS . 'route' . DS;
  141. $files = is_dir($path) ? scandir($path) : [];
  142. foreach ($files as $file) {
  143. if (strpos($file, '.php')) {
  144. include $path . $file;
  145. }
  146. }
  147. $route = $this->app->route->getRuleList();
  148. $action_arr = ['index', 'read', 'create', 'save', 'edit', 'update', 'delete'];
  149. foreach ($route as $key => $item) {
  150. $real_name = $item['option']['real_name'] ?? '';
  151. if (is_array($real_name)) {
  152. foreach ($action_arr as $a) {
  153. if (Str::contains($item['route'], $a)) {
  154. $real_name = $real_name[$a] ?? '';
  155. }
  156. }
  157. }
  158. $item['option']['real_name'] = $real_name;
  159. $route[$key] = $item;
  160. $except = $item['option']['except'] ?? [];
  161. $router = is_string($item['route']) ? explode('/', $item['route']) : [];
  162. $action = $router[count($router) - 1] ?? null;
  163. //去除不需要的路由
  164. if ($except && $action && in_array($action, $except)) {
  165. unset($route[$key]);
  166. }
  167. $only = $item['option']['only'] ?? [];
  168. if ($only && $action && !in_array($action, $only)) {
  169. unset($route[$key]);
  170. }
  171. }
  172. return $route;
  173. }
  174. /**
  175. * 获取顶级id
  176. * @param string $app
  177. * @return mixed
  178. * @author 等风来
  179. * @email 136327134@qq.com
  180. * @date 2023/4/11
  181. */
  182. public function topCateId(string $app, string $cateName, int $pid = 0)
  183. {
  184. $oneId = app()->make(SystemRouteCateServices::class)->value(['app_name' => $app, 'name' => $cateName, 'pid' => 0], 'id');
  185. if (!$oneId) {
  186. //修复重复同步后反复增加二级文件夹
  187. $id = app()->make(SystemRouteCateServices::class)->value(['app_name' => $app, 'name' => $cateName, 'pid' => $pid], 'id');
  188. if ($id) return $id;
  189. $res = app()->make(SystemRouteCateServices::class)->save([
  190. 'app_name' => $app,
  191. 'name' => $cateName,
  192. 'pid' => $pid,
  193. 'add_time' => time(),
  194. ]);
  195. return $res->id;
  196. }
  197. return $oneId;
  198. }
  199. /**
  200. * 同步路由
  201. * @author 等风来
  202. * @email 136327134@qq.com
  203. * @date 2023/4/6
  204. */
  205. public function syncRoute(string $app = 'adminapi')
  206. {
  207. $listAll = $this->getRouteListAll($app);
  208. $list = [];
  209. foreach ($listAll as $item) {
  210. if (!isset($item['option']['mark_name']) || strstr($item['rule'], '<MISS>') !== false) {
  211. continue;
  212. } else {
  213. $list[$item['option']['mark_name']][] = $item;
  214. }
  215. }
  216. $newsList = [];;
  217. foreach ($list as $key => $item) {
  218. $newItem = [];
  219. foreach ($item as $value) {
  220. if (isset($value['option']['cate_name'])) {
  221. $newItem[$value['option']['cate_name']][] = $value;
  222. } else {
  223. $newItem[$key][] = $value;
  224. }
  225. }
  226. $newsList[$key] = $newItem;
  227. }
  228. $list = [];
  229. foreach ($newsList as $key => $item) {
  230. $keys = array_keys($item);
  231. $pid = $this->topCateId($app, $key, 0);
  232. if ($keys == 1 && $key == $keys[0]) {
  233. foreach ($item[$key] as $value) {
  234. $list[$pid][] = $value;
  235. }
  236. } else {
  237. foreach ($item as $i => $k) {
  238. $cateId = $this->topCateId($app, $i, $pid);
  239. foreach ($k as $value) {
  240. $list[$cateId][] = $value;
  241. }
  242. }
  243. }
  244. }
  245. //保持新增的权限路由
  246. $data = $this->dao->selectList(['app_name' => $app], 'path,method')->toArray();
  247. $save = [];
  248. foreach ($list as $key => $value) {
  249. foreach ($value as $item) {
  250. if (!$this->diffRoute($data, $item['rule'], $item['method']) && strstr($item['rule'], '<MISS>') === false) {
  251. $pathAndAction = explode('/', $item['route']);
  252. $save[] = [
  253. 'name' => $item['option']['real_name'] ?? $item['name'],
  254. 'path' => $item['rule'],
  255. 'cate_id' => $key,
  256. 'app_name' => $app,
  257. 'file_path' => 'app/' . $app . '/controller/' . str_replace('.', '/', $pathAndAction[0]) . '.php',
  258. 'action' => $pathAndAction[1],
  259. 'type' => isset($item['option']['is_common']) && $item['option']['is_common'] ? 1 : 0,
  260. 'method' => $item['method'],
  261. 'add_time' => date('Y-m-d H:i:s'),
  262. ];
  263. }
  264. }
  265. }
  266. if ($save) {
  267. $this->dao->saveAll($save);
  268. }
  269. //删除不存在的权限路由
  270. $data = $this->dao->selectList(['app_name' => $app], 'path,method,id')->toArray();
  271. $delete = [];
  272. $deleteData = [];
  273. foreach ($data as $item) {
  274. if (!$this->diffRoute($listAll, $item['path'], $item['method'], 'rule') && $item['path'] !== '<MISS>') {
  275. $delete[] = $item['id'];
  276. $deleteData[] = [
  277. 'path' => $item['path'],
  278. 'method' => $item['method']
  279. ];
  280. }
  281. }
  282. //删除不存在的路由
  283. if ($delete) {
  284. $this->dao->delete([['id', 'in', $delete]]);
  285. }
  286. //删除不存在的权限
  287. if ($deleteData) {
  288. foreach ($deleteData as $item) {
  289. app()->make(SystemMenusServices::class)->deleteMenu($item['path'], $item['method']);
  290. }
  291. }
  292. $this->cacheDriver()->clear();
  293. }
  294. /**
  295. * 对比路由
  296. * @param array $data
  297. * @param string $path
  298. * @param string $method
  299. * @return bool
  300. * @author 等风来
  301. * @email 136327134@qq.com
  302. * @date 2023/4/6
  303. */
  304. protected function diffRoute(array $data, string $path, string $method, string $key = 'path')
  305. {
  306. $res = false;
  307. foreach ($data as $item) {
  308. if (strtolower($item['method']) == strtolower($method) && strtolower($item[$key]) == strtolower($path)) {
  309. $res = true;
  310. break;
  311. } else if ($method === '*' && strtolower($item[$key]) == strtolower($path)) {
  312. $res = true;
  313. break;
  314. }
  315. }
  316. return $res;
  317. }
  318. /**
  319. * 添加和修改路由
  320. * @param int $id
  321. * @return array
  322. * @author 等风来
  323. * @email 136327134@qq.com
  324. * @date 2023/4/7
  325. */
  326. public function getFrom(int $id = 0, string $appName = 'adminapi')
  327. {
  328. $cateList = app()->make(SystemRouteCateServices::class)->getAllList($appName, 'name as label,path as value');
  329. $url = '/system/route';
  330. $routeInfo = [];
  331. if ($id) {
  332. $routeInfo = $this->dao->get($id);
  333. $routeInfo = $routeInfo ? $routeInfo->toArray() : [];
  334. $url .= '/' . $id;
  335. }
  336. $rule = [
  337. FormBuilder::cascader('cate_id', '分类', $routeInfo['cate_id'] ?? 0)->data($cateList),
  338. FormBuilder::input('name', '路由名称', $routeInfo['name'] ?? '')->required(),
  339. FormBuilder::input('path', '路由路径', $routeInfo['path'] ?? '')->required(),
  340. FormBuilder::select('method', '请求方式', $routeInfo['method'] ?? '')->options([
  341. ['value' => 'POST', 'label' => 'POST'],
  342. ['value' => 'GET', 'label' => 'GET'],
  343. ['value' => 'DELETE', 'label' => 'DELETE'],
  344. ['value' => 'PUT', 'label' => 'PUT'],
  345. ['value' => '*', 'label' => '*'],
  346. ])->required(),
  347. FormBuilder::radio('type', '类型', $routeInfo['type'] ?? 0)->options([
  348. ['value' => 0, 'lable' => '普通路由'],
  349. ['value' => 1, 'lable' => '公共路由'],
  350. ]),
  351. FormBuilder::hidden('app_name', $appName),
  352. ];
  353. return create_form($id ? '修改路由' : '添加路由', $rule, $url, $id ? 'PUT' : 'POST');
  354. }
  355. }