OutInterfaceServices.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\services\out;
  3. use app\dao\out\OutInterfaceDao;
  4. use app\Request;
  5. use app\services\BaseServices;
  6. use crmeb\exceptions\AuthException;
  7. class OutInterfaceServices extends BaseServices
  8. {
  9. public function __construct(OutInterfaceDao $dao)
  10. {
  11. $this->dao = $dao;
  12. }
  13. /**
  14. * 验证对外接口权限
  15. * @param Request $request
  16. * @return bool
  17. */
  18. public function verifyAuth(Request $request)
  19. {
  20. $rule = trim(strtolower($request->rule()->getRule()));
  21. $method = trim(strtolower($request->method()));
  22. $authList = $this->dao->getColumn([['id', 'in', $request->outInfo()['rules']]], 'method,url');
  23. $rolesAuth = [];
  24. foreach ($authList as $item) {
  25. $rolesAuth[trim(strtolower($item['method']))][] = trim(strtolower(str_replace(' ', '', $item['url'])));
  26. }
  27. if (in_array('/' . $rule, $rolesAuth[$method])) {
  28. return true;
  29. } else {
  30. throw new AuthException(110000);
  31. }
  32. }
  33. /**
  34. * 对外接口列表
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function outInterfaceList(): array
  41. {
  42. $list = $this->dao->selectList([], '*,name as title')->toArray();
  43. $data = [];
  44. foreach ($list as $key => $item) {
  45. if ($item['pid'] == 0) {
  46. $data[] = $item;
  47. unset($list[$key]);
  48. }
  49. }
  50. foreach ($data as &$item_p) {
  51. foreach ($list as $item_c) {
  52. if ($item_p['id'] == $item_c['pid']) {
  53. $item_p['children'][] = $item_c;
  54. }
  55. }
  56. }
  57. return $data;
  58. }
  59. }