<?php
namespace wechat\modules\cp\controllers;

use wechat\models\AdminIdentity;
use wechat\models\AdminApi;
use Yii;

class AccountController extends BaseController
{
    /**
     * 登录界面
     * @return string
     */
    public function actionLogin()
    {
        $tip = '';

        if (Yii::$app->getRequest()->getIsPost()) {
            $data['username'] = trim(Yii::$app->request->post('j_username'));
            $data['password'] = trim(Yii::$app->request->post('j_password'));

            $api = new AdminApi();
            $result = $api->login($data);
            if ($result['code'] == 1) {
                $identity = new AdminIdentity();
                $identity->setAttributes($result['data']);
                Yii::$app->user->login($identity);
                return $this->redirect('/' . $this->module->id . '/default');
            } else {
                $tip = $result['message'];
            }
        }
        
        return $this->render('login', ['tip' => $tip]);
    }

    /**
     * 登出
     */
    public function actionLogout()
    {
        $redirectAction = 'login';
        Yii::$app->user->logout(false);
        $this->redirect([$redirectAction]);
    }
    
}