切换导航条
此项目
正在载入...
登录
何书鹏
/
recruit
·
提交
转到一个项目
GitLab
转到仪表盘
项目
活动
文件
提交
管道
0
构建
0
图表
里程碑
问题
0
合并请求
0
成员
标记
维基
派生
网络
创建新的问题
下载为
邮件补丁
差异文件
浏览文件
作者
Karson
7 years ago
提交
3c09a3f918fa90b8ffa51571415a6c833eab7173
1 个父辈
d2a523e4
新增配置默认输出格式
修复在JSONP请求下不响应输出的BUG
隐藏空白字符变更
内嵌
并排对比
正在显示
1 个修改的文件
包含
16 行增加
和
8 行删除
application/common/controller/Api.php
application/common/controller/Api.php
查看文件 @
3c09a3f
...
...
@@ -55,6 +55,12 @@ class Api
protected
$auth
=
null
;
/**
* 默认响应输出类型,支持json/xml
* @var string
*/
protected
$responseType
=
'json'
;
/**
* 构造方法
* @access public
* @param Request $request Request 对象
...
...
@@ -145,7 +151,7 @@ class Api
* @param string $type 输出类型
* @param array $header 发送的 Header 信息
*/
protected
function
success
(
$msg
=
''
,
$data
=
null
,
$code
=
1
,
$type
=
'json'
,
array
$header
=
[])
protected
function
success
(
$msg
=
''
,
$data
=
null
,
$code
=
1
,
$type
=
null
,
array
$header
=
[])
{
$this
->
result
(
$msg
,
$data
,
$code
,
$type
,
$header
);
}
...
...
@@ -158,7 +164,7 @@ class Api
* @param string $type 输出类型
* @param array $header 发送的 Header 信息
*/
protected
function
error
(
$msg
=
''
,
$data
=
null
,
$code
=
0
,
$type
=
'json'
,
array
$header
=
[])
protected
function
error
(
$msg
=
''
,
$data
=
null
,
$code
=
0
,
$type
=
null
,
array
$header
=
[])
{
$this
->
result
(
$msg
,
$data
,
$code
,
$type
,
$header
);
}
...
...
@@ -168,13 +174,13 @@ class Api
* @access protected
* @param mixed $msg 提示信息
* @param mixed $data 要返回的数据
* @param int $code 返回的 code
* @param string $type 返回数据格式
* @param int $code 错误码,默认为0
* @param string $type 输出类型,支持json/xml/jsonp
* @param array $header 发送的 Header 信息
* @return void
* @throws HttpResponseException
*/
protected
function
result
(
$msg
,
$data
=
null
,
$code
=
0
,
$type
=
'json'
,
array
$header
=
[])
protected
function
result
(
$msg
,
$data
=
null
,
$code
=
0
,
$type
=
null
,
array
$header
=
[])
{
$result
=
[
'code'
=>
$code
,
...
...
@@ -182,7 +188,9 @@ class Api
'time'
=>
Request
::
instance
()
->
server
(
'REQUEST_TIME'
),
'data'
=>
$data
,
];
$type
=
$type
?:
$this
->
getResponseType
();
// 如果未设置类型则自动判断
$type
=
$type
?
$type
:
(
$this
->
request
->
param
(
config
(
'var_jsonp_handler'
))
?
'jsonp'
:
$this
->
responseType
);
if
(
isset
(
$header
[
'statuscode'
]))
{
$code
=
$header
[
'statuscode'
];
...
...
@@ -190,10 +198,10 @@ class Api
}
else
{
$code
=
$code
>=
1000
?
200
:
$code
;
//未设置状态码,根据code值判断
$code
=
$code
>=
1000
||
$code
<
200
?
200
:
$code
;
}
$response
=
Response
::
create
(
$result
,
$type
,
$code
)
->
header
(
$header
);
throw
new
HttpResponseException
(
$response
);
}
...
...
请
注册
或
登录
后发表评论