<?php

if (!function_exists('dd')) {
    function dd($obj)
    {
        halt($obj);
    }
}

if (!function_exists('fail')) {
    /**
     * 主动抛错
     */
    function fail($message, $data = [], $code = 0)
    {
        $result = [
            'code' => $code,
            'data' => $data,
            'msg'  => is_array($message) ? json_encode($message) : $message,
        ];

        request_log_update($result);
        // 如果未设置类型则自动判断
        $type     = 'json';
        $response = \think\Response::create($result, $type, 200);

        throw new \think\exception\HttpResponseException($response);
    }
}

if (!function_exists('succ')) {
    /**
     * 成功返回
     */
    function succ($data = [], $message = '')
    {
        $result = [
            'code' => 1,
            'data' => $data,
            'msg'  => $message,
        ];

        request_log_update($result);
        // 如果未设置类型则自动判断
        $type     = 'json';
        $response = \think\Response::create($result, $type, 200);

        throw new \think\exception\HttpResponseException($response);
    }
}

if (!function_exists('getConfig')) {
    /**
     * 获取配置
     * @param string  $field   配置组名
     * @param string  $key     字段
     * @param string  $default 字段默认值
     * @param boolean $refresh 是否刷新缓存
     * @return mixed
     */
    function getConfig(string $field, $key = '', $default = '', $refresh = true)
    {
        $config = \think\Cache::get($field);
        if (!$config || $refresh) {
            $config = \think\Db::name('exam_config_info')->order('id')->limit(1)->value($field);
            if (!$config) {
                return null;
            }

            $config = json_decode($config, true);
            //存入缓存
            \think\Cache::set($field, $config);
        }

        if ($key) {
            return $config[$key] ?? $default;
        }

        return $config;
    }
}

if (!function_exists('getCurl')) {
    /**
     * get请求
     * @param $url
     * @return bool|string
     */
    function getCurl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }
}

if (!function_exists('postCurl')) {
    /**
     * post请求
     * @param        $url
     * @param string $data
     * @param string $type
     * @return bool|string
     */
    function postCurl($url, $data = '', $type = 'json')
    {
        if ($type == 'json') {
            $data   = json_encode($data); //对数组进行json编码
            $header = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        $res = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Error+' . curl_error($curl);
        }
        curl_close($curl);
        return $res;
    }
}

if (!function_exists('only_keys')) {
    /**
     * 只取数组部分key数据
     * @param array $array
     * @param array $keys
     * @return array
     */
    function only_keys(array $array, array $keys)
    {
        $result = [];
        foreach ($array as $k => $value) {
            if (in_array($k, $keys)) {
                $result[$k] = $value;
            }
        }
        return $result;
    }
}

if (!function_exists('hidden_keys')) {
    /**
     * 隐藏数组部分key数据
     * @param array $array
     * @param array $keys
     * @return array
     */
    function hidden_keys(array $array, array $keys)
    {
        $result = [];
        foreach ($array as $k => $value) {
            if (in_array($k, $keys)) {
                unset($value[$k]);
                $result[$k] = $value;
            }
        }
        return $result;
    }
}

if (!function_exists('hidden_list_keys')) {
    /**
     * 隐藏数组部分key数据
     * @param array $list
     * @param array $keys
     * @return array
     */
    function hidden_list_keys(array $list, array $keys)
    {
        $list   = collection($list)->toArray();
        $result = [];
        foreach ($list as $i => $item) {
            foreach ($item as $k => $value) {
                if (in_array($k, $keys)) {
                    unset($item[$k]);
                }
            }
            $result[$i] = $item;
        }
        return $result;
    }
}

if (!function_exists('is_empty_in_array')) {
    /**
     * 数组内是否包含且存在字段值
     * @param $array
     * @param $field
     * @return bool
     */
    function is_empty_in_array($array, $field)
    {
        if (!isset($array[$field]) || !$array[$field]) {
            return true;
        }

        return false;
    }
}

if (!function_exists('cache_data')) {
    /**
     * 获取/设置缓存数据
     * @param string  $cache_key   缓存key名
     * @param Closure $fun         用户函数，获取并返回数据
     * @param int     $expire_time 缓存过期时间
     * @return mixed
     */
    function cache_data(string $cache_key, Closure $fun, int $expire_time = 0, bool $refresh = false)
    {
        // 固定前缀
        $cache_key = "exam:{$cache_key}";

        // 存在缓存，返回缓存
        if (!$refresh && $cache = cache($cache_key)) {
            return $cache;
        }

        // 执行数据获取
        $data = $fun();
        $data = is_array($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;

        // 设置缓存
        cache($cache_key, $data, $expire_time);
        return $data;
    }
}

if (!function_exists('getUser')) {
    /**
     * 获取Api用户信息
     * @return mixed
     */
    function getUser()
    {
        if (\app\common\library\Auth::instance()->isLogin()) {
            return \app\common\library\Auth::instance();
        }

        return null;
    }
}

if (!function_exists('getUserId')) {
    /**
     * 获取Api用户ID
     * @return mixed
     */
    function getUserId()
    {
        if ($user = getUser()) {
            return $user->id;
        }

        return 0;
    }
}

if (!function_exists('str_trim')) {
    /**
     * 字符串去除空格
     * @return string
     */
    function str_trim($str)
    {
        return str_replace(' ', '', $str);
    }
}

if (!function_exists('generate_no')) {
    /**
     * 根据时间生成编号
     * @return string
     */
    function generate_no($pre = '')
    {
        $date         = date('YmdHis', time());
        $u_timestamp  = microtime(true);
        $timestamp    = floor($u_timestamp);
        $milliseconds = round(($u_timestamp - $timestamp) * 100); // 改这里的数值控制毫秒位数
        return $pre . $date . date(preg_replace('`(?<!\\\\)u`', $milliseconds, 'u'), $timestamp);
    }
}

function request_log_update($log_result){

    /*if ($this->logType === 1){
        if (strlen(json_encode($log_result['data'])) > 1000) {
            $log_result['data'] = '数据太多，不记录';
        }
        LogUtil::info('result', 'Api-Middleware-Log', 'request_log', $log_result);
    }else{*/
        if(defined('API_REQUEST_ID')) { //记录app正常返回结果
            if(strlen(json_encode($log_result['data'])) > 1000) {
                $log_result['data'] = '数据太多，不记录';
            }
            db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($log_result)]);
        }
    /*}*/
}