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.

171 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Helper\Client;
use App\Exception\BusinessException;
use App\Helper\Log;
use GuzzleHttp\Client;
use Hyperf\Guzzle\CoroutineHandler;
use GuzzleHttp\HandlerStack;
class Lanhuo
{
const SUCCESS = 0;
private static $accessToken;
private static $expiredAt;
private static $client;
public static function addUser($data)
{
$params = [
'user_id' => $data['user_id'],
'account' => $data['account'],
'phone' => $data['phone'],
'device_type' => $data['device_type'], // 设备类型(1-andriod 2-ios)
'device_number' => $data['device_number'],
'name' => $data['name'],
'id_card' => $data['id_card'],
'is_real' => $data['is_real'], // 是否实名(1-实名 0-未实名)
];
return self::api('/llwf/user/add', $params);
}
public static function updateUser($data)
{
$params = [
'user_id' => $data['user_id'],
'phone' => $data['phone'],
'device_type' => $data['device_type'], // 设备类型(1-andriod 2-ios)
'device_number' => $data['device_number'],
'name' => $data['name'],
'id_card' => $data['id_card'],
'is_real' => $data['is_real'], // 是否实名(1-实名 0-未实名)
];
return self::api('/llwf/user/update', $params);
}
public static function addLoginLog($data)
{
$params = [
'user_id' => $data['user_id'],
'account' => $data['account'],
'phone' => $data['phone'],
'device_type' => $data['device_type'], // 设备类型(1-andriod 2-ios)
'device_number' => $data['device_number'],
];
return self::api('/llwf/user/AddLoginLog', $params);
}
public static function addOrder($data)
{
$params = [
'order_code' => $data['pay_order_number'],
'user_id' => $data['user_id'],
'account' => $data['account'],
'service_id' => $data['service_id'],
'service_name' => $data['service_name'],
'role_id' => $data['role_id'],
'role_name' => $data['role_name'],
'game_name' => $data['game_name'],
'order_amount' => $data['order_amount'],
'pay_time' => $data['pay_time'],
'status' => $data['status'], //支付状态(待支付 0 已支付 1 支付失败 2)
];
return self::api('/llwf/order/add', $params);
}
public static function addRole($data)
{
$params = [
'user_id' => $data['user_id'],
'service_id' => $data['service_id'],
'service_name' => $data['service_name'],
'role_id' => $data['role_id'],
'role_name' => $data['role_name'],
'role_level' => $data['role_level'],
'profession_id' => '', // 职业ID
'profession_name' => '', // 职业名称
'power' => '', // 战力
];
return self::api('/llwf//role/add', $params);
}
private static function auth()
{
$params = [
'appId' => config('clients.lanhuo.app_id', ''),
'appKey' => config('clients.lanhuo.app_key', ''),
'appSecret' => config('clients.lanhuo.app_secret', ''),
];
return self::api('/auth', $params, false);
}
private static function api($uri, array $params, $withAccessToken = true)
{
$headers = [];
if ($withAccessToken) {
if (!self::$accessToken || time() > self::$expiredAt) {
self::refreshAccessToken();
}
$headers['accessToken'] = self::$accessToken;
}
$result = self::post($uri, $params, $headers);
if ($result['errorCode'] == self::SUCCESS) {
return $result['data'];
} else {
Log::error('LANHUO_REQUEST_ERROR[' . $uri . ']: ' . $result['msg'], $params, 'lanhuo');
throw new BusinessException('请求接口错误:' . $result['msg']);
}
}
private static function refreshAccessToken()
{
$data = self::auth();
self::$accessToken = $data['accessToken'];
self::$expiredAt = strtotime($data['createDt']) + $data['expireTime'] - 10; // 前置10秒
}
private static function post($uri, array $params, array $headers = []): array
{
try {
$response = self::getClient()->post('/openapi' . $uri, [
'verify' => false,
// 'form_params' => $params,
'json' => $params,
'headers' => $headers,
]);
$result = (string)$response->getBody();
return json_decode($result, true);
} catch (\Exception $e) {
Log::error('LANHUO_NET_ERROR[' . $uri . ']: ' . $e->getMessage(), $params, 'lanhuo');
return [
'errorCode' => 88888,
'msg' => env('APP_ENV') == 'prod' ? '网络异常' : '网络异常:' . $e->getMessage(),
'data' => [],
];
}
}
private static function getClient()
{
if (is_null(self::$client)) {
self::$client = new Client([
'base_uri' => config('clients.lanhuo.base_url', ''),
'handler' => HandlerStack::create(new CoroutineHandler()),
'timeout' => 5,
'swoole' => [
'timeout' => 10,
'socket_buffer_size' => 1024 * 1024 * 2,
],
'headers' => [
'Content-Type' => 'application/json',
],
]);
}
return self::$client;
}
}