<?php
namespace App\Lib;
class XTSign
{
    const  Appid="APP_7B3F36A14E99410A80B37AEF332E3247";
    const Key="DLwiH46Esb8ccNTkuSSVAadNTWUfW0sc";
    const BaseUrl="https://newcoss-dev.isignet.cn:10201";
    const ApiList=[
        'register'=>self::BaseUrl."/coss/service/v1/addUser", //注册
        'getAuthCode'=>self::BaseUrl."/coss/service/v1/getAuthCode", //产生激活码
        'addSignJob'=>self::BaseUrl."/coss/service/v1/addSignJob", //添加签名任务
        'verifySignData'=>self::BaseUrl."/coss/service/v1/verifySignData", //验签接口 3.4.9.3
        'getCertInfo'=>self::BaseUrl."/coss/service/v1/getCertInfo", //解析证书信息
    ];

    //系统签名接口调用
    public static function XTRequest($url,$data){
        if(!isset(self::ApiList[$url])) return \Yz::echoError1("接口不存在");
        $url= self::ApiList[$url];
        $baseData=[
            "version"=>"1.0",
            "appId"=>self::Appid,
            "signAlgo"=>"HMAC",
        ];

        $data=array_merge($baseData, $data);

        $sign=self::Sign($data);
        $data= array_merge($data,['signature'=>$sign]);
       // dd($data);
        $data=json_encode($data);
        $encryptStr=self::post($url,$data);
        $r_data=json_decode($encryptStr, true);
        if($r_data['status']==200){
            return \Yz::Return(true,$r_data['message'],$r_data['data']);
        }else{
            return \Yz::echoError1($r_data['message']);
        }


    }
    //产生激活码
//    public static function getAuthCode(){
//        $url= self::$baseUrl."/coss/service/v1/getAuthCode";
//        $data=[
//            "version"=>"1.0",
//            "appId"=>self::$appid,
//            "signAlgo"=>"HMAC",
//            "userId"=>"915b164cd8e883f7fb289e3bcf34ac68d971c7e1058f18bcb24ad33ec7a201e1",
//        ];
//        $sign=self::Sign($data);
//        $data= array_merge($data,['signature'=>$sign]);
//        // dd($data);
//        $data=json_encode($data);
//        $encryptStr=self::post($url,$data);
//        $r_data=json_decode($encryptStr, true);
//
//        return $r_data;
//    }
    public static function Sign($parameters){
        // 定义 M 集合内的参数值
//        $parameters = array(
//            "key1" => "value1",
//            "key2" => "value2",
//            // ...
//        );

// 按照参数名的 ASCII 码从小到大排序
        ksort($parameters);

// 拼接参数为 URL 键值对字符串
        $stringA = "";
        foreach ($parameters as $key => $value) {
            if ($value === "") {
                // 参数值为空，进行相应的处理
                // 比如跳过该参数或者抛出异常
                continue;
            }
            $stringA .= $key . "=" . $value . "&";
        }
        $stringA = rtrim($stringA, "&");

// HMAC 运算
        $secretKey =self::Key;
        $signature = base64_encode(hash_hmac("sha256", $stringA, $secretKey, true));
        return $signature;

    }

    public function post($url, $data_string)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($data_string)
        ]);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
        $r = curl_exec($curl);
        curl_close($curl);
        return $r;
    }
}
