Commit 70f31f44 authored by tufengqi's avatar tufengqi

修复yii swoft cookie_edit 兼容文件

parent 3157e7d8
......@@ -11,6 +11,25 @@ class ResponseMsg
public $header_list = [];
private static $default_header_list = [];
public function __construct()
{
$this->header_list = self::$default_header_list;
}
public static function setDefaultHeader($default_header_list)
{
foreach ($default_header_list as $key => $header) {
self::$default_header_list[$key] = $header;
}
}
public static function getDefaultHeader()
{
return self::$default_header_list;
}
public function arrSuccess($data = 'ok', $code = 200)
{
return [BaseConstant::ERROR => false, BaseConstant::MSG => $data, BaseConstant::CODE => $code];
......@@ -79,7 +98,9 @@ class ResponseMsg
{
$callback = '';
if (true === $this->is_support_jsonp) {
$this->header('Content-type', 'application/javascript');
if (!$this->isDebug()) {
$this->header('Content-type', 'application/javascript');
}
$callback_key = 'jsonpcallback';
$callback = $_GET[$callback_key];
if ($callback) {
......@@ -87,7 +108,7 @@ class ResponseMsg
$json = $callback . '(' . $json . ')';
}
}
if (!$callback) {
if (!$callback && !$this->isDebug()) {
$this->header('Content-type', 'application/json');
}
return $json;
......@@ -106,10 +127,14 @@ class ResponseMsg
}
if ($callback) {
$callback = Html::encode($callback_key);
$this->header('Content-type', 'application/javascript');
if (!$this->isDebug()) {
$this->header('Content-type', 'application/javascript');
}
return $callback . '(' . $json_str . ')';
} else {
$this->header('Content-type', 'application/json');
if (!$this->isDebug()) {
$this->header('Content-type', 'application/json');
}
return $json_str;
}
}
......@@ -127,10 +152,14 @@ class ResponseMsg
}
if ($callback) {
$callback = Html::encode($callback_key);
$this->header('Content-type', 'application/javascript');
if (!$this->isDebug()) {
$this->header('Content-type', 'application/javascript');
}
return $callback . '(' . json_encode($arr) . ')';
} else {
$this->header('Content-type', 'application/json');
if (!$this->isDebug()) {
$this->header('Content-type', 'application/json');
}
return json_encode($arr);
}
}
......@@ -148,10 +177,14 @@ class ResponseMsg
$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');
if (!$this->isDebug()) {
$this->header('Content-type', 'application/javascript');
}
return $callback . '(' . json_encode($arr) . ')';
} else {
$this->header('Content-type', 'application/json');
if (!$this->isDebug()) {
$this->header('Content-type', 'application/json');
}
return json_encode($arr);
}
}
......@@ -174,14 +207,28 @@ class ResponseMsg
$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');
if (!$this->isDebug()) {
$this->header('Content-type', 'application/javascript');
}
return $callback . '(' . json_encode($arr) . ')';
} else {
$this->header('Content-type', 'application/json');
if (!$this->isDebug()) {
$this->header('Content-type', 'application/json');
}
return json_encode($arr);
}
}
/**
* 解决xdebug cookie设置不了的问题
*/
private function isDebug()
{
if (defined('SERVICE_ENV') && (SERVICE_ENV === 'test' || SERVICE_ENV === 'local') && isset($_GET['debug'])) {
return true;
}
return false;
}
public function header($key, $value)
{
......
......@@ -6,6 +6,7 @@ use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\web\Response;
use fpf\response\ResponseMsg;
class Application extends Component
{
......@@ -115,20 +116,25 @@ class Application extends Component
$set_cookie_val = Yii::$app->request->get('set_cookie_val', '');
if ($set_cookie_val) {
setcookie(VERSION_KEY, $set_cookie_val, time() + 3600 * 24 * 7);
\Yii::$app->response->format = Response::FORMAT_RAW;
\Yii::$app->response->headers->add('Content-type', 'application/javascript;charset=utf-8');
\Yii::$app->response->send();
$header_arr = ['Content-type' => 'application/javascript;charset=utf-8'];
$header_arr = array_merge($header_arr, ResponseMsg::getDefaultHeader());
echo response()->withHeaders($header_arr)->withContent('');
return;
}
$header_arr = [];
$user_ip = Yii::$app->request->userIP;
$host_name = $_SERVER['SERVER_NAME'];
$is_online_host = $this->isOnlineHost($host_name);
ob_start();
if ($this->isAllowIp($user_ip)) {
\Yii::$app->response->headers->add('Content-type', 'text/html');
\Yii::$app->response->send();
$header_arr['Content-type'] = 'text/html';
$header_arr = array_merge($header_arr, ResponseMsg::getDefaultHeader());
// 开关开启,用户IP是公司IP,内外IP,允许打开cookie编辑
include(__DIR__ . '/views/CookieEdit.php');
}
$content = ob_get_contents();
ob_end_clean();
echo response()->withHeaders($header_arr)->withContent($content);
}
private function isOnlineHost($host_name)
......
......@@ -7,6 +7,7 @@ class response
foreach ($header_arr as $key => $val) {
\Yii::$app->response->headers->add($key, $val);
}
\Yii::$app->response->send();
return $this;
}
public function withContent($content)
......
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