158 lines
5.5 KiB
PHP
158 lines
5.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by chatfeed.
|
|
* @Author:$Id$
|
|
*/
|
|
|
|
namespace admin\foundation;
|
|
|
|
use common\jobs\LogJob;
|
|
use common\services\DataEncryptor;
|
|
use Yii;
|
|
use yii\base\UserException;
|
|
use yii\helpers\Json;
|
|
|
|
class JsonResponseFormatter extends \yii\web\JsonResponseFormatter
|
|
{
|
|
private static $param = [
|
|
'param' => [],
|
|
'result' => [],
|
|
'url' => ''
|
|
];
|
|
private $defaultErrorCode = -1;
|
|
|
|
public static function formatData($data, $errno = 0, $msg = '')
|
|
{
|
|
// 获取当前请求的URL
|
|
if (empty($data)) {
|
|
$data = [];
|
|
} else {
|
|
if (Yii::$app->user->isGuest) {
|
|
$data = self::aseEncode($data);
|
|
}
|
|
}
|
|
$result['data'] = $data;
|
|
$result['msg'] = $msg;
|
|
$result['errcode'] = $errno;
|
|
$result['ecs'] = env('ECS');
|
|
return $result;
|
|
}
|
|
|
|
private static function aseEncode($data)
|
|
{
|
|
// return $data;
|
|
if (is_array($data)) {
|
|
foreach ($data as $key => &$value) {
|
|
if (is_object($value)) {
|
|
$value = $value->toArray();
|
|
}
|
|
if (is_array($value)) {
|
|
$value = self::aseEncode($value); // 递归处理子数组
|
|
} else {
|
|
// 数据库中加密的方法需要解密
|
|
if (in_array($key, \Yii::$app->params['database_ase_list']) && is_encrypted($value)) {
|
|
$value = (new DataEncryptor(Yii::$app->params['ase_key']))->decrypt($value);
|
|
}
|
|
if (in_array($key, \Yii::$app->params['aseList'])) {
|
|
if (is_string($key)) {
|
|
$value = ase_encode($value); // 加密指定键值
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public $encrypt = true; //是不加密
|
|
|
|
/**
|
|
* Formats response data in JSON format.
|
|
* @param \yii\web\Response $response
|
|
*/
|
|
protected function formatJson($response)
|
|
{
|
|
if ($response->data !== null) {
|
|
$options = $this->encodeOptions;
|
|
if ($this->prettyPrint) {
|
|
$options |= JSON_PRETTY_PRINT;
|
|
}
|
|
$errcode = 0;
|
|
$msg = '';
|
|
$data = [];
|
|
if (!$response->isSuccessful) {
|
|
$response->statusCode = 200;
|
|
$errcode = -1;
|
|
if (Yii::$app->errorHandler->exception) {
|
|
// if(Yii::$app->errorHandler->exception instanceof )
|
|
if (isset(Yii::$app->errorHandler->exception->statusCode)) {
|
|
$errcode = Yii::$app->errorHandler->exception->statusCode;
|
|
} elseif (Yii::$app->errorHandler->exception->getCode() > 0) {
|
|
$errcode = Yii::$app->errorHandler->exception->getCode();
|
|
} else {
|
|
$errcode = $this->defaultErrorCode;
|
|
}
|
|
$msg = YII_DEBUG ? Yii::$app->errorHandler->exception->getMessage() : '服务器内部错误';
|
|
}
|
|
} else {
|
|
$data = $response->data;
|
|
}
|
|
|
|
// 记录日志
|
|
if (Yii::$app->request->getMethod() == 'GET') {
|
|
self::$param['param'] = Yii::$app->request->get();
|
|
} else {
|
|
self::$param['param'] = Yii::$app->request->post();
|
|
}
|
|
|
|
// self::$param['result'] = $data;
|
|
self::$param['result'] = [];
|
|
self::$param['url'] = Yii::$app->request->getUrl();
|
|
self::$param['type'] = $response->statusCode == 200? 0: 1;
|
|
self::$param['result_code'] = $errcode;
|
|
$op = get_device_type();
|
|
self::$param['ip'] = $op['ip'];
|
|
self::$param['equipment'] = $op['platform'];
|
|
self::$param['browser'] = $op['browser'];
|
|
if (Yii::$app->user->isGuest) {
|
|
self::$param['user_id'] = 0;
|
|
} else {
|
|
self::$param['user_id'] = Yii::$app->user->id;
|
|
}
|
|
// 日志队列
|
|
\Yii::$app->queue->push(new LogJob(['params' => self::$param]));
|
|
|
|
$response->data = self::formatData($data, $errcode, $msg);
|
|
$response->content = Json::encode($response->data, $options);
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Formats response data in JSONP format.
|
|
* @param \yii\web\Response $response
|
|
*/
|
|
protected function formatJsonp($response)
|
|
{
|
|
$response->getHeaders()->set('Content-Type', 'application/javascript; charset=UTF-8');
|
|
if ($response->data !== null) {
|
|
$options = $this->encodeOptions;
|
|
if ($this->prettyPrint) {
|
|
$options |= JSON_PRETTY_PRINT;
|
|
}
|
|
if (!$response->isSuccessful) {
|
|
$response->statusCode = 200;
|
|
}
|
|
|
|
}
|
|
$response->data = ['data' => $response->data, 'callback' => Yii::$app->getRequest()->get('callback')];
|
|
if (is_array($response->data) && isset($response->data['data'], $response->data['callback'])) {
|
|
$response->content = sprintf('%s(%s);', $response->data['callback'], Json::htmlEncode($response->data['data']));
|
|
} elseif ($response->data !== null) {
|
|
$response->content = '';
|
|
Yii::warning("The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements.", __METHOD__);
|
|
}
|
|
}
|
|
}
|