44use think \Log ;
55use 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}
0 commit comments