master
parent
54eb67efd7
commit
fa55a9690a
@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace Base\Tool\GameResource;
|
||||
|
||||
use Base\Tool\Log;
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
/**
|
||||
* 梦幻仙灵-测试资源/返利接口
|
||||
*/
|
||||
class MhxlClient
|
||||
{
|
||||
const SIGN_NAME = 'sign';
|
||||
const SIGN_KEY = 'oYoecgaatFN1Wnpsnobww3M2tNxLLps3';
|
||||
|
||||
protected $client;
|
||||
protected $baseUrl = 'https://hmlprwm-center.hnmaiji.com:9443';
|
||||
|
||||
private $apis = [
|
||||
'send-email' => ['uri' => '/hhsy/center/api.php?r=senditem', 'method' => 'post'],
|
||||
];
|
||||
|
||||
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[self::SIGN_NAME] = $this->sign($tmpApi, $params);
|
||||
|
||||
try {
|
||||
return $this->request($api, $params);
|
||||
} catch (\Exception $e) {
|
||||
$env = C('APP_ENV', null, 'prod');
|
||||
Log::error('Mhxl ' . $e->getMessage());
|
||||
return ['code' => 0, 'msg' => '接口请求错误。' . ($env == 'prod' ? '' : $e->getMessage()) , 'data' => []];
|
||||
}
|
||||
}
|
||||
|
||||
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('Mhxl ' . json_encode($params) . ' ' . $result);
|
||||
return $this->prepareResult($result);
|
||||
}
|
||||
|
||||
protected function get($uri, array $params = [])
|
||||
{
|
||||
$response = $this->client->get($uri, [
|
||||
'verify' => false,
|
||||
'query' => $params,
|
||||
]);
|
||||
$result = (string)$response->getBody();
|
||||
Log::info('Mhxl ' . json_encode($params) . ' ' . $result);
|
||||
return $this->prepareResult($result);
|
||||
}
|
||||
|
||||
protected function prepareResult($result)
|
||||
{
|
||||
if ($result == 'SUCCESS') {
|
||||
return [
|
||||
'code' => 0,
|
||||
'msg' => $result,
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'code' => 1,
|
||||
'msg' => $result,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
protected function sign($name, $params)
|
||||
{
|
||||
return md5(
|
||||
$params['appId'] .
|
||||
$params['orderNum'] .
|
||||
$params['userId'] .
|
||||
$params['mailId'] .
|
||||
$params['serviceId'] .
|
||||
$params['roleId'] .
|
||||
$params['time'] .
|
||||
static::SIGN_KEY
|
||||
);
|
||||
}
|
||||
|
||||
// 测试资源
|
||||
public function apply($order, $role)
|
||||
{
|
||||
throw new Exception('接口未实现!');
|
||||
}
|
||||
|
||||
//
|
||||
public function sendGold($order, $role)
|
||||
{
|
||||
throw new Exception('接口未实现!');
|
||||
}
|
||||
|
||||
public function sendProps($order)
|
||||
{
|
||||
throw new Exception('接口未实现!');
|
||||
}
|
||||
|
||||
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', [
|
||||
'appId' => 'wmtx',
|
||||
'serviceId' => $order['server_id'],
|
||||
'roleId' => $order['role_id'],
|
||||
'userId' => $order['user_id'],
|
||||
'mailId' => $giftItem['id'],
|
||||
'orderNum' => $this->generateOrderNumber($order, $giftItem['index'])
|
||||
]);
|
||||
return $this->getCommonResult($result);
|
||||
}
|
||||
|
||||
protected function getCommonResult($result, $successCode = 0)
|
||||
{
|
||||
if ($result['code'] == $successCode) {
|
||||
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)
|
||||
{
|
||||
$resources = [];
|
||||
return $resources;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue