OutInterfaceServices.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\out;
  12. use app\dao\out\OutInterfaceDao;
  13. use app\Request;
  14. use app\services\BaseServices;
  15. use app\services\system\SystemRouteCateServices;
  16. use app\services\system\SystemRouteServices;
  17. use crmeb\exceptions\AdminException;
  18. use crmeb\exceptions\AuthException;
  19. class OutInterfaceServices extends BaseServices
  20. {
  21. public function __construct(OutInterfaceDao $dao)
  22. {
  23. $this->dao = $dao;
  24. }
  25. /**
  26. * 验证对外接口权限
  27. * @param Request $request
  28. * @return bool
  29. */
  30. public function verifyAuth(Request $request)
  31. {
  32. $rule = trim(strtolower($request->rule()->getRule()));
  33. $method = trim(strtolower($request->method()));
  34. $authList = app()->make(SystemRouteServices::class)->getColumn([['id', 'in', $request->outInfo()['rules']], ['app_name', '=', 'outapi']], 'method,path');
  35. $rolesAuth = [];
  36. foreach ($authList as $item) {
  37. $rolesAuth[trim(strtolower($item['method']))][] = trim(strtolower(str_replace(' ', '', $item['path'])));
  38. }
  39. if (in_array($rule, $rolesAuth[$method])) {
  40. return true;
  41. }
  42. $rule = str_replace('<', '{', $rule);
  43. $rule = str_replace('>', '}', $rule);
  44. if (in_array($rule, $rolesAuth[$method])) {
  45. return true;
  46. } else {
  47. throw new AuthException('您暂时没有访问权限');
  48. }
  49. }
  50. /**
  51. * 对外接口列表
  52. * @return array
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function outInterfaceList(): array
  58. {
  59. // 获取系统路由分类列表
  60. $list = app()->make(SystemRouteCateServices::class)->selectList(['app_name' => 'outapi'], 'id,pid,name,name as title')->toArray();
  61. // 获取系统路由列表
  62. $data = app()->make(SystemRouteServices::class)->selectList(['app_name' => 'outapi'], 'id,cate_id as pid,name,name as title')->toArray();
  63. // 遍历分类列表,将分类下的路由添加到对应的子节点中
  64. foreach ($list as &$item) {
  65. foreach ($data as $k => $v) {
  66. if ($item['id'] == $v['pid']) {
  67. $item['children'][] = $v;
  68. }
  69. }
  70. }
  71. // 返回完整的外部接口列表
  72. return $list;
  73. }
  74. /**
  75. * 新增对外接口文档
  76. * @param $id
  77. * @param $data
  78. * @return bool
  79. */
  80. public function saveInterface($id, $data)
  81. {
  82. $data['request_params'] = json_encode($data['request_params']);
  83. $data['return_params'] = json_encode($data['return_params']);
  84. $data['error_code'] = json_encode($data['error_code']);
  85. if ($id) {
  86. $res = $this->dao->update($id, $data);
  87. } else {
  88. $res = $this->dao->save($data);
  89. }
  90. if (!$res) throw new AdminException('保存失败');
  91. return true;
  92. }
  93. /**
  94. * 对外接口文档
  95. * @param $id
  96. * @return array
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public function interfaceInfo($id)
  102. {
  103. if (!$id) throw new AdminException('参数错误');
  104. $info = $this->dao->get($id);
  105. if (!$info) throw new AdminException('数据不存在');
  106. $info = $info->toArray();
  107. $info['request_params'] = json_decode($info['request_params']);
  108. $info['return_params'] = json_decode($info['return_params']);
  109. $info['error_code'] = json_decode($info['error_code']);
  110. return $info;
  111. }
  112. /**
  113. * 修改接口名称
  114. * @param $data
  115. * @return bool
  116. */
  117. public function editInterfaceName($data)
  118. {
  119. $res = $this->dao->update($data['id'], ['name' => $data['name']]);
  120. if (!$res) throw new AdminException('修改失败');
  121. return true;
  122. }
  123. /**
  124. * 删除接口
  125. * @param $id
  126. * @return bool
  127. */
  128. public function delInterface($id)
  129. {
  130. $res = $this->dao->update($id, ['is_del' => 1]);
  131. if (!$res) throw new AdminException('删除失败');
  132. return true;
  133. }
  134. }