OutInterfaceServices.php 3.8 KB

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