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
e0159026
Commit
e0159026
authored
May 03, 2018
by
tufengqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fpf组件完善
parent
17c971f3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
155 additions
and
0 deletions
+155
-0
BaseConstant.php
classes/fpf/response/BaseConstant.php
+10
-0
ResponseApi.php
classes/fpf/response/ResponseApi.php
+144
-0
Application.php
classes/fpf/yii_component/Application.php
+1
-0
No files found.
classes/fpf/response/BaseConstant.php
0 → 100644
View file @
e0159026
<?php
namespace
fpf\response
;
class
BaseConstant
{
const
ERROR
=
'error'
;
const
MSG
=
'msg'
;
const
CODE
=
'code'
;
const
VAL
=
'val'
;
}
classes/fpf/response/ResponseApi.php
0 → 100644
View file @
e0159026
<?php
namespace
fpf\response
;
use
fpf\response\BaseConstant
;
use
yii\helpers\Html
;
trait
ResponseApi
{
public
static
$is_support_jsonp
=
false
;
public
static
function
arrSuccess
(
$data
=
'ok'
,
$code
=
200
)
{
return
[
BaseConstant
::
ERROR
=>
false
,
BaseConstant
::
MSG
=>
$data
,
BaseConstant
::
CODE
=>
$code
];
}
public
static
function
arrFail
(
$data
,
$code
=
-
1
)
{
return
[
BaseConstant
::
ERROR
=>
true
,
BaseConstant
::
MSG
=>
$data
,
BaseConstant
::
CODE
=>
$code
];
}
/**
* 失败返回接口
* @param string $msg
* @param string $flag
* @param int $code
*/
public
static
function
jsonError
(
$msg
=
''
,
$flag
=
'final'
,
$code
=
-
1
)
{
if
(
empty
(
$msg
))
{
$msg
=
'unknown error'
;
}
$view
=
[
'code'
=>
$code
,
'msg'
=>
$msg
,
];
$json
=
json_encode
(
$view
);
self
::
dumpJsonData
(
$json
,
$flag
);
}
/**
* 成功返回接口
* @param string $msg
* @param string $flag
* @param int $code
*/
public
static
function
jsonSuccess
(
$msg
=
''
,
$flag
=
'final'
,
$code
=
0
)
{
$view
=
[
BaseConstant
::
CODE
=>
$code
,
BaseConstant
::
MSG
=>
'ok'
,
BaseConstant
::
VAL
=>
$msg
];
$json
=
json_encode
(
$view
);
self
::
dumpJsonData
(
$json
,
$flag
);
}
/**
* 直接处理老接口数据
* @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'
)
{
if
(
true
===
$ret
[
'error'
])
{
self
::
jsonError
(
$ret
[
'msg'
]
?
$ret
[
'msg'
]
:
'unknown error'
);
}
else
{
self
::
jsonSuccess
(
$ret
[
'msg'
]
?
$ret
[
'msg'
]
:
'ok'
,
$flag
);
}
}
/**
* 根据是否为JSONP做特殊处理输出
* @param $json
* @param string $flag
*/
public
static
function
dumpJsonData
(
$json
,
$flag
=
'final'
)
{
$callback
=
''
;
if
(
true
===
self
::
$is_support_jsonp
)
{
header
(
'Content-type: application/javascript'
);
$callback_key
=
'jsonpcallback'
;
$callback
=
$_GET
[
$callback_key
];
if
(
$callback
)
{
$callback
=
Html
::
encode
(
$callback_key
);
$json
=
$callback
.
'('
.
$json
.
')'
;
}
}
if
(
!
$callback
)
{
header
(
'Content-type: application/json'
);
}
echo
$json
;
if
(
'final'
===
$flag
)
{
exit
;
}
}
/**
* @param $callback_key
* @param $json_str
*/
public
static
function
printByJson
(
$callback_key
,
$json_str
)
{
$callback
=
$_GET
[
$callback_key
];
if
(
$callback
)
{
$callback
=
Html
::
encode
(
$callback_key
);
header
(
'Content-type: application/javascript'
);
echo
$callback
.
'('
.
$json_str
.
')'
;
}
else
{
header
(
'Content-type: application/json'
);
echo
$json_str
;
}
}
}
classes/fpf/yii_component/Application.php
View file @
e0159026
...
...
@@ -7,6 +7,7 @@ use yii\base\InvalidConfigException;
class
Application
extends
Component
{
use
fpf\response\ResponseApi
;
private
$trace_config
;
public
$terry
;
private
$configures
=
[];
...
...
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