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.
247 lines
7.2 KiB
PHP
247 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace Base\Tool\GameResource;
|
|
|
|
use Base\Tool\Log;
|
|
use Exception;
|
|
use GuzzleHttp\Client;
|
|
use Think\Log as ThinkLog;
|
|
|
|
/**
|
|
* 御剑灵域-测试资源接口
|
|
*/
|
|
class YjlyClient
|
|
{
|
|
const APP_ID = 'A86C68D2BD587CEC6';
|
|
const SIGN_NAME = 'sign';
|
|
const SIGN_KEY = 'G5iPD9gce19PtlxDh';
|
|
|
|
protected $client;
|
|
protected $baseUrl = 'https://x2centerapi.xiaoxixinxi.com';
|
|
|
|
private $apis = [
|
|
'send-email' => ['uri' => '/9vjh/player_mail', 'method' => 'get', 'with' => [
|
|
]],
|
|
];
|
|
|
|
public function __construct($game = null)
|
|
{
|
|
$this->client = new Client([
|
|
'base_uri' => $this->baseUrl,
|
|
'timeout' => 10.0,
|
|
]);
|
|
}
|
|
|
|
public function api($api, array $params = [])
|
|
{
|
|
$tmpApi = $api;
|
|
$api = $this->apis[$api] ?? null;
|
|
if (is_null($api)) {
|
|
throw new \Exception('接口不存在');
|
|
}
|
|
|
|
$params['appId'] = self::APP_ID;
|
|
$params[self::SIGN_NAME] = $this->sign($tmpApi, $params);
|
|
if (is_array($api['with'])) {
|
|
$params = array_merge($params, $api['with']);
|
|
}
|
|
|
|
try {
|
|
return $this->request($api, $params);
|
|
} catch (\Exception $e) {
|
|
$env = C('APP_ENV', null, 'prod');
|
|
$this->log($e->getMessage(), ThinkLog::ERR);
|
|
return ['status' => 0, 'msg' => '接口请求错误。' . ($env == 'prod' ? '' : $e->getMessage()) , 'data' => []];
|
|
}
|
|
}
|
|
|
|
public function request($api, $params)
|
|
{
|
|
$this->log('params: ' . json_encode($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();
|
|
$this->log('response: ' . $result);
|
|
return json_decode($result, true);
|
|
}
|
|
|
|
protected function get($uri, array $params = [])
|
|
{
|
|
$response = $this->client->get($uri, [
|
|
'verify' => false,
|
|
'query' => $params,
|
|
]);
|
|
$result = (string)$response->getBody();
|
|
$this->log('response: ' . $result);
|
|
return json_decode($result, true);
|
|
}
|
|
|
|
protected function sign($name, $params)
|
|
{
|
|
$apiBizFieldMap = [
|
|
'send-email' => 'mailData',
|
|
'send-props' => 'propData',
|
|
'send-gold' => 'moneyFen',
|
|
];
|
|
$signFields = ['appId', 'serverId', 'roleId', 'userId', $apiBizFieldMap[$name], 'orderNum', 'time'];
|
|
$signArray = [];
|
|
foreach ($signFields as $field) {
|
|
$data = $params[$field];
|
|
if (is_array($data)) {
|
|
$data = json_encode($data);
|
|
}
|
|
$signArray[] = $data;
|
|
}
|
|
$signString = implode('', $signArray) . self::SIGN_KEY;
|
|
$this->log('SignString ' . $signString);
|
|
return md5($signString);
|
|
|
|
$signString = '';
|
|
$signString .= $params['appId'];
|
|
$signString .= $params['serverId'];
|
|
$signString .= $params['roleId'];
|
|
$signString .= $params['userId'];
|
|
if ($name == 'send-gold') {
|
|
$signString .= $params['moneyFen'];
|
|
} elseif ($name == 'send-email') {
|
|
$signString .= $params['mailData'];
|
|
} elseif ($name == 'send-props') {
|
|
$signString .= $params['propData'];
|
|
}
|
|
$signString .= $params['orderNum'];
|
|
$signString .= $params['time'];
|
|
$signString .= self::SIGN_KEY;
|
|
$this->log('signString: ' . $signString);
|
|
return md5($signString);
|
|
}
|
|
|
|
// 测试资源
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function apply($order, $role)
|
|
{
|
|
throw new Exception('没有接口');
|
|
}
|
|
|
|
//
|
|
public function sendGold($order, $role)
|
|
{
|
|
$result = $this->api('send-gold', [
|
|
'roleId' => $role['role_id'],
|
|
'moneyFen' => intval($order['ref_amount'] * 100), // TODO
|
|
'serverId' => $role['server_id'],
|
|
'userId' => $role['user_id'],
|
|
'orderNum' => $order['order_no']
|
|
]);
|
|
if (intval($result['code']) == 0) {
|
|
return [
|
|
'status' => true,
|
|
'message' => $result['msg'],
|
|
'result' => $result
|
|
];
|
|
} else {
|
|
return [
|
|
'status' => false,
|
|
'message' => $result['msg'],
|
|
'result' => $result ?? []
|
|
];
|
|
}
|
|
}
|
|
|
|
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'],
|
|
];
|
|
}
|
|
$result = $this->api('send-prop', [
|
|
'roleId' => $order['role_id'],
|
|
'propData' => $props,
|
|
'serverId' => $order['server_id'],
|
|
'userId' => $order['user_id'],
|
|
'orderNum' => $this->generateOrderNumber($order),
|
|
'time' => time(),
|
|
]);
|
|
|
|
if (intval($result['code']) == 0) {
|
|
return [
|
|
'status' => true,
|
|
'message' => $result['msg'],
|
|
'result' => $result
|
|
];
|
|
} else {
|
|
return [
|
|
'status' => false,
|
|
'message' => $result['msg'],
|
|
'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 sendEmail($giftItem, $order)
|
|
{
|
|
$result = $this->api('send-email', [
|
|
'serverId' => $order['server_id'],
|
|
'roleId' => $order['role_id'],
|
|
'userId' => $order['user_id'],
|
|
'mailData' => json_encode([['mailId' => $giftItem['id']]]),
|
|
'orderNum' => date("YmdHis").$order['id'].$order['user_id'].$giftItem['id'].rand(1000, 9999),
|
|
'time' => time(),
|
|
]);
|
|
if (intval($result['code']) === 0) {
|
|
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)
|
|
{
|
|
return [];
|
|
}
|
|
|
|
protected function log($message, $level = ThinkLog::INFO) {
|
|
Log::write($message, $level, 'game_api/yjly');
|
|
}
|
|
} |