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
PHTML

2 years ago
<?php
namespace Base\Tool\GameResource;
use Base\Tool\PlatformLog;
use GuzzleHttp\Client;
use Think\Log;
2 years ago
abstract class JhBaseClient
2 years ago
{
const SIGN_NAME = 'sign';
const SUCCESS = 1;
2 years ago
protected $baseUri;
2 years ago
protected $key;
protected $brAppId;
protected $brPCode;
protected $client;
2 years ago
protected $apis = [
2 years ago
'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'],
];
2 years ago
public function __construct()
2 years ago
{
$this->client = new Client([
2 years ago
'base_uri' => $this->baseUri,
2 years ago
'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);
2 years ago
$result = json_decode($response, true);
if (empty($result)) {
return [
'code' => -98,
'msg' => '返回数据异常',
];
}
return $result;
2 years ago
} 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 = [])
{
2 years ago
/* $response = $this->client->post($url, [
2 years ago
'verify' => false,
'form_params' => $params,
]);
2 years ago
$result = (string)$response->getBody(); */
$result = json_encode([
'code' => -97,
'msg' => '模拟发放,自定失败',
]);
2 years ago
$this->log('RequestData ' . json_encode($params));
$this->log('ResponseData ' . $result);
return $result;
}
protected function get($url, array $params = [])
{
2 years ago
/* $response = $this->client->get($url, [
2 years ago
'verify' => false,
'query' => $params,
]);
2 years ago
$result = (string)$response->getBody(); */
$result = json_encode([
'code' => -97,
'msg' => '模拟发放,自定失败',
]);
2 years ago
$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)
{
2 years ago
2 years ago
$data = [
'userId' => $role['user_id'],
'roleId' => $role['role_id'],
'serverId' => $role['server_id'],
2 years ago
'orderNum' => $this->generateOrderNumber($order),
'moneyFen' => $order['amount'],
2 years ago
'time' => time(),
];
2 years ago
}
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'],
];
2 years ago
}
2 years ago
$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);
}
2 years ago
2 years ago
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) {
2 years ago
if (intval($result['code']) === self::SUCCESS) {
2 years ago
return [
'status' => true,
'message' => $result['msg'],
'result' => ['result' => $result]
];
} else {
return [
'status' => false,
'message' => $result['msg'],
'result' => ['result' => $result]
];
}
}
2 years ago
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;
}
2 years ago
public function getResourceTypes($deviceType)
{
2 years ago
return [];
2 years ago
}
public function getResources($typeId, $deviceType)
{
2 years ago
return [];
2 years ago
}
2 years ago
protected function log($message, $level = Log::INFO) {
PlatformLog::write($message, 'game_api/jh_base', $level);
2 years ago
}
}