<?php

namespace wechat\controllers;

use Yii;
use wechat\models\Admin;

class AdminController extends BaseController
{
    /**
     * 登录
     */
    public function actionLogin()
    {
        $username = trim(Yii::$app->request->post('username'));
        $password = trim(Yii::$app->request->post('password'));
        $login_ip = trim(Yii::$app->request->post('login_ip'));

        $model = Admin::find()->where(['username' => $username])->limit(1)->one();
        if (!$model) {
            return $this->outJson(0, [], '您的用户名或密码错误');
        }
        
        $model->login_time = time();
        $model->login_ip = $login_ip;
        $model->save();
        
        if (Admin::hash($password) != $model->password) {
            return $this->outJson(0, [], '您的用户名或密码错误!');
        }

        return $this->outJson(1, $model->toArray());
    }
    
    /**
     * 某个管理员
     */
    public function actionGetAdminById()
    {
        $id = (int) Yii::$app->request->post('id');
        if (!$id) {
            return $this->outJson(0, [], '参数错误');
        }
        $admin = Admin::find()->where(['id' => $id])->limit(1)->asArray()->one();
        if (!$admin) {
            return $this->outJson(0, [], '没有该管理员');
        }
        return $this->outJson(1, $admin);
    }
    
}
