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 ...@@ -6,6 +6,7 @@ class BaseConstant
{ {
const ERROR = 'error'; const ERROR = 'error';
const MSG = 'msg'; const MSG = 'msg';
const MESSAGE = 'message';
const CODE = 'code'; const CODE = 'code';
const VAL = 'val'; const VAL = 'val';
const DATA = 'data'; const DATA = 'data';
......
...@@ -16,6 +16,15 @@ class ResponseMsg ...@@ -16,6 +16,15 @@ class ResponseMsg
public function __construct() public function __construct()
{ {
$this->header_list = self::$default_header_list; $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) public static function setDefaultHeader($default_header_list)
...@@ -30,14 +39,14 @@ class ResponseMsg ...@@ -30,14 +39,14 @@ class ResponseMsg
return self::$default_header_list; 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) 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 ...@@ -52,8 +61,8 @@ class ResponseMsg
$msg = 'unknown error'; $msg = 'unknown error';
} }
$view = [ $view = [
'code' => $code, BaseConstant::CODE => $code,
'msg' => $msg, BaseConstant::MESSAGE => $msg,
]; ];
$json = json_encode($view); $json = json_encode($view);
return $this->dumpJsonData($json); return $this->dumpJsonData($json);
...@@ -69,7 +78,7 @@ class ResponseMsg ...@@ -69,7 +78,7 @@ class ResponseMsg
{ {
$view = [ $view = [
BaseConstant::CODE => $code, BaseConstant::CODE => $code,
BaseConstant::MSG => BaseConstant::OK, BaseConstant::MESSAGE => BaseConstant::OK,
BaseConstant::DATA => $data BaseConstant::DATA => $data
]; ];
$json = json_encode($view); $json = json_encode($view);
...@@ -82,10 +91,10 @@ class ResponseMsg ...@@ -82,10 +91,10 @@ class ResponseMsg
*/ */
public function dealRet($ret) public function dealRet($ret)
{ {
if (true === $ret['error']) { if (true === $ret[BaseConstant::ERROR]) {
$this->jsonError($ret['msg'] ? : 'unknown error'); $this->jsonError($ret[BaseConstant::MESSAGE] ? : 'unknown error');
} else { } else {
$this->jsonSuccess($ret['msg'] ? : BaseConstant::OK); $this->jsonSuccess($ret[BaseConstant::MESSAGE] ? : BaseConstant::OK);
} }
} }
...@@ -166,10 +175,10 @@ class ResponseMsg ...@@ -166,10 +175,10 @@ class ResponseMsg
public function printOldFail($code, $code_msg, $detail_code, $detail_msg, $callback_key = '') public function printOldFail($code, $code_msg, $detail_code, $detail_msg, $callback_key = '')
{ {
self::header('Access-Control-Allow-Origin', '*'); $this->header('Access-Control-Allow-Origin', '*');
self::header('Access-Control-Allow-Methods', 'POST,GET'); $this->header('Access-Control-Allow-Methods', 'POST,GET');
self::header('Access-Control-Allow-Credentials', 'true'); $this->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-Headers', 'Authorization,FZM-REQUEST-OS,FZM-USER-IP,FZM-REQUEST-UUID,Content-Type,Content-Length');
$callback = ''; $callback = '';
if ($callback_key) { if ($callback_key) {
$callback = $_GET[$callback_key] ?? ''; $callback = $_GET[$callback_key] ?? '';
......
...@@ -9,8 +9,10 @@ use fpf\response\BaseConstant; ...@@ -9,8 +9,10 @@ use fpf\response\BaseConstant;
class ThriftServiceFactoryProxy class ThriftServiceFactoryProxy
{ {
private $instance = null; private $instance = null;
private $transport = null; private $request_base_params = null;
private $socket = null; private $tem_class_name = '';
private $class_name = '';
private $service_real_config;
const BINARY = 0; const BINARY = 0;
const JSON = 1; const JSON = 1;
...@@ -18,6 +20,7 @@ class ThriftServiceFactoryProxy ...@@ -18,6 +20,7 @@ class ThriftServiceFactoryProxy
{ {
$service_default_protocol = Yii::$app->fpf->getConfig('service_default_protocol', 'thrift_service'); $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_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'); $service_module_hosts = Yii::$app->fpf->getConfig('service_module_hosts', 'thrift_service');
$check_ret = $this->checkService($class_name); $check_ret = $this->checkService($class_name);
if (true === $check_ret[BaseConstant::ERROR]) { if (true === $check_ret[BaseConstant::ERROR]) {
...@@ -26,6 +29,8 @@ class ThriftServiceFactoryProxy ...@@ -26,6 +29,8 @@ class ThriftServiceFactoryProxy
$service_check_arr = $check_ret[BaseConstant::MSG]; $service_check_arr = $check_ret[BaseConstant::MSG];
$service_name_space_str = $service_check_arr[0]; $service_name_space_str = $service_check_arr[0];
$service_str = $service_check_arr[1]; $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])) { if (empty($service_module_hosts[$service_name_space_str . '-' . $service_str])) {
$service_config = $service_default_hosts; $service_config = $service_default_hosts;
} else { } else {
...@@ -47,32 +52,14 @@ class ThriftServiceFactoryProxy ...@@ -47,32 +52,14 @@ class ThriftServiceFactoryProxy
self::BINARY => 'application/thrift-binary', self::BINARY => 'application/thrift-binary',
self::JSON => 'application/thrift-json', self::JSON => 'application/thrift-json',
]; ];
if (defined('IS_SWOOLE_SERVICE') && IS_SWOOLE_SERVICE === true) { $this->service_real_config = $service_real;
$this->socket = new SwoftHttpClient( $this->request_base_params = [
$service_real['host'], 'host' => $service_real['host'],
$service_real['port'], 'port' => $service_real['port'],
'/' . $service_name_space_str . '/' . $service_str . '/', 'uri' => '/' . $service_name_space_str . '/' . $service_str . '/',
'http', 'content_type_str' => $content_type_mapping[$content_type],
$service_real 'content_type' => $content_type,
); ];
$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);
} }
private function checkService($class_name) private function checkService($class_name)
...@@ -100,10 +87,36 @@ class ThriftServiceFactoryProxy ...@@ -100,10 +87,36 @@ class ThriftServiceFactoryProxy
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
$this->transport->open(); if (defined('IS_SWOOLE_SERVICE') && IS_SWOOLE_SERVICE === true) {
$this->socket->setFuncUri($name); $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); $ret = call_user_func_array([$this->instance, $name], $arguments);
$this->transport->close(); $transport->close();
return $ret; return $ret;
} }
} }
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
$config['coin_type'] = [ $config['coin_type'] = [
1 => 'USDT', 1 => 'USDT',
2 => 'BTC', 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