<?php


namespace app\common\model;


use app\common\ErrorCode;

class User extends Base
{
    public static $fieldStr = "id,login_name,salt,valid,last_login_time,login_count,token";

    /**
     * 检测用户是否存在
     * @param $login_name
     * @return array
     */
    public static function checkRegUser($login_name)
    {
        //检测账号是否被注册
        $where['login_name'] = $login_name;
        $exist = self::where($where)->find();
        if ($exist) {
            //用户存在
            return self::standardOutput(9999, "用户已存在");
        }
        return self::standardOutput(ErrorCode::CODE_SUCC);
    }

    /**
     * 用户注册
     * @param $login_name
     * @param $password
     * @return array
     */
    public static function regUser($login_name, $password)
    {
        //注册用户
        $res = self::checkRegUser($login_name);
        if ($res["code"] != 0) {
            return $res;
        }
        //创建用户
        $salt = randNum(4);//生成随机盐
        $user = new User([
            "login_name" => $login_name,
            "salt" => $salt,
            "password" => md5($salt . $password),
            "ext1" => $salt . $password
        ]);
        $res = $user->save();

        if ($res === false) {
            return self::standardOutput(ErrorCode::CODE_DB_ERROR, ErrorCode::getError(ErrorCode::CODE_DB_ERROR));
        }
        return self::standardOutput(ErrorCode::CODE_SUCC);
    }

    /**
     * 用户登录
     * @param $login_name
     * @param $password
     */
    public static function login($login_name, $password)
    {
        $whereUser = [];
        $whereUser["login_name"] = $login_name;
        $user = self::where($whereUser)->find();
        if (!$user) {
            return self::standardOutput(9999, "用户不存在");
        }
        $passwordMd5 = md5($user->salt . $password);
        if ($passwordMd5 != $user->password) {
            return self::standardOutput(9999, "用户密码不正确");
        }
        //更新用户登录 信息
        $user->last_login_time = getNow();
        $user->login_count = $user->login_count + 1;
        $user->token = $user->getToken();
        $user->hidden(["salt","password"]);
        $res = $user->save();
        if ($res === false) {
            //数据库读写失败
            return self::standardOutput(ErrorCode::CODE_DB_ERROR, ErrorCode::getError(ErrorCode::CODE_DB_ERROR));
        }
        return self::standardOutput(ErrorCode::CODE_SUCC, "登录成功", $user);
    }

    /**
     * 获取token
     * @return string
     */
    public function getToken()
    {
        $expireDays = 7;//过期时间，单位天
        //token:  md5([用户名][当前时间])|[用户id]|[过期时间]
        $token = base64_encode(md5($this->login_name . getNow()) . "|" . $this->id . "|" . (time() + 86400 * $expireDays));
        return $token;
    }

    /**
     * 修改密码
     */
    public function changePassword($orgPassword, $newPassword)
    {
        //校验旧密码
        $passwordMd5 = md5($this->salt . $orgPassword);
        if ($passwordMd5 != $this->password) {
            $str=$this->salt . $orgPassword."---".$passwordMd5."---".$this->password;
            return self::standardOutput(9999, "原密码不正确".$str);
        }
        $this->password = md5($this->salt . $newPassword);
        $res = $this->save();
        if ($res === false) {
            //数据库读写失败
            return self::standardOutput(ErrorCode::CODE_DB_ERROR, ErrorCode::getError(ErrorCode::CODE_DB_ERROR));
        }
        return self::standardOutput(ErrorCode::CODE_SUCC);
    }

    /**
     * 退出登录
     */
    public function loginOut()
    {
        $this->token = "";
        $res = $this->save();
        if ($res === false) {
            //数据库读写失败
            return self::standardOutput(ErrorCode::CODE_DB_ERROR, ErrorCode::getError(ErrorCode::CODE_DB_ERROR));
        }
        return self::standardOutput(ErrorCode::CODE_SUCC);
    }

}