<?php
namespace app\service;

use think\Exception;

use app\common\lib\Sign;


class ApiService {
    private static $instance = null;

    private $httpClient;

    private function __construct() {
	    // 初始化 GuzzleHttp\Client 實例
        $this->httpClient = new \GuzzleHttp\Client([
            'base_uri' => env('utel.sso_base_url').'/api/v1/'
        ]);
    }

    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new ApiService();
        }
        return self::$instance;
    }

    public function callApi($url, $method = 'GET', $params = []) {
        $params['appid'] = 'sc';
        $params['timestamp'] = time();

        $params['sign'] = Sign::genSign($params);

        $options = ['query' => $params];
        $response = $this->httpClient->request($method, $url, $options);
        if($response->getStatusCode()!=200){
            //throw 異常;
            throw new Exception('get sso user info error!!!');
	}
	$sso_data = json_decode($response->getBody()->getContents(),true);
        return $sso_data;
    }
}
