<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace home\controllers;

use Yii;
use home\models\LoginForm;
use common\components\Tool;
use qcbase\models\Cache;

/**
 * 前台公共控制器
 * 
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */
class PublicController extends \yii\web\Controller
{

    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'minLength' => 4,
                'maxLength' => 4,
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * 登录页面 
     */
    public function actionLogin()
    {
        $cache = Yii::$app->cache;
        $users = $cache->get('user_list');
        if (false === $users) {
            Cache::updateAll();
        }

        //判断是否是手机端
        if (Tool::is_mobile_request()) {
            return $this->redirect('@web' . '/wap.php');
        }

        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }
        $model = new LoginForm();

        if($model->load(Yii::$app->request->post()) && $model->login()){
            \qcbase\models\ActionLog::insertLog('home', '登录');
            return $this->goHome();
        } else {
            $configs = $cache->get('basic_config');

            return $this->renderPartial('login', [
                'model' => $model,
                'configs' => $configs,
            ]);
        }
    }

    /**
     * 退出
     */
    public function actionLogout()
    {
        \qcbase\models\ActionLog::insertLog('home', '退出');
        Yii::$app->user->logout();
        return $this->goHome();
    }

}
