webApp.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Http\Controllers\Oapi;
  12. use App\Http\Controllers\BaseController;
  13. use App\Services\Api\MemberService;
  14. use App\Services\RedisService;
  15. /**
  16. * 控制器基类
  17. * @since 2020/11/10
  18. * Class BaseController
  19. * @package App\Http\Controllers
  20. */
  21. class webApp extends BaseController
  22. {
  23. // 模型
  24. protected $model;
  25. // 服务
  26. protected $service;
  27. // 校验
  28. protected $validate;
  29. // 登录ID
  30. protected $userId = 0;
  31. // 所属接口平台ID
  32. protected $apiId = 0;
  33. // 登录信息
  34. protected $userInfo = [];
  35. /**
  36. * 构造函数
  37. * @author wesmiler
  38. * @since 2020/11/10
  39. * Backend constructor.
  40. */
  41. public function __construct()
  42. {
  43. parent::__construct();
  44. // 初始化分页参数
  45. $this->initConfig();
  46. // 登录检测中间件
  47. $this->middleware('api.login');
  48. // 初始化登录信息
  49. $this->middleware(function ($request, $next) {
  50. // 请求头信息
  51. $params = $request->all();
  52. $token = isset($params['token'])? $params['token'] : '';
  53. $tokenData = RedisService::get("apis:tokens:{$token}");
  54. $userId = isset($tokenData['id'])? $tokenData['id'] : 0;
  55. $this->apiId = isset($tokenData['api_id'])? $tokenData['api_id'] : 0;
  56. // 请求频率限制
  57. $clientIp = get_client_ip();
  58. $ipStr = str_replace('.', '_', $clientIp);
  59. $cacheKey = "apis:request:temp:{$ipStr}";
  60. $requestCount = RedisService::get($cacheKey);
  61. $requestCount = $requestCount? $requestCount : 0;
  62. if($requestCount >= 10){
  63. return response()->json(message(1028, false, null, 403));
  64. }
  65. // 登录验证
  66. $userInfo = RedisService::get("apis:auths:info:{$userId}");
  67. if(empty($userInfo) && $userId){
  68. $this->initLogin($userId);
  69. }else{
  70. $this->userId = $userId;
  71. $this->userInfo = $userInfo;
  72. $this->apiId = isset($userInfo['api_id'])? $userInfo['api_id'] : $this->apiId;
  73. }
  74. if(RedisService::exists($cacheKey)){
  75. RedisService::incr($cacheKey, 1);
  76. }else{
  77. RedisService::incr($cacheKey, 1);
  78. RedisService::expire($cacheKey, 5);
  79. }
  80. return $next($request);
  81. });
  82. }
  83. /**
  84. * 初始化配置
  85. * @author laravel开发员
  86. * @since 2020/11/10
  87. */
  88. public function initConfig()
  89. {
  90. // 请求参数
  91. $this->param = \request()->input();
  92. // 分页基础默认值
  93. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  94. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  95. }
  96. /**
  97. * 登录验证
  98. * @param $userId 用户ID
  99. * @return
  100. * @author wesmiler
  101. * @since 2020/8/31
  102. */
  103. public function initLogin($userId)
  104. {
  105. // 登录用户ID
  106. $this->userId = $userId;
  107. // 登录用户信息
  108. if ($userId) {
  109. $userInfo = MemberService::make()->getInfo($this->userId);
  110. $this->userInfo = $userInfo;
  111. $this->apiId = isset($userInfo['api_id'])? $userInfo['api_id'] : 0;
  112. RedisService::set("apis:auths:info:{$userId}", $this->userInfo, 4*24*3600);
  113. }
  114. }
  115. }