1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace General\Middleware;
- use App\Consts\SysConsts;
- use Closure;
- use General\Helpers\CommonHelper;
- use General\Requests\CompanyAuth\CompanyAuthRequest;
- use General\Services\BaseAuthConfig;
- use General\Services\Config\ConfigService;
- use Illuminate\Http\Request;
- class CompanyAuth
- {
- use BaseAuthConfig;
- public function handle(Request $request, Closure $next)
- {
- $app_id = $request->get('app_id', '');
- $service = new ConfigService;
- $validate = new CompanyAuthRequest($request);
- $validate->validate();
- $client_ip = CommonHelper::GetClientIp();
- \Log::info('appid:'.$app_id.' ip:'.$client_ip);
- // if(true){
- if (in_array($client_ip, $service->CompanyAuthIps($app_id))) {
- $params = $request->except('_url');
- $timestamp = (int) $params['timestamp'];
- $diff = time() - $timestamp;
- if ($diff < SysConsts::ONE_MINUTE_SECONDS * 60) {
- $config = $service->findCompanyAuthConfig($params['app_id']);
- // 检查channel_id所属appid
- $channel_ids = $service->findCompanyChannelIds($config->company_id); // 获取该公司下的所有渠道ID
- $request_channel_id = $request->get('channel_id', '');
- if ($request_channel_id && !in_array($request_channel_id, $channel_ids)) {
- return response()->error('CHANNEL_AUTH_INVALID');
- }
- if (isset($params['sign']) && strcasecmp(CommonHelper::sign($params, $config->app_secret), $params['sign']) == 0) {
- $this->setGlobalConfig($config);
- return $next($request);
- } else {
- CommonHelper::myLog('test')->info(CommonHelper::sign($params, $config->app_secret));
- return response()->error('COMPANY_AUTH_SIGN_ERROR');
- }
- } else {
- return response()->error('COMPANY_AUTH_EXPIRED');
- }
- } else {
- return response()->error('COMPANY_AUTH_IP_NOT_EXISTS', [
- 'ip' => $client_ip
- ]);
- }
- }
- }
|