Skip to content

Commit

Permalink
修复逻辑,为指定包转发
Browse files Browse the repository at this point in the history
  • Loading branch information
Joyboo committed Apr 14, 2022
1 parent 7db7059 commit e1027d8
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/Common/Classes/FixBugTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace WonderGame\EsUtility\Common\Classes;

use App\Model\Package;
use EasySwoole\HttpClient\Bean\Response;
use EasySwoole\HttpClient\HttpClient;
use EasySwoole\Http\AbstractInterface\Controller;

/**
* @extends Controller
*/
trait FixBugTrait
{
protected function fixProxyUrl()
{
$servername = config('SERVNAME');;
$pkgbnd = 'com.wonderland.bombtankad';
$gameid = 5;
$input = $this->request()->getRequestParam();
if (explode('-', $servername)[0] !== 'xjp'
&& (isset($input['versioncode']) && intval($input['versioncode']) <= 6)
&& (isset($input['pkgbnd']) && $input['pkgbnd'] === $pkgbnd)
&& (isset($input['gameid']) && $input['gameid'] == $gameid)
) {
return $this->proxyServer($pkgbnd);
}
return true;
}

protected function proxyServer($pkgbnd)
{
$aname = explode('_', APP_NAME)[1];
/** @var Package $Package */
$Package = model('Package');
if ($domain = $Package->cacheInfo(['pkgbnd' => $pkgbnd])['domain'][$aname])
{
$Uri = $this->request()->getUri();
$url = rtrim($domain, '/') . $Uri->getPath();
if ($Uri->getQuery()) {
$url .= '?' . $Uri->getQuery();
}

$headers = $this->request()->getHeaders();
$headerArray = [];
foreach ($headers as $hk => $hd) {
if (is_array($hd)) {
$hd = current($hd);
}
$headerArray[$hk] = $hd;
}

$HttpClient = new HttpClient($url);
$HttpClient->setHeaders($headerArray, true, false);
$method = strtolower($this->request()->getMethod());

/** @var Response $response */
$response = null;
// 不需要body的方法
if (in_array($method, ['get', 'head', 'trace', 'delete']))
{
$response = $HttpClient->$method();
}
elseif (in_array($method, ['post', 'patch', 'put', 'download']))
{
$envKey = 'envkeydata';
$input = $this->request()->getRequestParam();
unset($input[$envKey]);
$rsa = config('RSA');
$openssl = LamOpenssl::getInstance($rsa['private'], $rsa['public']);
$post = [$envKey => $openssl->encrypt(json_encode($input))];
$response = $HttpClient->$method($post);
}

$array = json_decode($response->getBody(), true);
$this->writeJson($array['code'], $array['result'] ?? [], $array['message']);
return false;
}
return false;
}
}

0 comments on commit e1027d8

Please sign in to comment.