-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lobtao
committed
Apr 27, 2018
1 parent
eb8c9b9
commit 140b7ba
Showing
3 changed files
with
153 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |