提交thinkPHP

This commit is contained in:
2025-08-08 11:25:39 +08:00
parent ca1fed3588
commit 026e8d23a5
2987 changed files with 484280 additions and 0 deletions

View File

@@ -0,0 +1,167 @@
<?php
/**
* 用法:
* load_trait('controller/Jump');
* class index
* {
* use \traits\controller\Jump;
* public function index(){
* $this->error();
* $this->redirect();
* }
* }
*/
namespace traits\controller;
use think\Config;
use think\exception\HttpResponseException;
use think\Request;
use think\Response;
use think\response\Redirect;
use think\Url;
use think\View as ViewTemplate;
trait Jump
{
/**
* 操作成功跳转的快捷方法
* @access protected
* @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 = [])
{
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' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
if ('html' == strtolower($type)) {
$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 int $wait 跳转等待时间
* @param array $header 发送的 Header 信息
* @return void
* @throws HttpResponseException
*/
protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
{
if (is_null($url)) {
$url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
$url = Url::build($url);
}
$type = $this->getResponseType();
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
if ('html' == strtolower($type)) {
$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 数据到客户端
* @access protected
* @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' => Request::instance()->server('REQUEST_TIME'),
'data' => $data,
];
$type = $type ?: $this->getResponseType();
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* URL 重定向
* @access protected
* @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 = [])
{
if (is_integer($params)) {
$code = $params;
$params = [];
}
$response = new Redirect($url);
$response->code($code)->params($params)->with($with);
throw new HttpResponseException($response);
}
/**
* 获取当前的 response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
return Request::instance()->isAjax()
? Config::get('default_ajax_return')
: Config::get('default_return_type');
}
}

View File

@@ -0,0 +1,200 @@
<?php
namespace traits\model;
use think\Collection;
use think\db\Query;
use think\Model;
/**
* @mixin \Think\Model
*/
trait SoftDelete
{
/**
* 判断当前实例是否被软删除
* @access public
* @return boolean
*/
public function trashed()
{
$field = $this->getDeleteTimeField();
if ($field && !empty($this->data[$field])) {
return true;
}
return false;
}
/**
* 查询包含软删除的数据
* @access public
* @return Query
*/
public static function withTrashed()
{
return (new static )->getQuery();
}
/**
* 只查询软删除数据
* @access public
* @return Query
*/
public static function onlyTrashed()
{
$model = new static();
$field = $model->getDeleteTimeField(true);
if ($field) {
return $model->getQuery()->useSoftDelete($field, ['not null', '']);
} else {
return $model->getQuery();
}
}
/**
* 删除当前的记录
* @access public
* @param bool $force 是否强制删除
* @return integer
*/
public function delete($force = false)
{
if (false === $this->trigger('before_delete', $this)) {
return false;
}
$name = $this->getDeleteTimeField();
if ($name && !$force) {
// 软删除
$this->data[$name] = $this->autoWriteTimestamp($name);
$result = $this->isUpdate()->save();
} else {
// 强制删除当前模型数据
$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 bool $force 是否强制删除
* @return integer 成功删除的记录数
*/
public static function destroy($data, $force = false)
{
if (is_null($data)) {
return 0;
}
// 包含软删除数据
$query = (new static())->db(false);
if (is_array($data) && key($data) !== 0) {
$query->where($data);
$data = null;
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $query]);
$data = null;
}
$count = 0;
if ($resultSet = $query->select($data)) {
foreach ($resultSet as $data) {
$result = $data->delete($force);
$count += $result;
}
}
return $count;
}
/**
* 恢复被软删除的记录
* @access public
* @param array $where 更新条件
* @return integer
*/
public function restore($where = [])
{
if (empty($where)) {
$pk = $this->getPk();
$where[$pk] = $this->getData($pk);
}
$name = $this->getDeleteTimeField();
if ($name) {
// 恢复删除
return $this->getQuery()
->useSoftDelete($name, ['not null', ''])
->where($where)
->update([$name => null]);
} else {
return 0;
}
}
/**
* 查询默认不包含软删除数据
* @access protected
* @param Query $query 查询对象
* @return Query
*/
protected function base($query)
{
$field = $this->getDeleteTimeField(true);
return $field ? $query->useSoftDelete($field) : $query;
}
/**
* 获取软删除字段
* @access public
* @param bool $read 是否查询操作(写操作的时候会自动去掉表别名)
* @return string
*/
protected function getDeleteTimeField($read = false)
{
$field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ?
$this->deleteTime :
'delete_time';
if (false === $field) {
return false;
}
if (!strpos($field, '.')) {
$field = '__TABLE__.' . $field;
}
if (!$read && strpos($field, '.')) {
$array = explode('.', $field);
$field = array_pop($array);
}
return $field;
}
}

View File

@@ -0,0 +1,54 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace traits\think;
use think\Exception;
trait Instance
{
/**
* @var null|static 实例对象
*/
protected static $instance = null;
/**
* 获取示例
* @param array $options 实例配置
* @return static
*/
public static function instance($options = [])
{
if (is_null(self::$instance)) self::$instance = new self($options);
return self::$instance;
}
/**
* 静态调用
* @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();
$call = substr($method, 1);
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);
}
}