thinkphp3.2 后台登陆、退出

public function login () {
    if (IS_POST) {
        $role_phone = I('username');//获取用户输入的账号
        $role_password = strtolower(I('password'));//获取用户输入的密码

        empty($role_phone) && $this->jsonResponse(false, '手机号不能为空');//判断用户是否输入账号
        empty($role_password) && $this->jsonResponse(false, '密码不能为空');//判断用户是否输入密码

        $map = [];
        $map['role_phone'] = $role_phone;
        $map['role_password'] = $role_password;

        $result = M('role_user')->where($map)->find();//通过用户输入的账号密码,进行查询
        if (!$result) {//判断数据库里面是否有用户输入的账号密码
            $this->jsonResponse(false, '账号或密码有误, 请重新输入');
        }
        else {
            $result['status'] == 1 && $this->jsonResponse(false, '账号已禁用, 请联系管理员');

            $result['last_login_ip'] = $_SERVER["HTTP_X_REAL_IP"];//用户登陆的ip地址
            $result['last_login_time'] = date('Y-n-j H:i:s', $_SERVER['REQUEST_TIME']);//用户登陆的时间
            $res = M('role_user')->where('user_id = ' . $result['user_id'])->save($result);//将用户的ipi地址和登陆时间更新到表中

            if ($res) {//判断用户的信息是否入库,如果入库则保存在redis中
                $key = 'admin_' . $result['user_id'] . '_' . sha1($result['role_phone']);
                $value = [];
                $value['user_id'] = $result['user_id'];
                $value['user_name'] = $result['role_phone'];

                $role = M('role')->field('is_admin, name, role_name, status')->where('id = ' . $result['role_id'])->find();
                $role['status'] == 1 && $this->jsonResponse(false, '该部门已禁用, 请联系管理员');

                $value['is_admin'] = $role['is_admin'];
                $value['role'] = $role['role_name'];
                $value['role_name'] = $role['name'];

                $this->redis_set($key, $value, 60 * 60);
            }

            $this->jsonResponse(true, '登录成功', ['U-Admin-Token' => $key]);
        }
    }
    else {
        $this->jsonResponse(false, '无效请求');
    }
}

/*
 * 退出功能
 */
public function logout () {
    $user_info = $this->user_info;
    $res = M('role_user')->where('user_id = ' . $user_info['user_id'])->save(['last_logout_time' => date('Y-n-j H:i:s', $_SERVER['REQUEST_TIME'])]);
    if ($res) {
        $this->redis_del($_SERVER['HTTP_U_ADMIN_TOKEN']);
        $this->user_info = null;
    }

    $this->jsonResponse(true, '退出成功');
}

/*
 * 获取用户信息
 */
public function get_user_info () {

    $user_info = $this->user_info;
    if ($user_info) {
        $user = M('user')->field('wx_headimage')->where('id = ' . $user_info['user_id'])->find();
        $user_info['headimage'] = $user['wx_headimage'];

        $this->jsonResponse(true, 'success', ['user_info' => $user_info]);
    }

}
/*
 * 将数据存放在reids中
 * */
public function redis_set ($key, $value, $expires = 60 * 60 * 24 * 7)
{
    $redis = new \Redis();
    $redis->connect(C('REDIS_HOST'), C('REDIS_PORT'));
    $data = json_encode($value, JSON_UNESCAPED_UNICODE);
    $redis->set($key, $data);
    if ($expires > 0)
        $redis->expire($key, $expires);
}


/*
 * 将存放在redis中的数据删除
 * */
public function redis_del ($key)
{
    $redis = new \Redis();
    $redis->connect(C('REDIS_HOST'), C('REDIS_PORT'));
    $redis->del($key);
}


/*
 * 将数据以json格式返回
 * */
public function jsonResponse ($success = true, $message = '', $data = null, $code = 0)
{
    $response = [];

    $response['success'] = $success;

    if (!$message || empty($message)) {
        $response['message'] = '成功';
    }
    else {
        $response['message'] = $message;
    }

    if ($success) {
        $response['data'] = $data;
    }
    else {
        $code == 0 && $code = 50000;
    }

    $response['code'] = $code;

    die(json_encode($response, JSON_UNESCAPED_UNICODE));
}

你可能感兴趣的:(php)