Skip to content

Commit

Permalink
update workerman rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
lobtao committed Apr 27, 2018
1 parent eb8c9b9 commit 140b7ba
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 58 deletions.
92 changes: 92 additions & 0 deletions src/BaseRpc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Created by lobtao.
* Date: 2018/4/27
* Time: 上午10:36
*/

namespace lobtao\tp5helper;


abstract class BaseRpc {
protected $func;
protected $args;
protected $callback;
protected $namespace;

public abstract function handle($namespace, $filter = null);

/**
* 异常拦截回复
* @param RpcException $exception
* @return String
*/
protected function exception_handler($exception) {

if ($exception instanceof RpcException) {
$errMsg = $exception->getMessage();
} else {
$errMsg = '系统异常';
}
$data = $this->ajaxReturn([
'retid' => 0,
'retmsg' => $errMsg,
], $this->callback);


$msg = sprintf("Trace:%s\nClass: %s\nFile: %s\nLine: %s\n异常描述: %s", $exception->getTraceAsString(), get_class($exception), $exception->getFile(), $exception->getLine(), $exception->getMessage());
if (class_exists('\think\facade\Log')) {
\think\facade\Log::error($msg);
} else {
\think\Log::error($msg);
}

return $data;
}

/**
* 以‘-’来分割ajax传递过来的类名和方法名,调用该方法,并返回值
* @param $func
* @param $args
* @return mixed
* @throws RpcException
*/
protected function callFunc($func, $args) {

$params = explode('_', $func, 2);
if (count($params) != 2) throw new RpcException('请求参数错误');

$svname = ucfirst($params[0]);
$classname = $this->namespace . $svname . 'Service';
$funcname = $params[1];
if (!class_exists($classname)) throw new RpcException('' . $classname . '不存在!');

$object = new $classname();
if (!method_exists($object, $funcname)) throw new RpcException($svname . '中不存在' . $funcname . '方法');
$data = call_user_func_array([$object, $funcname], $args);

return $data;
}

/**
* ajax返回
* @param $result
* @param $callback
* @return \think\response\Json|\think\response\Jsonp
*/
protected function ajaxReturn($result, $callback) {

$data = json_encode($result, JSON_UNESCAPED_UNICODE);
return $callback ? sprintf('%s(%s)', $callback, $data) : $data;
}

/**
* 判断是否为序号数组
* @param $arr
* @return bool
*/
protected function is_assoc($arr) {

return array_keys($arr) !== range(0, count($arr) - 1);
}
}
63 changes: 5 additions & 58 deletions src/RpcController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
use think\Log;
use think\Response;

class RpcController {

private $func;
private $args;
private $callback;
private $namespace;
class RpcController extends BaseRpc{

/**
* 主方法
* @param $namespace
* @param null $filter
* @return \think\response\Json|\think\response\Jsonp
*/
public function handle($namespace, $filter = null) {

Expand All @@ -24,13 +20,12 @@ public function handle($namespace, $filter = null) {
//if ($request->isGet()) return 'API服务接口';

//异常拦截
// error_reporting(E_ERROR);
// set_exception_handler([$this, "exception_handler"]);
try {
$this->func = $request->param('f');
$this->args = $request->param('p', []);

if (gettype($this->args) == 'string') {//微信小程序特别设置;浏览器提交过来自动转换
$this->args = html_entity_decode($this->args);
$this->args = json_decode($this->args, true);
}
$this->callback = $request->param('callback');
Expand All @@ -49,57 +44,9 @@ public function handle($namespace, $filter = null) {
$this->callback//jsonp调用时的回调函数
);
return $response;
} catch (\Exception $exception) {
if ($exception instanceof RpcException) {
$errMsg = $exception->getMessage();
} else {
$errMsg = '系统异常';
}
$response = $this->ajaxReturn([
'retid' => 0,
'retmsg' => $errMsg,
], $this->callback);

$msg = sprintf("Trace:%s\nClass: %s\nFile: %s\nLine: %s\n异常描述: %s", $exception->getTraceAsString(), get_class($exception), $exception->getFile(), $exception->getLine(), $exception->getMessage());
if (class_exists('\think\facade\Log')) {
\think\facade\Log::error($msg);
} else {
\think\Log::error($msg);
}
return $response;
} catch (\Exception $ex) {
return $this->exception_handler($ex);
}
}

/**
* 以‘-’来分割ajax传递过来的类名和方法名,调用该方法,并返回值
* @param $func
* @param $args
* @return mixed
*/
private function callFunc($func, $args) {

$params = explode('_', $func, 2);
if (count($params) != 2) throw new RpcException('请求参数错误');

$svname = ucfirst($params[0]);
$classname = $this->namespace . $svname . 'Service';
$funcname = $params[1];
if (!class_exists($classname)) throw new RpcException('' . $svname . '不存在!!!');
$object = new $classname();
if (!method_exists($object, $funcname)) throw new RpcException($svname . '中不存在' . $funcname . '方法');
$data = call_user_func_array([$object, $funcname], $args);
return $data;
}

/**
* ajax返回
* @param $result
* @param $callback
* @return \think\response\Json|\think\response\Jsonp
*/
private function ajaxReturn($result, $callback) {

return $callback ? jsonp($result) : json($result);
}

}
56 changes: 56 additions & 0 deletions src/RpcWorker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017-7-26
* Time: 19:07
*/

namespace lobtao\tp5helper;

use think\Log;
use Workerman\Connection\TcpConnection;

class RpcWorker extends BaseRpc
{

/**
* 主方法
* @return string|\think\response\Json|\think\response\Jsonp
* @throws RpcException
*/
public function handle( $namespace, $filter = null) {
$this->namespace = $namespace;
//if ($request->isGet()) return 'API服务接口';

//异常捕获
try {
$this->func = isset($_REQUEST['f']) ? $_REQUEST['f'] : '';
$this->args = isset($_REQUEST['p']) ? $_REQUEST['p'] : [];

if (gettype($this->args) == 'string') {//微信小程序特别设置;浏览器提交过来自动转换
$this->args = html_entity_decode($this->args);
$this->args = json_decode($this->args, true);
}
$this->callback = isset($_REQUEST['callback']) ? $_REQUEST['callback'] : '';

//过滤处理
if ($filter) {
call_user_func_array($filter, [$this->func, $this->args]);
}
$result = $this->callFunc($this->func, $this->args);

$data = $this->ajaxReturn(
[
'data' => $result,//返回数据
'retid' => 1,//调用成功标识
],
$this->callback//jsonp调用时的回调函数
);
return $data;
}catch(\Exception $ex){
return $this->exception_handler($ex);
}
}

}

0 comments on commit 140b7ba

Please sign in to comment.