diff --git a/app/Controller/Admin/System.php b/app/Controller/Admin/System.php index 932c0a4..bb9a38c 100644 --- a/app/Controller/Admin/System.php +++ b/app/Controller/Admin/System.php @@ -21,6 +21,9 @@ use App\Request\Menu as mRequest; use App\Request\Post as pRequest; use App\Request\Role as rRequest; use App\Request\GenTable as gtRequest; +use App\Model\AssetCategory as acModel; +use App\Request\AssetCategory as acRequest; +use App\Model\Asset as asModel; use App\Utils\AppInfoHelper; use App\Utils\CpuHelper; use App\Utils\DiskInfoHelper; @@ -28,9 +31,11 @@ use App\Utils\Ip; use App\Utils\MemoryHelper; use App\Utils\Param; use App\Utils\RedisInfoHelper; +use App\Utils\Str; use App\Utils\SystemHelper; use Hyperf\Context\ApplicationContext; use Hyperf\DbConnection\Db; +use Hyperf\Filesystem\FilesystemFactory; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\DeleteMapping; use Hyperf\HttpServer\Annotation\GetMapping; @@ -595,6 +600,10 @@ class System extends Base $prefix = config("databases.default.prefix"); // 表名 $table_name = str_replace($prefix, "", $table['table_name']); + + preg_match_all('/([a-zA-Z])[a-zA-Z]*/', $table_name, $m); + $name1 = implode('', $m[1]); + $name2 = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $table_name)))); $module_name = $table['module_name']; $controller_name = $table['controller_name']; // 搜搜字段 @@ -623,11 +632,131 @@ class System extends Base $data = [ 'model.php' => $this->render->getContents('templates/model.php.twig', compact("table_name", "fields", "controller_name", "query_fields", "list_fields")), 'request.php' => $this->render->getContents('templates/request.php.twig', compact("controller_name", "insert_fields", "fields", "required_fields", "edit_fields")), - 'controller.php' => $this->render->getContents('templates/controller.php.twig', compact("controller_name", "module_name", "query_fields", "insert_fields", "edit_fields", "table_name")), + 'controller.php' => $this->render->getContents('templates/controller.php.twig', compact("controller_name", "module_name", "query_fields", "insert_fields", "edit_fields", "table_name", "name1", "name2")), 'api.ts' => $this->render->getContents('templates/api.ts.twig', compact('table_name')), 'index.vue' => $this->render->getContents('templates/index.vue.twig', compact('table_name', 'list_fields', 'query_fields')), 'save.vue' => $this->render->getContents('templates/save.vue.twig', compact('table_name', 'insert_fields', 'required_fields')) ]; return $this->success("模板信息", $data); } + + #[GetMapping(path: "asset_category/list")] + #[Auth(needAuth: false)] + public function assetCategoryList() + { + $param['belong_id'] = $this->account()['belong_id']; + $param['account_type'] = $this->account()['account_type']; + return $this->success("资源分类列表", acModel::list($param)); + } + + #[PostMapping(path: "asset_category/add")] + #[Auth(needAuth: false)] + public function assetCategoryAdd() + { + $request = $this->container->get(acRequest::class); + $request->scene('add')->validateResolved(); + $data = Param::only(["category_name", "pid" => 0, "rank"]); + $data['belong_id'] = $this->account()['belong_id']; + $data['account_type'] = $this->account()['account_type']; + return $this->toAjax(acModel::add($data)); + } + + #[PutMapping(path: "asset_category/edit")] + #[Auth(needAuth: false)] + public function assetCategoryEdit() + { + $request = $this->container->get(acRequest::class); + $request->scene('edit')->validateResolved(); + $data = Param::only(["category_id", "category_name", "pid" => 0, "rank"]); + return $this->toAjax(acModel::edit($data)); + } + + #[DeleteMapping(path: "asset_category/del")] + #[Auth(needAuth: false)] + public function assetCategoryDel() + { + $ids = $this->request->input("ids"); + return $this->toAjax(acModel::del($ids)); + } + + #[GetMapping(path: "asset/list")] + #[Auth(needAuth: false)] + public function assetList() + { + $param = Param::only(["category_id", "ori_name", "type", "limit" => 10]); + $param['belong_id'] = $this->account()['belong_id']; + $param['account_type'] = $this->account()['account_type']; + $param['account_id'] = $this->accountId(); + return $this->success("资源列表", asModel::list($param)); + } + + #[PutMapping(path: "asset/move")] + #[Auth(needAuth: false)] + public function assetMove() + { + $data = Param::only(["category_id", "ids" => []]); + return $this->toAjax(asModel::move($data)); + } + + #[PutMapping(path: "asset/edit")] + #[Auth(needAuth: false)] + public function assetEdit() + { + $data = Param::only(["asset_id", "ori_name", "category_id"]); + return $this->toAjax(asModel::edit($data)); + } + + #[DeleteMapping(path: "asset/del")] + #[Auth(needAuth: false)] + public function assetDel() + { + $ids = $this->request->input("ids"); + return $this->toAjax(asModel::del($ids)); + } + + // 文件上传 + #[PostMapping(path: "asset/upload")] + #[Auth(needAuth: false)] + public function assetUpload(FilesystemFactory $factory) + { + // 判断文件是否存在 + if (!$this->request->hasFile('file')) { + return $this->error('文件不存在!'); + } + $file = $this->request->file('file'); + // 移动文件到文件夹 + $path = 'upload' . DIRECTORY_SEPARATOR . date("Ym") . DIRECTORY_SEPARATOR . date("d") . DIRECTORY_SEPARATOR; + // 文件二进制信息 + $stream = fopen($file->getRealPath(), 'r'); + // 文件名称 + $filename = md5(date("YmdHis") . rand(1000, 9999)) . "." . $file->getExtension(); + // 获取文件存储位置信息 + $local = $factory->get('local'); + $local->writeStream($path . $filename, $stream); + fclose($stream); + + $fileData = [ + 'file_name' => $filename, + 'ori_name' => $file->getClientFilename(), + 'url' => $path . $filename, + 'type' => Str::fileType($file->getExtension()), + 'mime' => $file->getMimeType(), + 'size' => $file->getSize(), + 'create_time' => date("Y-m-d H:i:s") + ]; + + $category_id = $this->request->post('category_id', 0); + $fileData['account_type'] = $this->account()['account_type']; + $fileData['belong_id'] = $this->account()['belong_id']; + $fileData['category_id'] = $category_id; + $fileData['account_id'] = $this->accountId(); + + // 上传成功 记录 + $res = asModel::add($fileData); + $fileData['asset_id'] = $res; + if ($res) { + return $this->success("上传成功", $fileData); + } + return $this->error("上传失败"); + } } \ No newline at end of file diff --git a/app/Model/Asset.php b/app/Model/Asset.php new file mode 100644 index 0000000..4444556 --- /dev/null +++ b/app/Model/Asset.php @@ -0,0 +1,79 @@ + 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'account_id' => 'integer', 'category_id' => 'integer']; + + public static function list(array $param) + { + $model = (new self()); + if (isset($param['account_type']) && $param['account_type'] != '') { + $model = $model->where('account_type', $param['account_type']); + } + if (isset($param['belong_id']) && $param['belong_id'] != '') { + $model = $model->where('belong_id', $param['belong_id']); + } + if (isset($param['account_id']) && $param['account_id'] != '') { + $model = $model->where('account_id', $param['account_id']); + } + if (isset($param['category_id']) && $param['category_id'] != '') { + $model = $model->where('category_id', $param['category_id']); + } + + if (isset($param['ori_name']) && $param['ori_name'] != '') { + $model = $model->where('ori_name', "like", "%{$param['ori_name']}%"); + } + if (isset($param['type']) && $param['type'] != '') { + $model = $model->where('type', $param['type']); + } + return $model->orderByDesc("asset_id") + ->select(["asset_id", "file_name", "ori_name", "url", "type", "mime", "size", "create_time"]) + ->paginate((int)$param['limit']); + + } + + public static function move(array $data) + { + if (!isset($data['ids']) || count($data['ids']) == 0) { + return false; + } + return self::whereIn('asset_id', $data['ids'])->update([ + 'category_id' => $data['category_id'] + ]); + } +} diff --git a/app/Model/AssetCategory.php b/app/Model/AssetCategory.php new file mode 100644 index 0000000..c544df3 --- /dev/null +++ b/app/Model/AssetCategory.php @@ -0,0 +1,50 @@ + 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'pid' => 'integer', 'rank' => 'integer']; + + public static function list(array $param) + { + $model = (new self()); + if (isset($param['account_type']) && $param['account_type'] != '') { + $model = $model->where('account_type', $param['account_type']); + } + if (isset($param['belong_id']) && $param['belong_id'] != '') { + $model = $model->where('belong_id', $param['belong_id']); + } + return $model + ->select(["category_id", "category_name", "pid"]) + ->get(); + } +} diff --git a/app/Request/AssetCategory.php b/app/Request/AssetCategory.php new file mode 100644 index 0000000..811e15e --- /dev/null +++ b/app/Request/AssetCategory.php @@ -0,0 +1,48 @@ + ["category_name", "pid", "rank"], + 'edit' => ["category_name", "pid", "rank", "category_id"], + ]; + + /** + * Determine if the user is authorized to make this request. + */ + public function authorize(): bool + { + return true; + } + + /** + * Get the validation rules that apply to the request. + */ + public function rules(): array + { + return [ + 'category_id' => 'required', + 'category_name' => 'required', + 'account_type' => 'required', + 'belong_id' => 'required', + 'pid' => 'required', + 'rank' => 'required', + 'create_time' => 'required', + 'update_time' => 'required', + 'deleted_at' => 'required', + ]; + } + + public function messages(): array + { + return [ + 'category_name.required' => '分类名称必传!', + 'pid.required' => '上级分类必传!', + 'rank.required' => '排序必传!', + ]; + } +} diff --git a/app/Utils/Str.php b/app/Utils/Str.php index a094085..706ddf0 100644 --- a/app/Utils/Str.php +++ b/app/Utils/Str.php @@ -124,4 +124,34 @@ class Str return "text"; } } + + public static function fileType($ext): string + { + switch (strtolower($ext)) { + case 'bmp': + case 'jpg': + case 'jpeg': + case 'png': + case 'gif': + return "image"; + case 'avi': + case 'mp4': + case 'flv': + return "video"; + case 'doc': + case 'docx': + return "wold"; + case 'xls': + case 'xlsx': + return "excel"; + case 'ppt': + case 'pptx': + return "ppt"; + case 'mp3': + case 'wav': + return "audio"; + default: + return "other"; + } + } } \ No newline at end of file diff --git a/static/view/templates/controller.php.twig b/static/view/templates/controller.php.twig index 97f9279..4ad1006 100644 --- a/static/view/templates/controller.php.twig +++ b/static/view/templates/controller.php.twig @@ -6,8 +6,8 @@ namespace {{ module_name }}; use App\Annotation\Auth; -use App\Model\{{ controller_name }} as {{ table_name | slice(0, 1) }}Model; -use App\Request\{{ controller_name }} as {{ table_name | slice(0, 1) }}Request; +use App\Model\{{ controller_name }} as {{ name1 }}Model; +use App\Request\{{ controller_name }} as {{ name1 }}Request; use App\Utils\Param; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\DeleteMapping; @@ -20,52 +20,52 @@ class {{ controller_name }} extends Base { #[GetMapping(path: "{{ table_name }}/list")] #[Auth(auth: "{{ table_name }}:list")] - public function {{ table_name }}List() + public function {{ name2 }}List() { - $param = Param::only([{% for field in query_fields %}"{{ field.column_name }}"{% if not loop.last %}, {% endif %}{% endfor %}]); - return $this->success({{ table_name | slice(0, 1) }}Model::list($param)); + $param = Param::only([{% for field in query_fields %}"{{ field.column_name }}"{% if not loop.last %}, {% endif %}{% endfor %}, "limit"=>10]); + return $this->success({{ name1 }}Model::list($param)); } #[GetMapping(path: "{{ table_name }}/option")] #[Auth(needAuth: false)] - public function {{ table_name }}Option() + public function {{ name2 }}Option() { - return $this->success({{ table_name | slice(0, 1) }}Model::options()); + return $this->success({{ name1 }}Model::options()); } #[GetMapping(path: "{{ table_name }}/info")] #[Auth(auth: "{{ table_name }}:info")] - public function {{ table_name }}Info() + public function {{ name2 }}Info() { $id = $this->request->input("id"); - return $this->success({{ table_name | slice(0, 1) }}Model::getById()); + return $this->success({{ name1 }}Model::getById()); } #[PostMapping(path: "{{ table_name }}/add")] #[Auth(auth: "{{ table_name }}:add")] - public function {{ table_name }}Add() + public function {{ name2 }}Add() { - $request = $this->container->get({{ table_name | slice(0, 1)}}Request::class); + $request = $this->container->get({{ name1 }}Request::class); $request->scene('add')->validateResolved(); $data = Param::only([{% for field in insert_fields %}"{{ field.column_name }}"{% if not loop.last %}, {% endif %}{% endfor %}]); - return $this->toAjax({{ table_name | slice(0, 1) }}Model::add($data)); + return $this->toAjax({{ name1 }}Model::add($data)); } #[PutMapping(path: "{{ table_name }}/edit")] #[Auth(auth: "{{ table_name }}:edit")] - public function {{ table_name }}Edit() + public function {{ name2 }}Edit() { - $request = $this->container->get({{ table_name | slice(0, 1)}}Request::class); + $request = $this->container->get({{ name1 }}Request::class); $request->scene('edit')->validateResolved(); $data = Param::only([{% for field in edit_fields %}"{{ field.column_name }}"{% if not loop.last %}, {% endif %}{% endfor %}]); - return $this->toAjax({{ table_name | slice(0, 1) }}Model::edit($data)); + return $this->toAjax({{ name1 }}Model::edit($data)); } #[DeleteMapping(path: "{{ table_name }}/del")] #[Auth(auth: "{{ table_name }}:del")] - public function {{ table_name }}Del() + public function {{ name2 }}Del() { $ids = $this->request->input("ids"); - return $this->toAjax({{ table_name | slice(0, 1) }}Model::del($ids)); + return $this->toAjax({{ name1 }}Model::del($ids)); } } \ No newline at end of file