Commit 70f31f44 authored by tufengqi's avatar tufengqi

修复yii swoft cookie_edit 兼容文件

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