Api.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace lemo\api;
  3. use think\facade\Request;
  4. use lemo\api\Send;
  5. use lemo\api\Oauth;
  6. /**
  7. * api 入口文件基类,需要控制权限的控制器都应该继承该类
  8. */
  9. class Api
  10. {
  11. use Send;
  12. /**
  13. * @var \think\Request Request实例
  14. */
  15. protected $request;
  16. protected $clientInfo;
  17. /**
  18. * 不需要鉴权方法
  19. */
  20. protected $noAuth = [];
  21. protected $uid = '';
  22. /**
  23. * 构造方法
  24. * @param Request $request Request对象
  25. */
  26. public function __construct(Request $request)
  27. {
  28. $this->request = Request::instance();
  29. $this->init();
  30. $this->uid = $this->clientInfo['uid'];
  31. }
  32. /**
  33. * 初始化
  34. * 检查请求类型,数据格式等
  35. */
  36. public function init()
  37. {
  38. //所有ajax请求的options预请求都会直接返回200,如果需要单独针对某个类中的方法,可以在路由规则中进行配置
  39. if($this->request->isOptions()){
  40. return self::returnMsg(200,'success');
  41. }
  42. if(!Oauth::match($this->noAuth)){ //请求方法白名单
  43. $oauth = new Oauth();
  44. return $this->clientInfo = $oauth->authenticate();
  45. }
  46. }
  47. /**
  48. * 空方法
  49. */
  50. public function _empty()
  51. {
  52. return self::returnMsg(404, 'empty method!');
  53. }
  54. }