BasicApi.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace controller;
  3. use service\ToolsService;
  4. use think\Cache;
  5. use think\Request;
  6. use think\Response;
  7. /**
  8. * 数据接口通用控制器
  9. * @package controller
  10. */
  11. class BasicApi {
  12. /**
  13. * 访问请求对象
  14. * @var Request
  15. */
  16. public $request;
  17. /**
  18. * 当前访问身份
  19. * @var string
  20. */
  21. public $token;
  22. /**
  23. * 基础接口SDK
  24. * @param Request|null $request
  25. */
  26. public function __construct(Request $request = null) {
  27. // CORS 跨域 Options 检测响应
  28. ToolsService::corsOptionsHandler();
  29. // 获取当前 Request 对象
  30. $this->request = is_null($request) ? Request::instance() : $request;
  31. // 安全方法请求过滤
  32. if (in_array(strtolower($this->request->action()), ['response', 'setcache', 'getcache', 'delcache', '_empty'])) {
  33. exit($this->response('禁止访问接口安全方法!', 'ACCESS_NOT_ALLOWED')->send());
  34. }
  35. // 访问 Token 检测处理
  36. $this->token = $this->request->param('token', $this->request->header('token', false));
  37. if (empty($this->token) && !method_exists($this, $this->request->action())) {
  38. exit($this->response('访问TOKEN失效,请重新授权!', 'ACCESS_TOKEN_FAILD')->send());
  39. }
  40. }
  41. /**
  42. * 输出返回数据
  43. * @param string $msg 提示消息内容
  44. * @param string $code 业务状态码
  45. * @param mixed $data 要返回的数据
  46. * @param string $type 返回类型 JSON XML
  47. * @return Response
  48. */
  49. public function response($msg, $code = 'SUCCESS', $data = [], $type = 'json') {
  50. $result = ['msg' => $msg, 'code' => $code, 'data' => $data, 'token' => $this->token, 'dataType' => strtolower($type)];
  51. return Response::create($result, $type)->header(ToolsService::corsRequestHander())->code(200);
  52. }
  53. /**
  54. * 写入缓存
  55. * @param string $name 缓存标识
  56. * @param mixed $value 存储数据
  57. * @param int|null $expire 有效时间 0为永久
  58. * @return bool
  59. */
  60. public function setCache($name, $value, $expire = null) {
  61. return Cache::set("{$this->token}_{$name}", $value, $expire);
  62. }
  63. /**
  64. * 读取缓存
  65. * @param string $name 缓存标识
  66. * @param mixed $default 默认值
  67. * @return mixed
  68. */
  69. public function getCache($name, $default = false) {
  70. return Cache::get("{$this->token}_{$name}", $default);
  71. }
  72. /**
  73. * 删除缓存
  74. * @param string $name 缓存标识
  75. * @return bool
  76. */
  77. public function delCache($name) {
  78. return Cache::rm("{$this->token}_{$name}");
  79. }
  80. /**
  81. * API接口调度
  82. * @return Response
  83. */
  84. public function _empty() {
  85. list($module, $controller, $action, $method) = explode('/', $this->request->path() . '///');
  86. if (!empty($module) && !empty($controller) && !empty($action) && !empty($method)) {
  87. $action = ucfirst($action);
  88. $Api = config('app_namespace') . "\\{$module}\\{$controller}\\{$action}Api";
  89. if (method_exists($Api, $method)) {
  90. return $Api::$method($this);
  91. }
  92. return $this->response('访问的接口不存在!', 'API_NOT_FOUND');
  93. }
  94. return $this->response('不符合标准的接口!', 'API_ERROR');
  95. }
  96. }