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.

158 lines
4.6 KiB
PHTML

4 years ago
<?php
4 years ago
namespace Base\Tool\GameResource;
4 years ago
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
/**
4 years ago
* 远征手游之楚汉争霸-测试资源接口
4 years ago
*/
4 years ago
class YzchzbClient
4 years ago
{
4 years ago
const SIGN_NAME = 'sign';
4 years ago
const SUCCESS = '0000';
protected $client;
private $apis = [
4 years ago
'get-pay-type' => ['uri' => '/game/support/items/v1', 'method' => 'post'],
4 years ago
'provide' => ['uri' => '/game/support/provide/v1', 'method' => 'post'],
4 years ago
];
private $appIds = [
'andriod' => 1746,
'ios' => 1747,
];
private $channelIds = [
'andriod' => 11595,
'ios' => 11596,
4 years ago
];
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('接口不存在');
}
4 years ago
$deviceType = 'andriod';
if (isset($params['device_type'])) {
$deviceType = $params['device_type'];
unset($params['device_type']);
}
$params['appId'] = $this->appIds[$deviceType] ?? $this->appIds['andriod'];
$params['channelId'] = $this->channelIds[$deviceType] ?? $this->channelIds['andriod'];
4 years ago
$params['timestamp'] = time();
4 years ago
$params[self::SIGN_NAME] = $this->sign($params);
try {
return $this->request($api, $params);
} catch (\Exception $e) {
$env = C('APP_ENV', null, 'prod');
4 years ago
return ['code' => 1000, 'state' => 1000, 'message' => '接口请求错误。' . ($env == 'prod' ? '' : $e->getMessage()) , 'data' => []];
4 years ago
}
}
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');
4 years ago
$signRows = [];
foreach ($params as $key => $value) {
$signRows[] = $key . '=' . $value;
}
// var_dump(implode('&', $signRows));
return md5(implode('&', $signRows));
4 years ago
}
4 years ago
public function apply($order, $role)
{
$result = $this->api('provide', [
'roleId' => $role['role_id'],
'amount' => intval($order['ref_amount']),
'supportItem' => $order['ref_id'],
'supportType' => '0',
'channelUid' => $role['user_account'],
'applyRemark' => $order['remark'] == '' ? '测试资源申请' : $order['remark'],
'applyId' => $order['order_no'],
'device_type' => $role['sdk_version'] == 1 ? 'andriod' : 'ios',
]);
if ($result['state'] == 1 && $result['data']) {
return [
'status' => true,
'message' => $result['msg'],
'result' => $result
];
} else {
return [
'status' => false,
'message' => $result['msg'],
'result' => $result ?? []
];
}
}
public function getResourceTypes($deviceType)
{
if ($deviceType == 'andriod') {
return ['id' => 1, 'name' => '通用', 'device_type' => 'andriod'];
} elseif ($deviceType == 'ios') {
return ['id' => 2, 'name' => '通用', 'device_type' => 'ios'];
}
}
public function getResources($typeId, $deviceType)
{
$resources = [];
$result = $this->api('get-pay-type', ['device_type' => $deviceType]);
if ($result['state'] == 1) {
$items = $result['data'];
foreach ($items as $item) {
$resources[$item['supportItem']] = [
'ref_id' => $item['supportItem'],
'name' => $item['content'],
'amount' => $item['amount'],
];
}
}
return $resources;
}
4 years ago
}