OutInterfaceServices.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\services\out;
  3. use app\dao\out\OutInterfaceDao;
  4. use app\Request;
  5. use app\services\BaseServices;
  6. use app\services\system\SystemRouteCateServices;
  7. use app\services\system\SystemRouteServices;
  8. use crmeb\exceptions\AdminException;
  9. use crmeb\exceptions\AuthException;
  10. class OutInterfaceServices extends BaseServices
  11. {
  12. public function __construct(OutInterfaceDao $dao)
  13. {
  14. $this->dao = $dao;
  15. }
  16. /**
  17. * 验证对外接口权限
  18. * @param Request $request
  19. * @return bool
  20. */
  21. public function verifyAuth(Request $request)
  22. {
  23. $rule = trim(strtolower($request->rule()->getRule()));
  24. $method = trim(strtolower($request->method()));
  25. $authList = app()->make(SystemRouteServices::class)->getColumn([['id', 'in', $request->outInfo()['rules']], ['app_name', '=', 'outapi']], 'method,path');
  26. $rolesAuth = [];
  27. foreach ($authList as $item) {
  28. $rolesAuth[trim(strtolower($item['method']))][] = trim(strtolower(str_replace(' ', '', $item['path'])));
  29. }
  30. $rule = str_replace('<', '{', $rule);
  31. $rule = str_replace('>', '}', $rule);
  32. if (in_array($rule, $rolesAuth[$method])) {
  33. return true;
  34. } else {
  35. throw new AuthException(110000);
  36. }
  37. }
  38. /**
  39. * 对外接口列表
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function outInterfaceList(): array
  46. {
  47. // 获取系统路由分类列表
  48. $list = app()->make(SystemRouteCateServices::class)->selectList(['app_name' => 'outapi'], 'id,pid,name,name as title')->toArray();
  49. // 获取系统路由列表
  50. $data = app()->make(SystemRouteServices::class)->selectList(['app_name' => 'outapi'], 'id,cate_id as pid,name,name as title')->toArray();
  51. // 遍历分类列表,将分类下的路由添加到对应的子节点中
  52. foreach ($list as &$item) {
  53. foreach ($data as $k => $v) {
  54. if ($item['id'] == $v['pid']) {
  55. $item['children'][] = $v;
  56. }
  57. }
  58. }
  59. // 返回完整的外部接口列表
  60. return $list;
  61. }
  62. /**
  63. * 新增对外接口文档
  64. * @param $id
  65. * @param $data
  66. * @return bool
  67. */
  68. public function saveInterface($id, $data)
  69. {
  70. $data['request_params'] = json_encode($data['request_params']);
  71. $data['return_params'] = json_encode($data['return_params']);
  72. $data['error_code'] = json_encode($data['error_code']);
  73. if ($id) {
  74. $res = $this->dao->update($id, $data);
  75. } else {
  76. $res = $this->dao->save($data);
  77. }
  78. if (!$res) throw new AdminException(100006);
  79. return true;
  80. }
  81. /**
  82. * 对外接口文档
  83. * @param $id
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function interfaceInfo($id)
  90. {
  91. if (!$id) throw new AdminException(100100);
  92. $info = $this->dao->get($id);
  93. if (!$info) throw new AdminException(100026);
  94. $info = $info->toArray();
  95. $info['request_params'] = json_decode($info['request_params']);
  96. $info['return_params'] = json_decode($info['return_params']);
  97. $info['error_code'] = json_decode($info['error_code']);
  98. return $info;
  99. }
  100. /**
  101. * 修改接口名称
  102. * @param $data
  103. * @return bool
  104. */
  105. public function editInterfaceName($data)
  106. {
  107. $res = $this->dao->update($data['id'], ['name' => $data['name']]);
  108. if (!$res) throw new AdminException(100007);
  109. return true;
  110. }
  111. /**
  112. * 删除接口
  113. * @param $id
  114. * @return bool
  115. */
  116. public function delInterface($id)
  117. {
  118. $res = $this->dao->update($id, ['is_del' => 1]);
  119. if (!$res) throw new AdminException(100008);
  120. return true;
  121. }
  122. }