SystemRouteServices.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. $listAll = $this->getRouteListAll($app);
  116. //保持新增的权限路由
  117. $data = $this->dao->selectList(['app_name' => $app], 'path,method')->toArray();
  118. $save = [];
  119. foreach ($listAll as $item) {
  120. if (!$this->diffRoute($data, $item['rule'], $item['method']) && strstr($item['rule'], '<MISS>') === false) {
  121. $save[] = [
  122. 'name' => $item['option']['real_name'] ?? $item['name'],
  123. 'path' => $item['rule'],
  124. 'app_name' => $app,
  125. 'type' => isset($item['option']['is_common']) && $item['option']['is_common'] ? 1 : 0,
  126. 'method' => $item['method'],
  127. 'add_time' => date('Y-m-d H:i:s'),
  128. ];
  129. }
  130. }
  131. if ($save) {
  132. $this->dao->saveAll($save);
  133. }
  134. //删除不存在的权限路由
  135. $data = $this->dao->selectList(['app_name' => $app], 'path,method,id')->toArray();
  136. $delete = [];
  137. foreach ($data as $item) {
  138. if (!$this->diffRoute($listAll, $item['path'], $item['method'], 'rule') && $item['path'] !== '<MISS>') {
  139. $delete[] = $item['id'];
  140. }
  141. }
  142. if ($delete) {
  143. $this->dao->delete([['id', 'in', $delete]]);
  144. }
  145. }
  146. /**
  147. * 对比路由
  148. * @param array $data
  149. * @param string $path
  150. * @param string $method
  151. * @return bool
  152. * @author 等风来
  153. * @email 136327134@qq.com
  154. * @date 2023/4/6
  155. */
  156. protected function diffRoute(array $data, string $path, string $method, string $key = 'path')
  157. {
  158. $res = false;
  159. foreach ($data as $item) {
  160. if (strtolower($item['method']) == strtolower($method) && strtolower($item[$key]) == strtolower($path)) {
  161. $res = true;
  162. break;
  163. } else if ($method === '*' && strtolower($item[$key]) == strtolower($path)) {
  164. $res = true;
  165. break;
  166. }
  167. }
  168. return $res;
  169. }
  170. /**
  171. * 添加和修改路由
  172. * @param int $id
  173. * @return array
  174. * @author 等风来
  175. * @email 136327134@qq.com
  176. * @date 2023/4/7
  177. */
  178. public function getFrom(int $id = 0, string $appName = 'adminapi')
  179. {
  180. $cateList = app()->make(SystemRouteCateServices::class)->getAllList($appName, 'name as label,path as value');
  181. $url = '/system/route';
  182. $routeInfo = [];
  183. if ($id) {
  184. $routeInfo = $this->dao->get($id);
  185. $routeInfo = $routeInfo ? $routeInfo->toArray() : [];
  186. $url .= '/' . $id;
  187. }
  188. $rule = [
  189. FormBuilder::cascader('cate_id', '分类', $routeInfo['cate_id'] ?? 0)->data($cateList),
  190. FormBuilder::input('name', '路由名称', $routeInfo['name'] ?? '')->required(),
  191. FormBuilder::input('path', '路由路径', $routeInfo['path'] ?? '')->required(),
  192. FormBuilder::select('method', '请求方式', $routeInfo['method'] ?? '')->options([
  193. ['value' => 'POST', 'label' => 'POST'],
  194. ['value' => 'GET', 'label' => 'GET'],
  195. ['value' => 'DELETE', 'label' => 'DELETE'],
  196. ['value' => 'PUT', 'label' => 'PUT'],
  197. ['value' => '*', 'label' => '*'],
  198. ])->required(),
  199. FormBuilder::radio('type', '类型', $routeInfo['type'] ?? 0)->options([
  200. ['value' => 0, 'lable' => '普通路由'],
  201. ['value' => 1, 'lable' => '公共路由'],
  202. ]),
  203. FormBuilder::hidden('app_name', $appName),
  204. ];
  205. return create_form($id ? '修改路由' : '添加路由', $rule, $url, $id ? 'PUT' : 'POST');
  206. }
  207. }