From 60bfa1390c1d5622b8a859bd919a318e8d3985ca Mon Sep 17 00:00:00 2001 From: lwh <2679599887@qq.com> Date: Thu, 24 Aug 2023 14:43:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=88=B1=E6=83=85?= =?UTF-8?q?=E6=95=85=E4=BA=8B=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/Common.php | 7 +- app/admin/controller/LoveStory/LoveStory.php | 188 ++++++++++++++++ app/common/model/LoveStory/LoveStory.php | 216 +++++++++++++++++++ 3 files changed, 408 insertions(+), 3 deletions(-) create mode 100644 app/admin/controller/LoveStory/LoveStory.php create mode 100644 app/common/model/LoveStory/LoveStory.php diff --git a/app/admin/controller/Common.php b/app/admin/controller/Common.php index 82feb19..c8856bb 100644 --- a/app/admin/controller/Common.php +++ b/app/admin/controller/Common.php @@ -49,13 +49,14 @@ class Common extends BaseController public function uploadFile(Request $request) { $dirName = $request->param('dirName'); - + $uploadedFile = $request->file('file'); + $originalFilename = $uploadedFile->getOriginalName(); // 获取上传文件的原始文件名 $upload = new UploadFile('uploads', 'file'); - $path = $upload->putFile($dirName . 'File'); + $path = $upload->putFileAs($dirName . 'File', $originalFilename); return [ 'code' => 0, 'data' => [ - "name" => $path, + "fileName" => $originalFilename, "url" => "/uploads/" . $path, ], 'msg' => '上传成功!' diff --git a/app/admin/controller/LoveStory/LoveStory.php b/app/admin/controller/LoveStory/LoveStory.php new file mode 100644 index 0000000..52f1ff1 --- /dev/null +++ b/app/admin/controller/LoveStory/LoveStory.php @@ -0,0 +1,188 @@ +param(); + $con = []; + + $con = Tool::getOptionalQuery(['love_story_title', 'LIKE'], ['love_story_place', 'LIKE'],); + + $query = ModelLoveStory::where($con) + ->field([ + 'love_story_id', + 'love_story_guid', + 'love_story_title', + 'love_story_author', + 'love_story_place', + 'love_story_date', + 'love_story_cover', + 'love_story_sort', + 'love_story_music', + 'love_story_content' + ]) + ->append(['love_story_music_name']) + ->order('love_story_sort', 'asc'); + + return $isExport ? $query->select()->toArray() : msg("获取爱情故事列表成功!", $query); + } + + /** + * 添加爱情故事 + */ + public function addLoveStory(Request $request): array + { + Db::startTrans(); + Tool::adminLockTableWrite('love_story'); + try { + $params = $request->param(); + $this->validate($params, [ + 'love_story_title|标题' => 'require', + 'love_story_place|地点' => 'require', + 'love_story_date|日期' => 'require', + 'love_story_cover|封面' => 'require', + // 'love_story_sort|排序' => 'require' + ]); + $model = ModelLoveStory::create($params, [ + 'love_story_title', + 'love_story_author', + 'love_story_place', + 'love_story_date', + 'love_story_cover', + 'love_story_sort', + 'love_story_music', + 'love_story_content', + 'love_story_guid', + 'love_story_create_user_guid', + 'love_story_update_user_guid' + ]); + Db::commit(); + Tool::unlockTable(); + return msg('添加成功!'); + } catch (\Throwable $th) { + Db::rollback(); + Tool::unlockTable(); + throw $th; + } + } + + /** + * 编辑爱情故事 + */ + public function editLoveStory(Request $request): array + { + Db::startTrans(); + Tool::adminLockTableWrite('love_story'); + try { + $params = $request->param(); + $this->validate($params, [ + 'love_story_title|标题' => 'require', + 'love_story_place|地点' => 'require', + 'love_story_date|日期' => 'require', + 'love_story_cover|封面' => 'require', + // 'love_story_sort|排序' => 'require' + ]); + $model = ModelLoveStory::where('love_story_guid', $params['love_story_guid'])->find(); + if (!$model) throwErrorMsg("该爱情故事不存在", 1); + $model->allowField([ + 'love_story_title', + 'love_story_author', + 'love_story_place', + 'love_story_date', + 'love_story_cover', + 'love_story_sort', + 'love_story_music', + 'love_story_content', + 'love_story_update_user_guid' + ])->save($params); + Db::commit(); + Tool::unlockTable(); + return msg('编辑成功!'); + } catch (\Throwable $th) { + Db::rollback(); + Tool::unlockTable(); + throw $th; + } + } + + /** + * 删除爱情故事 + */ + public function deleteLoveStory(Request $request): array + { + Db::startTrans(); + Tool::adminLockTableWrite('love_story'); + try { + $params = $request->param(); + $this->validate($params, [ + 'love_story_guid' => 'require', + ]); + $love_story = ModelLoveStory::where([ + 'love_story_guid' => explode(',', $params['love_story_guid']) + ])->select(); + $love_story->delete(); + Db::commit(); + Tool::unlockTable(); + return msg('删除成功!'); + } catch (\Throwable $th) { + Db::rollback(); + Tool::unlockTable(); + throw $th; + } + } + + /** + * 导出Excel + */ + public function exportExcel(Request $request): void + { + ModelLoveStory::exportExcel(self::getLoveStoryList($request, true)); + } + + /** + * 下载导入模板 + */ + public function downloadTemplate(Request $request): void + { + $params = $request->param(); + $data = [ + array_values(ModelLoveStory::EXCELFIELD), + ['默认值1', '默认值2', '默认值3', '默认值4', '默认值5', '默认值6', '默认值7', '默认值8',] + ]; + $excel = (new Excel())->exporTsheet($data); + $excel->save('爱情故事导入模板.xlsx'); + } + + /** + * 导入excel + */ + public function importExcel(Request $request): array + { + $file = new UploadFile('uploads', 'fileExt:xlsx'); + $file->putFile('love_story'); + $msg = ModelLoveStory::importExcel($file); + return [ + 'code' => 0, + 'msg' => $msg + ]; + } +} diff --git a/app/common/model/LoveStory/LoveStory.php b/app/common/model/LoveStory/LoveStory.php new file mode 100644 index 0000000..e5dec6e --- /dev/null +++ b/app/common/model/LoveStory/LoveStory.php @@ -0,0 +1,216 @@ + 'int', + 'love_story_guid' => 'string', + 'love_story_title' => 'string', + 'love_story_author' => 'string', + 'love_story_place' => 'string', + 'love_story_date' => '', + 'love_story_cover' => 'string', + 'love_story_sort' => 'int', + 'love_story_music' => 'string', + 'love_story_content' => '', + 'love_story_create_time' => 'datetime', + 'love_story_create_user_guid' => 'string', + 'love_story_update_time' => 'datetime', + 'love_story_update_user_guid' => 'string', + 'love_story_delete_time' => 'datetime', + 'love_story_delete_user_guid' => 'string', + + ]; + // 设置json类型字段 + protected $json = ['']; + // 开启自动写入时间戳字段 + protected $autoWriteTimestamp = 'datetime'; + // 创建时间 + protected $createTime = 'love_story_create_time'; + // 修改时间 + protected $updateTime = 'love_story_update_time'; + + //排序字段 + public $order_field = 'love_story_sort'; + + // excel导入/下载模板表头 + public const EXCELFIELD = [ + 'love_story_title' => '标题', + 'love_story_author' => '作者', + 'love_story_place' => '地点', + 'love_story_date' => '日期', + 'love_story_cover' => '封面', + 'love_story_sort' => '排序', + 'love_story_music' => '音乐', + 'love_story_content' => '内容', + ]; + + /** + * 新增前 + */ + public static function onBeforeInsert(self $model): void + { + Tool::dataAddSortProc($model); + $model->completeCreateField(); + } + + /** + * 更新前 + */ + public static function onBeforeUpdate(self $model): void + { + Tool::dataEditSortProc($model); + $model->completeUpdateField(); + } + + /** + * 删除前 + */ + public static function onBeforeDelete(self $model): void + { + Tool::dataDeleteSortProc($model); + $model->completeDeleteField(); + } + + + /** + * 音乐名称获取器 + */ + public function getLoveStoryMusicNameAttr($value, $data) + { + $love_story_music_path = $data['love_story_music']; + $file_name = basename($love_story_music_path); // 使用basename获取文件名部分 + return $file_name; + } + + + /** + * 导出Excel + * + * @param array $select 导出的数据 + */ + public static function exportExcel(array $select): void + { + $data = [[ + '标题', + '作者', + '地点', + '日期', + '封面', + '排序', + '音乐', + '内容' + ]]; + foreach ($select as $key => $val) { + $data[] = [ + $val['love_story_title'], + $val['love_story_author'], + $val['love_story_place'], + $val['love_story_date'], + Excel::ExportImgFiled($val['love_story_cover']), + $val['love_story_sort'], + $val['love_story_music'], + $val['love_story_content'], + ]; + } + $excel = (new Excel())->exporTsheet($data); + $excel->save('爱情故事.xlsx'); + } + + + /** + * 导入excel + * + * @param \app\common\arw\adjfut\src\UploadFile $file excel + */ + public static function importExcel(\app\common\arw\adjfut\src\UploadFile $file): string + { + $msg = []; + + Db::startTrans(); + try { + $excel = new Excel($file); + $data = $excel->parseExcel( + Tool::getExcelRule(self::EXCELFIELD), + ['titleLine' => [1]] + ); + if (!$data) throwErrorMsg('excel无数据', 1); + $msg = []; + foreach ($data as $line => $value) { + try { + $model = self::importExcelInit($value); + $msg[] = "{$line} 新增成功!
"; + } catch (\Throwable $th) { + $msg[] = "{$line} {$th->getMessage()}
"; + } + } + Db::commit(); + return implode(', ', $msg); + } catch (\Throwable $th) { + Db::rollback(); + throw $th; + } + } + + + /** + * 导入excel初始化 + * + * @param array $value excel每行数据 + */ + public static function importExcelInit(array $value): void + { + $love_story_title = $value['love_story_title']; + $love_story_author = $value['love_story_author']; + $love_story_place = $value['love_story_place']; + $love_story_date = $value['love_story_date']; + $love_story_cover = $value['love_story_cover']; + $love_story_sort = $value['love_story_sort']; + $love_story_music = $value['love_story_music']; + $love_story_content = $value['love_story_content']; + + self::create( + [ + 'love_story_title' => $love_story_title, + 'love_story_author' => $love_story_author, + 'love_story_place' => $love_story_place, + 'love_story_date' => $love_story_date, + 'love_story_cover' => $love_story_cover, + 'love_story_sort' => $love_story_sort, + 'love_story_music' => $love_story_music, + 'love_story_content' => $love_story_content, + ], + [ + 'love_story_title', + 'love_story_author', + 'love_story_place', + 'love_story_date', + 'love_story_cover', + 'love_story_sort', + 'love_story_music', + 'love_story_content', + 'love_story_guid', + 'love_story_create_user_guid', + 'love_story_update_user_guid' + ] + ); + } +}