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.

154 lines
4.7 KiB
PHP

<?php
namespace Base\Tool\GameResource;
use Base\Tool\Log;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
/**
* 乐游-通用测试资源接口
*/
class LeyouClient
{
const SIGN_NAME = 'sign';
const SUCCESS = '0000';
const KEY = '6c9VLnZFlhEppATKKWeH5vV900K6Nhy5';
protected $client;
// http://chat.leniu.com/api/game/sendGold/zhuimeng/jxlm/57972
private $apis = [
'provide' => ['uri' => '/api/game/sendGold/zhuimeng/jxlm/57972', 'method' => 'post'],
];
public function __construct($game = null)
{
$this->client = new Client([
'base_uri' => 'http://chat.leniu.com',
'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');
Log::error('Leyou ' . $e->getMessage());
return $e->getMessage();
}
}
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();
Log::info('Leyou ' . json_encode($params) . ' ' . $result);
return $result;
}
protected function get($uri, array $params = [])
{
$response = $this->client->get($uri, [
'verify' => false,
'query' => $params,
]);
$result = (string)$response->getBody();
Log::info('Leyou ' . json_encode($params) . ' ' . $result);
return $result;
}
protected function sign($params)
{
unset($params[self::SIGN_NAME]);
ksort($params);
$signRows = [];
foreach ($params as $key => $value) {
$signRows[] = $key . '=' . $value;
}
return md5(implode('&', $signRows) . self::KEY);
}
public function apply($order, $role)
{
$data = [
'role_id' => $role['role_id'],
'server_no' => $role['server_id'],
];
if ($order['ref_id'] < 100) {
// $data['money'] = intval($order['ref_amount']) * 10;
// $data['type'] = 2;
$data['goods_id'] = 9900001;
$data['goods_num'] = intval($order['ref_amount']);
$data['type'] = 1;
} else {
$data['goods_id'] = $order['ref_id'];
$data['goods_num'] = $order['num'];
$data['type'] = 1;
}
$result = $this->api('provide', $data);
if ($result == 'SUCCESS') {
return [
'status' => true,
'message' => $result,
'result' => ['result' => $result]
];
} else {
return [
'status' => false,
'message' => $result,
'result' => ['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)
{
return [
1 => ['ref_id' => 1, 'name' => '6气球', 'amount' => 6],
2 => ['ref_id' => 2, 'name' => '30气球', 'amount' => 30],
3 => ['ref_id' => 3, 'name' => '98气球', 'amount' => 98],
// 4 => ['ref_id' => 4, 'name' => '128气球', 'amount' => 128],
5 => ['ref_id' => 5, 'name' => '198气球', 'amount' => 198],
6 => ['ref_id' => 6, 'name' => '328气球', 'amount' => 328],
7 => ['ref_id' => 7, 'name' => '648气球', 'amount' => 648],
8 => ['ref_id' => 8, 'name' => '1000气球', 'amount' => 1000],
9 => ['ref_id' => 9, 'name' => '2000气球', 'amount' => 2000],
// 10 => ['ref_id' => 10, 'name' => '3000气球', 'amount' => 3000],
11 => ['ref_id' => 11, 'name' => '5000气球', 'amount' => 5000],
// 12 => ['ref_id' => 12, 'name' => '10000气球', 'amount' => 10000],
// 13 => ['ref_id' => 13, 'name' => '20000气球', 'amount' => 20000],
];
}
}