<?php
// 应用公共文件
function jump($msg = '', $url = null, $wait = 3)
{
    if (is_null($url)) {
        $url = 'javascript:history.back(-1);';
    } else {
        $url = "location.href = '" . url($url) . "'";
    }

    $result = [
        'msg'  => $msg,
        'url'  => $url,
        'wait' => $wait,
    ];

    $html = view('/public/jump', $result);
    throw new \think\exception\HttpResponseException($html);
}

function ajax_success($data)
{
    $res      = ['code' => 0, 'msg' => '成功', 'data' => $data];
    $response = \think\Response::create($res, 'json');
    throw new \think\exception\HttpResponseException($response);
}

function get_user()
{
    $sessionUserId = session('home.user.id');
    if (empty($sessionUserId)) {
        if (request()->isAjax()) {
            $res      = ['code' => 401, 'msg' => '请登录'];
            $response = \think\Response::create($res, 'json');
            throw new \think\exception\HttpResponseException($response);
        } else {
            session('back_url',request()->url());
            $response = redirect('/home/login/login');
            throw new \think\exception\HttpResponseException($response);
        }
    }

    $user = \app\common\model\UserModel::where('id',$sessionUserId)->hidden(['password','salt'])->find();
    if (empty($user)) {
        jump('该用户已删除');
    }
    if ($user['status'] != \app\common\model\UserModel::STATUS_PASS) {
        jump('该账号暂时无法使用');
    }

    return $user;
}

function get_user_id()
{
    $sessionUserId = session('home.user.id');
    if (empty($sessionUserId)) {
        return 0;
    }

    return $sessionUserId;
}