<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Created by PhpStorm.
 * User: joe
 * Date: 16-9-7
 * Time: 下午3:01
 */
class Login extends CI_Controller {

    public $user;
    function __construct() {
        parent::__construct();
        $this->load->model('users_model', 'users');
        $this->user=$this->session->EXM_USER;
    }

    public function index() {
        if ($this->user) {
            redirect('/');
        }
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        if ($username) {
            if (!$password) {
                show_json(['error' => '密码未填写。']);
                return;
            }
            $user = $this->users->valid($username, $password);
            if (!is_array($user)) {
                echo($user);
            } else {
                $this->session->set_userdata('EXM_USER', $user);
                show_json(['success' => '登录成功', 'user' => $user]);
                session_write_close();
            }
        } else {
            $this->load->view('login');
        }
    }

    function out() {
        session_destroy();
        redirect('/');
    }
}