167 lines
5.5 KiB
PHP
167 lines
5.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by chatfeed.
|
|
* @Author:$Id$
|
|
*/
|
|
|
|
namespace admin\foundation;
|
|
|
|
use common\jobs\LogJob;
|
|
use Yii;
|
|
use yii\base\UserException;
|
|
use yii\helpers\Json;
|
|
|
|
class JsonResponseFormatter extends \yii\web\JsonResponseFormatter
|
|
{
|
|
private static $arr = [
|
|
'name',
|
|
'contact',
|
|
'mobile',
|
|
'express_address',
|
|
'clinical_diagnose',
|
|
'drug_name',
|
|
'drug_alias',
|
|
'function',
|
|
'usage',
|
|
'express_mobile',
|
|
'express_name',
|
|
'express_region',
|
|
'doctor',
|
|
'accept_tel',
|
|
'patient',
|
|
'patient_mobile'
|
|
];
|
|
private static $param = [
|
|
'param' => [],
|
|
'result' => [],
|
|
'url' => '',
|
|
];
|
|
private $defaultErrorCode = -1;
|
|
|
|
public static function formatData($data, $errno = 0, $msg = '')
|
|
{
|
|
|
|
// if(empty($data)) $data = new \stdClass();
|
|
// TODO 加密后续再完善
|
|
// if (empty($data)) {
|
|
// $data = [];
|
|
// } else {
|
|
// if (array_key_exists('list', $data)) {
|
|
// $data['list'] = self::aseEncode($data['list']);
|
|
// }
|
|
// }
|
|
$result['data'] = $data;
|
|
$result['msg'] = $msg;
|
|
$result['errcode'] = $errno;
|
|
return $result;
|
|
}
|
|
|
|
private static function aseEncode($data)
|
|
{
|
|
return $data;
|
|
// if (is_array($data)) {
|
|
// foreach ($data as $key => &$value) {
|
|
// if (is_array($value)) {
|
|
// $value = self::aseEncode($value); // 递归处理子数组
|
|
// } else {
|
|
// if (in_array($key, self::$arr)) {
|
|
// $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__);
|
|
}
|
|
}
|
|
}
|