master
parent
ba40f0e747
commit
5e54a94e47
@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Tool\GameResource;
|
||||
|
||||
use Base\Tool\PlatformLog;
|
||||
use GuzzleHttp\Client;
|
||||
use Think\Log;
|
||||
|
||||
/**
|
||||
* 天之禁2-测试资源接口
|
||||
*/
|
||||
class TzjClient
|
||||
{
|
||||
const SIGN_NAME = 'sign';
|
||||
const SUCCESS = 1;
|
||||
|
||||
protected $key;
|
||||
protected $brAppId;
|
||||
protected $brPCode;
|
||||
|
||||
protected $client;
|
||||
|
||||
private $apis = [
|
||||
'mail' => ['url' => '/wf/m/mail/brRom/1/brPCode/{brPCode}/brAppId/{brAppId}', 'method' => 'post'],
|
||||
'prop' => ['url' => '/wf/m/prop/brRom/1/brPCode/{brPCode}/brAppId/{brAppId}', 'method' => 'post'],
|
||||
'pay' => ['url' => '/wf/m/pay/brRom/1/brPCode/{brPCode}/brAppId/{brAppId}', 'method' => 'post'],
|
||||
];
|
||||
|
||||
public function __construct($game = null)
|
||||
{
|
||||
$this->client = new Client([
|
||||
'base_uri' => 'http://club.game.267zf.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($api, $params);
|
||||
try {
|
||||
$response = $this->request($api, $params);
|
||||
return json_decode($response, true);
|
||||
} catch (\Exception $e) {
|
||||
$env = C('APP_ENV', null, 'prod');
|
||||
$this->log('RequestError ' . $e->getMessage(), Log::ERR);
|
||||
return [
|
||||
'code' => -99,
|
||||
'msg' => '网络异常',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
protected function buildUrl($url) {
|
||||
$url = str_replace('{brPCode}', $this->brPCode, $url);
|
||||
$url = str_replace('{brAppId}', $this->brAppId, $url);
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function request($api, $params)
|
||||
{
|
||||
if ($api['method'] == 'get') {
|
||||
return $this->get($this->buildUrl($api['url']), $params);
|
||||
} else {
|
||||
return $this->post($this->buildUrl($api['url']), $params);
|
||||
}
|
||||
}
|
||||
|
||||
protected function post($url, array $params = [])
|
||||
{
|
||||
$response = $this->client->post($url, [
|
||||
'verify' => false,
|
||||
'form_params' => $params,
|
||||
]);
|
||||
$result = (string)$response->getBody();
|
||||
$this->log('RequestData ' . json_encode($params));
|
||||
$this->log('ResponseData ' . $result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function get($url, array $params = [])
|
||||
{
|
||||
$response = $this->client->get($url, [
|
||||
'verify' => false,
|
||||
'query' => $params,
|
||||
]);
|
||||
$result = (string)$response->getBody();
|
||||
$this->log('RequestData ' . json_encode($params));
|
||||
$this->log('ResponseData ' . $result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function sign($api, $params)
|
||||
{
|
||||
$apiBizFieldMap = [
|
||||
'mail' => 'mailData',
|
||||
'prop' => 'propData',
|
||||
'pay' => 'moneyFen',
|
||||
];
|
||||
$signFields = ['userId', 'serverId', 'roleId', $apiBizFieldMap[$api], 'orderNum', 'time'];
|
||||
$signArray = [];
|
||||
foreach ($signFields as $field) {
|
||||
$signArray[] = $params[$field];
|
||||
}
|
||||
$signString = implode('', $signArray) . $this->key;
|
||||
$this->log('SignString ' . $signString);
|
||||
return md5($signString);
|
||||
}
|
||||
|
||||
public function apply($order, $role)
|
||||
{
|
||||
$data = [
|
||||
'userId' => $role['user_id'],
|
||||
'roleId' => $role['role_id'],
|
||||
'serverId' => $role['server_id'],
|
||||
'orderNum' => $order['order_no'],
|
||||
'time' => time(),
|
||||
];
|
||||
|
||||
$refId = $order['ref_id'];
|
||||
$api = '';
|
||||
if ($api == 'mail') {
|
||||
$data['mailData'] = $refId;
|
||||
} elseif ($api == 'prop') {
|
||||
$data['propData'] = $refId;
|
||||
} elseif ($api == 'pay') {
|
||||
$data['moneyFen'] = intval($order['ref_amount'] * 100);
|
||||
}
|
||||
|
||||
$result = $this->api($api, $data);
|
||||
if ($result == self::SUCCESS) {
|
||||
return [
|
||||
'status' => true,
|
||||
'message' => $result['msg'],
|
||||
'result' => ['result' => $result]
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => $result['msg'],
|
||||
'result' => ['result' => $result]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function getResourceTypes($deviceType)
|
||||
{
|
||||
if ($deviceType == 'andriod') {
|
||||
return [
|
||||
['id' => 1, 'name' => '邮件', 'device_type' => 'andriod'],
|
||||
['id' => 2, 'name' => '道具', 'device_type' => 'andriod'],
|
||||
['id' => 3, 'name' => '元宝', 'device_type' => 'andriod'],
|
||||
];
|
||||
} elseif ($deviceType == 'ios') {
|
||||
return [
|
||||
['id' => 1, 'name' => '邮件', 'device_type' => 'andriod'],
|
||||
['id' => 2, 'name' => '道具', 'device_type' => 'andriod'],
|
||||
['id' => 3, 'name' => '元宝', 'device_type' => 'andriod'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function getResources($typeId, $deviceType)
|
||||
{
|
||||
if ($typeId == 3) {
|
||||
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],
|
||||
];
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public function log($message, $level = Log::INFO) {
|
||||
PlatformLog::write($message, 'game_api/tzj', $level);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue