Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
system
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
_site-res
system
Commits
aeb01c63
Commit
aeb01c63
authored
May 03, 2018
by
tufengqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
接口format类优化
parent
99609d00
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
46 deletions
+46
-46
BaseConstant.php
classes/fpf/response/BaseConstant.php
+2
-0
ResponseApi.php
classes/fpf/response/ResponseApi.php
+44
-46
No files found.
classes/fpf/response/BaseConstant.php
View file @
aeb01c63
...
...
@@ -7,4 +7,6 @@ class BaseConstant
const
MSG
=
'msg'
;
const
CODE
=
'code'
;
const
VAL
=
'val'
;
const
OK
=
'ok'
;
const
FINALTAG
=
'finaltag'
;
}
classes/fpf/response/ResponseApi.php
View file @
aeb01c63
...
...
@@ -21,10 +21,10 @@ trait ResponseApi
/**
* 失败返回接口
* @param string $msg
* @param string $flag
* @param int $code
* @param string $flag
*/
public
static
function
jsonError
(
$msg
=
''
,
$
flag
=
'final'
,
$code
=
-
1
)
public
static
function
jsonError
(
$msg
=
''
,
$
code
=
-
1
,
$flag
=
''
)
{
if
(
empty
(
$msg
))
{
$msg
=
'unknown error'
;
...
...
@@ -43,11 +43,11 @@ trait ResponseApi
* @param string $flag
* @param int $code
*/
public
static
function
jsonSuccess
(
$msg
=
''
,
$
flag
=
'final'
,
$code
=
0
)
public
static
function
jsonSuccess
(
$msg
=
''
,
$
code
=
0
,
$flag
=
''
)
{
$view
=
[
BaseConstant
::
CODE
=>
$code
,
BaseConstant
::
MSG
=>
'ok'
,
BaseConstant
::
MSG
=>
BaseConstant
::
OK
,
BaseConstant
::
VAL
=>
$msg
];
$json
=
json_encode
(
$view
);
...
...
@@ -55,47 +55,16 @@ trait ResponseApi
}
/**
* 直接处理老接口数据
* @param $ret
* @param string $flag
*/
public
static
function
dealOldRet
(
$ret
,
$flag
=
'final'
)
{
if
(
true
===
$ret
[
'error'
])
{
$ret
=
json_encode
([]);
}
else
{
$ret
=
json_encode
(
$ret
[
'msg'
]);
}
$callback
=
''
;
if
(
true
===
self
::
$is_support_jsonp
)
{
$callback_key
=
'jsonpcallback'
;
$callback
=
$_GET
[
$callback_key
];
if
(
$callback
)
{
header
(
'Content-type: application/javascript'
);
$callback
=
Html
::
encode
(
$callback_key
);
$ret
=
$callback
.
'('
.
$ret
.
')'
;
}
}
if
(
!
$callback
)
{
header
(
'Content-type: application/json'
);
}
echo
$ret
;
if
(
'final'
===
$flag
)
{
exit
;
}
}
/**
* 直接处理接口数据
* @param $ret
* @param string $flag
*/
public
static
function
dealRet
(
$ret
,
$flag
=
'
final
'
)
public
static
function
dealRet
(
$ret
,
$flag
=
''
)
{
if
(
true
===
$ret
[
'error'
])
{
self
::
jsonError
(
$ret
[
'msg'
]
?
$ret
[
'msg'
]
:
'unknown error'
);
}
else
{
self
::
jsonSuccess
(
$ret
[
'msg'
]
?
$ret
[
'msg'
]
:
'ok'
,
$flag
);
self
::
jsonSuccess
(
$ret
[
'msg'
]
?
$ret
[
'msg'
]
:
BaseConstant
::
OK
,
$flag
);
}
}
...
...
@@ -104,11 +73,11 @@ trait ResponseApi
* @param $json
* @param string $flag
*/
public
static
function
dumpJsonData
(
$json
,
$flag
=
'
final
'
)
public
static
function
dumpJsonData
(
$json
,
$flag
=
''
)
{
$callback
=
''
;
if
(
true
===
self
::
$is_support_jsonp
)
{
header
(
'Content-type: application/javascript'
);
self
::
header
(
'Content-type: application/javascript'
);
$callback_key
=
'jsonpcallback'
;
$callback
=
$_GET
[
$callback_key
];
if
(
$callback
)
{
...
...
@@ -117,28 +86,57 @@ trait ResponseApi
}
}
if
(
!
$callback
)
{
header
(
'Content-type: application/json'
);
self
::
header
(
'Content-type: application/json'
);
}
echo
$json
;
if
(
'final'
===
$flag
)
{
if
(
BaseConstant
::
FINALTAG
===
$flag
)
{
exit
;
}
}
/**
* @param $callback_key
* @param $json_str
* @param $callback_key
*/
public
static
function
printByJson
(
$
callback_key
,
$json_str
)
public
static
function
printByJson
(
$
json_str
,
$callback_key
=
''
)
{
$callback
=
$_GET
[
$callback_key
];
$callback
=
''
;
if
(
$callback_key
)
{
$callback
=
$_GET
[
$callback_key
]
??
''
;
}
if
(
$callback
)
{
$callback
=
Html
::
encode
(
$callback_key
);
header
(
'Content-type: application/javascript'
);
self
::
header
(
'Content-type: application/javascript'
);
echo
$callback
.
'('
.
$json_str
.
')'
;
}
else
{
header
(
'Content-type: application/json'
);
self
::
header
(
'Content-type: application/json'
);
echo
$json_str
;
}
}
/**
* @param $json_str
* @param $callback_key
*/
public
static
function
printByArr
(
$arr
,
$callback_key
=
''
)
{
$callback
=
''
;
if
(
$callback_key
)
{
$callback
=
$_GET
[
$callback_key
]
??
''
;
}
if
(
$callback
)
{
$callback
=
Html
::
encode
(
$callback_key
);
self
::
header
(
'Content-type: application/javascript'
);
echo
$callback
.
'('
.
json_encode
(
$arr
)
.
')'
;
}
else
{
self
::
header
(
'Content-type: application/json'
);
echo
json_encode
(
$arr
);
}
}
public
function
header
(
$value
)
{
if
(
1
!=
\Yii
::
$app
->
request
->
get
(
'debug'
))
{
header
(
$value
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment