<?php
namespace Helpers;

use Api\PhpUtils\Log\FileLog;
use Api\PhpUtils\Log\Tracer;
use Api\PhpUtils\Http\HttpUtil;
use Api\PhpUtils\Http\Request;
use App\Exception\BaseException;

/**
 * Sdk 获取接口
 */
class Sdk {

    /**
     * @param $current_amount
     * @param $total_amount
     * @param $cleared_amount
     * @param $total_tip
     * @param $cleared_tip
     * 计算微信手续费公式，计算单位是分
     */
    public static function call($url, $params, $headers = []) {

        /**/ // X-TRACE-ID、X-REQUEST-ID
        $header = [
            //HTTP-X-TRACE-ID
            'X-TRACE-ID'=>Tracer::getTraceId(),
        ];

        $headers = array_merge($headers, $header);
        //$data = HttpUtil::post($url, $params, 2000, 1, $headers);
        $data  = (new Request())->post($url, $params,2000, 'json', $headers);

        /*
        [
            "http_code"=> 200,
            "code"=> 0,
            "response"=> [
                "code"=> 102002,
                "status"=> "failed",
                "reason"=> "暂未开通钱包"
            ]
        ]
        */

        FileLog::info('sdk api:', json_encode(['url'=>$url, 'req'=>$params, 'res'=>$data]));

        if(!empty($data['response']['code'])) {
            throw new BaseException(['msg'=>$data['response']['reason']??'', 'code'=>$data['response']['code']]);
        }

        return $data['response'];
    }
}