<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


function pre($content, $is_die = true)
{
    header('Content-type: text/html; charset=utf-8');
    echo '<pre>' . print_r($content, true);
    $is_die && die();
}

/**
 * 异常处理函数
 * @param string $code
 * @param string $msg
 */
function json_error_exception($code = '', $msg = '')
{
    $error = config('errorCode.');
    $errorCode = isset($error[$code]) ? $code : 10000;
    $errorMsg = $msg != '' ? (isset($error[$code]) ? $error[$code] . " :" . $msg : $msg) : (isset($error[$code]) ? $error[$code] : "请求错误");
    return array('code' => $errorCode, 'msg' => $errorMsg);
}

/**
 * 成功Json返回函数
 * @param string $code
 * @param array  $data
 * @param string $msg
 */
function json_return($code,$data=[],$msg='')
{
    exit(json_encode(['code'=>$code,'data'=>$data,'msg'=>$msg],JSON_UNESCAPED_UNICODE ));
}

/**
 * php发送http请求
 * @param array $params 相关请求参数
 * @param booble $is_json  数据格式，默认false为数组，true为json
 * @return string
 */
function _curl(array $params,$is_json=false )
{
    $_data = $params['data'] ?? null;
    $url = $params['url'];
    $defaults = [
        CURLOPT_HEADER => 0,
        CURLOPT_URL => $params['url'],
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_TIMEOUT => 60,
        CURLOPT_SSL_VERIFYHOST=>2,
        CURLOPT_SSL_VERIFYPEER=>0,
    ];
    switch ($params['method'])
    {
        case 'post':
            $defaults[CURLOPT_CUSTOMREQUEST] = "POST";
            break;
        case 'get':
            $defaults[CURLOPT_CUSTOMREQUEST] = "GET";
            break;
        case 'put':
            $defaults[CURLOPT_CUSTOMREQUEST] = "PUT";
            break;
        case 'delete':
            $defaults[CURLOPT_CUSTOMREQUEST] = "DELETE";
            break;
    }
    if ($_data && $params['method']!='get') {
        if($is_json){
            $defaults[CURLOPT_POSTFIELDS] = json_encode($_data);
            $defaults[CURLOPT_HTTPHEADER] = [
                'Content-Type: application/json',
                'Content-Length: ' . strlen(json_encode($_data))
            ];
        }else{
            $defaults[CURLOPT_POSTFIELDS] = $_data;
        }
    }elseif($params['method']=='get'){
        if(is_array($_data)){
            $param = '';
            foreach ($_data as $k=>$v){
                $param .= $k."=".$v."&";
            }
            $param = rtrim($param ,"&");
            if(!strpos($url,"?")){
                $url .= "?".$param;
            }else{
                $url .= "&".$param;
            }
        }
        $defaults[CURLOPT_URL] = $url;
    }


    $ch = curl_init();
    curl_setopt_array($ch, ($defaults));

    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}

/**
 * 整理菜单住方法
 * @param $param
 * @return array
 */
function prepareMenu($param)
{
    $param = objToArray($param);
    $parent = []; //父类
    $child = [];  //子类

    foreach($param as $key=>$vo){

        if(0 == $vo['type_id']){
            $vo['href'] = '#';
            $parent[] = $vo;
        }else{
            $vo['href'] = url($vo['control_name'] .'/'. $vo['action_name']); //跳转地址
            $child[] = $vo;
        }
    }

    foreach($parent as $key=>$vo){
        foreach($child as $k=>$v){

            if($v['type_id'] == $vo['id']){
                $parent[$key]['child'][] = $v;
            }
        }
    }
    unset($child);

    return $parent;
}

/**
 * 统一返回信息
 * @param $code
 * @param $data
 * @param $msge
 */
function msg($code, $data, $msg)
{
    return compact('code', 'data', 'msg');
}

/**
 * 整理出tree数据 ---  layui tree
 * @param $pInfo
 * @param $spread
 */
function getTree($pInfo, $spread = true)
{

    $res = [];
    $tree = [];
    //整理数组
    foreach($pInfo as $key=>$vo){

        if($spread){
            $vo['spread'] = true;  //默认展开
        }
        $res[$vo['id']] = $vo;
        $res[$vo['id']]['children'] = [];
    }
    unset($pInfo);

    //查找子孙
    foreach($res as $key=>$vo){
        if(0 != $vo['pid']){
            $res[$vo['pid']]['children'][] = &$res[$key];
        }
    }

    //过滤杂质
    foreach( $res as $key=>$vo ){
        if(0 == $vo['pid']){
            $tree[] = $vo;
        }
    }
    unset( $res );

    return $tree;
}

/**
 * 对象转换成数组
 * @param $obj
 */
function objToArray($obj)
{
    return json_decode(json_encode($obj), true);
}


/**
 * 权限检测
 * @param $rule
 */
function authCheck($rule)
{
    $control = explode('/', $rule)['0'];
    if(in_array($control, ['login', 'index'])){
        return true;
    }

    if(in_array($rule, cache(session('role_id')))){
        return true;
    }

    return false;
}