SystemRouteServices.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. return $routeInfo->toArray();
  64. }
  65. /**
  66. * 获取tree数据
  67. * @param string $appName
  68. * @return array
  69. * @author 等风来
  70. * @email 136327134@qq.com
  71. * @date 2023/4/7
  72. */
  73. public function getTreeList(array $where, string $appName = 'adminapi')
  74. {
  75. $list = app()->make(SystemRouteCateServices::class)
  76. ->selectList(['app_name' => $appName], '*', 0, 0, 'id asc,sort desc', [
  77. 'children' => function ($query) use ($where) {
  78. $query->where('app_name', $where['app_name'])
  79. ->when('' !== $where['name_like'], function ($q) use ($where) {
  80. $q->where('name|path', 'LIKE', '%' . $where['name_like'] . '%');
  81. });
  82. }
  83. ])
  84. ->toArray();
  85. return get_tree_children($list);
  86. }
  87. /**
  88. * 获取某个应用下的所有路由权限
  89. * @param string $app
  90. * @return array
  91. * @author 等风来
  92. * @email 136327134@qq.com
  93. * @date 2023/4/6
  94. */
  95. public function getRouteListAll(string $app = 'adminapi')
  96. {
  97. //获取所有的路由
  98. $this->app = app();
  99. $this->app->route->setTestMode(true);
  100. $this->app->route->clear();
  101. $path = $this->app->getRootPath() . 'app' . DS . $app . DS . 'route' . DS;
  102. $files = is_dir($path) ? scandir($path) : [];
  103. foreach ($files as $file) {
  104. if (strpos($file, '.php')) {
  105. include $path . $file;
  106. }
  107. }
  108. $route = $this->app->route->getRuleList();
  109. $action_arr = ['index', 'read', 'create', 'save', 'edit', 'update', 'delete'];
  110. foreach ($route as &$item) {
  111. $real_name = $item['option']['real_name'] ?? '';
  112. if (is_array($real_name)) {
  113. foreach ($action_arr as $action) {
  114. if (Str::contains($item['route'], $action)) {
  115. $real_name = $real_name[$action] ?? '';
  116. }
  117. }
  118. }
  119. $item['option']['real_name'] = $real_name;
  120. }
  121. return $route;
  122. }
  123. /**
  124. * 同步路由
  125. * @author 等风来
  126. * @email 136327134@qq.com
  127. * @date 2023/4/6
  128. */
  129. public function syncRoute(string $app = 'adminapi')
  130. {
  131. $id = app()->make(SystemRouteCateServices::class)->value(['app_name' => $app, 'name' => '全部权限', 'pid' => 0], 'id');
  132. if (!$id) {
  133. $res = app()->make(SystemRouteCateServices::class)->save([
  134. 'app_name' => $app,
  135. 'name' => '全部权限',
  136. 'pid' => 0,
  137. 'add_time' => time(),
  138. ]);
  139. $id = $res->id;
  140. }
  141. $listAll = $this->getRouteListAll($app);
  142. //保持新增的权限路由
  143. $data = $this->dao->selectList(['app_name' => $app], 'path,method')->toArray();
  144. $save = [];
  145. foreach ($listAll as $item) {
  146. if (!$this->diffRoute($data, $item['rule'], $item['method']) && strstr($item['rule'], '<MISS>') === false) {
  147. $save[] = [
  148. 'name' => $item['option']['real_name'] ?? $item['name'],
  149. 'path' => $item['rule'],
  150. 'cate_id' => $id,
  151. 'app_name' => $app,
  152. 'type' => isset($item['option']['is_common']) && $item['option']['is_common'] ? 1 : 0,
  153. 'method' => $item['method'],
  154. 'add_time' => date('Y-m-d H:i:s'),
  155. ];
  156. }
  157. }
  158. if ($save) {
  159. $this->dao->saveAll($save);
  160. }
  161. //删除不存在的权限路由
  162. $data = $this->dao->selectList(['app_name' => $app], 'path,method,id')->toArray();
  163. $delete = [];
  164. foreach ($data as $item) {
  165. if (!$this->diffRoute($listAll, $item['path'], $item['method'], 'rule') && $item['path'] !== '<MISS>') {
  166. $delete[] = $item['id'];
  167. }
  168. }
  169. if ($delete) {
  170. $this->dao->delete([['id', 'in', $delete]]);
  171. }
  172. }
  173. /**
  174. * 对比路由
  175. * @param array $data
  176. * @param string $path
  177. * @param string $method
  178. * @return bool
  179. * @author 等风来
  180. * @email 136327134@qq.com
  181. * @date 2023/4/6
  182. */
  183. protected function diffRoute(array $data, string $path, string $method, string $key = 'path')
  184. {
  185. $res = false;
  186. foreach ($data as $item) {
  187. if (strtolower($item['method']) == strtolower($method) && strtolower($item[$key]) == strtolower($path)) {
  188. $res = true;
  189. break;
  190. } else if ($method === '*' && strtolower($item[$key]) == strtolower($path)) {
  191. $res = true;
  192. break;
  193. }
  194. }
  195. return $res;
  196. }
  197. /**
  198. * 添加和修改路由
  199. * @param int $id
  200. * @return array
  201. * @author 等风来
  202. * @email 136327134@qq.com
  203. * @date 2023/4/7
  204. */
  205. public function getFrom(int $id = 0, string $appName = 'adminapi')
  206. {
  207. $cateList = app()->make(SystemRouteCateServices::class)->getAllList($appName, 'name as label,path as value');
  208. $url = '/system/route';
  209. $routeInfo = [];
  210. if ($id) {
  211. $routeInfo = $this->dao->get($id);
  212. $routeInfo = $routeInfo ? $routeInfo->toArray() : [];
  213. $url .= '/' . $id;
  214. }
  215. $rule = [
  216. FormBuilder::cascader('cate_id', '分类', $routeInfo['cate_id'] ?? 0)->data($cateList),
  217. FormBuilder::input('name', '路由名称', $routeInfo['name'] ?? '')->required(),
  218. FormBuilder::input('path', '路由路径', $routeInfo['path'] ?? '')->required(),
  219. FormBuilder::select('method', '请求方式', $routeInfo['method'] ?? '')->options([
  220. ['value' => 'POST', 'label' => 'POST'],
  221. ['value' => 'GET', 'label' => 'GET'],
  222. ['value' => 'DELETE', 'label' => 'DELETE'],
  223. ['value' => 'PUT', 'label' => 'PUT'],
  224. ['value' => '*', 'label' => '*'],
  225. ])->required(),
  226. FormBuilder::radio('type', '类型', $routeInfo['type'] ?? 0)->options([
  227. ['value' => 0, 'lable' => '普通路由'],
  228. ['value' => 1, 'lable' => '公共路由'],
  229. ]),
  230. FormBuilder::hidden('app_name', $appName),
  231. ];
  232. return create_form($id ? '修改路由' : '添加路由', $rule, $url, $id ? 'PUT' : 'POST');
  233. }
  234. }