BasicApi.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace controller;
  3. use library\Tools;
  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. Tools::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->request('token', $this->request->header('token', false));
  37. // if ((empty($this->token) || !$this->getCache($this->token)) && ($this->request->action() !== 'auth')) {
  38. if (empty($this->token) && $this->request->action() !== 'auth') {
  39. exit($this->response('访问TOKEN失效,请重新授权!', 'ACCESS_TOKEN_FAILD')->send());
  40. }
  41. }
  42. /**
  43. * 输出返回数据
  44. * @access protected
  45. * @param mixed $data 要返回的数据
  46. * @param String $type 返回类型 JSON XML
  47. * @param integer $code HTTP状态码
  48. * @return Response
  49. */
  50. public function response($msg, $code = 'SUCCESS', $data = [], $type = 'json') {
  51. $result = ['code' => $code, 'msg' => $msg, 'data' => $data, 'token' => $this->token, 'dataType' => strtolower($type)];
  52. return Response::create($result, $type)->code(200);
  53. }
  54. /**
  55. * 写入缓存
  56. * @param string $name 缓存标识
  57. * @param mixed $value 存储数据
  58. * @param int|null $expire 有效时间 0为永久
  59. * @return bool
  60. */
  61. public function setCache($name, $value, $expire = null) {
  62. return Cache::set("{$this->token}_{$name}", $value, $expire);
  63. }
  64. /**
  65. * 读取缓存
  66. * @param string $name 缓存标识
  67. * @param mixed $default 默认值
  68. * @return mixed
  69. */
  70. public function getCache($name, $default = false) {
  71. return Cache::get("{$this->token}_{$name}", $default);
  72. }
  73. /**
  74. * 删除缓存
  75. * @param string $name 缓存标识
  76. * @return bool
  77. */
  78. public function delCache($name) {
  79. return Cache::rm("{$this->token}_{$name}");
  80. }
  81. /**
  82. * API接口调度
  83. * @return Response
  84. */
  85. public function _empty() {
  86. list($module, $controller, $action, $method) = explode('/', $this->request->path() . '///');
  87. if (!empty($module) && !empty($controller) && !empty($action) && !empty($method)) {
  88. $action = ucfirst($action);
  89. $Api = "app\\{$module}\\{$controller}\\{$action}Api";
  90. if (method_exists($Api, $method)) {
  91. return $Api::$method($this);
  92. }
  93. return $this->response('访问的接口不存在!', 'NOT_FOUND');
  94. }
  95. return $this->response('不符合标准的接口!', 'API_ERROR');
  96. }
  97. }