Authored by wuxiao

添加jsonp返回方法

... ... @@ -52,7 +52,7 @@ class BaseController extends Controller
*/
public function renderJson($code, $message, $data = '')
{
if (is_array($data)){
if (is_array($data) || is_object($data)){
$retData = [
'code' => (int)$code,
'message' => $message,
... ... @@ -77,6 +77,29 @@ class BaseController extends Controller
Yii::$app->end();
}
/**
* jsonp返回
*/
public function renderJsonp($data, $callback = null)
{
if ($callback === null){
$callback = Yii::$app->request->get('callback','callback');
}
if (is_array($data) || is_object($data)){
$data = json_encode($data);
}elseif (is_numeric($data)){
$data = (int)$data;
}else{
$data = '"'.$data.'"';
}
Yii::$app->response->format = \yii\web\Response::FORMAT_JSONP;
Yii::$app->response->content = sprintf('%s(%s)',$callback,$data);
Yii::$app->response->send();
Yii::$app->end();
}
public function afterAction($action, $result)
{
return parent::afterAction($action, $result);
... ...