You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.2 KiB
PHP
84 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Base\Tool;
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
/**
|
|
* 游戏猫接口客户端
|
|
*/
|
|
class GameCatClient
|
|
{
|
|
const SIGN_NAME = 'Sign';
|
|
const SUCCESS = '0000';
|
|
|
|
protected $client;
|
|
|
|
private $apis = [
|
|
'get-pay-type' => ['uri' => '/api/pay/GetPayType', 'method' => 'get'],
|
|
'internal-pay' => ['uri' => '/api/pay/InternalPayOrder/yushi', 'method' => 'post'],
|
|
'check-role' => ['uri' => '/api/pay/CheckActorID', 'method' => 'get']
|
|
];
|
|
|
|
public function __construct()
|
|
{
|
|
$this->client = new Client([
|
|
'base_uri' => C('GAME_CAT_URL'),
|
|
'timeout' => 10.0,
|
|
]);
|
|
}
|
|
|
|
public function api($api, array $params = [])
|
|
{
|
|
$api = $this->apis[$api] ?? null;
|
|
if (is_null($api)) {
|
|
throw new \Exception('接口不存在');
|
|
}
|
|
$params[self::SIGN_NAME] = $this->sign($params);
|
|
|
|
try {
|
|
return $this->request($api, $params);
|
|
} catch (\Exception $e) {
|
|
$env = C('APP_ENV', null, 'prod');
|
|
return ['code' => '1000', 'message' => '接口请求错误。' . ($env == 'prod' ? '' : $e->getMessage()) , 'data' => []];
|
|
}
|
|
}
|
|
|
|
public function request($api, $params)
|
|
{
|
|
if ($api['method'] == 'get') {
|
|
return $this->get($api['uri'], $params);
|
|
} else {
|
|
return $this->post($api['uri'], $params);
|
|
}
|
|
}
|
|
|
|
protected function post($uri, array $params = [])
|
|
{
|
|
$response = $this->client->post($uri, [
|
|
'verify' => false,
|
|
'form_params' => $params,
|
|
]);
|
|
$result = (string)$response->getBody();
|
|
return json_decode($result, true);
|
|
}
|
|
|
|
protected function get($uri, array $params = [])
|
|
{
|
|
$response = $this->client->get($uri, [
|
|
'verify' => false,
|
|
'query' => $params,
|
|
]);
|
|
$result = (string)$response->getBody();
|
|
return json_decode($result, true);
|
|
}
|
|
|
|
protected function sign($params)
|
|
{
|
|
unset($params[self::SIGN_NAME]);
|
|
ksort($params);
|
|
$params['key'] = C('GAME_CAT_KEY');
|
|
return md5(http_build_query($params));
|
|
}
|
|
} |