CompanyAuth.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace General\Middleware;
  3. use App\Consts\SysConsts;
  4. use Closure;
  5. use General\Helpers\CommonHelper;
  6. use General\Requests\CompanyAuth\CompanyAuthRequest;
  7. use General\Services\BaseAuthConfig;
  8. use General\Services\Config\ConfigService;
  9. use Illuminate\Http\Request;
  10. class CompanyAuth
  11. {
  12. use BaseAuthConfig;
  13. public function handle(Request $request, Closure $next)
  14. {
  15. $app_id = $request->get('app_id', '');
  16. $service = new ConfigService;
  17. $validate = new CompanyAuthRequest($request);
  18. $validate->validate();
  19. $client_ip = CommonHelper::GetClientIp();
  20. \Log::info('appid:'.$app_id.' ip:'.$client_ip);
  21. // if(true){
  22. if (in_array($client_ip, $service->CompanyAuthIps($app_id))) {
  23. $params = $request->except('_url');
  24. $timestamp = (int) $params['timestamp'];
  25. $diff = time() - $timestamp;
  26. if ($diff < SysConsts::ONE_MINUTE_SECONDS * 60) {
  27. $config = $service->findCompanyAuthConfig($params['app_id']);
  28. // 检查channel_id所属appid
  29. $channel_ids = $service->findCompanyChannelIds($config->company_id); // 获取该公司下的所有渠道ID
  30. $request_channel_id = $request->get('channel_id', '');
  31. if ($request_channel_id && !in_array($request_channel_id, $channel_ids)) {
  32. return response()->error('CHANNEL_AUTH_INVALID');
  33. }
  34. if (isset($params['sign']) && strcasecmp(CommonHelper::sign($params, $config->app_secret), $params['sign']) == 0) {
  35. $this->setGlobalConfig($config);
  36. return $next($request);
  37. } else {
  38. CommonHelper::myLog('test')->info(CommonHelper::sign($params, $config->app_secret));
  39. return response()->error('COMPANY_AUTH_SIGN_ERROR');
  40. }
  41. } else {
  42. return response()->error('COMPANY_AUTH_EXPIRED');
  43. }
  44. } else {
  45. return response()->error('COMPANY_AUTH_IP_NOT_EXISTS', [
  46. 'ip' => $client_ip
  47. ]);
  48. }
  49. }
  50. }