<?php namespace App\Controllers;
helper('statusCode');

class SignCtr extends BaseController
{
    /****************************************************
     *       用户登录
     /****************************************************/
    public function login(){
        $result = array();
        $param = $this->request->getJSON();
        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);
        $item = array();
        $item["username"] = $param->username;
        $item["password"] = $param->password;
        try{
            $signModel = new \App\Models\SignModel();
            $userInfo = $signModel->getItemByUsername($param->username);
            if(count($userInfo) == 0){
                $result["status"] = COMMON_FAIL;
                $result["message"] = "该用户不存在！";
            }else{
                if($userInfo[0]["password"] == $param->password){
                    $tokenInfo = array();
                    $exInfo = $param->username.$param->password.$curTime;     //用于生成token的字符串
                    $tokenStr = md5($exInfo);
                    $tokenInfo["token"] = $tokenStr;
                    $tokenInfo["login_time"] = $curTime;
                    $tokenInfo["username"] = $param->username;
                    $signModel->writeIntoToken($tokenInfo);
                    $result["status"] = SUCESS;
                    $result["message"] = "登录成功！";
                    $userInfo[0]["token"] = $tokenStr;
                    $result["user"] = $userInfo[0];
                }else{
                    $result["status"] = COMMON_FAIL;
                    $result["message"] = "用户名或密码不对！";
                }
            }
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
    }

    /****************************************************
     *       用户登出
     /****************************************************/
    public function logout(){
        $result = array();
        $param = $this->request->getJSON();
        $item = array();
        $item["username"] = $param->username;
        try{
            $signModel = new \App\Models\SignModel();
            $userInfo = $signModel->getItemByUsername($param->username);
            $signModel->clearToken($item);
            $result["status"] = SUCESS;
            $result["message"] = "退出登录成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
    }

    /****************************************************
     *       注册用户
     /****************************************************/
     public function register(){
        $result = array();
        $param = $this->request->getJSON();
        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);
        $item = array();
        $item["username"] = $param->username;
        $item["nick"] = $param->nick;
        $item["password"] = $param->password;
        $item["status"] = 0;           //正常用户
        $item["role"] = 2;             //普通用户
        $item["register_time"] = $curTime;
        
        try{
            $signModel = new \App\Models\SignModel();
            $userInfo = $signModel->getItemByUsername($param->username);
            if(count($userInfo) > 0){
                $result["status"] = COMMON_FAIL;
                $result["message"] = "该用户已存在！";
            }else{
                $signModel->register($item);
                $result["status"] = SUCESS;
                $result["message"] = "注册成功！";
            }
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
    }
}