- 增加返回门面

- 文件增加方法
v6
Chaim 4 years ago
parent 1a3ff1510d
commit 59987bcfc3

@ -29,6 +29,8 @@ use think\Facade;
* @method helper delete(string $name) bool 删除文件
* @method helper deletes(string $name) bool 删除文件夹
* @method helper folderZip(string $name, string $suffix_name = '.png', string $file_name = '*') bool 把文件夹里面的文件打包成zip文件
* @method helper getFiles(string $path) array|string 获取目录下的所有文件和目录
* @method helper rmFiles(string $path) bool 删除目录下的文件
*/
class Files extends Facade
{

@ -0,0 +1,43 @@
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\facade;
use DtApp\ThinkLibrary\helper\Returns as helper;
use think\Facade;
/**
* 返回门面
* Class Returns
* @see \DtApp\ThinkLibrary\helper\Returns
* @package think\facade
* @mixin helper
*
* @method helper jsonSuccess(array $data = [], string $msg = 'success', int $code = 0) 返回Json-成功
* @method helper jsonError(string $msg = 'error', int $code = 1, array $data = []) 返回Json-错误
*/
class Returns extends Facade
{
/**
* 获取当前Facade对应类名或者已经绑定的容器对象标识
* @access protected
* @return string
*/
public static function getFacadeClass()
{
return helper::class;
}
}

@ -97,4 +97,56 @@ class Files
return false;
}
}
/**
* 获取目录下的所有文件和目录
* @param string $path
* @return array|string
*/
public function getFiles(string $path)
{
$files = [];
if (is_dir($path)) {
$path = dirname($path) . '/' . basename($path) . '/';
$file = dir($path);
while (false !== ($entry = $file->read())) {
if ($entry !== '.' && $entry !== '..') {
$cur = $path . $entry;
if (is_dir($cur)) {
$subPath = $cur . '/';
$this->getFiles($subPath);
}
$files[] = $cur;
}
}
$file->close();
return $files;
} else {
return [];
}
}
/**
* 删除目录下的文件
* @param string $path
* @return bool
*/
public function rmFiles(string $path)
{
$files = $this->getFiles($path);
if (!is_array($files)) {
return false;
} elseif (empty($files)) {
return false;
} else {
foreach ($files as $item => $file) {
if (is_dir($file)) {
rmdir($file);
} elseif (is_file($file)) {
unlink($file);
}
}
}
return true;
}
}

@ -0,0 +1,53 @@
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\helper;
use think\exception\HttpResponseException;
/**
* 返回管理类
* Class Returns
* @package DtApp\ThinkLibrary\helper
*/
class Returns
{
/**
* 返回Json-成功
* @param array $data 数据
* @param string $msg 描述
* @param int $code 状态
*/
public function jsonSuccess(array $data = [], string $msg = 'success', int $code = 0)
{
date_default_timezone_set('Asia/Shanghai');
header('Content-Type:application/json; charset=utf-8');
throw new HttpResponseException(json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => $data]));
}
/**
* 返回Json-错误
* @param string $msg 描述
* @param int $code 状态码
* @param array $data 数据
*/
public function jsonError(string $msg = 'error', int $code = 1, array $data = [])
{
date_default_timezone_set('Asia/Shanghai');
header('Content-Type:application/json; charset=utf-8');
throw new HttpResponseException(json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => $data]));
}
}
Loading…
Cancel
Save