webApp.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. $token = $request->all('token');
  55. $tokenData = RedisService::get("apis:tokens:{$token}");
  56. $userId = isset($tokenData['id'])? $tokenData['id'] : 0;
  57. // 请求频率限制
  58. $clientIp = get_client_ip();
  59. $ipStr = str_replace('.', '_', $clientIp);
  60. $cacheKey = "apis:request:temp:{$ipStr}";
  61. $requestCount = RedisService::get($cacheKey);
  62. $requestCount = $requestCount? $requestCount : 0;
  63. if($requestCount >= 10){
  64. return message(1028, false, null, 403);
  65. }
  66. // 登录验证
  67. $userInfo = RedisService::get("apis:auths:info:{$userId}");
  68. if(empty($userInfo) && $userId){
  69. $this->initLogin($userId);
  70. }else{
  71. $this->userId = $userId;
  72. $this->userInfo = $userInfo;
  73. $this->apiId = isset($userInfo['api_id'])? $userInfo['api_id'] : 0;
  74. }
  75. RedisService::set($cacheKey, $requestCount+1, rand(1,2));
  76. return $next($request);
  77. });
  78. }
  79. /**
  80. * 初始化配置
  81. * @author laravel开发员
  82. * @since 2020/11/10
  83. */
  84. public function initConfig()
  85. {
  86. // 请求参数
  87. $this->param = \request()->input();
  88. // 分页基础默认值
  89. defined('PERPAGE') or define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
  90. defined('PAGE') or define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
  91. }
  92. /**
  93. * 登录验证
  94. * @param $userId 用户ID
  95. * @return
  96. * @author wesmiler
  97. * @since 2020/8/31
  98. */
  99. public function initLogin($userId)
  100. {
  101. // 登录用户ID
  102. $this->userId = $userId;
  103. // 登录用户信息
  104. if ($userId) {
  105. $userInfo = MemberService::make()->getInfo($this->userId);
  106. $this->userInfo = $userInfo;
  107. $this->apiId = isset($userInfo['api_id'])? $userInfo['api_id'] : 0;
  108. RedisService::set("apis:auths:info:{$userId}", $this->userInfo, 4*24*3600);
  109. }
  110. }
  111. }