Commit 330c149e authored by tufengqi's avatar tufengqi

Merge branch 'TRADE-169' into 'master'

Resolve TRADE-169 Closes TRADE-169 See merge request !3
parents 699aded9 58ecc596
......@@ -6,6 +6,7 @@ class BaseConstant
{
const ERROR = 'error';
const MSG = 'msg';
const MESSAGE = 'message';
const CODE = 'code';
const VAL = 'val';
const DATA = 'data';
......
......@@ -16,6 +16,15 @@ class ResponseMsg
public function __construct()
{
$this->header_list = self::$default_header_list;
$this->fzmCrossHeader();
}
public function fzmCrossHeader()
{
$this->header('Access-Control-Allow-Origin', '*');
$this->header('Access-Control-Allow-Methods', 'POST,GET');
$this->header('Access-Control-Allow-Credentials', 'true');
$this->header('Access-Control-Allow-Headers', 'Authorization,FZM-REQUEST-OS,FZM-USER-IP,FZM-REQUEST-UUID,Content-Type,Content-Length');
}
public static function setDefaultHeader($default_header_list)
......@@ -30,14 +39,14 @@ class ResponseMsg
return self::$default_header_list;
}
public function arrSuccess($data = 'ok', $code = 200)
public function arrSuccess($data = BaseConstant::OK, $code = 200)
{
return [BaseConstant::ERROR => false, BaseConstant::MSG => $data, BaseConstant::CODE => $code];
return [BaseConstant::ERROR => false, BaseConstant::MESSAGE => $data, BaseConstant::CODE => $code];
}
public function arrFail($data, $code = -1)
{
return [BaseConstant::ERROR => true, BaseConstant::MSG => $data, BaseConstant::CODE => $code];
return [BaseConstant::ERROR => true, BaseConstant::MESSAGE => $data, BaseConstant::CODE => $code];
}
/**
......@@ -52,8 +61,8 @@ class ResponseMsg
$msg = 'unknown error';
}
$view = [
'code' => $code,
'msg' => $msg,
BaseConstant::CODE => $code,
BaseConstant::MESSAGE => $msg,
];
$json = json_encode($view);
return $this->dumpJsonData($json);
......@@ -69,7 +78,7 @@ class ResponseMsg
{
$view = [
BaseConstant::CODE => $code,
BaseConstant::MSG => BaseConstant::OK,
BaseConstant::MESSAGE => BaseConstant::OK,
BaseConstant::DATA => $data
];
$json = json_encode($view);
......@@ -82,10 +91,10 @@ class ResponseMsg
*/
public function dealRet($ret)
{
if (true === $ret['error']) {
$this->jsonError($ret['msg'] ? : 'unknown error');
if (true === $ret[BaseConstant::ERROR]) {
$this->jsonError($ret[BaseConstant::MESSAGE] ? : 'unknown error');
} else {
$this->jsonSuccess($ret['msg'] ? : BaseConstant::OK);
$this->jsonSuccess($ret[BaseConstant::MESSAGE] ? : BaseConstant::OK);
}
}
......@@ -166,10 +175,10 @@ class ResponseMsg
public function printOldFail($code, $code_msg, $detail_code, $detail_msg, $callback_key = '')
{
self::header('Access-Control-Allow-Origin', '*');
self::header('Access-Control-Allow-Methods', 'POST,GET');
self::header('Access-Control-Allow-Credentials', 'true');
self::header('Access-Control-Allow-Headers', 'Authorization,FZM-REQUEST-OS,FZM-USER-IP,FZM-REQUEST-UUID,Content-Type,Content-Length');
$this->header('Access-Control-Allow-Origin', '*');
$this->header('Access-Control-Allow-Methods', 'POST,GET');
$this->header('Access-Control-Allow-Credentials', 'true');
$this->header('Access-Control-Allow-Headers', 'Authorization,FZM-REQUEST-OS,FZM-USER-IP,FZM-REQUEST-UUID,Content-Type,Content-Length');
$callback = '';
if ($callback_key) {
$callback = $_GET[$callback_key] ?? '';
......
......@@ -9,8 +9,10 @@ use fpf\response\BaseConstant;
class ThriftServiceFactoryProxy
{
private $instance = null;
private $transport = null;
private $socket = null;
private $request_base_params = null;
private $tem_class_name = '';
private $class_name = '';
private $service_real_config;
const BINARY = 0;
const JSON = 1;
......@@ -18,6 +20,7 @@ class ThriftServiceFactoryProxy
{
$service_default_protocol = Yii::$app->fpf->getConfig('service_default_protocol', 'thrift_service');
$service_default_hosts = Yii::$app->fpf->getConfig('service_default_hosts', 'thrift_service');
$service_default_failure_policy = Yii::$app->fpf->getConfig('service_default_failure_policy', 'thrift_service');
$service_module_hosts = Yii::$app->fpf->getConfig('service_module_hosts', 'thrift_service');
$check_ret = $this->checkService($class_name);
if (true === $check_ret[BaseConstant::ERROR]) {
......@@ -26,6 +29,8 @@ class ThriftServiceFactoryProxy
$service_check_arr = $check_ret[BaseConstant::MSG];
$service_name_space_str = $service_check_arr[0];
$service_str = $service_check_arr[1];
$this->tem_class_name = '\\service\\' . $service_name_space_str . '\\' . $service_str;
$this->class_name = $class_name;
if (empty($service_module_hosts[$service_name_space_str . '-' . $service_str])) {
$service_config = $service_default_hosts;
} else {
......@@ -47,32 +52,14 @@ class ThriftServiceFactoryProxy
self::BINARY => 'application/thrift-binary',
self::JSON => 'application/thrift-json',
];
if (defined('IS_SWOOLE_SERVICE') && IS_SWOOLE_SERVICE === true) {
$this->socket = new SwoftHttpClient(
$service_real['host'],
$service_real['port'],
'/' . $service_name_space_str . '/' . $service_str . '/',
'http',
$service_real
);
$this->socket->addHeaders(['Contents-Type' => $content_type_mapping[$content_type]]);
} else {
$this->socket = new TCurlClient(
$service_real['host'],
$service_real['port'],
'/' . $service_name_space_str . '/' . $service_str . '/',
'http',
$service_real
);
$this->socket->addHeaders(['Content-type' => $content_type_mapping[$content_type]]);
}
$this->transport = new \Thrift\Transport\TBufferedTransport($this->socket, 1024, 1024);
if (self::BINARY === $content_type) {
$protocol = new \Thrift\Protocol\TBinaryProtocol($this->transport, $strict_read = true, $strict_write = true);
} elseif (self::JSON === $content_type) {
$protocol = new \Thrift\Protocol\TJSONProtocol($this->transport);
}
$this->instance = new $class_name($protocol);
$this->service_real_config = $service_real;
$this->request_base_params = [
'host' => $service_real['host'],
'port' => $service_real['port'],
'uri' => '/' . $service_name_space_str . '/' . $service_str . '/',
'content_type_str' => $content_type_mapping[$content_type],
'content_type' => $content_type,
];
}
private function checkService($class_name)
......@@ -100,10 +87,36 @@ class ThriftServiceFactoryProxy
public function __call($name, $arguments)
{
$this->transport->open();
$this->socket->setFuncUri($name);
if (defined('IS_SWOOLE_SERVICE') && IS_SWOOLE_SERVICE === true) {
$socket = new SwoftHttpClient(
$this->request_base_params['host'],
$this->request_base_params['port'],
$this->request_base_params['uri'],
'http',
$service_real
);
$socket->addHeaders(['Contents-Type' => $this->request_base_params['content_type_str']]);
} else {
$socket = new TCurlClient(
$this->request_base_params['host'],
$this->request_base_params['port'],
$this->request_base_params['uri'],
'http',
$this->service_real_config
);
$socket->addHeaders(['Content-type' => $this->request_base_params['content_type_str']]);
}
$transport = new \Thrift\Transport\TBufferedTransport($socket, 1024, 1024);
if (self::BINARY === $this->request_base_params['content_type']) {
$protocol = new \Thrift\Protocol\TBinaryProtocol($transport, $strict_read = true, $strict_write = true);
} elseif (self::JSON === $this->request_base_params['content_type']) {
$protocol = new \Thrift\Protocol\TJSONProtocol($transport);
}
$this->instance = new $this->class_name($protocol);
$transport->open();
$socket->setFuncUri($name);
$ret = call_user_func_array([$this->instance, $name], $arguments);
$this->transport->close();
$transport->close();
return $ret;
}
}
\ No newline at end of file
......@@ -2,5 +2,6 @@
$config['coin_type'] = [
1 => 'USDT',
2 => 'BTC',
3 => 'ETH'
3 => 'ETH',
4 => 'BTY'
];
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment