<?php
function appExec($module, $class, $action)
{
    S('MODULE', $module);
    S("CONTROLLER", $class);
    S("ACTION", $action);
    S('URL_PATH', "/{$module}/{$class}/{$action}");
    $ctrl = \App::make("\\App\\Http\\Controllers\\" . ucfirst($module) . "\\" . ucfirst($class) . "Controller");
    return \App::call([$ctrl, $action]);
}
function toJson($status, $msg = '', $data = '')
{
    $return = array(); //返回数组
    //组装状态码和返回数据
    if ($status < 0) {
        $return['status'] = $status;
        $return['data'] = $data;
    } else {
        if (!is_numeric($status)) {
            $return['data'] = $status;
            $return['status'] = 1;
        } else {
            $return['status'] = $status;
            $return['data'] = $data;
        }
    }
    if(empty($return['data'])){
        $return['data']=[];
    }
    //组装返回信息
    if (empty($msg)) {
        $return['msg'] = trans('error.' . $return['status']);
    } else {
        $return['msg'] = $msg;
    }
    //返回json数据
    return response()->json($return);
}

function getOrderId($prefix = '')
{
    $num = mt_rand(100, 999);
    list($s, $m) = explode(' ', microtime());
    $order = date("YmdHis") . ($s * 1000000) . $num;
    return $prefix . $order;
}
//identity生产
function getUUID()
{
    $prefix = '';
    $uuid = '';
    $str = md5(uniqid(mt_rand(), true));
    $uuid = substr($str, 0, 8) . '-';
    $uuid .= substr($str, 8, 4) . '-';
    $uuid .= substr($str, 12, 4) . '-';
    $uuid .= substr($str, 16, 4) . '-';
    $uuid .= substr($str, 20, 12);
    return $prefix . $uuid;
}


function S($key, $value = '')
{
    if (!isset($GLOBALS[$key]) && empty($value)) {
        return -1;
    }
    if (isset($GLOBALS[$key]) && empty($value)) {
        return $GLOBALS[$key];
    }
    return $GLOBALS[$key] = $value;
}

/**
 *密码加密码
 */
function randcode($num=6)
{
    srand((double) microtime() * 1000000); //create a random number feed.
    $ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
    $list = explode(",", $ychar);
    $authnum = "";
    for ($i = 0; $i < $num; $i++) {
        $randnum = rand(0, 61); // 10+26;
        $authnum .= $list[$randnum];
    }
    return $authnum;
}

function createPasswd($passwd,$solt){
    return md5(md5($solt.$passwd));
}

/**
 * 获取客户端真实IP
 */
function GETIP() {
    global $ip;

    if (getenv("HTTP_CLIENT_IP")) {
        $ip = getenv("HTTP_CLIENT_IP");
    } else if (getenv("HTTP_X_FORWARDED_FOR")) {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    } else if (getenv("REMOTE_ADDR")) {
        $ip = getenv("REMOTE_ADDR");
    } else {
        $ip = "Unknow";
    }

    return $ip;

}

//创建目录
function  createDir($path){
    if  (is_dir($path)) { return true ; }
    return  mkdir($path,0777,true);
}
