<?php
/**
 * Created by IntelliJ IDEA.
 * User: BPCoder
 * Date: 2017/3/30
 * Time: 10:28
 */

namespace backend\modules\test;

use backend\modules\ApiController;
use common\exceptions\BaseException;
use common\libs\AES;
use common\libs\Redis;
use common\services\UserService;

class TestApiBaseController extends ApiController {

    public $notLoginMaps = [];
    public $userInfo = [];

    public function beforeAction($action) {
        if (!in_array($action->actionMethod, $this->notLoginMaps)) {
            $token = $this->getToken();
            $userId = AES::decrypt($token, AES_SECRET_KEY);
            if (Redis::getInstance()->getString(sprintf(USER_TOKEN, $userId)) != $token) {
                throw new BaseException(BP_API_ERROR_LOG_IN_AGAIN, BP_API_ERROR_LOG_IN_AGAIN_MSG);
            }
            $this->userInfo = UserService::getInstance()->getUserInfo($userId);
        }
        return parent::beforeAction($action);
    }

    public function afterAction($action, $result) {
        return parent::afterAction($action, $result);
    }
}