From 6d93eaa0c2295e61338cfedc6558b9ca50b465cc Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq.com> Date: Thu, 5 Jun 2025 09:30:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=8B=E5=8F=8B=E5=9C=88=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E8=A7=86=E9=A2=91=E6=8E=A5=E5=8F=A33?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/enterprise/controller/Posts.php | 83 ++++++++++++++++++----------- 1 file changed, 51 insertions(+), 32 deletions(-) diff --git a/app/enterprise/controller/Posts.php b/app/enterprise/controller/Posts.php index 02160ee..a2d4c3b 100644 --- a/app/enterprise/controller/Posts.php +++ b/app/enterprise/controller/Posts.php @@ -176,7 +176,8 @@ class Posts extends BaseController $privacy = $this->request->param('privacy', ''); $location = $this->request->param('location', ''); $address = $this->request->param('address', ''); - $video_file = $this->request->param('video_file', []); + $video_file = $this->request->param('video_file', ''); + $poster_file = $this->request->param('poster_file', ''); $imgArr = $this->request->param('img_arr', []); $posts_id = $this->request->param('posts_id', ''); $user_ids = $this->request->param('user_ids', []); @@ -210,6 +211,38 @@ class Posts extends BaseController } } + $fileSaveArr = []; + if ($imgArr) { + foreach ($imgArr as $key => $img_url) { + $fileSaveArr = $this->addPostsFileData( + $img_url, + $posts_id, + $fileSaveArr, + $key + 1 + ); + } + } elseif ($video_file && $poster_file) { + $fileSaveArr = $this->addPostsFileData( + $video_file, + $posts_id, + $fileSaveArr, + 1, + 2 + ); + $fileSaveArr = $this->addPostsFileData( + $poster_file, + $posts_id, + $fileSaveArr, + 2 + ); + } + + if ($imgArr || ($video_file && $poster_file)) { + if (empty($fileSaveArr)) { + return error(lang('system.error')); + } + } + Db::startTrans(); try { $info = [ @@ -233,37 +266,6 @@ class Posts extends BaseController $posts_id = (new PostsModel())->insertGetId($info); } - $fileSaveArr = []; - foreach ($imgArr as $key => $img_url) { - $imgUrl = str_replace($this->url, '', $img_url); - $whereFile = ['src' => $imgUrl, 'user_id' => $this->uid, 'delete_time' => 0]; - $img_file_id = File::where($whereFile)->value('file_id'); - if ($img_file_id) { - $fileSaveArr[] = [ - 'posts_id' => $posts_id, - 'type' => 1, - 'file_id' => $img_file_id, - 'sort' => $key + 1 - ]; - } - } - - if (!empty($video_file)) { - foreach ($video_file as $v_file) { - $videoUrl = str_replace($this->url, '', $v_file); - $whereFile = ['src' => $videoUrl, 'user_id' => $this->uid, 'delete_time' => 0]; - $video_file_id = File::where($whereFile)->value('file_id'); - if ($video_file_id) { - $fileSaveArr[] = [ - 'posts_id' => $posts_id, - 'type' => 2, - 'file_id' => $video_file_id, - 'sort' => 1 - ]; - } - } - } - if ($fileSaveArr) { (new PostsFile())->saveAll($fileSaveArr); } @@ -290,6 +292,23 @@ class Posts extends BaseController } } + // 添加图片视频数据处理 + protected function addPostsFileData($url, $posts_id, $data, $sort = 1, $type = 1) + { + $imgUrl = str_replace($this->url, '', $url); + $whereFile = ['src' => $imgUrl, 'user_id' => $this->uid, 'delete_time' => 0]; + $img_file_id = File::where($whereFile)->value('file_id'); + if ($img_file_id) { + $data[] = [ + 'posts_id' => $posts_id, + 'type' => $type, + 'file_id' => $img_file_id, + 'sort' => $sort + ]; + } + return $data; + } + // 获取上传草稿帖子数据 public function getLastPosts() {