Skip to content

Commit 140b7ba

Browse files
author
lobtao
committed
update workerman rpc
1 parent eb8c9b9 commit 140b7ba

File tree

3 files changed

+153
-58
lines changed

3 files changed

+153
-58
lines changed

src/BaseRpc.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Created by lobtao.
4+
* Date: 2018/4/27
5+
* Time: 上午10:36
6+
*/
7+
8+
namespace lobtao\tp5helper;
9+
10+
11+
abstract class BaseRpc {
12+
protected $func;
13+
protected $args;
14+
protected $callback;
15+
protected $namespace;
16+
17+
public abstract function handle($namespace, $filter = null);
18+
19+
/**
20+
* 异常拦截回复
21+
* @param RpcException $exception
22+
* @return String
23+
*/
24+
protected function exception_handler($exception) {
25+
26+
if ($exception instanceof RpcException) {
27+
$errMsg = $exception->getMessage();
28+
} else {
29+
$errMsg = '系统异常';
30+
}
31+
$data = $this->ajaxReturn([
32+
'retid' => 0,
33+
'retmsg' => $errMsg,
34+
], $this->callback);
35+
36+
37+
$msg = sprintf("Trace:%s\nClass: %s\nFile: %s\nLine: %s\n异常描述: %s", $exception->getTraceAsString(), get_class($exception), $exception->getFile(), $exception->getLine(), $exception->getMessage());
38+
if (class_exists('\think\facade\Log')) {
39+
\think\facade\Log::error($msg);
40+
} else {
41+
\think\Log::error($msg);
42+
}
43+
44+
return $data;
45+
}
46+
47+
/**
48+
* 以‘-’来分割ajax传递过来的类名和方法名,调用该方法,并返回值
49+
* @param $func
50+
* @param $args
51+
* @return mixed
52+
* @throws RpcException
53+
*/
54+
protected function callFunc($func, $args) {
55+
56+
$params = explode('_', $func, 2);
57+
if (count($params) != 2) throw new RpcException('请求参数错误');
58+
59+
$svname = ucfirst($params[0]);
60+
$classname = $this->namespace . $svname . 'Service';
61+
$funcname = $params[1];
62+
if (!class_exists($classname)) throw new RpcException('' . $classname . '不存在!');
63+
64+
$object = new $classname();
65+
if (!method_exists($object, $funcname)) throw new RpcException($svname . '中不存在' . $funcname . '方法');
66+
$data = call_user_func_array([$object, $funcname], $args);
67+
68+
return $data;
69+
}
70+
71+
/**
72+
* ajax返回
73+
* @param $result
74+
* @param $callback
75+
* @return \think\response\Json|\think\response\Jsonp
76+
*/
77+
protected function ajaxReturn($result, $callback) {
78+
79+
$data = json_encode($result, JSON_UNESCAPED_UNICODE);
80+
return $callback ? sprintf('%s(%s)', $callback, $data) : $data;
81+
}
82+
83+
/**
84+
* 判断是否为序号数组
85+
* @param $arr
86+
* @return bool
87+
*/
88+
protected function is_assoc($arr) {
89+
90+
return array_keys($arr) !== range(0, count($arr) - 1);
91+
}
92+
}

src/RpcController.php

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
use think\Log;
55
use think\Response;
66

