master
elf 1 year ago
parent f3e4890dfb
commit 0f50dc7627

@ -1,6 +1,7 @@
<?php <?php
namespace Base\Service; namespace Base\Service;
use Base\Tool\GameResource\JmxyClient;
use Base\Tool\GameResource\ZzylClient; use Base\Tool\GameResource\ZzylClient;
use Think\Model; use Think\Model;
@ -115,6 +116,9 @@ class GameRebateService
case 11: case 11:
$client = new ZzylClient(); $client = new ZzylClient();
break; break;
case 11:
$client = new JmxyClient();
break;
default: default:
throw new \Exception('客户端未实现'); throw new \Exception('客户端未实现');
break; break;

@ -0,0 +1,189 @@
<?php
namespace Base\Tool\GameResource;
use Base\Tool\PlatformLog;
use Exception;
use GuzzleHttp\Client;
use Think\Log;
class JmxyClient
{
const SIGN_NAME = 'sign';
const SUCCESS = 0;
protected $baseUri = 'http://cl3-uc01gh.huanyuantech.com';
protected $key = 'a3f579b5add2342a7ac07017b7650c45';
protected $client;
protected $apis = [
'mail' => ['url' => '/acf/send_gift.php', 'method' => 'post'],
];
public function __construct()
{
$this->client = new Client([
'base_uri' => $this->baseUri,
'timeout' => 10.0,
]);
}
public function api($apiName, array $params = [])
{
$api = $this->apis[$apiName] ?? null;
if (is_null($api)) {
throw new \Exception('接口不存在');
}
$params[self::SIGN_NAME] = $this->sign($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) {
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($params)
{
unset($params[self::SIGN_NAME]);
ksort($params);
$signString = '';
foreach ($params as $key => $value) {
$signString .= $value;
}
$signString .= $this->key;
$this->log('SignString ' . $signString);
return md5($signString);
}
public function apply($order, $role)
{
throw new Exception('接口未实现');
}
public function sendProps($order)
{
}
public function sendGold($gold, $order)
{
}
public function sendEmail($giftItem, $order)
{
$data = [
'appId' => 'chengfeng',
'userId' => $order['user_id'],
'orderNum' => $this->generateOrderNumber($order, $giftItem['index']),
'title' => '测试邮件',
'content' => '测试礼包发送',
'packages' => json_encode([
[
'packageId' => $giftItem['id'],
'count' => 1,
],
]),
'serverId' => $order['server_id'],
'roleId' => $order['role_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/jmxy', $level);
}
}
Loading…
Cancel
Save