| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 | <?phpnamespace App\Service;use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Redis;class MayCurVouchersServerService extends Service{    protected $param = "";    protected $domain_url = "";    protected $is_formal_vouch = false;    protected $head = null;    public function __construct($data)    {        $param = config("maycur");        if(isset($data['is_voucher']) && ! $data['is_voucher']){ // 测试            $this->param = $param['voucher_cs'] ?? [];            $this->domain_url = $this->param['ip'] ?? "";        }else{            $this->param = $param['voucher_zs'] ?? [];            $this->domain_url = $this->param['ip'] ?? "";            $this->is_formal_vouch = true;        }    }    private function getSecret($appCode, $appSecret, $timeStamp) {        // 拼接字符串并计算 SHA-256 哈希值        $stringToHash = $appSecret . ":" . $appCode . ":" . $timeStamp;        $hashedString = hash('sha256', $stringToHash);        return $hashedString;    }    public function getToken(){        //每刻所有配置        $url_array = $this->param;        //环境        $is_voucher_formal = $this->is_formal_vouch;        $name = $is_voucher_formal ? "每刻凭证正试环境" : "每刻凭证测试环境";        //redis 存储的token        $key = $this->domain_url . $url_array['login_expire_time'];        $token = Redis::get($key);        if(! empty($token)) {            $this->head = json_decode($token,true);            return [true, ''];        }        //获取token的参数        $appcode = $url_array['appkey'];        $appsercet = $url_array['appsecret'];        if(empty($appcode) || empty($appsercet)) return [false, $name . '鉴权认证参数不能为空'];        //组织获取参数        $url = $this->domain_url . $url_array['login'];        $time = microtime(true);        $timeStamp = intval($time * 1000);        //生成secret        $secret = $this->getSecret($appcode, $appsercet, $timeStamp);        $post = [            "secretToken" => $secret,            "userCode" => $appcode,            "timestamp" => $timeStamp        ];        $header = ['Content-Type:application/json'];        list($status, $result) = $this->post_helper($url,$post, $header);        if(! $status) return [$status, $result];        if(isset($result['code']) && $result['code'] != '200') return [false, $result['errMsg'] ?? $name . '鉴权失败,请联系开发者'];        $token_array = [            'Authorization:Bearer ' . $result['data']['token'],        ];        $this->head = $token_array;        Redis::setex($key, $url_array['login_expire_time'], json_encode($token_array));        return [true, ''];    }    public function voucher($data){        if(empty($data['body'])) return [false, '凭证同步入参请放入body内'];        //获取token        list($status, $token) = $this->getToken();        if(! $status) return [false, $token];        $header = $this->head;        //每刻所有配置        $url_array = $this->param;        //组织获取参数        $url = $this->domain_url . $url_array['voucher_insert'];        $post = $data['body'] ?? [];        $header = array_merge(['Content-Type:application/json'], $header);        list($status, $result) = $this->post_helper($url,$post, $header);        if(! $status) return [$status, $result];        if(isset($result['code']) && $result['code'] == 1) return [false, $result['errData'][0]['errorMsg'] ?? ""];        if(isset($result['code']) && $result['code'] != 0 && ! empty($result['errMsg'])) return [false, $result['errMsg']];        return [true, ''];    }    public function post_helper($url, $data, $header = [], $timeout = 20){        Log::channel('apiMcLog')->info('每刻凭证POST', ["api" => $url , "param" => $data ,"header" => $header]);        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_ENCODING, '');        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);        if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));        $r = curl_exec($ch);        if ($r === false) {            // 获取错误号            $errorNumber = curl_errno($ch);            // 获取错误信息            $errorMessage = curl_error($ch);            $message = "cURL Error #{$errorNumber}: {$errorMessage}";            Log::channel('apiMcLog')->info('每刻凭证POST结果', ["message" => $message]);            return [false, $message];        }        curl_close($ch);        $return = json_decode($r, true);        Log::channel('apiMcLog')->info('每刻凭证POST结果', ["message" => $return]);        return [true, $return];    }    public function get_helper($url,$header=[],$timeout = 20){        Log::channel('apiMcLog')->info('每刻凭证GET', ["api" => $url ,"header" => $header]);        $ch = curl_init();        curl_setopt_array($ch, array(            CURLOPT_URL => $url,            CURLOPT_RETURNTRANSFER => true,            CURLOPT_ENCODING => '',            CURLOPT_MAXREDIRS => 10,            CURLOPT_TIMEOUT => $timeout,            CURLOPT_FOLLOWLOCATION => true,            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,            CURLOPT_CUSTOMREQUEST => 'GET',            CURLOPT_SSL_VERIFYPEER => false,            CURLOPT_HTTPHEADER => $header,        ));        $r = curl_exec($ch);        if ($r === false) {            // 获取错误号            $errorNumber = curl_errno($ch);            // 获取错误信息            $errorMessage = curl_error($ch);            $message = "cURL Error #{$errorNumber}: {$errorMessage}";            Log::channel('apiMcLog')->info('每刻凭证GET', ["message" => $message]);            return [false, $message];        }        curl_close($ch);        $return = json_decode($r, true);        Log::channel('apiMcLog')->info('每刻凭证GET', ["message" => $return]);        return [true, $return];    }}
 |