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

// 应用公共文件
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 ajax_return($code = 0, $msg = '', $data = [])
{
    $res      = ['code' => $code, 'msg' => $msg, 'data' => $data];
    $response = \think\Response::create($res, 'json');
    throw new \think\exception\HttpResponseException($response);
}

function get_user_id()
{
    $sessionUserId = session('mobile.user.id');
    if (empty($sessionUserId)) {
        session('back_url', request()->url());
        $response = redirect('/mobile/login/login');
        throw new \think\exception\HttpResponseException($response);
    }

    return $sessionUserId;
}

function get_user()
{
    $sessionUserId = session('mobile.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('/mobile/login/login');
            throw new \think\exception\HttpResponseException($response);
        }
    }

    $user = \app\common\model\User::where('id', $sessionUserId)->find();
    if (empty($user)) {
        jump('该用户已删除');
    }

    return $user;
}

function get_broker()
{
    $broker_id = session('mobile.broker.id');
    if (empty($broker_id)) {
        $user   = get_user();
        $broker = \app\common\model\Broker::where('userid', $user['id'])->find();
        if (empty($broker)) {
            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('/mobile/login/login');
                throw new \think\exception\HttpResponseException($response);
            }
        }

        if ($broker['type'] == 1) {
            jump('仅限省外经纪人登录');
        }
        session('mobile.broker.id', $broker_id);
    } else {
        $broker = \app\common\model\Broker::where('id', $broker_id)->find();
    }

    return $broker;
}