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.

227 lines
6.6 KiB
PHP

<?php
namespace Base\Tool\GameResource;
use Base\Tool\PlatformLog;
use GuzzleHttp\Client;
use Think\Log;
abstract class JhBaseClient
{
const SIGN_NAME = 'sign';
const SUCCESS = 1;
protected $baseUri;
protected $key;
protected $brAppId;
protected $brPCode;
protected $client;
protected $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()
{
$this->client = new Client([
'base_uri' => $this->baseUri,
'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);
$result = json_decode($response, true);
if (empty($result)) {
return [
'code' => -98,
'msg' => '返回数据异常',
];
}
return $result;
} 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(); */
$result = json_encode([
'code' => -97,
'msg' => '模拟发放,自定失败',
]);
$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(); */
$result = json_encode([
'code' => -97,
'msg' => '模拟发放,自定失败',
]);
$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' => $this->generateOrderNumber($order),
'moneyFen' => $order['amount'],
'time' => time(),
];
}
public function sendProps($order)
{
$props = [];
$order['props'] = json_decode($order['props'], true);
foreach ($order['props'] as $prop) {
$props[] = [
'propId' => $prop['ref_id'],
'count' => $prop['num'],
];
}
$data = [
'userId' => $order['user_id'],
'roleId' => $order['role_id'],
'serverId' => $order['server_id'],
'orderNum' => $this->generateOrderNumber($order),
'propData' => $props,
'time' => time(),
];
$result = $this->api('prop', $data);
return $this->getCommonResult($result, 200);
}
public function sendGold($gold, $order)
{
$data = [
'userId' => $order['user_id'],
'roleId' => $order['role_id'],
'serverId' => $order['server_id'],
'orderNum' => $this->generateOrderNumber($order),
'moneyFen' => $gold,
'time' => time(),
];
$result = $this->api('pay', $data);
return $this->getCommonResult($result);
}
public function sendEmail($giftItem, $order)
{
$data = [
'userId' => $order['user_id'],
'roleId' => $order['role_id'],
'serverId' => $order['server_id'],
'orderNum' => $this->generateOrderNumber($order, $giftItem['index']),
'mailData' => [$giftItem['id']],
'time' => time(),
];
$result = $this->api('mail', $data);
return $this->getCommonResult($result);
}
protected function getCommonResult($result) {
if (intval($result['code']) === self::SUCCESS) {
return [
'status' => true,
'message' => $result['msg'],
'result' => ['result' => $result]
];
} else {
return [
'status' => false,
'message' => $result['msg'],
'result' => ['result' => $result]
];
}
}
protected function generateOrderNumber($order, $index = 0)
{
$length = 8 - strlen(strval($order['id']));
$indexLength = 3 - strlen(strval($index));
return date('Ymd') . str_repeat('0', $length) . $order['id'] . str_repeat('0', $indexLength) . $index;
}
public function getResourceTypes($deviceType)
{
return [];
}
public function getResources($typeId, $deviceType)
{
return [];
}
protected function log($message, $level = Log::INFO) {
PlatformLog::write($message, 'game_api/jh_base', $level);
}
}