Commit e0159026 authored by tufengqi's avatar tufengqi

fpf组件完善

parent 17c971f3
<?php
namespace fpf\response;
class BaseConstant
{
const ERROR = 'error';
const MSG = 'msg';
const CODE = 'code';
const VAL = 'val';
}
<?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;
}
}
}
......@@ -7,6 +7,7 @@ use yii\base\InvalidConfigException;
class Application extends Component
{
use fpf\response\ResponseApi;
private $trace_config;
public $terry;
private $configures = [];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment