<?php
/**
 * Here is your custom functions.
 */

if (!function_exists('http_post')) {
    function http_post($url, $params)
    {
        $ch = curl_init();
        $this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}

if (!function_exists('http_post_json')) {
    function http_post_json($url, $params)
    {
        $ch = curl_init();
        $this_header = array('Content-Type: application/json;charset=UTF-8');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}

if (!function_exists('month_12')) {
    function month_12()
    {
        $months = [];
        for ($i = 0; $i < 12; $i++) {
            $months[$i] = date('Y/m', strtotime(date('Y-m-01') . "-" . $i . "month"));
        }

        return $months;
    }
}


/**
 * @Desc 管理员操作日志
 * @Author Gorden
 * @Date 2024/3/29 17:07
 *
 * adminId = 1001 充值
 *
 *
 * @param $name
 * @param $operation
 * @return void
 */

if (!function_exists('_syslog')) {
    function _syslog($name, $operation, $operationData = false, $requestParams = false, $adminId = false)
    {
        $logAdminId = $adminId ? $adminId : \Tinywan\Jwt\JwtToken::getCurrentId();
        $model = new \app\model\SysLog();
        $model->log_admin_id = $logAdminId;
        $model->log_name = $name;
        $model->log_route = \request() && \request()->uri() ? \request()->uri() : (\request() && \request()->route && \request()->route->getPath() ? \request()->route->getPath() : '');
        $model->log_operation = $operation;
        $model->log_ip = \request() && \request()->getRealIp() ? \request()->getRealIp() : '0.0.0.0';
        $model->log_request_params = $requestParams ? json_encode($requestParams) : json_encode(\request()->all());
        $model->log_operation_data = $operationData ? json_encode($operationData) : null;
        $model->save();
    }
}

if (!function_exists('json_throw')) {
    function json_throw($code, $message, $data = '', $options = JSON_UNESCAPED_UNICODE)
    {
        $return = [
            'code' => $code,
            'message' => $message,
            'data' => $data,
        ];

        return new \support\Response(200, ['Content-Type' => 'application/json'], json_encode($return, $options));
    }
}