OutInterfaceServices.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. if (in_array($rule, $rolesAuth[$method])) {
  31. return true;
  32. }
  33. $rule = str_replace('<', '{', $rule);
  34. $rule = str_replace('>', '}', $rule);
  35. if (in_array($rule, $rolesAuth[$method])) {
  36. return true;
  37. } else {
  38. throw new AuthException(110000);
  39. }
  40. }
  41. /**
  42. * 对外接口列表
  43. * @return array
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public function outInterfaceList(): array
  49. {
  50. // 获取系统路由分类列表
  51. $list = app()->make(SystemRouteCateServices::class)->selectList(['app_name' => 'outapi'], 'id,pid,name,name as title')->toArray();
  52. // 获取系统路由列表
  53. $data = app()->make(SystemRouteServices::class)->selectList(['app_name' => 'outapi'], 'id,cate_id as pid,name,name as title')->toArray();
  54. // 遍历分类列表,将分类下的路由添加到对应的子节点中
  55. foreach ($list as &$item) {
  56. foreach ($data as $k => $v) {
  57. if ($item['id'] == $v['pid']) {
  58. $item['children'][] = $v;
  59. }
  60. }
  61. }
  62. // 返回完整的外部接口列表
  63. return $list;
  64. }
  65. /**
  66. * 新增对外接口文档
  67. * @param $id
  68. * @param $data
  69. * @return bool
  70. */
  71. public function saveInterface($id, $data)
  72. {
  73. $data['request_params'] = json_encode($data['request_params']);
  74. $data['return_params'] = json_encode($data['return_params']);
  75. $data['error_code'] = json_encode($data['error_code']);
  76. if ($id) {
  77. $res = $this->dao->update($id, $data);
  78. } else {
  79. $res = $this->dao->save($data);
  80. }
  81. if (!$res) throw new AdminException(100006);
  82. return true;
  83. }
  84. /**
  85. * 对外接口文档
  86. * @param $id
  87. * @return array
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function interfaceInfo($id)
  93. {
  94. if (!$id) throw new AdminException(100100);
  95. $info = $this->dao->get($id);
  96. if (!$info) throw new AdminException(100026);
  97. $info = $info->toArray();
  98. $info['request_params'] = json_decode($info['request_params']);
  99. $info['return_params'] = json_decode($info['return_params']);
  100. $info['error_code'] = json_decode($info['error_code']);
  101. return $info;
  102. }
  103. /**
  104. * 修改接口名称
  105. * @param $data
  106. * @return bool
  107. */
  108. public function editInterfaceName($data)
  109. {
  110. $res = $this->dao->update($data['id'], ['name' => $data['name']]);
  111. if (!$res) throw new AdminException(100007);
  112. return true;
  113. }
  114. /**
  115. * 删除接口
  116. * @param $id
  117. * @return bool
  118. */
  119. public function delInterface($id)
  120. {
  121. $res = $this->dao->update($id, ['is_del' => 1]);
  122. if (!$res) throw new AdminException(100008);
  123. return true;
  124. }
  125. }