diff --git a/.gitignore b/.gitignore index 8295ea5c7..fce675338 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ /template/uni-app/unpackage/ /template/uni-app/.hbuilderx/ crmeb/.idea/ +crmeb/backup/ diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php b/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php index f22f1895d..1fd145b83 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemCrud.php @@ -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,82 @@ 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'; + if (is_file($zipName)) { + unlink($zipName); + } + $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', + ], 300); + return app('json')->success(['download_url' => sys_config('site_url') . '/adminapi/download/' . $key]); + } + /** * @return string * @author 等风来 @@ -304,26 +381,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; } } diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemMenus.php b/crmeb/app/adminapi/controller/v1/setting/SystemMenus.php index 08125597f..93db4b4a8 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemMenus.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemMenus.php @@ -50,13 +50,13 @@ class SystemMenus extends AuthController } /** - * @author 等风来 - * @email 136327134@qq.com - * @date 2023/4/14 * @return \think\Response * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/14 */ public function unique() { @@ -139,6 +139,8 @@ class SystemMenus extends AuthController 'unique_auth' => $menu['unique_auth'] ?? '', 'api_url' => $menu['api_url'], 'pid' => $menu['path'], + 'auth_type' => 2, + 'is_show' => 0, ]; } @@ -277,7 +279,7 @@ class SystemMenus extends AuthController if ($menuApiList) $menuApiList = array_column($menuApiList, 'rule'); $list = []; foreach ($ruleList as $item) { - if (!in_array($item['path'] . '_' . strtolower($item['method']), $menuApiList)) { + if ($item['type'] || !in_array($item['path'] . '_' . strtolower($item['method']), $menuApiList)) { $item['real_name'] = $item['name'] ?? ''; $item['method'] = strtoupper($item['method']); $list[] = $item; diff --git a/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php b/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php index ef3488c56..9e9eb4ee3 100644 --- a/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php +++ b/crmeb/app/adminapi/controller/v1/setting/SystemRoute.php @@ -158,7 +158,7 @@ class SystemRoute extends AuthController return app('json')->fail('缺少参数'); } - $this->services->delete($id); + $this->services->destroy($id); return app('json')->success('删除成功'); } diff --git a/crmeb/app/adminapi/route/system.php b/crmeb/app/adminapi/route/system.php index 77abb7dbb..75158915a 100644 --- a/crmeb/app/adminapi/route/system.php +++ b/crmeb/app/adminapi/route/system.php @@ -8,8 +8,8 @@ // +---------------------------------------------------------------------- // | Author: CRMEB Team // +---------------------------------------------------------------------- -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/:id', 'v1.setting.SystemCrud/download')->option(['real_name' => '下载生成的文件']); //获取CRUD列表 Route::get('crud/column_type', 'v1.setting.SystemCrud/columnType')->option(['real_name' => '获取CRUD列表']); //获取菜单TREE形数据 diff --git a/crmeb/app/dao/BaseDao.php b/crmeb/app/dao/BaseDao.php index 0379a78aa..343118314 100644 --- a/crmeb/app/dao/BaseDao.php +++ b/crmeb/app/dao/BaseDao.php @@ -253,6 +253,20 @@ abstract class BaseDao return $this->getModel()->where($where)->delete(); } + /** + * 删除记录 + * @param int $id + * @param bool $force + * @return bool + * @author 等风来 + * @email 136327134@qq.com + * @date 2023/4/15 + */ + public function destroy(int $id, bool $force = false) + { + return $this->getModel()->destroy($id, $force); + } + /** * 更新数据 * @param int|string|array $id diff --git a/crmeb/app/jobs/ProductCopyJob.php b/crmeb/app/jobs/ProductCopyJob.php index 8941bbc59..14ef48cc8 100644 --- a/crmeb/app/jobs/ProductCopyJob.php +++ b/crmeb/app/jobs/ProductCopyJob.php @@ -48,7 +48,7 @@ class ProductCopyJob extends BaseJobs $d_image = 'http://' . ltrim($image, '\//'); } $description_cache = CacheService::get('desc_images_' . $id); - if ($description_cache === null) { + if ($description_cache === null || $description_cache === '') { $description_cache = $description; CacheService::set('desc_images_count' . $id, 0); } @@ -64,7 +64,7 @@ class ProductCopyJob extends BaseJobs CacheService::set('desc_images_count' . $id, $desc_count); } } catch (\Throwable $e) { - Log::error('下载商品详情图片失败,失败原因:' . $e->getMessage()); + Log::error('下载商品详情图片失败,失败原因:' . $e->getMessage() . '_' . $e->getFile() . '_' . $e->getLine()); } return true; } @@ -86,9 +86,9 @@ class ProductCopyJob extends BaseJobs //下载图片 $res = $copyTaobao->downloadCopyImage($image); //获取缓存中的轮播图 - $slider_images = CacheService::get('slider_images_' . $id); + $slider_images = CacheService::get('slider_images_' . $id) ?? []; //缓存为null则赋值[] - if ($slider_images === null) $slider_images = []; + if ($slider_images === null || $slider_images === '') $slider_images = []; //将下载的图片插入数组 array_push($slider_images, $res); //如果$slider_images中图片数量和传入的$count相等,说明已经下载完成,写入商品表,如果不等则继续插入缓存 @@ -102,7 +102,7 @@ class ProductCopyJob extends BaseJobs CacheService::set('slider_images_' . $id, $slider_images); } } catch (\Throwable $e) { - Log::error('下载商品轮播图片失败,失败原因:' . $e->getMessage()); + Log::error('下载商品轮播图片失败,失败原因:' . $e->getMessage() . '_' . $e->getFile() . '_' . $e->getLine()); } return true; } diff --git a/crmeb/app/services/system/SystemCrudServices.php b/crmeb/app/services/system/SystemCrudServices.php index afa802c1e..fb735e3b5 100644 --- a/crmeb/app/services/system/SystemCrudServices.php +++ b/crmeb/app/services/system/SystemCrudServices.php @@ -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, config('app.crud_make', 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, [ diff --git a/crmeb/config/app.php b/crmeb/config/app.php index d132a1df2..09848e994 100644 --- a/crmeb/config/app.php +++ b/crmeb/config/app.php @@ -14,7 +14,9 @@ // +---------------------------------------------------------------------- use think\facade\Env; + defined('DS') || define('DS', DIRECTORY_SEPARATOR); + return [ // 应用地址 'app_host' => Env::get('app.host', ''), @@ -50,4 +52,6 @@ return [ 'admin_prefix' => 'admin', //后台前端模板根路径 'admin_template_path' => dirname(app()->getRootPath()) . DS . 'template' . DS . 'admin' . DS . 'src' . DS, + //在保存crud的是否是否直接生成文件 + 'crud_make' => false ]; diff --git a/crmeb/crmeb/services/FileService.php b/crmeb/crmeb/services/FileService.php index 47b979b59..7b0a5d8bc 100644 --- a/crmeb/crmeb/services/FileService.php +++ b/crmeb/crmeb/services/FileService.php @@ -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) { diff --git a/crmeb/crmeb/services/crud/Make.php b/crmeb/crmeb/services/crud/Make.php index 740ab0e08..5dd8752dd 100644 --- a/crmeb/crmeb/services/crud/Make.php +++ b/crmeb/crmeb/services/crud/Make.php @@ -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 = ''; } } diff --git a/crmeb/crmeb/services/crud/stubs/controller/delete.stub b/crmeb/crmeb/services/crud/stubs/controller/delete.stub index c73cb4d73..293b74fbb 100644 --- a/crmeb/crmeb/services/crud/stubs/controller/delete.stub +++ b/crmeb/crmeb/services/crud/stubs/controller/delete.stub @@ -10,7 +10,7 @@ return app('json')->fail(100100); } - if ($this->service->delete($id)) { + if ($this->service->destroy($id)) { return app('json')->success(100002); } else { return app('json')->success(100008); diff --git a/template/uni-app/pages/users/user_distribution_level/index.vue b/template/uni-app/pages/users/user_distribution_level/index.vue index a21ac9299..9602d00e9 100644 --- a/template/uni-app/pages/users/user_distribution_level/index.vue +++ b/template/uni-app/pages/users/user_distribution_level/index.vue @@ -1,595 +1,603 @@ - - - - -