SystemRouteServices.php 7.9 KB

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