<?php
// 应用公共文件


/**
 * 随机码
 * @param type $length
 * @return string
 */
function randString($length, $c = false) {
    if ($c) {
        $_codeSet = '0123456789';
    } else {
        $_codeSet = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
    }
    $code = '';
    for ($i = 0; $i < $length; $i++) {
        $code.= $_codeSet[mt_rand(0, strlen($_codeSet) - 1)];
    }
    return $code;
}

/**
 * openssl aes 加密
 * @param $data
 * @param $key
 * @param int $options
 * @return string
 */
function crypto_encrypt($data, $key, $options = \OPENSSL_RAW_DATA) {
    $key = md5($key);
    $iv = substr($key, 0, -16);
    $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, $options, $iv);
    return $encrypted;
}

/**
 * openssl aes 解密
 * @param $data
 * @param $key
 * @param int $options
 * @return string
 */
function crypto_decrypt($data, $key, $options = \OPENSSL_RAW_DATA) {
    $key = md5($key);
    $iv = substr($key, 0, -16);
    return openssl_decrypt($data, 'aes-256-cbc', $key, $options, $iv);
}




/**
 * 验证手机号是否正确
 * @author honfei
 * @param number $mobile
 */
function isMobile($mobile) {
    if (!is_numeric($mobile)) {
        return false;
    }
    return preg_match('/^1[23456789]{1}\d{9}$/', $mobile) ? true : false;
}

/**
 * 设置网址参数
 * @param $url
 */
function setParam($url,$parm){
    $url = explode('?',$url);
    $web = $url[0];
    $parmAr = [];
    $parmStr = count($url) == 2 ? $url[1] : "" ;
    if(!empty($parmStr)) {
        parse_str($parmStr,$parmAr);
    }
    foreach ($parm as $k=>$v){
        $parmAr[$k] = $v;
    }
    $parmStr2 = http_build_query($parmAr);
    return $web .( empty($parmStr2) ? "" : ("?" . $parmStr2));
}


/**
 * 几天未消费
 * @param $time
 */
function getLastTime($time) {
    $nowTime = time();
    $day = 3600 * 24;

    if(empty($time)) {
        return '从无消费';
    }

    if($nowTime - $time < $day) {
        return '今天有消费';
    }
    $lDay = intval(($nowTime - $time) / $day);

    return ($lDay + 1).'天未消费';
}

/**
 * 分级排序
 * @param $data
 * @param int $pid
 * @param string $field
 * @param string $pk
 * @param string $html
 * @param int $level
 * @param bool $clear
 * @return array
 */
function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
{
    static $list = [];
    if ($clear) $list = [];
    foreach ($data as $k => $res) {
        if ($res[$field] == $pid) {
            $res['html'] = str_repeat($html, $level);
            $list[] = $res;
            unset($data[$k]);
            sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
        }
    }
    return $list;
}

/**
 * 上传路径转化,默认路径
 * @param $path
 * @param int $type
 * @param bool $force
 * @return string
 */
function make_path($path, int $type = 2, bool $force = false)
{
    $path = DS . ltrim(rtrim($path));
    switch ($type) {
        case 1:
            $path .= DS . date('Y');
            break;
        case 2:
            $path .= DS . date('Y') . DS . date('m');
            break;
        case 3:
            $path .= DS . date('Y') . DS . date('m') . DS . date('d');
            break;
    }
    try {
        if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
            return trim(str_replace(DS, '/', $path), '.');
        } else return '';
    } catch (\Exception $e) {
        if ($force)
            throw new \Exception($e->getMessage());
        return '无法创建文件夹，请检查您的上传目录权限：' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
    }

}

/**
 * 格式化属性
 * @param $arr
 * @return array
 */
function attr_format($arr)
{
    $data = [];
    $res = [];
    $count = count($arr);
    if ($count > 1) {
        for ($i = 0; $i < $count - 1; $i++) {
            if ($i == 0) $data = $arr[$i]['detail'];
            //替代变量1
            $rep1 = [];
            foreach ($data as $v) {
                foreach ($arr[$i + 1]['detail'] as $g) {
                    //替代变量2
                    $rep2 = ($i != 0 ? '' : $arr[$i]['value'] . '_$_') . $v . '-$-' . $arr[$i + 1]['value'] . '_$_' . $g;
                    $tmp[] = $rep2;
                    if ($i == $count - 2) {
                        foreach (explode('-$-', $rep2) as $k => $h) {
                            //替代变量3
                            $rep3 = explode('_$_', $h);
                            //替代变量4
                            $rep4['detail'][$rep3[0]] = isset($rep3[1]) ? $rep3[1] : '';
                        }
                        if($count == count($rep4['detail']))
                            $res[] = $rep4;
                    }
                }
            }
            $data = isset($tmp) ? $tmp : [];
        }
    } else {
        $dataArr = [];
        foreach ($arr as $k => $v) {
            foreach ($v['detail'] as $kk => $vv) {
                $dataArr[$kk] = $v['value'] . '_' . $vv;
                $res[$kk]['detail'][$v['value']] = $vv;
            }
        }
        $data[] = implode('-', $dataArr);
    }
    return [$data, $res];
}
