<?php

namespace Application\Mvc\Controller;

use Application\Common\Container;
use Application\Mvc\Controller\Plugins\RenderApiJson;
use Application\Service\Exceptions\InvalidArgumentException;
use Laminas\Cache\Exception\ExceptionInterface;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Mvc\MvcEvent;

/**
 * @method Container|mixed LocalService()
 * @method RenderApiJson RenderApiJson()
 * @method BasicController Introduce()
 */
class ThirdToolController extends AbstractActionController
{
    public function attachDefaultListeners()
    {
        // 验证第三方登录信息
        $event = $this->getEventManager();
        $event->attach(MvcEvent::EVENT_DISPATCH, [$this, 'checkLogin'], 99);
        parent::attachDefaultListeners(); // TODO: Change the autogenerated stub
    }

    /**
     * @throws ExceptionInterface
     */
    public function checkLogin(MvcEvent $event) {
        $headers = $this->getRequest()->getHeaders()->toArray();
        if($this->LocalService()->toolSys->getAccessToken() !== $headers['Token']) {
            throw new InvalidArgumentException('授权失败！');
        }
    }
}