更新tp5内核

This commit is contained in:
2018-01-02 23:03:31 +08:00
parent 590696a06b
commit 3818619504
99 changed files with 3362 additions and 2006 deletions
+58 -49
View File
@@ -1,5 +1,4 @@
<?php
/**
* 用法:
* load_trait('controller/Jump');
@@ -27,132 +26,142 @@ trait Jump
/**
* 操作成功跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @param mixed $msg 提示信息
* @param string $url 跳转的 URL 地址
* @param mixed $data 返回的数据
* @param int $wait 跳转等待时间
* @param array $header 发送的 Header 信息
* @return void
* @throws HttpResponseException
*/
protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{
$code = 1;
if (is_numeric($msg)) {
$code = $msg;
$msg = '';
}
if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
$url = $_SERVER["HTTP_REFERER"];
} elseif ('' !== $url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
if (is_null($url) && !is_null(Request::instance()->server('HTTP_REFERER'))) {
$url = Request::instance()->server('HTTP_REFERER');
} elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
$url = Url::build($url);
}
$type = $this->getResponseType();
$result = [
'code' => $code,
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = $this->getResponseType();
if ('html' == strtolower($type)) {
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
$template = Config::get('template');
$view = Config::get('view_replace_str');
$result = ViewTemplate::instance($template, $view)
->fetch(Config::get('dispatch_success_tmpl'), $result);
}
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* 操作错误跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @param mixed $msg 提示信息
* @param string $url 跳转的 URL 地址
* @param mixed $data 返回的数据
* @param int $wait 跳转等待时间
* @param array $header 发送的 Header 信息
* @return void
* @throws HttpResponseException
*/
protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{
$code = 0;
if (is_numeric($msg)) {
$code = $msg;
$msg = '';
}
if (is_null($url)) {
$url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ('' !== $url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
} elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
$url = Url::build($url);
}
$type = $this->getResponseType();
$result = [
'code' => $code,
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = $this->getResponseType();
if ('html' == strtolower($type)) {
$result = ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
$template = Config::get('template');
$view = Config::get('view_replace_str');
$result = ViewTemplate::instance($template, $view)
->fetch(Config::get('dispatch_error_tmpl'), $result);
}
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* 返回封装后的API数据到客户端
* 返回封装后的 API 数据到客户端
* @access protected
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @param mixed $data 要返回的数据
* @param int $code 返回的 code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的 Header 信息
* @return void
* @throws HttpResponseException
*/
protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => $_SERVER['REQUEST_TIME'],
'time' => Request::instance()->server('REQUEST_TIME'),
'data' => $data,
];
$type = $type ?: $this->getResponseType();
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* URL重定向
* URL 重定向
* @access protected
* @param string $url 跳转的URL表达式
* @param array|integer $params 其它URL参数
* @param integer $code http code
* @param array $with 隐式传参
* @param string $url 跳转的 URL 表达式
* @param array|int $params 其它 URL 参数
* @param int $code http code
* @param array $with 隐式传参
* @return void
* @throws HttpResponseException
*/
protected function redirect($url, $params = [], $code = 302, $with = [])
{
$response = new Redirect($url);
if (is_integer($params)) {
$code = $params;
$params = [];
}
$response = new Redirect($url);
$response->code($code)->params($params)->with($with);
throw new HttpResponseException($response);
}
/**
* 获取当前的response 输出类型
* 获取当前的 response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
$isAjax = Request::instance()->isAjax();
return $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
return Request::instance()->isAjax()
? Config::get('default_ajax_return')
: Config::get('default_return_type');
}
}
+53 -25
View File
@@ -3,10 +3,13 @@
namespace traits\model;
use think\db\Query;
use think\Model;
/**
* @mixin \Think\Model
*/
trait SoftDelete
{
/**
* 判断当前实例是否被软删除
* @access public
@@ -15,22 +18,18 @@ trait SoftDelete
public function trashed()
{
$field = $this->getDeleteTimeField();
if (!empty($this->data[$field])) {
return true;
}
return false;
return !empty($this->data[$field]);
}
/**
* 查询软删除数据
* 查询包含软删除数据
* @access public
* @return Query
*/
public static function withTrashed()
{
$model = new static();
$field = $model->getDeleteTimeField(true);
return $model->getQuery();
return (new static )->getQuery();
}
/**
@@ -42,14 +41,14 @@ trait SoftDelete
{
$model = new static();
$field = $model->getDeleteTimeField(true);
return $model->getQuery()
->useSoftDelete($field, ['not null', '']);
return $model->getQuery()->useSoftDelete($field, ['not null', '']);
}
/**
* 删除当前的记录
* @access public
* @param bool $force 是否强制删除
* @param bool $force 是否强制删除
* @return integer
*/
public function delete($force = false)
@@ -57,28 +56,53 @@ trait SoftDelete
if (false === $this->trigger('before_delete', $this)) {
return false;
}
$name = $this->getDeleteTimeField();
if (!$force) {
// 软删除
$this->data[$name] = $this->autoWriteTimestamp($name);
$result = $this->isUpdate()->save();
} else {
$result = $this->getQuery()->delete($this->data);
// 强制删除当前模型数据
$result = $this->getQuery()->where($this->getWhere())->delete();
}
// 关联删除
if (!empty($this->relationWrite)) {
foreach ($this->relationWrite as $key => $name) {
$name = is_numeric($key) ? $name : $key;
$result = $this->getRelation($name);
if ($result instanceof Model) {
$result->delete();
} elseif ($result instanceof Collection || is_array($result)) {
foreach ($result as $model) {
$model->delete();
}
}
}
}
$this->trigger('after_delete', $this);
// 清空原始数据
$this->origin = [];
return $result;
}
/**
* 删除记录
* @access public
* @param mixed $data 主键列表 支持闭包查询条件
* @param mixed $data 主键列表(支持闭包查询条件)
* @param bool $force 是否强制删除
* @return integer 成功删除的记录数
*/
public static function destroy($data, $force = false)
{
if (is_null($data)) {
return 0;
}
// 包含软删除数据
$query = self::withTrashed();
if (is_array($data) && key($data) !== 0) {
@@ -87,18 +111,16 @@ trait SoftDelete
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $query]);
$data = null;
} elseif (is_null($data)) {
return 0;
}
$resultSet = $query->select($data);
$count = 0;
if ($resultSet) {
$count = 0;
if ($resultSet = $query->select($data)) {
foreach ($resultSet as $data) {
$result = $data->delete($force);
$count += $result;
}
}
return $count;
}
@@ -110,11 +132,13 @@ trait SoftDelete
*/
public function restore($where = [])
{
$name = $this->getDeleteTimeField();
if (empty($where)) {
$pk = $this->getPk();
$where[$pk] = $this->getData($pk);
}
$name = $this->getDeleteTimeField();
// 恢复删除
return $this->getQuery()
->useSoftDelete($name, ['not null', ''])
@@ -126,30 +150,34 @@ trait SoftDelete
* 查询默认不包含软删除数据
* @access protected
* @param Query $query 查询对象
* @return void
* @return Query
*/
protected function base($query)
{
$field = $this->getDeleteTimeField(true);
$query->useSoftDelete($field);
return $query->useSoftDelete($this->getDeleteTimeField(true));
}
/**
* 获取软删除字段
* @access public
* @param bool $read 是否查询操作 写操作的时候会自动去掉表别名
* @param bool $read 是否查询操作(写操作的时候会自动去掉表别名)
* @return string
*/
protected function getDeleteTimeField($read = false)
{
$field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ? $this->deleteTime : 'delete_time';
$field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ?
$this->deleteTime :
'delete_time';
if (!strpos($field, '.')) {
$field = '__TABLE__.' . $field;
}
if (!$read && strpos($field, '.')) {
$array = explode('.', $field);
$field = array_pop($array);
}
return $field;
}
}
+22 -13
View File
@@ -2,7 +2,7 @@
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
@@ -15,31 +15,40 @@ use think\Exception;
trait Instance
{
/**
* @var null|static 实例对象
*/
protected static $instance = null;
/**
* @param array $options
* 获取示例
* @param array $options 实例配置
* @return static
*/
public static function instance($options = [])
{
if (is_null(self::$instance)) {
self::$instance = new self($options);
}
if (is_null(self::$instance)) self::$instance = new self($options);
return self::$instance;
}
// 静态调用
public static function __callStatic($method, $params)
/**
* 静态调用
* @param string $method 调用方法
* @param array $params 调用参数
* @return mixed
* @throws Exception
*/
public static function __callStatic($method, array $params)
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
if (is_null(self::$instance)) self::$instance = new self();
$call = substr($method, 1);
if (0 === strpos($method, '_') && is_callable([self::$instance, $call])) {
return call_user_func_array([self::$instance, $call], $params);
} else {
if (0 !== strpos($method, '_') || !is_callable([self::$instance, $call])) {
throw new Exception("method not exists:" . $method);
}
return call_user_func_array([self::$instance, $call], $params);
}
}