Showing
1 changed file
with
24 additions
and
1 deletions
@@ -52,7 +52,7 @@ class BaseController extends Controller | @@ -52,7 +52,7 @@ class BaseController extends Controller | ||
52 | */ | 52 | */ |
53 | public function renderJson($code, $message, $data = '') | 53 | public function renderJson($code, $message, $data = '') |
54 | { | 54 | { |
55 | - if (is_array($data)){ | 55 | + if (is_array($data) || is_object($data)){ |
56 | $retData = [ | 56 | $retData = [ |
57 | 'code' => (int)$code, | 57 | 'code' => (int)$code, |
58 | 'message' => $message, | 58 | 'message' => $message, |
@@ -76,6 +76,29 @@ class BaseController extends Controller | @@ -76,6 +76,29 @@ class BaseController extends Controller | ||
76 | Yii::$app->response->send(); | 76 | Yii::$app->response->send(); |
77 | Yii::$app->end(); | 77 | Yii::$app->end(); |
78 | } | 78 | } |
79 | + | ||
80 | + /** | ||
81 | + * jsonp返回 | ||
82 | + */ | ||
83 | + public function renderJsonp($data, $callback = null) | ||
84 | + { | ||
85 | + if ($callback === null){ | ||
86 | + $callback = Yii::$app->request->get('callback','callback'); | ||
87 | + } | ||
88 | + | ||
89 | + if (is_array($data) || is_object($data)){ | ||
90 | + $data = json_encode($data); | ||
91 | + }elseif (is_numeric($data)){ | ||
92 | + $data = (int)$data; | ||
93 | + }else{ | ||
94 | + $data = '"'.$data.'"'; | ||
95 | + } | ||
96 | + | ||
97 | + Yii::$app->response->format = \yii\web\Response::FORMAT_JSONP; | ||
98 | + Yii::$app->response->content = sprintf('%s(%s)',$callback,$data); | ||
99 | + Yii::$app->response->send(); | ||
100 | + Yii::$app->end(); | ||
101 | + } | ||
79 | 102 | ||
80 | public function afterAction($action, $result) | 103 | public function afterAction($action, $result) |
81 | { | 104 | { |
-
Please register or login to post a comment