7-
class RpcController {
8-
9-
private $func;
10-
private $args;
11-
private $callback;
12-
private $namespace;
7+
class RpcController extends BaseRpc{
138

149
/**
1510
* 主方法
1611
* @param $namespace
1712
* @param null $filter
13+
* @return \think\response\Json|\think\response\Jsonp
1814
*/
1915
public function handle($namespace, $filter = null) {
2016

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

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

3327
if (gettype($this->args) == 'string') {//微信小程序特别设置;浏览器提交过来自动转换
28+
$this->args = html_entity_decode($this->args);
3429
$this->args = json_decode($this->args, true);
3530
}
3631
$this->callback = $request->param('callback');
@@ -49,57 +44,9 @@ public function handle($namespace, $filter = null) {
4944
$this->callback//jsonp调用时的回调函数
5045
);
5146
return $response;
52-
} catch (\Exception $exception) {
53-
if ($exception instanceof RpcException) {
54-
$errMsg = $exception->getMessage();
55-
} else {
56-
$errMsg = '系统异常';
57-
}
58-
$response = $this->ajaxReturn([
59-
'retid' => 0,
60-
'retmsg' => $errMsg,
61-
], $this->callback);
62-
63-
$msg = sprintf("Trace:%s\nClass: %s\nFile: %s\nLine: %s\n异常描述: %s", $exception->getTraceAsString(), get_class($exception), $exception->getFile(), $exception->getLine(), $exception->getMessage());
64-
if (class_exists('\think\facade\Log')) {
65-
\think\facade\Log::error($msg);
66-
} else {
67-
\think\Log::error($msg);
68-
}
69-
return $response;
47+
} catch (\Exception $ex) {
48+
return $this->exception_handler($ex);
7049
}
7150
}
7251

73-
/**
74-
* 以‘-’来分割ajax传递过来的类名和方法名,调用该方法,并返回值
75-
* @param $func
76-
* @param $args
77-
* @return mixed
78-
*/
79-
private function callFunc($func, $args) {
80-
81-
$params = explode('_', $func, 2);
82-
if (count($params) != 2) throw new RpcException('请求参数错误');
83-
84-
$svname = ucfirst($params[0]);
85-
$classname = $this->namespace . $svname . 'Service';
86-
$funcname = $params[1];
87-
if (!class_exists($classname)) throw new RpcException('' . $svname . '不存在!!!');
88-
$object = new $classname();
89-
if (!method_exists($object, $funcname)) throw new RpcException($svname . '中不存在' . $funcname . '方法');
90-
$data = call_user_func_array([$object, $funcname], $args);
91-
return $data;
92-
}
93-
94-
/**
95-
* ajax返回
96-
* @param $result
97-
* @param $callback
98-
* @return \think\response\Json|\think\response\Jsonp
99-
*/
100-
private function ajaxReturn($result, $callback) {
101-
102-
return $callback ? jsonp($result) : json($result);
103-
}
104-
10552
}

src/RpcWorker.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Administrator
5+
* Date: 2017-7-26
6+
* Time: 19:07
7+
*/
8+
9+
namespace lobtao\tp5helper;
10+
11+
use think\Log;
12+
use Workerman\Connection\TcpConnection;
13+
14+
class RpcWorker extends BaseRpc
15+
{
16+
17+
/**
18+
* 主方法
19+
* @return string|\think\response\Json|\think\response\Jsonp
20+
* @throws RpcException
21+
*/
22+
public function handle( $namespace, $filter = null) {
23+
$this->namespace = $namespace;
24+
//if ($request->isGet()) return 'API服务接口';
25+
26+
//异常捕获
27+
try {
28+
$this->func = isset($_REQUEST['f']) ? $_REQUEST['f'] : '';
29+
$this->args = isset($_REQUEST['p']) ? $_REQUEST['p'] : [];
30+
31+
if (gettype($this->args) == 'string') {//微信小程序特别设置;浏览器提交过来自动转换
32+
$this->args = html_entity_decode($this->args);
33+
$this->args = json_decode($this->args, true);
34+
}
35+
$this->callback = isset($_REQUEST['callback']) ? $_REQUEST['callback'] : '';
36+
37+
//过滤处理
38+
if ($filter) {
39+
call_user_func_array($filter, [$this->func, $this->args]);
40+
}
41+
$result = $this->callFunc($this->func, $this->args);
42+
43+
$data = $this->ajaxReturn(
44+
[
45+
'data' => $result,//返回数据
46+
'retid' => 1,//调用成功标识
47+
],
48+
$this->callback//jsonp调用时的回调函数
49+
);
50+
return $data;
51+
}catch(\Exception $ex){
52+
return $this->exception_handler($ex);
53+
}
54+
}
55+
56+
}

0 commit comments

Comments
 (0)