<?php

use App\Services\LogService;
use Illuminate\Support\Facades\DB;

class Yz{
    public static function echo($data=[],$code=200){
        $result=array();
        $result['code']=$code;
        $result['data']=$data;
        return response()->json($result)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }
    public static function echoError($msg){
        $result=array();
        $result['status']='no';
        $result['msg']=$msg;
        return response()->json($result)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }
    public static function echoError1($msg){
        $result=array();
        $result['status']=false;
        $result['msg']=$msg;
        return response()->json($result)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }

    public static function ReturnNew($status,$msg,$data=null){
        $result=array();
        $result['status']=$status;
        $result['msg']=$msg;
        $result['data']=$data;
        return $result;
    }

    public static function Return($status,$msg,$data=[]){
        $result=array();
        $result['status']=$status;
        $result['msg']=$msg;
        $result['data']=$data;
        return $result;
    }

    public static function XmlHttp($SendData,$url){


        $xml_post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://com.wondersgroup.jkda.application.modules.webservice">
   <soapenv:Header/>
   <soapenv:Body>'
            .$SendData.
            '</soapenv:Body>
</soapenv:Envelope>';

        $headers = array(
            "Content-type: text/xml;charset=utf-8",
            "Accept: text/xml",
            "Cache-Control: no-cache",
            "Pragma: no-cache",
            "Content-length: ".strlen($xml_post_string),
            "appCode: glMzZT",
            "sign: E98BB73DE7356B1641BCA0D025D7CD11",
            "apiCode: 9051ecd6-dd16-4cdb-9a38-ca159114febd",
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);



        $resdata = curl_exec($ch);
        if(!$resdata==""){
            $result="";
            try {
                if(curl_errno($ch)) {
                    $result=curl_error($ch);
                    // print curl_error($ch);
                } else {
                    curl_close($ch);

                    $res = explode('<soap:Body>', $resdata)[1];
                    $res = explode('</soap:Body>', $res)[0];

                    $xmlObject = simplexml_load_string($res);
                    $jsonData = json_encode($xmlObject);
                    $result = json_decode($jsonData, true);
                }
            }catch (Exception $e) {
                $result = $e->getMessage();
            }
        }else{
            $result=$resdata;
        }

        LogService::CheckTableName();
        date_default_timezone_set('PRC');
        $table_name='zz_request_log_' . date('ym');
        $formatted_date= date("Y-m-d H:i:s");
        DB::table($table_name)->insert([
            'request_ip'=>'',
            'response_data'=>json_encode($result, JSON_UNESCAPED_UNICODE),
            'header_data'=>'',
            'post_data'=>$SendData,
            'get_data'=>'',
            'request_url'=>$url,
            'create_time' => $formatted_date,
            'update_time' => $formatted_date

        ]);

        return $result;
    }

}

