diff --git a/app/index/controller/ArticlesController.php b/app/index/controller/ArticlesController.php index 8c31493..0846b65 100644 --- a/app/index/controller/ArticlesController.php +++ b/app/index/controller/ArticlesController.php @@ -358,9 +358,9 @@ class ArticlesController extends BaseController ['delete_time', '=', null], ['status', '=', 2] ]) - ->field('id,title,image') - ->order('id', 'desc') - ->find(); + ->field('id,title,image') + ->order('id', 'desc') + ->find(); // 查询下一篇(同分类,ID更大的,按ID正序取第一个) $nextArticle = Articles::where([ @@ -369,9 +369,9 @@ class ArticlesController extends BaseController ['delete_time', '=', null], ['status', '=', 2] ]) - ->field('id,title,image') - ->order('id', 'asc') - ->find(); + ->field('id,title,image') + ->order('id', 'asc') + ->find(); // 查询相关文章(同分类,排除当前文章,按发布时间倒序取5个) $relatedArticles = Articles::where([ @@ -380,12 +380,12 @@ class ArticlesController extends BaseController ['a.delete_time', '=', null], ['a.status', '=', 2] ]) - ->field('a.id,a.title,IF(a.image IS NULL OR a.image = \'\', ac.image, a.image) as image,a.views,a.likes,a.publishdate') - ->alias('a') - ->join('articles_category ac', 'a.cate = ac.id', 'LEFT') - ->order('publishdate', 'desc') - ->limit(5) - ->select(); + ->field('a.id,a.title,IF(a.image IS NULL OR a.image = \'\', ac.image, a.image) as image,a.views,a.likes,a.publishdate') + ->alias('a') + ->join('articles_category ac', 'a.cate = ac.id', 'LEFT') + ->order('publishdate', 'desc') + ->limit(5) + ->select(); // 查询当前作者的文章发布数量 $articleCount = Articles::where([ @@ -855,9 +855,7 @@ class ArticlesController extends BaseController ]); } - /** - * 更新文章访问次数 - */ + //更新文章访问次数 public function updateViews() { if (!Request::isPost()) { @@ -940,4 +938,464 @@ class ArticlesController extends BaseController return json(['code' => 0, 'msg' => '获取作者信息失败:' . $e->getMessage()]); } } + + //获取用户文章列表 + public function getUserArticleList() + { + try { + // 获取前端传递的用户ID + $userId = input('uid/d', 0); + + // 验证用户ID + if ($userId <= 0) { + return json([ + 'code' => 1, + 'msg' => '用户ID参数错误' + ]); + } + + // 根据用户ID获取用户名 + $user = Users::where('uid', $userId)->field('uid, name')->find(); + if (!$user) { + return json([ + 'code' => 1, + 'msg' => '用户不存在' + ]); + } + + // 获取用户文章的分类ID列表,用于批量查询分类信息 + $cateIds = Articles::where([ + ['publisher', '=', $user['uid']], + ['delete_time', '=', null], + ['status', '=', 2] // 已发布的文章 + ])->column('cate'); + + // 批量查询分类信息,避免N+1查询问题 + $categories = []; + if (!empty($cateIds)) { + $categories = ArticlesCategory::where('id', 'in', array_unique($cateIds)) + ->column('id,name,image', 'id'); + } + + // 查询用户的文章列表 + $articles = Articles::where([ + ['publisher', '=', $user['uid']], + ['delete_time', '=', null], + ['status', '=', 2] // 已发布的文章 + ]) + ->field('id, title, cate, image, author, publisher, publishdate, views, likes, update_time') + ->order('publishdate', 'desc') + ->select(); + + // 处理文章数据 + $articleList = []; + if ($articles) { + foreach ($articles as $article) { + $articleData = $article->toArray(); + + // 格式化时间 + $articleData['publishdate'] = date('Y-m-d H:i:s', strtotime($articleData['publishdate'])); + + // 从预加载的分类数据中获取信息 + $cateId = $articleData['cate']; + if (isset($categories[$cateId])) { + $category = $categories[$cateId]; + + // 如果文章图片为空,使用分类图片 + if (empty($articleData['image'])) { + $articleData['image'] = !empty($category['image']) ? $category['image'] : '/static/images/default.jpg'; + } + + // 设置分类名称 + $articleData['category_name'] = $category['name']; + } else { + // 如果没有找到分类信息 + if (empty($articleData['image'])) { + $articleData['image'] = '/static/images/default.jpg'; + } + $articleData['category_name'] = '未分类'; + } + + $articleList[] = $articleData; + } + } + + return json([ + 'code' => 0, + 'msg' => '获取成功', + 'data' => [ + 'articles' => $articleList, + 'total' => count($articleList) + ] + ]); + + } catch (\Exception $e) { + return json([ + 'code' => 1, + 'msg' => '获取失败:' . $e->getMessage() + ]); + } + } + + //获取文章分类 + public function getArticleCategory() + { + try { + // 获取所有顶级分类(cid=0) + $topCategories = ArticlesCategory::where('cid', 0) + ->where('delete_time', null) + ->where('status', 1) + ->field('id, cid, name, image, sort') + ->order('sort', 'asc') + ->select() + ->toArray(); + + $categories = []; + + // 为每个顶级分类获取其子分类 + foreach ($topCategories as $topCategory) { + $category = [ + 'id' => $topCategory['id'], + 'name' => $topCategory['name'], + 'image' => $topCategory['image'], + 'sort' => $topCategory['sort'], + 'children' => [] + ]; + + // 获取子分类 + $children = ArticlesCategory::where('cid', $topCategory['id']) + ->where('delete_time', null) + ->where('status', 1) + ->field('id, cid, name, image, sort') + ->order('sort', 'asc') + ->select() + ->toArray(); + + // 为每个子分类获取文章数量 + foreach ($children as &$child) { + $child['article_count'] = Articles::where('cate', $child['id']) + ->where('delete_time', null) + ->where('status', 2) + ->count(); + } + + $category['children'] = $children; + $categories[] = $category; + } + + return json([ + 'code' => 0, + 'msg' => '获取文章分类成功', + 'data' => $categories + ]); + + } catch (\Exception $e) { + return json([ + 'code' => 1, + 'msg' => '获取文章分类失败:' . $e->getMessage() + ]); + } + } + + //发布文章 + public function publishArticle() + { + try { + // 获取前端传递的数据 + $data = []; + + // 标题(必需) + $title = input('title', ''); + if (empty($title)) { + return json([ + 'code' => 1, + 'msg' => '文章标题不能为空' + ]); + } + $data['title'] = $title; + + // 分类ID(必需) + $cate = input('cate/d', 0); + if ($cate <= 0) { + return json([ + 'code' => 1, + 'msg' => '请选择文章分类' + ]); + } + + //发布者 + $publisher = input('user_id', ''); + $data['publisher'] = $publisher; + + // 验证分类是否存在 + $category = ArticlesCategory::where('id', $cate) + ->where('delete_time', null) + ->where('status', 1) + ->find(); + if (!$category) { + return json([ + 'code' => 1, + 'msg' => '选择的分类不存在' + ]); + } + $data['cate'] = $cate; + + // 内容(必需) + $content = input('content', ''); + if (empty($content)) { + return json([ + 'code' => 1, + 'msg' => '文章内容不能为空' + ]); + } + $data['content'] = $content; + + // 作者(必需) + $author = input('author', ''); + if (empty($author)) { + return json([ + 'code' => 1, + 'msg' => '作者不能为空' + ]); + } + $data['author'] = $author; + + // 图片(可选) + $image = input('image', ''); + if (!empty($image)) { + $data['image'] = $image; + } + + // 描述(可选) + $desc = input('desc', ''); + if (!empty($desc)) { + $data['desc'] = $desc; + } + + // 发布者(可选) + $publisher = input('publisher', ''); + if (!empty($publisher)) { + $data['publisher'] = $publisher; + } + + // 状态(默认为2-已发布) + $status = input('status/d', 1); + if ($status >= 0 && $status <= 2) { + $data['status'] = $status; + } else { + $data['status'] = 2; // 默认已发布 + } + + // 发布日期(默认为当前时间) + $publishdate = input('publishdate', ''); + if (!empty($publishdate)) { + // 验证日期格式 + if (!strtotime($publishdate)) { + return json([ + 'code' => 1, + 'msg' => '发布日期格式错误' + ]); + } + $data['publishdate'] = $publishdate; + } else { + $data['publishdate'] = date('Y-m-d H:i:s'); + } + + // 是否翻译(可选) + $is_trans = input('is_trans/d', 0); + $data['is_trans'] = $is_trans ? 1 : 0; + + // 翻译链接(可选) + $transurl = input('transurl', ''); + if (!empty($transurl)) { + $data['transurl'] = $transurl; + } + + // 推送状态(可选) + $push = input('push/d', 0); + $data['push'] = $push ? 1 : 0; + + // 浏览量和点赞数默认为0 + $data['views'] = 0; + $data['likes'] = 0; + + // 创建时间和更新时间 + $data['create_time'] = date('Y-m-d H:i:s'); + + // 执行插入 + $result = Articles::insert($data); + + if (!$result) { + return json([ + 'code' => 1, + 'msg' => '发布失败' + ]); + } + + // 获取新插入的文章ID + $articleId = Articles::getLastInsID(); + + return json([ + 'code' => 0, + 'msg' => '文章发布成功', + 'data' => [ + 'article_id' => $articleId + ] + ]); + + } catch (\Exception $e) { + return json([ + 'code' => 1, + 'msg' => '发布失败:' . $e->getMessage() + ]); + } + } + + //编辑文章 + public function updateArticle() + { + try { + // 获取文章ID + $id = input('id/d', 0); + + // 验证ID + if ($id <= 0) { + return json([ + 'code' => 1, + 'msg' => '文章ID参数错误' + ]); + } + + // 检查文章是否存在 + $article = Articles::where('id', $id) + ->where('delete_time', null) + ->find(); + + if (!$article) { + return json([ + 'code' => 1, + 'msg' => '文章不存在' + ]); + } + + // 获取要更新的数据 + $data = []; + + // 标题 + $title = input('title', ''); + if (!empty($title)) { + $data['title'] = $title; + } + + // 分类ID + $cate = input('cate/d', 0); + if ($cate > 0) { + // 验证分类是否存在 + $category = ArticlesCategory::where('id', $cate) + ->where('delete_time', null) + ->where('status', 1) + ->find(); + if (!$category) { + return json([ + 'code' => 1, + 'msg' => '选择的分类不存在' + ]); + } + $data['cate'] = $cate; + } + + // 图片 + $image = input('image', ''); + if ($image !== '') { + $data['image'] = $image; + } + + // 描述 + $desc = input('desc', ''); + if ($desc !== '') { + $data['desc'] = $desc; + } + + // 内容 + $content = input('content', ''); + if ($content !== '') { + $data['content'] = $content; + } + + // 作者 + $author = input('author', ''); + if (!empty($author)) { + $data['author'] = $author; + } + + // 状态 + $status = input('status/d', -1); + if ($status >= 0 && $status <= 2) { + $data['status'] = $status; + } + + // 发布日期 + $publishdate = input('publishdate', ''); + if (!empty($publishdate)) { + // 验证日期格式 + if (!strtotime($publishdate)) { + return json([ + 'code' => 1, + 'msg' => '发布日期格式错误' + ]); + } + $data['publishdate'] = $publishdate; + } + + // 是否翻译 + $is_trans = input('is_trans/d', -1); + if ($is_trans >= 0 && $is_trans <= 1) { + $data['is_trans'] = $is_trans; + } + + // 翻译链接 + $transurl = input('transurl', ''); + if ($transurl !== '') { + $data['transurl'] = $transurl; + } + + // 推送状态 + $push = input('push/d', -1); + if ($push >= 0 && $push <= 1) { + $data['push'] = $push; + } + + // 如果没有任何要更新的数据 + if (empty($data)) { + return json([ + 'code' => 1, + 'msg' => '没有要更新的内容' + ]); + } + + // 添加更新时间 + $data['update_time'] = date('Y-m-d H:i:s'); + + // 执行更新 + $result = Articles::where('id', $id)->update($data); + + if ($result === false) { + return json([ + 'code' => 1, + 'msg' => '更新失败' + ]); + } + + return json([ + 'code' => 0, + 'msg' => '文章更新成功' + ]); + + } catch (\Exception $e) { + return json([ + 'code' => 1, + 'msg' => '更新失败:' . $e->getMessage() + ]); + } + } } diff --git a/app/index/controller/IndexController.php b/app/index/controller/IndexController.php index a5d34b9..eb0efe1 100644 --- a/app/index/controller/IndexController.php +++ b/app/index/controller/IndexController.php @@ -425,19 +425,18 @@ class IndexController extends BaseController public function update_imgs() { // 获取上传的文件 - $file = request()->file(); $files = request()->file('file'); // 检查是否有文件上传 - if (empty($file)) { + if (empty($files)) { return json(['code' => 1, 'msg' => '没有文件上传']); } try { // 验证上传的文件 validate([ - 'image' => 'filesize:51200|fileExt:jpg,png,gif,jpeg,webp' - ])->check($file); + 'file' => 'filesize:512000|fileExt:jpg,png,gif,jpeg,webp' + ])->check(['file' => $files]); // 存储文件到public磁盘的uploads目录 $info = Filesystem::disk('public')->putFile('uploads', $files); diff --git a/app/index/controller/ResourcesController.php b/app/index/controller/ResourcesController.php index 320324e..489f426 100644 --- a/app/index/controller/ResourcesController.php +++ b/app/index/controller/ResourcesController.php @@ -429,4 +429,102 @@ class ResourcesController extends BaseController return json(['code' => 0, 'msg' => '更新失败:' . $e->getMessage()]); } } + + //获取用户资源列表 + public function getUserResourceList() + { + try { + // 获取前端传递的用户ID + $userId = input('uid/d', 0); + + // 验证用户ID + if ($userId <= 0) { + return json([ + 'code' => 1, + 'msg' => '用户ID参数错误' + ]); + } + + // 根据用户ID获取用户名 + $user = Users::where('uid', $userId)->field('uid, name')->find(); + if (!$user) { + return json([ + 'code' => 1, + 'msg' => '用户不存在' + ]); + } + + // 获取用户资源的分类ID列表,用于批量查询分类信息 + $cateIds = Resources::where([ + ['uploader', '=', $user['name']], + ['delete_time', '=', null], + ['status', '=', 1] // 已发布的资源 + ])->column('cate'); + + // 批量查询分类信息,避免N+1查询问题 + $categories = []; + if (!empty($cateIds)) { + $categories = ResourcesCategory::where('id', 'in', array_unique($cateIds)) + ->column('id,name,icon', 'id'); + } + + // 查询用户的资源列表 + $resources = Resources::where([ + ['uploader', '=', $user['name']], + ['delete_time', '=', null], + ['status', '=', 1] // 已发布的资源 + ]) + ->field('id, title, cate, icon, uploader, create_time, views, downloads, price') + ->order('create_time', 'desc') + ->select(); + + // 处理资源数据 + $resourceList = []; + if ($resources) { + foreach ($resources as $resource) { + $resourceData = $resource->toArray(); + + // 格式化时间 + $resourceData['create_time'] = date('Y-m-d H:i:s', strtotime($resourceData['create_time'])); + + // 从预加载的分类数据中获取信息 + $cateId = $resourceData['cate']; + if (isset($categories[$cateId])) { + $category = $categories[$cateId]; + + // 如果资源图片为空,使用分类图片 + if (empty($resourceData['icon'])) { + $resourceData['icon'] = !empty($category['icon']) ? $category['icon'] : '/static/images/default.jpg'; + } + + // 设置分类名称 + $resourceData['category_name'] = $category['name']; + } else { + // 如果没有找到分类信息 + if (empty($resourceData['icon'])) { + $resourceData['icon'] = '/static/images/default.jpg'; + } + $resourceData['category_name'] = '未分类'; + } + + $resourceList[] = $resourceData; + } + } + + return json([ + 'code' => 0, + 'msg' => '获取成功', + 'data' => [ + 'resources' => $resourceList, + 'total' => count($resourceList) + ] + ]); + + } catch (\Exception $e) { + return json([ + 'code' => 1, + 'msg' => '获取失败:' . $e->getMessage() + ]); + } + } } diff --git a/app/index/controller/UserController.php b/app/index/controller/UserController.php index 662e121..fa8bca5 100644 --- a/app/index/controller/UserController.php +++ b/app/index/controller/UserController.php @@ -86,7 +86,7 @@ class UserController extends BaseController 'code' => 0, 'msg' => '登录成功', 'data' => [ - 'user_id' => $user->id, + 'uid' => $user->uid, 'user_name' => $user->name, 'user_account' => $user->account, 'user_avatar' => $user->avatar ?? '/static/images/avatar.png', diff --git a/public/storage/uploads/20251227/608acef8e5c6993f3155fe7904ac2bd4.png b/public/storage/uploads/20251227/608acef8e5c6993f3155fe7904ac2bd4.png new file mode 100644 index 0000000..12779bf Binary files /dev/null and b/public/storage/uploads/20251227/608acef8e5c6993f3155fe7904ac2bd4.png differ diff --git a/public/storage/uploads/20251227/b31f0a03e05084f79c27f2ab4484d7c5.png b/public/storage/uploads/20251227/b31f0a03e05084f79c27f2ab4484d7c5.png new file mode 100644 index 0000000..1482ae0 Binary files /dev/null and b/public/storage/uploads/20251227/b31f0a03e05084f79c27f2ab4484d7c5.png differ diff --git a/route/app.php b/route/app.php index d4b7b06..c848346 100644 --- a/route/app.php +++ b/route/app.php @@ -50,4 +50,19 @@ Route::group('api', function () { // 搜索接口 Route::get('search', 'index/Search/apiSearch'); + + // 用户文章列表接口 + Route::get('user/articles', 'index/Articles/getUserArticleList'); + + // 用户资源列表接口 + Route::get('user/resources', 'index/Resources/getUserResourceList'); + + // 文章分类接口 + Route::get('articles/categories', 'index/Articles/getArticleCategory'); + + // 编辑文章接口 + Route::post('articles/update', 'index/Articles/updateArticle'); + + // 发布文章接口 + Route::post('articles/publish', 'index/Articles/publishArticle'); })->middleware(\app\middleware\Cors::class);