webApp.php 3.8 KB

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