<?php

// app/Libraries/CommonHelper.php
namespace App\Libraries;

use Dcat\Admin\Admin;
use JsonRPC\Client;
use Illuminate\Http\Request;
use JsonRPC\HttpClient;

class RpcClient
{
    // RPC超时时间
    public static $timeout = 5;

    /*
     * RPC客户端
     * @param $procedure string 远程过程调用名称
     * @param $params array 远程过程调用参数
     */
    public static function albumExecute ($procedure, $params = []) {
        $apiKey =  time();
        $apiSecret = env('ALBUM_RPC_SECRET');
        $payload = json_encode($params);
        $payload = $payload.$apiKey;
        $signature = hash_hmac('sha256', $payload, $apiSecret);
        $clientHost = env('ALBUM_RPC_URL');
        $httpClient = new HttpClient($clientHost);
        $httpClient = $httpClient->withTimeout(self::$timeout);
        $client = new Client($clientHost,false,$httpClient);
        try {
            $result = $client->execute( $procedure, $params,[], null,[
                    'X-API-Key: '.$apiKey,
                    'X-API-Signature: '.$signature,
            ]);
            return $result;
        } catch (\Exception $e) {
            return ['status'=>false,'msg'=>$e->getMessage(),'data'=>[]];
        }
    }

}

