You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
51
thinkphp/library/think/response/Json.php
Executable file
51
thinkphp/library/think/response/Json.php
Executable file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Response;
|
||||
|
||||
class Json extends Response
|
||||
{
|
||||
// 输出参数
|
||||
protected $options = [
|
||||
'json_encode_param' => JSON_UNESCAPED_UNICODE,
|
||||
];
|
||||
|
||||
protected $contentType = 'application/json';
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function output($data)
|
||||
{
|
||||
try {
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
$data = json_encode($data, $this->options['json_encode_param']);
|
||||
|
||||
if ($data === false) {
|
||||
throw new \InvalidArgumentException(json_last_error_msg());
|
||||
}
|
||||
|
||||
return $data;
|
||||
} catch (\Exception $e) {
|
||||
if ($e->getPrevious()) {
|
||||
throw $e->getPrevious();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
58
thinkphp/library/think/response/Jsonp.php
Executable file
58
thinkphp/library/think/response/Jsonp.php
Executable file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Request;
|
||||
use think\Response;
|
||||
|
||||
class Jsonp extends Response
|
||||
{
|
||||
// 输出参数
|
||||
protected $options = [
|
||||
'var_jsonp_handler' => 'callback',
|
||||
'default_jsonp_handler' => 'jsonpReturn',
|
||||
'json_encode_param' => JSON_UNESCAPED_UNICODE,
|
||||
];
|
||||
|
||||
protected $contentType = 'application/javascript';
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function output($data)
|
||||
{
|
||||
try {
|
||||
// 返回JSON数据格式到客户端 包含状态信息 [当url_common_param为false时是无法获取到$_GET的数据的,故使用Request来获取<xiaobo.sun@qq.com>]
|
||||
$var_jsonp_handler = Request::instance()->param($this->options['var_jsonp_handler'], "");
|
||||
$handler = !empty($var_jsonp_handler) ? $var_jsonp_handler : $this->options['default_jsonp_handler'];
|
||||
|
||||
$data = json_encode($data, $this->options['json_encode_param']);
|
||||
|
||||
if ($data === false) {
|
||||
throw new \InvalidArgumentException(json_last_error_msg());
|
||||
}
|
||||
|
||||
$data = $handler . '(' . $data . ');';
|
||||
return $data;
|
||||
} catch (\Exception $e) {
|
||||
if ($e->getPrevious()) {
|
||||
throw $e->getPrevious();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
105
thinkphp/library/think/response/Redirect.php
Executable file
105
thinkphp/library/think/response/Redirect.php
Executable file
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Request;
|
||||
use think\Response;
|
||||
use think\Session;
|
||||
use think\Url;
|
||||
|
||||
class Redirect extends Response
|
||||
{
|
||||
|
||||
protected $options = [];
|
||||
|
||||
// URL参数
|
||||
protected $params = [];
|
||||
|
||||
public function __construct($data = '', $code = 302, array $header = [], array $options = [])
|
||||
{
|
||||
parent::__construct($data, $code, $header, $options);
|
||||
$this->cacheControl('no-cache,must-revalidate');
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return mixed
|
||||
*/
|
||||
protected function output($data)
|
||||
{
|
||||
$this->header['Location'] = $this->getTargetUrl();
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重定向传值(通过Session)
|
||||
* @access protected
|
||||
* @param string|array $name 变量名或者数组
|
||||
* @param mixed $value 值
|
||||
* @return $this
|
||||
*/
|
||||
public function with($name, $value = null)
|
||||
{
|
||||
if (is_array($name)) {
|
||||
foreach ($name as $key => $val) {
|
||||
Session::flash($key, $val);
|
||||
}
|
||||
} else {
|
||||
Session::flash($name, $value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取跳转地址
|
||||
* @return string
|
||||
*/
|
||||
public function getTargetUrl()
|
||||
{
|
||||
if (strpos($this->data, '://') || (0 === strpos($this->data, '/') && empty($this->params))) {
|
||||
return $this->data;
|
||||
} else {
|
||||
return Url::build($this->data, $this->params);
|
||||
}
|
||||
}
|
||||
|
||||
public function params($params = [])
|
||||
{
|
||||
$this->params = $params;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记住当前url后跳转
|
||||
* @return $this
|
||||
*/
|
||||
public function remember()
|
||||
{
|
||||
Session::set('redirect_url', Request::instance()->url());
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转到上次记住的url
|
||||
* @return $this
|
||||
*/
|
||||
public function restore()
|
||||
{
|
||||
if (Session::has('redirect_url')) {
|
||||
$this->data = Session::get('redirect_url');
|
||||
Session::delete('redirect_url');
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
89
thinkphp/library/think/response/View.php
Executable file
89
thinkphp/library/think/response/View.php
Executable file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Config;
|
||||
use think\Response;
|
||||
use think\View as ViewTemplate;
|
||||
|
||||
class View extends Response
|
||||
{
|
||||
// 输出参数
|
||||
protected $options = [];
|
||||
protected $vars = [];
|
||||
protected $replace = [];
|
||||
protected $contentType = 'text/html';
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return mixed
|
||||
*/
|
||||
protected function output($data)
|
||||
{
|
||||
// 渲染模板输出
|
||||
return ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||
->fetch($data, $this->vars, $this->replace);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取视图变量
|
||||
* @access public
|
||||
* @param string $name 模板变量
|
||||
* @return mixed
|
||||
*/
|
||||
public function getVars($name = null)
|
||||
{
|
||||
if (is_null($name)) {
|
||||
return $this->vars;
|
||||
} else {
|
||||
return isset($this->vars[$name]) ? $this->vars[$name] : null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板变量赋值
|
||||
* @access public
|
||||
* @param mixed $name 变量名
|
||||
* @param mixed $value 变量值
|
||||
* @return $this
|
||||
*/
|
||||
public function assign($name, $value = '')
|
||||
{
|
||||
if (is_array($name)) {
|
||||
$this->vars = array_merge($this->vars, $name);
|
||||
return $this;
|
||||
} else {
|
||||
$this->vars[$name] = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 视图内容替换
|
||||
* @access public
|
||||
* @param string|array $content 被替换内容(支持批量替换)
|
||||
* @param string $replace 替换内容
|
||||
* @return $this
|
||||
*/
|
||||
public function replace($content, $replace = '')
|
||||
{
|
||||
if (is_array($content)) {
|
||||
$this->replace = array_merge($this->replace, $content);
|
||||
} else {
|
||||
$this->replace[$content] = $replace;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
102
thinkphp/library/think/response/Xml.php
Executable file
102
thinkphp/library/think/response/Xml.php
Executable file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Collection;
|
||||
use think\Model;
|
||||
use think\Response;
|
||||
|
||||
class Xml extends Response
|
||||
{
|
||||
// 输出参数
|
||||
protected $options = [
|
||||
// 根节点名
|
||||
'root_node' => 'think',
|
||||
// 根节点属性
|
||||
'root_attr' => '',
|
||||
//数字索引的子节点名
|
||||
'item_node' => 'item',
|
||||
// 数字索引子节点key转换的属性名
|
||||
'item_key' => 'id',
|
||||
// 数据编码
|
||||
'encoding' => 'utf-8',
|
||||
];
|
||||
|
||||
protected $contentType = 'text/xml';
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return mixed
|
||||
*/
|
||||
protected function output($data)
|
||||
{
|
||||
// XML数据转换
|
||||
return $this->xmlEncode($data, $this->options['root_node'], $this->options['item_node'], $this->options['root_attr'], $this->options['item_key'], $this->options['encoding']);
|
||||
}
|
||||
|
||||
/**
|
||||
* XML编码
|
||||
* @param mixed $data 数据
|
||||
* @param string $root 根节点名
|
||||
* @param string $item 数字索引的子节点名
|
||||
* @param string $attr 根节点属性
|
||||
* @param string $id 数字索引子节点key转换的属性名
|
||||
* @param string $encoding 数据编码
|
||||
* @return string
|
||||
*/
|
||||
protected function xmlEncode($data, $root, $item, $attr, $id, $encoding)
|
||||
{
|
||||
if (is_array($attr)) {
|
||||
$array = [];
|
||||
foreach ($attr as $key => $value) {
|
||||
$array[] = "{$key}=\"{$value}\"";
|
||||
}
|
||||
$attr = implode(' ', $array);
|
||||
}
|
||||
$attr = trim($attr);
|
||||
$attr = empty($attr) ? '' : " {$attr}";
|
||||
$xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
|
||||
$xml .= "<{$root}{$attr}>";
|
||||
$xml .= $this->dataToXml($data, $item, $id);
|
||||
$xml .= "</{$root}>";
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据XML编码
|
||||
* @param mixed $data 数据
|
||||
* @param string $item 数字索引时的节点名称
|
||||
* @param string $id 数字索引key转换为的属性名
|
||||
* @return string
|
||||
*/
|
||||
protected function dataToXml($data, $item, $id)
|
||||
{
|
||||
$xml = $attr = '';
|
||||
|
||||
if ($data instanceof Collection || $data instanceof Model) {
|
||||
$data = $data->toArray();
|
||||
}
|
||||
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
$id && $attr = " {$id}=\"{$key}\"";
|
||||
$key = $item;
|
||||
}
|
||||
$xml .= "<{$key}{$attr}>";
|
||||
$xml .= (is_array($val) || is_object($val)) ? $this->dataToXml($val, $item, $id) : $val;
|
||||
$xml .= "</{$key}>";
|
||||
}
|
||||
return $xml;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user