Init Repo

This commit is contained in:
root
2019-09-06 23:53:10 +08:00
commit f0ef89dfbb
7905 changed files with 914138 additions and 0 deletions

View File

@ -0,0 +1,148 @@
<?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 integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
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) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
}
$result = [
'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'))
->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信息
* @return void
*/
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) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
}
$result = [
'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'))
->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 integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return void
*/
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|integer $params 其它URL参数
* @param integer $code http code
* @param array $with 隐式传参
* @return void
*/
protected function redirect($url, $params = [], $code = 302, $with = [])
{
$response = new Redirect($url);
if (is_integer($params)) {
$code = $params;
$params = [];
}
$response->code($code)->params($params)->with($with);
throw new HttpResponseException($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');
}
}

View File

@ -0,0 +1,175 @@
<?php
namespace traits\model;
use think\db\Query;
use think\Model;
/**
* @mixin \Think\Model
*/
trait SoftDelete
{
/**
* 判断当前实例是否被软删除
* @access public
* @return boolean
*/
public function trashed()
{
$field = $this->getDeleteTimeField();
if (!empty($this->data[$field])) {
return true;
}
return false;
}
/**
* 查询软删除数据
* @access public
* @return Query
*/
public static function withTrashed()
{
$model = new static();
$field = $model->getDeleteTimeField(true);
return $model->getQuery();
}
/**
* 只查询软删除数据
* @access public
* @return Query
*/
public static function onlyTrashed()
{
$model = new static();
$field = $model->getDeleteTimeField(true);
return $model->getQuery()
->useSoftDelete($field, ['not null', '']);
}
/**
* 删除当前的记录
* @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 (!$force) {
// 软删除
$this->data[$name] = $this->autoWriteTimestamp($name);
$result = $this->isUpdate()->save();
} else {
// 删除条件
$where = $this->getWhere();
// 删除当前模型数据
$result = $this->getQuery()->where($where)->delete();
}
// 关联删除
if (!empty($this->relationWrite)) {
foreach ($this->relationWrite as $key => $name) {
$name = is_numeric($key) ? $name : $key;
$model = $this->getAttr($name);
if ($model instanceof Model) {
$model->delete($force);
}
}
}
$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)
{
// 包含软删除数据
$query = self::withTrashed();
if (is_array($data) && key($data) !== 0) {
$query->where($data);
$data = null;
} 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) {
foreach ($resultSet as $data) {
$result = $data->delete($force);
$count += $result;
}
}
return $count;
}
/**
* 恢复被软删除的记录
* @access public
* @param array $where 更新条件
* @return integer
*/
public function restore($where = [])
{
$name = $this->getDeleteTimeField();
if (empty($where)) {
$pk = $this->getPk();
$where[$pk] = $this->getData($pk);
}
// 恢复删除
return $this->getQuery()
->useSoftDelete($name, ['not null', ''])
->where($where)
->update([$name => null]);
}
/**
* 查询默认不包含软删除数据
* @access protected
* @param Query $query 查询对象
* @return void
*/
protected function base($query)
{
$field = $this->getDeleteTimeField(true);
$query->useSoftDelete($field);
}
/**
* 获取软删除字段
* @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 (!strpos($field, '.')) {
$field = '__TABLE__.' . $field;
}
if (!$read && strpos($field, '.')) {
$array = explode('.', $field);
$field = array_pop($array);
}
return $field;
}
}

View File

@ -0,0 +1,45 @@
<?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 traits\think;
use think\Exception;
trait Instance
{
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;
}
// 静态调用
public static function __callStatic($method, $params)
{
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 {
throw new Exception("method not exists:" . $method);
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB