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
bf4061b5
Commit
bf4061b5
authored
Jun 26, 2018
by
tufengqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add new response
parent
2d93b2ea
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
195 additions
and
0 deletions
+195
-0
ResponseMsg.php
classes/fpf/response/ResponseMsg.php
+195
-0
No files found.
classes/fpf/response/ResponseMsg.php
0 → 100644
View file @
bf4061b5
<?php
namespace
fpf\response
;
use
yii\helpers\Html
;
use
yii\web\Response
;
class
ResponseMsg
{
public
$is_support_jsonp
=
false
;
public
$header_list
=
[];
public
function
arrSuccess
(
$data
=
'ok'
,
$code
=
200
)
{
return
[
BaseConstant
::
ERROR
=>
false
,
BaseConstant
::
MSG
=>
$data
,
BaseConstant
::
CODE
=>
$code
];
}
public
function
arrFail
(
$data
,
$code
=
-
1
)
{
return
[
BaseConstant
::
ERROR
=>
true
,
BaseConstant
::
MSG
=>
$data
,
BaseConstant
::
CODE
=>
$code
];
}
/**
* 失败返回接口
* @param string $msg
* @param int $code
* @return string
*/
public
function
jsonError
(
$msg
=
''
,
$code
=
-
1
)
{
if
(
empty
(
$msg
))
{
$msg
=
'unknown error'
;
}
$view
=
[
'code'
=>
$code
,
'msg'
=>
$msg
,
];
$json
=
json_encode
(
$view
);
return
$this
->
dumpJsonData
(
$json
);
}
/**
* 成功返回接口
* @param string $msg
* @param int $code
* @return string
*/
public
function
jsonSuccess
(
$msg
=
''
,
$code
=
0
)
{
$view
=
[
BaseConstant
::
CODE
=>
$code
,
BaseConstant
::
MSG
=>
BaseConstant
::
OK
,
BaseConstant
::
VAL
=>
$msg
];
$json
=
json_encode
(
$view
);
return
$this
->
dumpJsonData
(
$json
);
}
/**
* 直接处理接口数据
* @param $ret
*/
public
function
dealRet
(
$ret
)
{
if
(
true
===
$ret
[
'error'
])
{
$this
->
jsonError
(
$ret
[
'msg'
]
?
:
'unknown error'
);
}
else
{
$this
->
jsonSuccess
(
$ret
[
'msg'
]
?
:
BaseConstant
::
OK
);
}
}
/**
* 根据是否为JSONP做特殊处理输出
* @param $json
* @return string
*/
public
function
dumpJsonData
(
$json
)
{
$callback
=
''
;
if
(
true
===
$this
->
is_support_jsonp
)
{
$this
->
header
(
'Content-type'
,
'application/javascript'
);
$callback_key
=
'jsonpcallback'
;
$callback
=
$_GET
[
$callback_key
];
if
(
$callback
)
{
$callback
=
Html
::
encode
(
$callback_key
);
$json
=
$callback
.
'('
.
$json
.
')'
;
}
}
if
(
!
$callback
)
{
$this
->
header
(
'Content-type'
,
'application/json'
);
}
return
$json
;
}
/**
* @param $json_str
* @param string $callback_key
* @return string
*/
public
function
printByJson
(
$json_str
,
$callback_key
=
''
)
{
$callback
=
''
;
if
(
$callback_key
)
{
$callback
=
$_GET
[
$callback_key
]
??
''
;
}
if
(
$callback
)
{
$callback
=
Html
::
encode
(
$callback_key
);
$this
->
header
(
'Content-type'
,
'application/javascript'
);
return
$callback
.
'('
.
$json_str
.
')'
;
}
else
{
$this
->
header
(
'Content-type'
,
'application/json'
);
return
$json_str
;
}
}
/**
* @param $arr
* @param string $callback_key
* @return string
*/
public
function
printByArr
(
$arr
,
$callback_key
=
''
)
{
$callback
=
''
;
if
(
$callback_key
)
{
$callback
=
$_GET
[
$callback_key
]
??
''
;
}
if
(
$callback
)
{
$callback
=
Html
::
encode
(
$callback_key
);
$this
->
header
(
'Content-type'
,
'application/javascript'
);
return
$callback
.
'('
.
json_encode
(
$arr
)
.
')'
;
}
else
{
$this
->
header
(
'Content-type'
,
'application/json'
);
return
json_encode
(
$arr
);
}
}
public
function
printOldFail
(
$code
,
$code_msg
,
$detail_code
,
$detail_msg
,
$callback_key
=
''
)
{
self
::
header
(
'Access-Control-Allow-Origin'
,
'*'
);
self
::
header
(
'Access-Control-Allow-Methods'
,
'POST,GET'
);
self
::
header
(
'Access-Control-Allow-Credentials'
,
'true'
);
self
::
header
(
'Access-Control-Allow-Headers'
,
'Authorization,FZM-REQUEST-OS,FZM-USER-IP,FZM-REQUEST-UUID,Content-Type,Content-Length'
);
$callback
=
''
;
if
(
$callback_key
)
{
$callback
=
$_GET
[
$callback_key
]
??
''
;
}
$arr
=
[
'code'
=>
$code
,
'error'
=>
$code_msg
,
'ecode'
=>
$detail_code
,
'message'
=>
$detail_msg
,
'data'
=>
[]];
if
(
$callback
)
{
$callback
=
Html
::
encode
(
$callback_key
);
$this
->
header
(
'Content-type'
,
'application/javascript'
);
return
$callback
.
'('
.
json_encode
(
$arr
)
.
')'
;
}
else
{
$this
->
header
(
'Content-type'
,
'application/json'
);
return
json_encode
(
$arr
);
}
}
/**
* @param $success_data
* @param string $callback_key
* @return string
*/
public
function
printOldSuccess
(
$success_data
,
$callback_key
=
''
)
{
$this
->
header
(
'Access-Control-Allow-Origin'
,
'*'
);
$this
->
header
(
'Access-Control-Allow-Methods'
,
'POST,GET'
);
$this
->
header
(
'Access-Control-Allow-Credentials'
,
'true'
);
$this
->
header
(
'Access-Control-Allow-Headers'
,
'Authorization,FZM-REQUEST-OS,FZM-USER-IP,FZM-REQUEST-UUID,Content-Type,Content-Length'
);
$callback
=
''
;
if
(
$callback_key
)
{
$callback
=
$_GET
[
$callback_key
]
??
''
;
}
$arr
=
[
'code'
=>
200
,
'ecode'
=>
200
,
'error'
=>
'OK'
,
'message'
=>
'OK'
,
'data'
=>
$success_data
];
if
(
$callback
)
{
$callback
=
Html
::
encode
(
$callback_key
);
$this
->
header
(
'Content-type'
,
'application/javascript'
);
return
$callback
.
'('
.
json_encode
(
$arr
)
.
')'
;
}
else
{
$this
->
header
(
'Content-type'
,
'application/json'
);
return
json_encode
(
$arr
);
}
}
public
function
header
(
$key
,
$value
)
{
$this
->
header_list
[
$key
]
=
$value
;
}
public
function
getHeaders
()
{
return
$this
->
header_list
;
}
}
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