mirror of
https://github.com/crmeb/CRMEB.git
synced 2026-10-29 05:50:50 +00:00
增加下载文件接口
This commit is contained in:
parent
8f0d0a6138
commit
3ad1b8f046
1
.gitignore
vendored
1
.gitignore
vendored
@ -20,3 +20,4 @@
|
||||
/template/uni-app/unpackage/
|
||||
/template/uni-app/.hbuilderx/
|
||||
crmeb/.idea/
|
||||
crmeb/backup/
|
||||
|
||||
@ -17,6 +17,7 @@ namespace app\adminapi\controller\v1\setting;
|
||||
use app\adminapi\controller\AuthController;
|
||||
use app\services\system\SystemCrudServices;
|
||||
use app\services\system\SystemMenusServices;
|
||||
use crmeb\services\CacheService;
|
||||
use crmeb\services\crud\Make;
|
||||
use crmeb\services\FileService;
|
||||
use crmeb\utils\Terminal;
|
||||
@ -290,6 +291,79 @@ class SystemCrud extends AuthController
|
||||
return app('json')->success('删除成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
* @param $id
|
||||
* @return Response
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/4/15
|
||||
*/
|
||||
public function download($id)
|
||||
{
|
||||
if (!$id) {
|
||||
return app('json')->fail('缺少参数');
|
||||
}
|
||||
|
||||
$info = $this->services->get($id);
|
||||
if (!$info) {
|
||||
return app('json')->fail('下载的文件不存在');
|
||||
}
|
||||
$zipPath = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name);
|
||||
$zipName = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name) . '.zip';
|
||||
$makePath = $info->make_path ?? [];
|
||||
foreach ($makePath as $key => $item) {
|
||||
if (in_array($key, ['pages', 'router', 'api'])) {
|
||||
$item = $zipPath . str_replace(dirname(app()->getRootPath()), '', Make::adminTemplatePath()) . $item;
|
||||
} else {
|
||||
$item = $zipPath . DS . $item;
|
||||
}
|
||||
$makePath[$key] = $item;
|
||||
}
|
||||
|
||||
$routeName = 'crud/' . Str::snake($info->table_name);
|
||||
|
||||
$column = $this->services->getColumnNamesList($info->table_name);
|
||||
$key = 'id';
|
||||
foreach ($column as $value) {
|
||||
if ($value['primaryKey']) {
|
||||
$key = $value['name'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$softDelete = false;
|
||||
|
||||
foreach ((array)$info->field['tableField'] as $item) {
|
||||
if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') {
|
||||
$softDelete = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->services->makeFile($info->table_name, $routeName, true, [
|
||||
'menuName' => $info->name,
|
||||
'key' => $key,
|
||||
'softDelete' => $softDelete,
|
||||
'fromField' => $info->field['fromField'] ?? [],
|
||||
'columnField' => $info->field['columnField'] ?? [],
|
||||
], $makePath);
|
||||
|
||||
if (!extension_loaded('zip')) {
|
||||
return app('json')->fail('请安装zip扩展后在尝试下载');
|
||||
}
|
||||
|
||||
$fileService = new FileService();
|
||||
$fileService->addZip($zipPath, $zipName, app()->getRootPath() . 'backup');
|
||||
|
||||
$key = md5($zipName);
|
||||
CacheService::set($key, [
|
||||
'path' => $zipName,
|
||||
'fileName' => Str::camel($info->table_name) . '.zip'
|
||||
]);
|
||||
return app()->success(['download_url' => sys_config('site_url') . '/adminapi/download/' . $key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @author 等风来
|
||||
@ -304,26 +378,27 @@ class SystemCrud extends AuthController
|
||||
|
||||
$adminPath = dirname($adminPath);
|
||||
|
||||
$dir = $adminPath . DS . 'node_modules';
|
||||
if (!is_dir($dir)) {
|
||||
$terminal->run('npm-install');
|
||||
try {
|
||||
$dir = $adminPath . DS . 'node_modules';
|
||||
if (!is_dir($dir)) {
|
||||
// $terminal->run('npm-install');
|
||||
}
|
||||
|
||||
// $res = $terminal->run('npm-build');
|
||||
} catch (\Throwable $e) {
|
||||
$terminal->echoOutputFlag($e->getMessage());
|
||||
}
|
||||
|
||||
$terminal->run('npm-build');
|
||||
|
||||
if (!is_dir($adminPath . DS . 'dist')) {
|
||||
return Response::create([
|
||||
echo Response::create([
|
||||
'message' => '打包失败',
|
||||
], 'json')->getContent();
|
||||
}
|
||||
|
||||
$build = public_path() . config('app.admin_prefix');
|
||||
|
||||
$this->app->make(FileService::class)->copyDir($adminPath . DS . 'dist', $build);
|
||||
// $this->app->make(FileService::class)->copyDir($adminPath . DS . 'dist', $build);
|
||||
|
||||
return Response::create([
|
||||
'message' => '打包成功',
|
||||
'success' => 'ok'
|
||||
], 'json')->getContent();
|
||||
// echo $res;
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,8 +8,8 @@
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
use think\facade\Route;
|
||||
|
||||
use think\facade\Route;
|
||||
|
||||
/**
|
||||
* 维护 相关路由
|
||||
@ -133,8 +133,8 @@ Route::group('system', function () {
|
||||
'delete' => '删除路由分类'
|
||||
],
|
||||
]);
|
||||
//执行重新打包
|
||||
Route::get('crud/npm', 'v1.setting.SystemCrud/npm')->option(['real_name' => '执行重新打包']);
|
||||
//下载生成的文件
|
||||
Route::get('crud/download', 'v1.setting.SystemCrud/download')->option(['real_name' => '下载生成的文件']);
|
||||
//获取CRUD列表
|
||||
Route::get('crud/column_type', 'v1.setting.SystemCrud/columnType')->option(['real_name' => '获取CRUD列表']);
|
||||
//获取菜单TREE形数据
|
||||
|
||||
@ -369,7 +369,7 @@ class SystemCrudServices extends BaseServices
|
||||
$menuIds = array_column($menus->toArray(), 'id');
|
||||
array_push($menuIds, $menuInfo->id);
|
||||
//生成文件
|
||||
$make = $this->makeFile($tableName, $routeName, true, $data, $filePath);
|
||||
$make = $this->makeFile($tableName, $routeName, false, $data, $filePath);
|
||||
$makePath = [];
|
||||
foreach ($make as $key => $item) {
|
||||
$makePath[$key] = $item['path'];
|
||||
@ -555,7 +555,6 @@ class SystemCrudServices extends BaseServices
|
||||
[$apiContent, $apiPath] = $viewApi->setFilePathName($filePath['api'] ?? '')->isMake($isMake)->handle($tableName, [
|
||||
'route' => $routeName,
|
||||
]);
|
||||
|
||||
//生成前台页面
|
||||
$viewPages = app()->make(ViewPages::class);
|
||||
[$pagesContent, $pagesPath] = $viewPages->setFilePathName($filePath['pages'] ?? '')->isMake($isMake)->handle($tableName, [
|
||||
|
||||
@ -1018,9 +1018,11 @@ class FileService
|
||||
$file = str_replace('\\', '/', $file);
|
||||
if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..'))) continue;
|
||||
if (is_dir($file) === true) {
|
||||
$zip->addEmptyDir(str_replace($folder . '/', '', $file . '/'));
|
||||
$fileName = $folder ? str_replace($folder . '/', '', $file . '/') : $file . '/';
|
||||
$zip->addEmptyDir($fileName);
|
||||
} else if (is_file($file) === true) {
|
||||
$zip->addFromString(str_replace($folder . '/', '', $file), file_get_contents($file));
|
||||
$fileName = $folder ? str_replace($folder . '/', '', $file) : $file;
|
||||
$zip->addFromString($fileName, file_get_contents($file));
|
||||
}
|
||||
}
|
||||
} else if (is_file($source) === true) {
|
||||
|
||||
@ -51,6 +51,18 @@ abstract class Make
|
||||
*/
|
||||
protected $fileBasePath;
|
||||
|
||||
/**
|
||||
* 文件内容
|
||||
* @var string
|
||||
*/
|
||||
protected $content = '';
|
||||
|
||||
/**
|
||||
* 文件存放
|
||||
* @var null
|
||||
*/
|
||||
protected $filePath = null;
|
||||
|
||||
/**
|
||||
* 变量名称
|
||||
* @var array
|
||||
@ -68,6 +80,12 @@ abstract class Make
|
||||
*/
|
||||
protected $isMake = true;
|
||||
|
||||
/**
|
||||
* 文件存在是否生成
|
||||
* @var bool
|
||||
*/
|
||||
protected $isExistsMake = true;
|
||||
|
||||
/**
|
||||
* 后台前端模板根路径
|
||||
* @var string
|
||||
@ -416,25 +434,19 @@ abstract class Make
|
||||
return 'app' . ($app ? '\\' . $app : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行创建文件
|
||||
* @return string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/3/13
|
||||
*/
|
||||
protected function makeFile(string $pathname, string $content)
|
||||
|
||||
public function make(string $pathname)
|
||||
{
|
||||
|
||||
$pathname = $this->filePathName ?: $pathname;
|
||||
|
||||
|
||||
$content = str_replace('', '', $content);
|
||||
$pathname = $this->filePathName ?: ($this->filePath ?: $pathname);
|
||||
|
||||
if ($this->isMake) {
|
||||
|
||||
if (is_file($pathname)) {
|
||||
throw new CrudException($this->name . ':' . $pathname . ' already exists!');
|
||||
if ($this->isExistsMake) {
|
||||
unlink($pathname);
|
||||
} else {
|
||||
throw new CrudException($this->name . ':' . $pathname . ' already exists!');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
@ -446,12 +458,44 @@ abstract class Make
|
||||
}
|
||||
|
||||
try {
|
||||
file_put_contents($pathname, $content);
|
||||
file_put_contents($pathname, $this->content);
|
||||
} catch (\Throwable $e) {
|
||||
throw new CrudException('CRUD生成文件报错,无法写入:' . $pathname);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
/**
|
||||
* 设置内容
|
||||
* @param string $content
|
||||
* @return array|string|string[]
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/4/14
|
||||
*/
|
||||
protected function setContent(string $content)
|
||||
{
|
||||
$this->content = str_replace('', '', $content);
|
||||
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行创建文件
|
||||
* @return string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/3/13
|
||||
*/
|
||||
protected function makeFile(string $pathname, string $content)
|
||||
{
|
||||
$this->content = str_replace('', '', $content);
|
||||
$this->make($pathname);
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->content = '';
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user