chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,301 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller;
|
||||
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use addon\blog\model\BlogCategory as CategoryModel;
|
||||
use addon\blog\model\BlogComment as CommentModel;
|
||||
use addon\blog\model\BlogTag as TagModel;
|
||||
use addon\blog\model\BlogLike as LikeModel;
|
||||
use addon\blog\model\BlogFavorite as FavoriteModel;
|
||||
use ywxapp\model\MemberUser;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
|
||||
/**
|
||||
* Article 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Article extends BlogFrontend
|
||||
{
|
||||
|
||||
public function initialize()
|
||||
{
|
||||
}
|
||||
|
||||
protected $noNeedLogin = ['*'];
|
||||
|
||||
/**
|
||||
* 网站首页
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$page = Request::param('page', 1);
|
||||
|
||||
$articles = ArticleModel::with(['user', 'category', 'tags'])
|
||||
->where('status', 1)
|
||||
->order('create_at', 'desc')
|
||||
->paginate(['list_rows' => 10, 'page' => $page]);
|
||||
|
||||
$hotArticles = ArticleModel::with(['user', 'category'])
|
||||
->where('status', 1)
|
||||
->order('view_count', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
|
||||
$recommendedArticles = ArticleModel::with(['user', 'category'])
|
||||
->where('status', 1)
|
||||
->where('is_top', 1)
|
||||
->order('like_count', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
|
||||
$categories = CategoryModel::withCount(['articles' =>
|
||||
function ($query, &$alias) {
|
||||
$query->where('status', 1);
|
||||
$alias = 'card_count';
|
||||
}])
|
||||
->order('sort', 'asc')
|
||||
->select();
|
||||
|
||||
$tags = TagModel::withCount('articles')
|
||||
->order('create_at', 'desc')
|
||||
->limit(20)
|
||||
->select();
|
||||
|
||||
$latestComments = CommentModel::with(['user', 'article'])
|
||||
->where('status', 1)
|
||||
->order('create_at', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
|
||||
$this->view->assign('articles', $articles);
|
||||
$this->view->assign('hotArticles', $hotArticles);
|
||||
$this->view->assign('recommendedArticles', $recommendedArticles);
|
||||
$this->view->assign('categories', $categories);
|
||||
$this->view->assign('tags', $tags);
|
||||
$this->view->assign('latestComments', $latestComments);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章详情页
|
||||
*/
|
||||
public function detail($id)
|
||||
{
|
||||
$article = ArticleModel::with(['user', 'category', 'tags'])
|
||||
->where('id', $id)
|
||||
->where('status', 1)
|
||||
->find();
|
||||
|
||||
if (!$article) {
|
||||
return $this->error('文章不存在或已被删除');
|
||||
}
|
||||
|
||||
$article->view_count += 1;
|
||||
$article->save();
|
||||
|
||||
$comments = CommentModel::with([
|
||||
'member',
|
||||
'children' =>
|
||||
function ($q) {
|
||||
$q->with(['user'])->where('status', 1)->order('create_at', 'asc');
|
||||
},
|
||||
])
|
||||
->where('aid', $id)
|
||||
->where('pid', 0)
|
||||
->where('status', 1)
|
||||
->order('create_at', 'desc')
|
||||
->paginate(10);
|
||||
|
||||
$relatedArticles = \addon\blog\service\BlogSearchService::related($article->id, 5);
|
||||
|
||||
return View::fetch('article/detail', [
|
||||
'article' => $article,
|
||||
'comments' => $comments,
|
||||
'relatedArticles' => $relatedArticles
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章点赞
|
||||
*/
|
||||
public function like($id)
|
||||
{
|
||||
if (! $this->auth->isLogin) {
|
||||
return json(['code' => 0, 'msg' => '请先登录']);
|
||||
}
|
||||
$uid = $this->auth->model->uid;
|
||||
|
||||
$article = ArticleModel::find($id);
|
||||
if (!$article) {
|
||||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||||
}
|
||||
|
||||
$like = LikeModel::where('uid', $uid)
|
||||
->where('aid', $id)
|
||||
->find();
|
||||
|
||||
if ($like) {
|
||||
$like->delete();
|
||||
$article->like_count = max(0, $article->like_count - 1);
|
||||
$action = 'cancel';
|
||||
} else {
|
||||
$like = new LikeModel();
|
||||
$like->uid = $uid;
|
||||
$like->aid = $id;
|
||||
$like->save();
|
||||
$article->like_count += 1;
|
||||
$action = 'add';
|
||||
}
|
||||
|
||||
$article->save();
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => $action == 'add' ? '点赞成功' : '已取消点赞',
|
||||
'count' => $article->like_count,
|
||||
'action' => $action
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章收藏
|
||||
*/
|
||||
public function favorite($id)
|
||||
{
|
||||
if (! $this->auth->isLogin) {
|
||||
return json(['code' => 0, 'msg' => '请先登录']);
|
||||
}
|
||||
$uid = $this->auth->model->uid;
|
||||
|
||||
$article = ArticleModel::find($id);
|
||||
if (!$article) {
|
||||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||||
}
|
||||
|
||||
$favorite = FavoriteModel::where('uid', $uid)
|
||||
->where('aid', $id)
|
||||
->find();
|
||||
|
||||
if ($favorite) {
|
||||
$favorite->delete();
|
||||
$article->favorite_count = max(0, $article->favorite_count - 1);
|
||||
$action = 'cancel';
|
||||
} else {
|
||||
$favorite = new FavoriteModel();
|
||||
$favorite->uid = $uid;
|
||||
$favorite->aid = $id;
|
||||
$favorite->save();
|
||||
$article->favorite_count += 1;
|
||||
$action = 'add';
|
||||
}
|
||||
|
||||
$article->save();
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => $action == 'add' ? '收藏成功' : '已取消收藏',
|
||||
'count' => $article->favorite_count,
|
||||
'action' => $action
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章评论
|
||||
*/
|
||||
public function getComments($id)
|
||||
{
|
||||
$page = Request::param('page', 1);
|
||||
$pageSize = Request::param('page_size', 10);
|
||||
|
||||
$comments = CommentModel::with(['user'])
|
||||
->where('aid', $id)
|
||||
->where('pid', 0)
|
||||
->where('status', 1)
|
||||
->order('create_at', 'desc')
|
||||
->page($page, $pageSize)
|
||||
->select();
|
||||
|
||||
$total = CommentModel::where('aid', $id)
|
||||
->where('pid', 0)
|
||||
->where('status', 1)
|
||||
->count();
|
||||
|
||||
$commentList = [];
|
||||
foreach ($comments as $comment) {
|
||||
$commentList[] = [
|
||||
'id' => $comment->id,
|
||||
'content' => $comment->content,
|
||||
'create_at' => date('Y-m-d H:i', $comment->create_at),
|
||||
'member' => [
|
||||
'uid' => $comment->user->uid,
|
||||
'nickname' => $comment->user->nickname,
|
||||
'avatar' => $comment->user->avatar ?? '/static/images/avatar.png'
|
||||
],
|
||||
'like_count' => $comment->like_count
|
||||
];
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'data' => [
|
||||
'list' => $commentList,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'page_size' => $pageSize
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人博客主页:展示该用户的资料 + 已发布文章(多用户门户)
|
||||
*/
|
||||
public function author($id)
|
||||
{
|
||||
$author = MemberUser::find($id);
|
||||
if (!$author) {
|
||||
return $this->error('用户不存在');
|
||||
}
|
||||
|
||||
$stats = [
|
||||
'articles' => ArticleModel::where('uid', $id)->where('status', 1)->count(),
|
||||
'views' => (int) ArticleModel::where('uid', $id)->where('status', 1)->sum('view_count'),
|
||||
'likes' => (int) ArticleModel::where('uid', $id)->where('status', 1)->sum('like_count'),
|
||||
];
|
||||
|
||||
$join = $author->create_at;
|
||||
if (is_numeric($join)) {
|
||||
$join = date('Y-m-d', (int) $join);
|
||||
} else {
|
||||
$ts = strtotime((string) $join);
|
||||
$join = $ts !== false ? date('Y-m-d', $ts) : (string) $join;
|
||||
}
|
||||
|
||||
$avatar = $author->avatar ?: '/static/blog/img/portrait.jpg';
|
||||
|
||||
$articles = ArticleModel::with(['user', 'category', 'tags'])
|
||||
->where('uid', $id)
|
||||
->where('status', 1)
|
||||
->order('create_at', 'desc')
|
||||
->paginate(10);
|
||||
|
||||
return View::fetch('article/author', [
|
||||
'author' => $author,
|
||||
'avatar' => $avatar,
|
||||
'author_join' => $join,
|
||||
'stats' => $stats,
|
||||
'articles' => $articles,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace addon\blog\controller;
|
||||
use ywxapp\controller\FrontendBase;
|
||||
|
||||
use addon\blog\model\BlogArticle;
|
||||
use addon\blog\model\BlogCategory;
|
||||
use addon\blog\model\BlogTag;
|
||||
use addon\blog\model\BlogComment;
|
||||
|
||||
/**
|
||||
* 博客前台基类:统一注入侧边栏/统计等共享数据
|
||||
*/
|
||||
class BlogFrontend extends FrontendBase
|
||||
{
|
||||
// 博客前台为公开页面,不做强制登录拦截
|
||||
protected $noNeedLogin = ['*'];
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
|
||||
$articleModel = new BlogArticle();
|
||||
$categoryModel = new BlogCategory();
|
||||
$tagModel = new BlogTag();
|
||||
$commentModel = new BlogComment();
|
||||
|
||||
// 主动解析登录态:verifyAuth 因 noNeedLogin='*' 会跳过 tokenParse,
|
||||
// 必须显式 checkLogin() 从 session(access_token) 还原 isLogin。
|
||||
$isLogin = $this->auth && $this->auth->isLogin;
|
||||
$currentUid = 0;
|
||||
$loginUser = null;
|
||||
if ($isLogin) {
|
||||
$u = $this->auth->info;
|
||||
$loginUser = $u;
|
||||
$currentUid = is_array($u) ? (int) ($u['uid'] ?? 0) : (int) ($u->uid ?? 0);
|
||||
}
|
||||
|
||||
$this->view->assign([
|
||||
'total_articles' => $articleModel->where('status', 1)->count(),
|
||||
'total_categories' => $categoryModel->count(),
|
||||
'total_views' => (int) $articleModel->where('status', 1)->sum('view_count'),
|
||||
'hotArticles' => $articleModel->with(['user', 'category'])
|
||||
->where('status', 1)->order('view_count', 'desc')->limit(5)->select(),
|
||||
'categories' => $categoryModel->withCount(['articles' =>
|
||||
function ($query, &$alias) {
|
||||
$query->where('status', 1);
|
||||
$alias = 'card_count';
|
||||
}])->order('sort', 'asc')->select(),
|
||||
'tags' => $tagModel->withCount('articles')->order('create_at', 'desc')->limit(20)->select(),
|
||||
'latestComments' => $commentModel->with(['user', 'article'])
|
||||
->where('status', 1)->order('create_at', 'desc')->limit(5)->select(),
|
||||
'nav_active' => '',
|
||||
'is_login' => $isLogin,
|
||||
'member' => $loginUser,
|
||||
'current_uid' => $currentUid,
|
||||
// 主题色:后台「基本设置」可配置,默认 #2d6cdf
|
||||
'theme_color' => \think\facade\Config::get('site.theme_color', '#2d6cdf'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller;
|
||||
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use addon\blog\model\BlogComment as CommentModel;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use think\facade\Session;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* Comment 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Comment extends BlogFrontend
|
||||
{
|
||||
/**
|
||||
* 公开访问(登录校验由方法内部通过 $this->auth 处理,会员统一以 uid 标识)
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedLogin = ['*'];
|
||||
|
||||
/**
|
||||
* 发表评论
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (! $this->auth->isLogin) {
|
||||
return json(['code' => 0, 'msg' => '请先登录']);
|
||||
}
|
||||
$uid = $this->auth->model->uid;
|
||||
|
||||
$data = Request::post();
|
||||
|
||||
$validate = new \think\Validate([
|
||||
'content|评论内容' => 'require|min:5|max:500',
|
||||
'article_id|文章ID' => 'require|number',
|
||||
'parent_id|父评论ID' => 'number'
|
||||
]);
|
||||
|
||||
if (!$validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
$article = ArticleModel::find($data['article_id']);
|
||||
if (!$article) {
|
||||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||||
}
|
||||
|
||||
if ($data['parent_id'] > 0) {
|
||||
$parentComment = CommentModel::find($data['parent_id']);
|
||||
if (!$parentComment || $parentComment->aid != $data['article_id']) {
|
||||
return json(['code' => 0, 'msg' => '回复的评论不存在']);
|
||||
}
|
||||
}
|
||||
|
||||
$sensitiveWords = ['垃圾', '广告', '违法', '政治'];
|
||||
$content = str_replace($sensitiveWords, '***', $data['content']);
|
||||
|
||||
$comment = new CommentModel();
|
||||
$comment->uid = $uid;
|
||||
$comment->aid = $data['article_id'];
|
||||
$comment->pid = $data['parent_id'] ?? 0;
|
||||
$comment->content = $content;
|
||||
$comment->status = 1;
|
||||
$comment->ip = Request::ip();
|
||||
$comment->user_agent = Request::server('HTTP_USER_AGENT');
|
||||
$comment->save();
|
||||
|
||||
$article->comment_count = Db::raw('comment_count + 1');
|
||||
$article->save();
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => '评论发表成功',
|
||||
'data' => [
|
||||
'id' => $comment->id,
|
||||
'content' => $comment->content,
|
||||
'create_at' => $comment->create_text,
|
||||
'member' => [
|
||||
'uid' => $uid,
|
||||
'nickname' => $this->auth->model->nickname,
|
||||
'avatar' => $this->auth->model->avatar ?? '/static/images/avatar.png'
|
||||
],
|
||||
'parent_id' => $comment->pid
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除评论
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if (! $this->auth->isLogin) {
|
||||
return json(['code' => 0, 'msg' => '请先登录']);
|
||||
}
|
||||
$uid = $this->auth->model->uid;
|
||||
|
||||
$comment = CommentModel::find($id);
|
||||
if (!$comment) {
|
||||
return json(['code' => 0, 'msg' => '评论不存在']);
|
||||
}
|
||||
|
||||
if ($comment->uid != $uid) {
|
||||
return json(['code' => 0, 'msg' => '无权限操作']);
|
||||
}
|
||||
|
||||
$this->deleteCommentWithChildren($id);
|
||||
|
||||
$article = ArticleModel::find($comment->aid);
|
||||
if ($article) {
|
||||
$article->comment_count = Db::raw('comment_count - 1');
|
||||
$article->save();
|
||||
}
|
||||
|
||||
return json(['code' => 1, 'msg' => '评论已删除']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归删除评论及其子评论
|
||||
*/
|
||||
private function deleteCommentWithChildren($commentId)
|
||||
{
|
||||
$comment = CommentModel::find($commentId);
|
||||
if ($comment) {
|
||||
$children = CommentModel::where('pid', $commentId)->select();
|
||||
foreach ($children as $child) {
|
||||
$this->deleteCommentWithChildren($child->id);
|
||||
}
|
||||
$comment->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 评论点赞
|
||||
*/
|
||||
public function like($id)
|
||||
{
|
||||
if (! $this->auth->isLogin) {
|
||||
return json(['code' => 0, 'msg' => '请先登录']);
|
||||
}
|
||||
$uid = $this->auth->model->uid;
|
||||
|
||||
$comment = CommentModel::find($id);
|
||||
if (!$comment) {
|
||||
return json(['code' => 0, 'msg' => '评论不存在']);
|
||||
}
|
||||
|
||||
$likeKey = "comment_like_{$id}_{$uid}";
|
||||
if (Session::get($likeKey)) {
|
||||
Session::delete($likeKey);
|
||||
$comment->like_count = max(0, $comment->like_count - 1);
|
||||
$action = 'cancel';
|
||||
} else {
|
||||
Session::set($likeKey, true);
|
||||
$comment->like_count += 1;
|
||||
$action = 'add';
|
||||
}
|
||||
|
||||
$comment->save();
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => $action == 'add' ? '点赞成功' : '已取消点赞',
|
||||
'count' => $comment->like_count,
|
||||
'action' => $action
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace addon\blog\controller;
|
||||
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use addon\blog\model\BlogCategory as CategoryModel;
|
||||
use addon\blog\model\BlogComment as CommentModel;
|
||||
use addon\blog\model\BlogTag as TagModel;
|
||||
use think\facade\Cache;
|
||||
use think\facade\View;
|
||||
use think\facade\Request;
|
||||
|
||||
|
||||
/**
|
||||
* Index 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Index extends BlogFrontend
|
||||
{
|
||||
|
||||
public function initialize() {}
|
||||
|
||||
|
||||
/**
|
||||
* Summary of needLogin
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedLogin = ['*'];
|
||||
/**
|
||||
* 网站首页
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// 尝试从缓存获取首页数据(v2 键:绕过旧版本可能残留的损坏缓存)
|
||||
$cacheKey = 'homepage_data_v2';
|
||||
$data = Cache::get($cacheKey);
|
||||
// 判断缓存中的文章是否为空(兼容数组与模型集合对象)
|
||||
$articlesEmpty = true;
|
||||
if (! empty($data) && ! empty($data['articles'])) {
|
||||
$cachedArticles = $data['articles'];
|
||||
$articlesEmpty = (is_object($cachedArticles) && method_exists($cachedArticles, 'isEmpty'))
|
||||
? $cachedArticles->isEmpty()
|
||||
: (count($cachedArticles) === 0);
|
||||
}
|
||||
if (empty($data) || $articlesEmpty) {
|
||||
// 获取最新文章
|
||||
$articles = ArticleModel::with(['user', 'category', 'tags'])
|
||||
->where('status', 1)
|
||||
->order('create_at', 'desc')
|
||||
->limit(10)
|
||||
->select();
|
||||
|
||||
// 获取热门文章
|
||||
$hotArticles = ArticleModel::with(['user', 'category'])
|
||||
->where('status', 1)
|
||||
->order('view_count', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
|
||||
// 获取推荐文章(置顶+高赞)
|
||||
$recommendedArticles = ArticleModel::with(['user', 'category'])
|
||||
->where('status', 1)
|
||||
->where('is_top', 1)
|
||||
->order('like_count', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
|
||||
// 获取分类(公开站点展示全部分类)
|
||||
$categories = CategoryModel::withCount(['articles' =>
|
||||
function ($query, &$alias) {
|
||||
$query->where('status', 1);
|
||||
$alias = 'card_count';
|
||||
}])
|
||||
->order('sort', 'asc')
|
||||
->select();
|
||||
|
||||
// 获取标签
|
||||
$tags = TagModel::withCount('articles')
|
||||
->order('create_at', 'desc')
|
||||
->limit(20)
|
||||
->select();
|
||||
|
||||
// 获取最新评论
|
||||
$latestComments = CommentModel::with(['user', 'article'])
|
||||
->where('status', 1)
|
||||
->order('create_at', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
$data = [
|
||||
'articles' => $articles,
|
||||
'hotArticles' => $hotArticles,
|
||||
'recommendedArticles' => $recommendedArticles,
|
||||
'categories' => $categories,
|
||||
'tags' => $tags,
|
||||
'latestComments' => $latestComments,
|
||||
];
|
||||
// 仅在存在文章时缓存,避免空结果被长期缓存挡住新数据
|
||||
if (! empty($articles) && ! $articles->isEmpty()) {
|
||||
Cache::set($cacheKey, $data, 3600);
|
||||
}
|
||||
}
|
||||
$this->view->assign('articles', $data['articles']);
|
||||
$this->view->assign('hotArticles', $data['hotArticles']);
|
||||
$this->view->assign('recommendedArticles', $data['recommendedArticles']);
|
||||
$this->view->assign('categories', $data['categories']);
|
||||
$this->view->assign('tags', $data['tags']);
|
||||
$this->view->assign('latestComments', $data['latestComments']);
|
||||
return $this->view->fetch('index/index');
|
||||
}
|
||||
|
||||
/**
|
||||
* 分类文章列表(无 $id 时展示全部分类)
|
||||
*/
|
||||
public function category($id = null)
|
||||
{
|
||||
if ($id === null) {
|
||||
$categories = CategoryModel::withCount(['articles' =>
|
||||
function ($query, &$alias) {
|
||||
$query->where('status', 1);
|
||||
$alias = 'card_count';
|
||||
}])->order('sort', 'asc')->select();
|
||||
return View::fetch('category/index', [
|
||||
'category' => null,
|
||||
'categories' => $categories,
|
||||
]);
|
||||
}
|
||||
|
||||
$category = CategoryModel::find($id);
|
||||
if (! $category) {
|
||||
return $this->result->error('分类不存在');
|
||||
}
|
||||
|
||||
$page = Request::param('page', 1);
|
||||
$pageSize = 10;
|
||||
|
||||
$articles = ArticleModel::with(['user', 'tags'])
|
||||
->where('cid', $id)
|
||||
->where('status', 1)
|
||||
->order('create_at', 'desc')
|
||||
->paginate([
|
||||
'list_rows' => $pageSize,
|
||||
'page' => $page,
|
||||
]);
|
||||
|
||||
return View::fetch('category/index', [
|
||||
'category' => $category,
|
||||
'articles' => $articles,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 标签文章列表(无 $id 时展示全部标签)
|
||||
*/
|
||||
public function tag($id = null)
|
||||
{
|
||||
if ($id === null) {
|
||||
$tags = TagModel::withCount('articles')->order('create_at', 'desc')->select();
|
||||
return View::fetch('tag/index', [
|
||||
'tag' => null,
|
||||
'tags' => $tags,
|
||||
]);
|
||||
}
|
||||
|
||||
$tag = TagModel::find($id);
|
||||
if (! $tag) {
|
||||
return $this->result->error('标签不存在');
|
||||
}
|
||||
|
||||
$page = Request::param('page', 1);
|
||||
$pageSize = 10;
|
||||
|
||||
$articles = $tag->articles()
|
||||
->with(['user', 'category'])
|
||||
->where('status', 1)
|
||||
->order('create_at', 'desc')
|
||||
->paginate([
|
||||
'list_rows' => $pageSize,
|
||||
'page' => $page,
|
||||
]);
|
||||
|
||||
return View::fetch('tag/index', [
|
||||
'tag' => $tag,
|
||||
'articles' => $articles,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索页面
|
||||
*/
|
||||
public function search()
|
||||
{
|
||||
$keyword = Request::param('keyword', '');
|
||||
if (empty($keyword)) {
|
||||
return $this->result->error('请输入搜索关键词');
|
||||
}
|
||||
|
||||
$page = Request::param('page', 1);
|
||||
$pageSize = 10;
|
||||
|
||||
$articles = \addon\blog\service\BlogSearchService::search($keyword, (int) $page, $pageSize);
|
||||
|
||||
return View::fetch('index/search', [
|
||||
'keyword' => $keyword,
|
||||
'articles' => $articles,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关于页面
|
||||
*/
|
||||
public function about()
|
||||
{
|
||||
return View::fetch('index/about');
|
||||
}
|
||||
|
||||
/**
|
||||
* 联系页面
|
||||
*/
|
||||
public function contact()
|
||||
{
|
||||
return View::fetch('index/contact');
|
||||
}
|
||||
|
||||
/**
|
||||
* 附近文章(GEO/LBS)
|
||||
*/
|
||||
public function nearby()
|
||||
{
|
||||
$lat = (float) Request::param('lat', 0);
|
||||
$lng = (float) Request::param('lng', 0);
|
||||
$radius = (float) Request::param('radius', 10);
|
||||
$list = \addon\blog\service\BlogSearchService::nearby($lat, $lng, $radius, 30);
|
||||
$data = [];
|
||||
foreach ($list as $a) {
|
||||
$data[] = [
|
||||
'id' => $a->id,
|
||||
'title' => $a->title,
|
||||
'distance' => isset($a->distance_km) ? round($a->distance_km, 2) : null,
|
||||
'category' => $a->category ? $a->category->name : '',
|
||||
'url' => url('blog/article/detail', ['id' => $a->id]),
|
||||
];
|
||||
}
|
||||
if (Request::isAjax()) {
|
||||
return json(['code' => 1, 'data' => $data]);
|
||||
}
|
||||
return View::fetch('index/nearby', [
|
||||
'list' => $data,
|
||||
'lat' => $lat,
|
||||
'lng' => $lng,
|
||||
'radius' => $radius,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller;
|
||||
use ywxapp\controller\FrontendBase;
|
||||
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use addon\blog\model\BlogLike as LikeModel;
|
||||
use addon\blog\model\BlogFavorite as FavoriteModel;
|
||||
use think\facade\Request;
|
||||
|
||||
/**
|
||||
* Interaction 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Interaction extends FrontendBase
|
||||
{
|
||||
/**
|
||||
* 公开访问(登录校验由方法内部通过 $this->auth 处理,会员统一以 uid 标识)
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedLogin = ['*'];
|
||||
|
||||
/**
|
||||
* 点赞/取消点赞
|
||||
*/
|
||||
public function like()
|
||||
{
|
||||
$article_id = Request::param('article_id');
|
||||
if (! $article_id) {
|
||||
return json(['code' => 0, 'msg' => '参数错误']);
|
||||
}
|
||||
if (! $this->auth->isLogin) {
|
||||
return json(['code' => 0, 'msg' => '请先登录']);
|
||||
}
|
||||
$uid = $this->auth->model->uid;
|
||||
|
||||
$article = ArticleModel::find($article_id);
|
||||
if (! $article) {
|
||||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||||
}
|
||||
|
||||
$like = LikeModel::where('uid', $uid)
|
||||
->where('aid', $article_id)
|
||||
->find();
|
||||
|
||||
if ($like) {
|
||||
$like->delete();
|
||||
$article->like_count = max(0, $article->like_count - 1);
|
||||
$action = 'cancel';
|
||||
} else {
|
||||
$like = new LikeModel();
|
||||
$like->uid = $uid;
|
||||
$like->aid = $article_id;
|
||||
$like->save();
|
||||
$article->like_count += 1;
|
||||
$action = 'add';
|
||||
}
|
||||
|
||||
$article->save();
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => $action == 'add' ? '点赞成功' : '已取消点赞',
|
||||
'count' => $article->like_count,
|
||||
'action' => $action,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收藏/取消收藏
|
||||
*/
|
||||
public function favorite()
|
||||
{
|
||||
$article_id = Request::param('article_id');
|
||||
if (! $article_id) {
|
||||
return json(['code' => 0, 'msg' => '参数错误']);
|
||||
}
|
||||
if (! $this->auth->isLogin) {
|
||||
return json(['code' => 0, 'msg' => '请先登录']);
|
||||
}
|
||||
$uid = $this->auth->model->uid;
|
||||
|
||||
$article = ArticleModel::find($article_id);
|
||||
if (! $article) {
|
||||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||||
}
|
||||
|
||||
$favorite = FavoriteModel::where('uid', $uid)
|
||||
->where('aid', $article_id)
|
||||
->find();
|
||||
|
||||
if ($favorite) {
|
||||
$favorite->delete();
|
||||
$article->favorite_count = max(0, $favorite->favorite_count - 1);
|
||||
$action = 'cancel';
|
||||
} else {
|
||||
$favorite = new FavoriteModel();
|
||||
$favorite->uid = $uid;
|
||||
$favorite->aid = $article_id;
|
||||
$favorite->save();
|
||||
$article->favorite_count += 1;
|
||||
$action = 'add';
|
||||
}
|
||||
|
||||
$article->save();
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => $action == 'add' ? '收藏成功' : '已取消收藏',
|
||||
'count' => $article->favorite_count,
|
||||
'action' => $action,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分享计数 +1(无需登录)
|
||||
*/
|
||||
public function share()
|
||||
{
|
||||
$article_id = Request::param('article_id');
|
||||
$article = ArticleModel::find($article_id);
|
||||
if (! $article) {
|
||||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||||
}
|
||||
|
||||
$article->share_count += 1;
|
||||
$article->save();
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => '已记录分享',
|
||||
'count' => $article->share_count,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户互动状态
|
||||
*/
|
||||
public function getUserInteractions($article_id)
|
||||
{
|
||||
$interactions = [
|
||||
'liked' => false,
|
||||
'favorited' => false,
|
||||
];
|
||||
|
||||
if ($this->auth->isLogin) {
|
||||
$uid = $this->auth->model->uid;
|
||||
$interactions['liked'] = LikeModel::where('uid', $uid)
|
||||
->where('aid', $article_id)
|
||||
->count() > 0;
|
||||
|
||||
$interactions['favorited'] = FavoriteModel::where('uid', $uid)
|
||||
->where('aid', $article_id)
|
||||
->count() > 0;
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'data' => $interactions,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取热门互动文章
|
||||
*/
|
||||
public function getHotInteractions()
|
||||
{
|
||||
$type = Request::param('type', 'like');
|
||||
$limit = Request::param('limit', 10);
|
||||
|
||||
$field = $type == 'like' ? 'like_count' : 'favorite_count';
|
||||
|
||||
$articles = ArticleModel::with(['user', 'category'])
|
||||
->where('status', 1)
|
||||
->order($field, 'desc')
|
||||
->limit($limit)
|
||||
->select();
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'data' => $articles,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller;
|
||||
use ywxapp\controller\FrontendBase;
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use think\facade\Config;
|
||||
use think\facade\Response;
|
||||
use think\facade\View;
|
||||
|
||||
/**
|
||||
* Rss 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Rss extends FrontendBase
|
||||
{
|
||||
/**
|
||||
* Summary of needLogin
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedLogin = ['*'];
|
||||
|
||||
/**
|
||||
* Summary of needRight
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
/**
|
||||
* 控制器初始化 _initialize
|
||||
* @return void
|
||||
*/
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
|
||||
/**
|
||||
* RSS订阅页面
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$page = Request::param('page', 1);
|
||||
$pageSize = 20;
|
||||
|
||||
$articles = ArticleModel::with(['user', 'category'])
|
||||
->where('status', 1)
|
||||
->order('create_at', 'desc')
|
||||
->page($page, $pageSize)
|
||||
->select();
|
||||
|
||||
$siteName = Config::get('site.name', 'TP-Blog');
|
||||
$siteUrl = Config::get('site.url', 'http://localhost');
|
||||
$description = Config::get('site.description', 'TP-Blog是一个基于ThinkPHP的博客系统');
|
||||
|
||||
$xml = $this->generateRssXml($articles, $siteName, $siteUrl, $description);
|
||||
|
||||
return Response::create($xml, 'xml');
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成RSS XML
|
||||
*/
|
||||
private function generateRssXml($articles, $siteName, $siteUrl, $description)
|
||||
{
|
||||
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
|
||||
$xml .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
|
||||
$xml .= '<channel>';
|
||||
$xml .= '<title>' . htmlspecialchars($siteName) . '</title>';
|
||||
$xml .= '<link>' . htmlspecialchars($siteUrl) . '</link>';
|
||||
$xml .= '<description>' . htmlspecialchars($description) . '</description>';
|
||||
$xml .= '<language>zh-cn</language>';
|
||||
$xml .= '<lastBuildDate>' . date('r') . '</lastBuildDate>';
|
||||
$xml .= '<atom:link href="' . htmlspecialchars($siteUrl) . '/rss" rel="self" type="application/rss+xml" />';
|
||||
|
||||
foreach ($articles as $article) {
|
||||
$xml .= '<item>';
|
||||
$xml .= '<title>' . htmlspecialchars($article->title) . '</title>';
|
||||
$xml .= '<link>' . htmlspecialchars($siteUrl . '/article/' . $article->id) . '</link>';
|
||||
$xml .= '<description>' . htmlspecialchars($article->summary ?: strip_tags(mb_substr($article->content, 0, 200))) . '</description>';
|
||||
$xml .= '<pubDate>' . date('r', $article->create_at) . '</pubDate>';
|
||||
$xml .= '<author>' . htmlspecialchars($article->user->nickname) . '</author>';
|
||||
$xml .= '<category>' . htmlspecialchars($article->category->name) . '</category>';
|
||||
$xml .= '<guid>' . htmlspecialchars($siteUrl . '/article/' . $article->id) . '</guid>';
|
||||
$xml .= '</item>';
|
||||
}
|
||||
|
||||
$xml .= '</channel>';
|
||||
$xml .= '</rss>';
|
||||
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* RSS阅读器页面
|
||||
*/
|
||||
public function reader()
|
||||
{
|
||||
$siteName = Config::get('site.name', 'TP-Blog');
|
||||
$rssUrl = Config::get('site.url', 'http://localhost') . '/rss';
|
||||
|
||||
return View::fetch('index/rss/reader', [
|
||||
'siteName' => $siteName,
|
||||
'rssUrl' => $rssUrl
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\backend;
|
||||
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use addon\blog\model\BlogCategory as CategoryModel;
|
||||
use addon\blog\model\BlogTag as TagModel;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use think\Validate;
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
* Article 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Article extends BackendBase
|
||||
{
|
||||
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 文章列表(ajax 请求返回 layui table 所需 JSON,普通请求渲染视图)
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$status = Request::param('status', 'all');
|
||||
$audit = Request::param('audit', 'all');
|
||||
$keyword = Request::param('keyword', '');
|
||||
$page = Request::param('page', 1);
|
||||
$limit = Request::param('limit', 15);
|
||||
|
||||
if (Request::isAjax()) {
|
||||
$query = ArticleModel::with(['user', 'category', 'tags']);
|
||||
|
||||
if ($status !== 'all') {
|
||||
$query->where('status', $status);
|
||||
}
|
||||
if ($audit !== 'all' && in_array((int) $audit, [0, 1, 2, 3], true)) {
|
||||
$query->where('audit_status', (int) $audit);
|
||||
}
|
||||
if ($keyword) {
|
||||
$query->where('title|content', 'like', "%{$keyword}%");
|
||||
}
|
||||
|
||||
$paginator = $query->order('create_at', 'desc')
|
||||
->paginate(['list_rows' => $limit, 'page' => $page]);
|
||||
|
||||
$rows = [];
|
||||
foreach ($paginator->items() as $a) {
|
||||
$rows[] = [
|
||||
'id' => $a->id,
|
||||
'title' => $a->title,
|
||||
'is_top' => $a->is_top,
|
||||
'author' => $a->user->nickname ?? '-',
|
||||
'category' => $a->category->name ?? '-',
|
||||
'tags' => implode(',', $a->tags->column('name')),
|
||||
'status' => $a->status,
|
||||
'audit_status' => $a->audit_status,
|
||||
'view_count' => (int) $a->view_count,
|
||||
'comment_count' => (int) $a->comment_count,
|
||||
'like_count' => (int) $a->like_count,
|
||||
'favorite_count' => (int) $a->favorite_count,
|
||||
'share_count' => (int) $a->share_count,
|
||||
'create_at' => $a->create_at,
|
||||
'create_text' => (function ($t) {
|
||||
if ($t instanceof \DateTime) return $t->format('Y-m-d H:i');
|
||||
if (is_numeric($t)) return date('Y-m-d H:i', (int) $t);
|
||||
$ts = strtotime((string) $t);
|
||||
return $ts !== false ? date('Y-m-d H:i', $ts) : (string) $t;
|
||||
})($a->create_at),
|
||||
'edit_url' => (string) url('blog/backend.article/edit', ['id' => $a->id]),
|
||||
];
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'count' => $paginator->total(),
|
||||
'data' => $rows,
|
||||
]);
|
||||
}
|
||||
|
||||
return View::fetch('article/index', [
|
||||
'status' => $status,
|
||||
'audit' => $audit,
|
||||
'keyword' => $keyword,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑文章页面
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
$article = ArticleModel::with(['tags'])->find($id);
|
||||
if (! $article) {
|
||||
return redirect('/blog/backend/article');
|
||||
}
|
||||
|
||||
$categories = CategoryModel::where('status', 1)->select();
|
||||
$tags = TagModel::select();
|
||||
$articleTags = $article->tags->column('id');
|
||||
$tagNamesStr = implode(',', $article->tags->column('name'));
|
||||
|
||||
return View::fetch('article/edit', [
|
||||
'article' => $article,
|
||||
'categories' => $categories,
|
||||
'tags' => $tags,
|
||||
'articleTags' => $articleTags,
|
||||
'tagNamesStr' => $tagNamesStr,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文章
|
||||
*/
|
||||
public function update($id)
|
||||
{
|
||||
$article = ArticleModel::find($id);
|
||||
if (! $article) {
|
||||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||||
}
|
||||
|
||||
$data = Request::post();
|
||||
$data['update_at'] = time();
|
||||
$data['uid'] = $data['user_id'] ?? $article->uid;
|
||||
$data['is_top'] = isset($data['is_top']) ? 1 : 0;
|
||||
unset($data['user_id']);
|
||||
|
||||
$validate = new Validate([
|
||||
'title|标题' => 'require|max:255',
|
||||
'content|内容' => 'require',
|
||||
'uid|作者' => 'require|number',
|
||||
'cid|分类' => 'require|number',
|
||||
]);
|
||||
|
||||
if (! $validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
// 处理标签
|
||||
$tagIds = [];
|
||||
if (! empty($data['tags'])) {
|
||||
$tagNames = is_array($data['tags']) ? $data['tags'] : explode(',', $data['tags']);
|
||||
foreach ($tagNames as $name) {
|
||||
$name = trim($name);
|
||||
if ($name === '') {
|
||||
continue;
|
||||
}
|
||||
$tag = TagModel::where('name', $name)->find() ?: TagModel::create(['name' => $name]);
|
||||
$tagIds[] = $tag->id;
|
||||
}
|
||||
}
|
||||
|
||||
$article->save($data);
|
||||
$article->tags()->sync($tagIds);
|
||||
\addon\blog\service\BlogSearchService::sync($article->id);
|
||||
|
||||
return json(['code' => 1, 'msg' => '文章更新成功', 'url' => '/blog/backend/article']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文章
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$article = ArticleModel::find($id);
|
||||
if ($article) {
|
||||
\addon\blog\service\BlogSearchService::remove($article->id);
|
||||
$article->tags()->detach();
|
||||
$article->delete();
|
||||
return json(['code' => 1, 'msg' => '文章已删除']);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量操作
|
||||
*/
|
||||
public function batch()
|
||||
{
|
||||
$action = Request::post('action');
|
||||
$ids = Request::post('ids/a');
|
||||
|
||||
if (empty($ids)) {
|
||||
return json(['code' => 0, 'msg' => '请选择文章']);
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'delete':
|
||||
ArticleModel::destroy($ids);
|
||||
return json(['code' => 1, 'msg' => '批量删除成功']);
|
||||
case 'publish':
|
||||
ArticleModel::whereIn('id', $ids)->update(['status' => 1]);
|
||||
return json(['code' => 1, 'msg' => '批量发布成功']);
|
||||
case 'draft':
|
||||
ArticleModel::whereIn('id', $ids)->update(['status' => 0]);
|
||||
return json(['code' => 1, 'msg' => '批量设为草稿成功']);
|
||||
default:
|
||||
return json(['code' => 0, 'msg' => '无效操作']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 审核通过(人工复核):置通过态并对外可见
|
||||
*/
|
||||
public function audit($id = 0)
|
||||
{
|
||||
$article = ArticleModel::find($id);
|
||||
if (! $article) {
|
||||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||||
}
|
||||
$article->audit_status = 2;
|
||||
$article->status = 1;
|
||||
$article->save();
|
||||
return json(['code' => 1, 'msg' => '审核通过']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 审核驳回:置驳回态并下架
|
||||
*/
|
||||
public function reject($id = 0)
|
||||
{
|
||||
$article = ArticleModel::find($id);
|
||||
if (! $article) {
|
||||
return json(['code' => 0, 'msg' => '文章不存在']);
|
||||
}
|
||||
$reason = trim((string) Request::post('reason', ''));
|
||||
$article->audit_status = 3;
|
||||
$article->status = 0;
|
||||
$article->save();
|
||||
return json(['code' => 1, 'msg' => '已驳回' . ($reason !== '' ? ':' . $reason : '')]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\backend;
|
||||
|
||||
use addon\blog\model\BlogCategory as CategoryModel;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use think\Validate;
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
* Category 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Category extends BackendBase
|
||||
{
|
||||
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 分类列表(后台管理全部分类:全局共享 + 各会员自建)
|
||||
* 以 layui treeTable 所需的扁平结构返回(带 pid / is_parent),由前端 treeTable 自动成树。
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$categories = CategoryModel::withCount('articles')
|
||||
->order('uid', 'asc')
|
||||
->order('sort', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 收集归属会员昵称
|
||||
$uids = [];
|
||||
foreach ($categories as $c) {
|
||||
if (!empty($c['uid'])) {
|
||||
$uids[] = $c['uid'];
|
||||
}
|
||||
}
|
||||
$owners = [];
|
||||
if ($uids) {
|
||||
$owners = \think\facade\Db::name('member')
|
||||
->whereIn('uid', array_unique($uids))
|
||||
->column('nickname', 'uid');
|
||||
}
|
||||
|
||||
// 计算哪些分类拥有子分类(用于 treeTable 的 is_parent 标记)
|
||||
$parentIds = array_unique(array_filter(array_column($categories, 'pid')));
|
||||
|
||||
$treeData = [];
|
||||
foreach ($categories as $c) {
|
||||
$treeData[] = [
|
||||
'id' => $c['id'],
|
||||
'pid' => $c['pid'],
|
||||
'title' => $c['name'], // treeTable 节点显示名称
|
||||
'name' => $c['name'],
|
||||
'description' => $c['description'] ?? '',
|
||||
'sort' => $c['sort'],
|
||||
'status' => $c['status'],
|
||||
'is_parent' => in_array($c['id'], $parentIds, true) ? 1 : 0,
|
||||
'owner' => empty($c['uid']) ? '全局共享' : ($owners[$c['uid']] ?? ('#' . $c['uid'])),
|
||||
'articles_count' => $c['articles_count'] ?? 0,
|
||||
];
|
||||
}
|
||||
|
||||
return View::fetch('category/index', [
|
||||
'treeData' => json_encode($treeData, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加分类页面
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
$categories = CategoryModel::order('sort', 'asc')->select();
|
||||
return View::fetch('category/add', ['categories' => $categories]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存分类
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$data = Request::post();
|
||||
$data['uid'] = 0; // 后台管理的分类为全局共享分类
|
||||
$data['pid'] = (int) Request::post('pid', 0);
|
||||
$data['create_at'] = time();
|
||||
$data['update_at'] = time();
|
||||
|
||||
$validate = new Validate([
|
||||
'name|分类名称' => 'require|unique:BlogCategory,name,0,id,uid,0',
|
||||
'pid|上级分类' => 'number',
|
||||
'sort|排序' => 'number|between:0,999',
|
||||
]);
|
||||
|
||||
if (! $validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
CategoryModel::create($data);
|
||||
return json(['code' => 1, 'msg' => '分类添加成功', 'url' => '/blog/backend/category']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑分类页面
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
$category = CategoryModel::find($id);
|
||||
$categories = CategoryModel::order('sort', 'asc')->select();
|
||||
return View::fetch('category/edit', ['category' => $category, 'categories' => $categories]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新分类
|
||||
*/
|
||||
public function update($id)
|
||||
{
|
||||
$category = CategoryModel::find($id);
|
||||
if (! $category) {
|
||||
return json(['code' => 0, 'msg' => '分类不存在']);
|
||||
}
|
||||
|
||||
if ((int) Request::post('pid', 0) === (int) $id) {
|
||||
return json(['code' => 0, 'msg' => '上级分类不能选择自己']);
|
||||
}
|
||||
|
||||
$data = Request::post();
|
||||
$data['pid'] = (int) Request::post('pid', 0);
|
||||
$data['update_at'] = time();
|
||||
|
||||
$validate = new Validate([
|
||||
'name|分类名称' => 'require|unique:BlogCategory,name,' . $id . ',id,uid,' . $category->uid,
|
||||
'pid|上级分类' => 'number',
|
||||
'sort|排序' => 'number|between:0,999',
|
||||
]);
|
||||
|
||||
if (! $validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
$category->save($data);
|
||||
return json(['code' => 1, 'msg' => '分类更新成功', 'url' => '/blog/backend/category']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除分类
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$category = CategoryModel::find($id);
|
||||
if ($category) {
|
||||
if ($category->children()->count() > 0) {
|
||||
return json(['code' => 0, 'msg' => '该分类下有子分类,请先删除子分类']);
|
||||
}
|
||||
if ($category->articles()->count() > 0) {
|
||||
return json(['code' => 0, 'msg' => '该分类下有文章,无法删除']);
|
||||
}
|
||||
$category->delete();
|
||||
return json(['code' => 1, 'msg' => '分类已删除']);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '分类不存在']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除分类(前端 treeTable 勾选后调用)
|
||||
*/
|
||||
public function batchDelete()
|
||||
{
|
||||
$ids = Request::post('ids', '');
|
||||
$idArr = array_filter(array_map('intval', explode(',', (string) $ids)));
|
||||
if (empty($idArr)) {
|
||||
return json(['code' => 0, 'msg' => '请选择要删除的分类']);
|
||||
}
|
||||
|
||||
$deleted = 0;
|
||||
$skipped = 0;
|
||||
foreach ($idArr as $id) {
|
||||
$category = CategoryModel::find($id);
|
||||
if (! $category) {
|
||||
continue;
|
||||
}
|
||||
if ($category->children()->count() > 0) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
if ($category->articles()->count() > 0) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
$category->delete();
|
||||
$deleted++;
|
||||
}
|
||||
|
||||
if ($deleted === 0) {
|
||||
return json(['code' => 0, 'msg' => '选中的分类均包含子分类或文章,无法删除']);
|
||||
}
|
||||
$msg = '成功删除 ' . $deleted . ' 个分类';
|
||||
if ($skipped > 0) {
|
||||
$msg .= ',' . $skipped . ' 个因含子分类/文章已跳过';
|
||||
}
|
||||
return json(['code' => 1, 'msg' => $msg]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\backend;
|
||||
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use addon\blog\model\BlogComment as CommentModel;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
* Comment 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Comment extends BackendBase
|
||||
{
|
||||
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 评论列表(ajax 请求返回 layui table 所需 JSON,普通请求渲染视图)
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$status = Request::param('status', 'all');
|
||||
$keyword = Request::param('keyword', '');
|
||||
$page = Request::param('page', 1);
|
||||
$limit = Request::param('limit', 15);
|
||||
|
||||
if (Request::isAjax()) {
|
||||
$query = CommentModel::with(['user', 'article']);
|
||||
|
||||
if ($status !== 'all') {
|
||||
$query->where('status', $status);
|
||||
}
|
||||
if ($keyword) {
|
||||
$query->where('content', 'like', "%{$keyword}%");
|
||||
}
|
||||
|
||||
$paginator = $query->order('create_at', 'desc')
|
||||
->paginate(['list_rows' => $limit, 'page' => $page]);
|
||||
|
||||
$rows = [];
|
||||
foreach ($paginator->items() as $c) {
|
||||
$createText = (function ($t) {
|
||||
if ($t instanceof \DateTime) return $t->format('Y-m-d H:i');
|
||||
if (is_numeric($t)) return date('Y-m-d H:i', (int) $t);
|
||||
$ts = strtotime((string) $t);
|
||||
return $ts !== false ? date('Y-m-d H:i', $ts) : (string) $t;
|
||||
})($c->create_at);
|
||||
|
||||
$rows[] = [
|
||||
'id' => $c->id,
|
||||
'nickname' => $c->user->nickname ?? '-',
|
||||
'avatar' => $c->user->avatar ?? '',
|
||||
'content' => $c->content,
|
||||
'article_id' => $c->aid,
|
||||
'article_title' => $c->article->title ?? '-',
|
||||
'article_url' => $c->article ? (string) url('blog/backend.article/edit', ['id' => $c->aid]) : '',
|
||||
'status' => $c->status,
|
||||
'create_text' => $createText,
|
||||
];
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'count' => $paginator->total(),
|
||||
'data' => $rows,
|
||||
]);
|
||||
}
|
||||
|
||||
return View::fetch('comment/index', [
|
||||
'status' => $status,
|
||||
'keyword' => $keyword,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 审核通过
|
||||
*/
|
||||
public function approve($id)
|
||||
{
|
||||
$comment = CommentModel::find($id);
|
||||
if ($comment) {
|
||||
$comment->status = 1;
|
||||
$comment->save();
|
||||
|
||||
ArticleModel::where('id', $comment->aid)->inc('comment_count')->update();
|
||||
|
||||
return json(['code' => 1, 'msg' => '评论已通过']);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '评论不存在']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拒绝评论
|
||||
*/
|
||||
public function reject($id)
|
||||
{
|
||||
$comment = CommentModel::find($id);
|
||||
if ($comment) {
|
||||
$comment->status = 0;
|
||||
$comment->save();
|
||||
|
||||
ArticleModel::where('id', $comment->aid)->dec('comment_count')->update();
|
||||
|
||||
return json(['code' => 1, 'msg' => '评论已拒绝']);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '评论不存在']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除评论
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$comment = CommentModel::find($id);
|
||||
if ($comment) {
|
||||
$this->deleteChildren($id);
|
||||
|
||||
ArticleModel::where('id', $comment->aid)->dec('comment_count')->update();
|
||||
|
||||
return json(['code' => 1, 'msg' => '评论已删除']);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '评论不存在']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归删除子评论
|
||||
*/
|
||||
private function deleteChildren($parentId)
|
||||
{
|
||||
$children = CommentModel::where('pid', $parentId)->select();
|
||||
foreach ($children as $child) {
|
||||
$this->deleteChildren($child->id);
|
||||
$child->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量操作
|
||||
*/
|
||||
public function batch()
|
||||
{
|
||||
$action = Request::post('action');
|
||||
$ids = Request::post('ids/a');
|
||||
|
||||
if (empty($ids)) {
|
||||
return json(['code' => 0, 'msg' => '请选择评论']);
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'approve':
|
||||
CommentModel::whereIn('id', $ids)->update(['status' => 1]);
|
||||
return json(['code' => 1, 'msg' => '批量通过成功']);
|
||||
case 'reject':
|
||||
CommentModel::whereIn('id', $ids)->update(['status' => 0]);
|
||||
return json(['code' => 1, 'msg' => '批量拒绝成功']);
|
||||
case 'delete':
|
||||
CommentModel::destroy($ids);
|
||||
return json(['code' => 1, 'msg' => '批量删除成功']);
|
||||
default:
|
||||
return json(['code' => 0, 'msg' => '无效操作']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\backend;
|
||||
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use ywxapp\model\MemberUser as UserModel;
|
||||
use addon\blog\model\BlogComment as CommentModel;
|
||||
use addon\blog\model\BlogVisit as VisitModel;
|
||||
use addon\blog\model\BlogLike as LikeModel;
|
||||
use think\facade\View;
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
* Dashboard 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Dashboard extends BackendBase
|
||||
{
|
||||
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
protected function initialize()
|
||||
{
|
||||
}
|
||||
/**
|
||||
* 今日起始时间戳
|
||||
*/
|
||||
private function todayStart()
|
||||
{
|
||||
return strtotime(date('Y-m-d'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 仪表盘首页
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$today = $this->todayStart();
|
||||
|
||||
$stats = [
|
||||
'article_count' => ArticleModel::count(),
|
||||
'user_count' => UserModel::count(),
|
||||
'comment_count' => CommentModel::count(),
|
||||
'today_article' => ArticleModel::where('create_at', '>=', $today)->count(),
|
||||
'today_user' => UserModel::where('create_at', '>=', $today)->count(),
|
||||
'today_comment' => CommentModel::where('create_at', '>=', $today)->count(),
|
||||
'today_visit' => VisitModel::where('create_at', '>=', $today)->count(),
|
||||
'total_visit' => VisitModel::count(),
|
||||
];
|
||||
|
||||
$latestArticles = ArticleModel::with(['user', 'category'])
|
||||
->order('create_at', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
|
||||
$latestComments = CommentModel::with(['user', 'article'])
|
||||
->order('create_at', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
|
||||
$hotArticles = ArticleModel::with(['user', 'category'])
|
||||
->order('view_count', 'desc')
|
||||
->limit(5)
|
||||
->select();
|
||||
|
||||
$visitTrend = [];
|
||||
for ($i = 6; $i >= 0; $i--) {
|
||||
$date = date('Y-m-d', strtotime("-{$i} days"));
|
||||
$count = VisitModel::whereBetween('create_at', [strtotime($date), strtotime($date . ' +1 day') - 1])->count();
|
||||
$visitTrend[] = ['date' => $date, 'count' => $count];
|
||||
}
|
||||
|
||||
return $this->view->fetch('dashboard/index', [
|
||||
'stats' => $stats,
|
||||
'latestArticles' => $latestArticles,
|
||||
'latestComments' => $latestComments,
|
||||
'hotArticles' => $hotArticles,
|
||||
'visitTrend' => $visitTrend,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\backend;
|
||||
|
||||
use think\facade\View;
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
* 插件后台入口页(addon/blog/controller/admin/Index.php)
|
||||
*
|
||||
* 继承 AddonBackend,并处于 route/app.php 的 admin 路由组中,
|
||||
* 该组已挂 \addon\blog\middleware\AdminAuth 中间件,自动将 auth 绑定为管理员并强制登录。
|
||||
*/
|
||||
class Index extends BackendBase
|
||||
{
|
||||
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 后台首页
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$admin = $this->auth->info ?? null;
|
||||
// 同时赋值 user,供 layout.html 头部的 {$user.nickname} 使用
|
||||
$this->view->assign('member', $admin);
|
||||
|
||||
return View::fetch('index/index', [
|
||||
'backend' => $admin,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\backend;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use think\facade\Config;
|
||||
use think\facade\Cache;
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
* Setting 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Setting extends BackendBase
|
||||
{
|
||||
/**
|
||||
* Summary of needLogin
|
||||
* @var array
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Summary of needRight
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
/**
|
||||
* 控制器初始化 _initialize
|
||||
* @return void
|
||||
*/
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 系统设置首页
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$settings = [
|
||||
'site' => Config::get('site'),
|
||||
'seo' => Config::get('seo'),
|
||||
'email' => Config::get('email'),
|
||||
'upload' => Config::get('upload')
|
||||
];
|
||||
|
||||
return View::fetch('setting/index', ['settings' => $settings]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 基本设置
|
||||
*/
|
||||
public function site()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$data = Request::post();
|
||||
$this->saveConfig('site', $data);
|
||||
return json(['code' => 1, 'msg' => '基本设置已保存']);
|
||||
}
|
||||
|
||||
$settings = Config::get('site');
|
||||
return View::fetch('setting/site', ['settings' => $settings]);
|
||||
}
|
||||
|
||||
/**
|
||||
* SEO设置
|
||||
*/
|
||||
public function seo()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$data = Request::post();
|
||||
$this->saveConfig('seo', $data);
|
||||
return json(['code' => 1, 'msg' => 'SEO设置已保存']);
|
||||
}
|
||||
|
||||
$settings = Config::get('seo');
|
||||
return View::fetch('setting/seo', ['settings' => $settings]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 邮件设置
|
||||
*/
|
||||
public function email()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$data = Request::post();
|
||||
$this->saveConfig('email', $data);
|
||||
return json(['code' => 1, 'msg' => '邮件设置已保存']);
|
||||
}
|
||||
|
||||
$settings = Config::get('email');
|
||||
return View::fetch('setting/email', ['settings' => $settings]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存配置
|
||||
*/
|
||||
private function saveConfig($key, $data)
|
||||
{
|
||||
$configFile = app_path() . 'config/' . $key . '.php';
|
||||
$content = "<?php\nreturn " . var_export($data, true) . ";\n";
|
||||
file_put_contents($configFile, $content);
|
||||
|
||||
// 清除配置缓存
|
||||
Config::set([], $key);
|
||||
Cache::delete('config_' . $key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\backend;
|
||||
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use addon\blog\model\BlogComment as CommentModel;
|
||||
use ywxapp\model\MemberUser as UserModel;
|
||||
use addon\blog\model\BlogVisit as VisitModel;
|
||||
use think\facade\Db;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
* Stat 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Stat extends BackendBase
|
||||
{
|
||||
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 今日起始时间戳
|
||||
*/
|
||||
private function todayStart()
|
||||
{
|
||||
return strtotime(date('Y-m-d'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计分析首页
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$range = request()->get('range', 'week');
|
||||
$startTime = $this->getTimeRange($range);
|
||||
$today = $this->todayStart();
|
||||
|
||||
$stats = [
|
||||
'total_visit' => VisitModel::count(),
|
||||
'today_visit' => VisitModel::where('create_at', '>=', $today)->count(),
|
||||
'total_article' => ArticleModel::count(),
|
||||
'today_article' => ArticleModel::where('create_at', '>=', $today)->count(),
|
||||
'total_user' => UserModel::count(),
|
||||
'today_user' => UserModel::where('create_at', '>=', $today)->count(),
|
||||
'total_comment' => CommentModel::count(),
|
||||
'today_comment' => CommentModel::where('create_at', '>=', $today)->count(),
|
||||
];
|
||||
|
||||
$visitTrend = $this->getTrendData('visit', $startTime, $range);
|
||||
$articleTrend = $this->getTrendData('article', $startTime, $range);
|
||||
$userTrend = $this->getTrendData('member', $startTime, $range);
|
||||
|
||||
$hotPages = VisitModel::field('url, count(*) as count')
|
||||
->where('create_at', '>=', $startTime)
|
||||
->group('url')
|
||||
->order('count', 'desc')
|
||||
->limit(10)
|
||||
->select();
|
||||
|
||||
$hotArticles = ArticleModel::with(['author'])
|
||||
->where('create_at', '>=', $startTime)
|
||||
->order('view_count', 'desc')
|
||||
->limit(10)
|
||||
->select();
|
||||
|
||||
return View::fetch('stat/index', [
|
||||
'stats' => $stats,
|
||||
'visitTrend' => $visitTrend,
|
||||
'articleTrend' => $articleTrend,
|
||||
'userTrend' => $userTrend,
|
||||
'hotPages' => $hotPages,
|
||||
'hotArticles' => $hotArticles,
|
||||
'range' => $range,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间范围
|
||||
*/
|
||||
private function getTimeRange($range)
|
||||
{
|
||||
switch ($range) {
|
||||
case 'today':
|
||||
return strtotime(date('Y-m-d'));
|
||||
case 'week':
|
||||
return strtotime('-7 days');
|
||||
case 'month':
|
||||
return strtotime('-30 days');
|
||||
case 'year':
|
||||
return strtotime('-1 year');
|
||||
default:
|
||||
return strtotime('-7 days');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取趋势数据
|
||||
*/
|
||||
private function getTrendData($type, $startTime, $range)
|
||||
{
|
||||
$dateFormat = $range == 'year' ? '%Y-%m' : '%Y-%m-%d';
|
||||
|
||||
$table = [
|
||||
'visit' => 'blog_visit',
|
||||
'article' => 'blog_article',
|
||||
'member' => 'member',
|
||||
][$type];
|
||||
|
||||
$dateField = 'create_at';
|
||||
|
||||
$rows = Db::name($table)
|
||||
->field("FROM_UNIXTIME({$dateField}, '{$dateFormat}') as date, count(*) as count")
|
||||
->where($dateField, '>=', $startTime)
|
||||
->group("FROM_UNIXTIME({$dateField}, '{$dateFormat}')")
|
||||
->order('date', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return array_column($rows, 'count', 'date');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\backend;
|
||||
|
||||
use ywxapp\controller\BackendBase;
|
||||
use addon\blog\model\BlogTag as TagModel;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* Tag 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Tag extends BackendBase
|
||||
{
|
||||
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 标签列表(ajax 请求返回 layui table 所需 JSON,普通请求渲染视图)
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$page = Request::param('page', 1);
|
||||
$limit = Request::param('limit', 15);
|
||||
|
||||
if (Request::isAjax()) {
|
||||
$paginator = TagModel::withCount('articles')
|
||||
->order('create_at', 'desc')
|
||||
->paginate(['list_rows' => $limit, 'page' => $page]);
|
||||
|
||||
$rows = [];
|
||||
foreach ($paginator->items() as $t) {
|
||||
$rows[] = [
|
||||
'id' => $t->id,
|
||||
'name' => $t->name,
|
||||
'description' => $t->description,
|
||||
'articles_count' => $t->articles_count,
|
||||
'create_at' => $t->create_at,
|
||||
'create_text' => date('Y-m-d', $t->create_at),
|
||||
'edit_url' => (string) url('blog/backend.tag/edit', ['id' => $t->id]),
|
||||
];
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'count' => $paginator->total(),
|
||||
'data' => $rows,
|
||||
]);
|
||||
}
|
||||
|
||||
return View::fetch('tag/index');
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加标签页面
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
return View::fetch('tag/add');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存标签
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$data = Request::post();
|
||||
$data['create_at'] = time();
|
||||
$data['update_at'] = time();
|
||||
|
||||
$validate = new Validate([
|
||||
'name|标签名称' => 'require|unique:BlogTag',
|
||||
'description|描述' => 'max:255',
|
||||
]);
|
||||
|
||||
if (!$validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
TagModel::create($data);
|
||||
return json(['code' => 1, 'msg' => '标签添加成功', 'url' => '/blog/backend/tag']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑标签页面
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
$tag = TagModel::find($id);
|
||||
return View::fetch('tag/edit', ['tag' => $tag]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新标签
|
||||
*/
|
||||
public function update($id)
|
||||
{
|
||||
$tag = TagModel::find($id);
|
||||
if (!$tag) {
|
||||
return json(['code' => 0, 'msg' => '标签不存在']);
|
||||
}
|
||||
|
||||
$data = Request::post();
|
||||
$data['update_at'] = time();
|
||||
|
||||
$validate = new Validate([
|
||||
'name|标签名称' => 'require|unique:BlogTag,name,' . $id,
|
||||
'description|描述' => 'max:255',
|
||||
]);
|
||||
|
||||
if (!$validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
$tag->save($data);
|
||||
return json(['code' => 1, 'msg' => '标签更新成功', 'url' => '/blog/backend/tag']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除标签
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$tag = TagModel::find($id);
|
||||
if ($tag) {
|
||||
if ($tag->articles()->count() > 0) {
|
||||
return json(['code' => 0, 'msg' => '该标签下有文章,无法删除']);
|
||||
}
|
||||
$tag->delete();
|
||||
return json(['code' => 1, 'msg' => '标签已删除']);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '标签不存在']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除标签
|
||||
*/
|
||||
public function batchDelete()
|
||||
{
|
||||
$ids = Request::post('ids/a');
|
||||
if (empty($ids)) {
|
||||
return json(['code' => 0, 'msg' => '请选择标签']);
|
||||
}
|
||||
|
||||
$tags = TagModel::withCount('articles')->whereIn('id', $ids)->select();
|
||||
foreach ($tags as $tag) {
|
||||
if ($tag->articles_count > 0) {
|
||||
return json(['code' => 0, 'msg' => "标签【{$tag->name}】下有文章,无法删除"]);
|
||||
}
|
||||
}
|
||||
|
||||
TagModel::destroy($ids);
|
||||
return json(['code' => 1, 'msg' => '批量删除成功']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\backend;
|
||||
|
||||
use ywxapp\model\MemberUser as UserModel;
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
* Member 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Member extends BackendBase
|
||||
{
|
||||
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 用户列表(ajax 请求返回 layui table 所需 JSON,普通请求渲染视图)
|
||||
* 这里管理的是全站用户(博客作者 / 会员),与博客文章通过 uid 关联。
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$status = Request::param('status', 'all');
|
||||
$keyword = Request::param('keyword', '');
|
||||
$page = Request::param('page', 1);
|
||||
$limit = Request::param('limit', 15);
|
||||
|
||||
if (Request::isAjax()) {
|
||||
$query = UserModel::where('delete_at', null);
|
||||
|
||||
if ($status !== 'all') {
|
||||
$query->where('status', $status);
|
||||
}
|
||||
if ($keyword) {
|
||||
$query->where('account|nickname|email|mobile', 'like', "%{$keyword}%");
|
||||
}
|
||||
|
||||
$paginator = $query->order('create_at', 'desc')
|
||||
->paginate(['list_rows' => $limit, 'page' => $page]);
|
||||
|
||||
$items = $paginator->items();
|
||||
$uids = array_column($items, 'uid');
|
||||
$counts = [];
|
||||
if ($uids) {
|
||||
$counts = ArticleModel::whereIn('uid', $uids)
|
||||
->group('uid')
|
||||
->column('COUNT(*)', 'uid');
|
||||
}
|
||||
|
||||
$statusMap = [1 => '正常', 0 => '禁用', -1 => '删除'];
|
||||
$rows = [];
|
||||
foreach ($items as $u) {
|
||||
$rows[] = [
|
||||
'uid' => $u->uid,
|
||||
'account' => $u->account,
|
||||
'nickname' => $u->nickname,
|
||||
'email' => $u->email,
|
||||
'mobile' => $u->mobile,
|
||||
'status' => $u->status,
|
||||
'status_text' => $statusMap[$u->status] ?? '未知',
|
||||
'article_count' => $counts[$u->uid] ?? 0,
|
||||
'create_at' => $u->create_at ? date('Y-m-d H:i', $u->create_at) : '-',
|
||||
'update_at' => $u->update_at ? date('Y-m-d H:i', $u->update_at) : '从未登录',
|
||||
'edit_url' => (string) url('blog/backend.user/edit', ['id' => $u->uid]),
|
||||
];
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'count' => $paginator->total(),
|
||||
'data' => $rows,
|
||||
]);
|
||||
}
|
||||
|
||||
return View::fetch('user/index', [
|
||||
'status' => $status,
|
||||
'keyword' => $keyword,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑用户页面
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
$user = UserModel::find($id);
|
||||
if (! $user) {
|
||||
return redirect('/blog/backend/user');
|
||||
}
|
||||
|
||||
return View::fetch('user/edit', ['member' => $user]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户
|
||||
*/
|
||||
public function update($id)
|
||||
{
|
||||
$user = UserModel::find($id);
|
||||
if (! $user) {
|
||||
return json(['code' => 0, 'msg' => '用户不存在']);
|
||||
}
|
||||
|
||||
$data = Request::post();
|
||||
$user->nickname = $data['nickname'] ?? $user->nickname;
|
||||
$user->email = $data['email'] ?? $user->email;
|
||||
$user->mobile = $data['mobile'] ?? $user->mobile;
|
||||
$user->status = (int) ($data['status'] ?? $user->status);
|
||||
$user->update_at = time();
|
||||
$user->save();
|
||||
|
||||
return json(['code' => 1, 'msg' => '用户更新成功', 'url' => '/blog/backend/user']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户(软删除,Member 模型已启用 SoftDelete)
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$user = UserModel::find($id);
|
||||
if ($user) {
|
||||
$user->delete();
|
||||
return json(['code' => 1, 'msg' => '用户已删除']);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '用户不存在']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除用户
|
||||
*/
|
||||
public function batch()
|
||||
{
|
||||
$ids = Request::post('ids/a');
|
||||
if (empty($ids)) {
|
||||
return json(['code' => 0, 'msg' => '请选择用户']);
|
||||
}
|
||||
|
||||
UserModel::destroy($ids);
|
||||
return json(['code' => 1, 'msg' => '批量删除成功']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\member;
|
||||
|
||||
use addon\blog\model\BlogArticle as ArticleModel;
|
||||
use addon\blog\model\BlogCategory as CategoryModel;
|
||||
use addon\blog\model\BlogTag as TagModel;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use think\Validate;
|
||||
use ywxapp\controller\MemberBase;
|
||||
|
||||
/**
|
||||
* 会员中心文章管理(多用户模式:文章由会员在用户中心发布/管理)
|
||||
* 继承 AddonMember,处于 route/app.php 的 member 路由组中,强制会员登录。
|
||||
* 文章作者固定为当前登录会员(uid = $this->auth->info->id)。
|
||||
*/
|
||||
class Article extends MemberBase
|
||||
{
|
||||
protected $noNeedLogin = [];
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
// 当前登录会员 id(文章作者);会员主键为 uid,需从模型取 uid
|
||||
|
||||
protected function uid()
|
||||
{
|
||||
return $this->auth->model->uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 我的文章列表(ajax 请求返回 layui table 所需 JSON,普通请求渲染视图)
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$status = Request::param('status', 'all');
|
||||
$keyword = Request::param('keyword', '');
|
||||
$page = Request::param('page', 1);
|
||||
$limit = Request::param('limit', 15);
|
||||
|
||||
if (Request::isAjax()) {
|
||||
$query = ArticleModel::with(['category', 'tags'])->where('uid', $this->uid());
|
||||
|
||||
if ($status !== 'all') {
|
||||
$query->where('status', $status);
|
||||
}
|
||||
if ($keyword) {
|
||||
$query->where('title|content', 'like', "%{$keyword}%");
|
||||
}
|
||||
|
||||
$paginator = $query->order('create_at', 'desc')
|
||||
->paginate(['list_rows' => $limit, 'page' => $page]);
|
||||
|
||||
$rows = [];
|
||||
foreach ($paginator->items() as $a) {
|
||||
$rows[] = [
|
||||
'id' => $a->id,
|
||||
'title' => $a->title,
|
||||
'is_top' => $a->is_top,
|
||||
'category' => $a->category->name ?? '-',
|
||||
'status' => $a->status,
|
||||
'view_count' => (int) $a->view_count,
|
||||
'comment_count' => (int) $a->comment_count,
|
||||
'like_count' => (int) $a->like_count,
|
||||
'favorite_count' => (int) $a->favorite_count,
|
||||
'share_count' => (int) $a->share_count,
|
||||
'create_at' => $a->create_at,
|
||||
'create_text' => (function ($t) {
|
||||
if ($t instanceof \DateTime) return $t->format('Y-m-d H:i');
|
||||
if (is_numeric($t)) return date('Y-m-d H:i', (int) $t);
|
||||
$ts = strtotime((string) $t);
|
||||
return $ts !== false ? date('Y-m-d H:i', $ts) : (string) $t;
|
||||
})($a->create_at),
|
||||
'detail_url' => (string) url('index/article/detail', ['id' => $a->id]),
|
||||
'edit_url' => (string) url('blog/member.article/edit', ['id' => $a->id]),
|
||||
];
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'count' => $paginator->total(),
|
||||
'data' => $rows,
|
||||
]);
|
||||
}
|
||||
|
||||
return View::fetch('article/index', [
|
||||
'status' => $status,
|
||||
'keyword' => $keyword,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写文章页面
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$categories = CategoryModel::getAvailable($this->uid());
|
||||
$tags = TagModel::select();
|
||||
|
||||
return View::fetch('article/create', [
|
||||
'categories' => $categories,
|
||||
'tags' => $tags,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文章(作者为当前会员,不可指定)
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$data = Request::post();
|
||||
$data['uid'] = $this->uid(); // 多用户:作者固定为当前会员
|
||||
$data['lat'] = (float) Request::post('lat', 0);
|
||||
$data['lng'] = (float) Request::post('lng', 0);
|
||||
unset($data['user_id'], $data['tags']);
|
||||
|
||||
$validate = new Validate([
|
||||
'title|标题' => 'require|max:255',
|
||||
'content|内容' => 'require',
|
||||
'cid|分类' => 'require|number',
|
||||
]);
|
||||
if (! $validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
$tagIds = $this->parseTags(Request::post('tags'));
|
||||
|
||||
// 内容审核:开启 need_audit 时进入审核流(机审 + 人工复核)
|
||||
$needAudit = (int) config('blog.need_audit');
|
||||
$auditStatus = 2; // 默认通过(2=通过)
|
||||
$auditReason = '';
|
||||
if ($needAudit) {
|
||||
$ai = new \addon\wxchat\service\AiAuditService();
|
||||
$res = $ai->audit(($data['title'] ?? '') . "\n" . ($data['content'] ?? ''));
|
||||
if ($res['code'] === 0 && !empty($res['data']['blocked'])) {
|
||||
$auditStatus = 3; // 机审命中违规:直接驳回
|
||||
$auditReason = '机审不通过:' . ($res['data']['reason'] ?? '内容违规');
|
||||
} elseif ($res['code'] === 0) {
|
||||
$auditStatus = 1; // 机审通过:进入待审,等待人工复核
|
||||
}
|
||||
// AI 服务不可用:维持待审,转人工队列
|
||||
}
|
||||
$data['status'] = ($auditStatus === 2) ? 1 : 0; // 仅通过态对外可见
|
||||
$data['audit_status'] = $auditStatus;
|
||||
|
||||
\addon\blog\library\BlogSchema::ensure();
|
||||
$article = ArticleModel::create($data);
|
||||
$article->tags()->sync($tagIds);
|
||||
\addon\blog\service\BlogSearchService::sync($article->id);
|
||||
|
||||
if ($auditStatus === 3) {
|
||||
return json(['code' => 0, 'msg' => $auditReason, 'url' => '/blog/member/article']);
|
||||
}
|
||||
if ($needAudit && $auditStatus === 1) {
|
||||
return json(['code' => 1, 'msg' => '发布成功,等待审核', 'url' => '/blog/member/article']);
|
||||
}
|
||||
return json(['code' => 1, 'msg' => '文章发布成功', 'url' => '/blog/member/article']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑文章页面(仅能编辑自己的文章)
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
$article = ArticleModel::with(['tags'])
|
||||
->where('id', $id)
|
||||
->where('uid', $this->uid())
|
||||
->find();
|
||||
if (! $article) {
|
||||
return redirect('/blog/member/article');
|
||||
}
|
||||
|
||||
$categories = CategoryModel::getAvailable($this->uid());
|
||||
$tags = TagModel::select();
|
||||
$articleTagsStr = implode(',', $article->tags->column('name'));
|
||||
|
||||
return View::fetch('article/edit', [
|
||||
'article' => $article,
|
||||
'categories' => $categories,
|
||||
'tags' => $tags,
|
||||
'articleTagsStr' => $articleTagsStr,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文章(仅能更新自己的文章,作者不可改)
|
||||
*/
|
||||
public function update($id)
|
||||
{
|
||||
$article = ArticleModel::where('id', $id)
|
||||
->where('uid', $this->uid())
|
||||
->find();
|
||||
if (! $article) {
|
||||
return json(['code' => 0, 'msg' => '文章不存在或无权限']);
|
||||
}
|
||||
|
||||
$data = Request::post();
|
||||
$data['lat'] = (float) Request::post('lat', $article->lat ?? 0);
|
||||
$data['lng'] = (float) Request::post('lng', $article->lng ?? 0);
|
||||
unset($data['uid'], $data['user_id'], $data['tags'], $data['tag_names'], $data['id']);
|
||||
|
||||
$validate = new Validate([
|
||||
'title|标题' => 'require|max:255',
|
||||
'content|内容' => 'require',
|
||||
'cid|分类' => 'require|number',
|
||||
]);
|
||||
if (! $validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
$tagIds = $this->parseTags(Request::post('tags'));
|
||||
$article->save($data);
|
||||
$article->tags()->sync($tagIds);
|
||||
\addon\blog\service\BlogSearchService::sync($article->id);
|
||||
|
||||
return json(['code' => 1, 'msg' => '文章更新成功', 'url' => '/blog/member/article']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文章(仅能删除自己的文章)
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$article = ArticleModel::where('id', $id)
|
||||
->where('uid', $this->uid())
|
||||
->find();
|
||||
if ($article) {
|
||||
\addon\blog\service\BlogSearchService::remove($article->id);
|
||||
$article->tags()->detach();
|
||||
$article->delete();
|
||||
return json(['code' => 1, 'msg' => '文章已删除']);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '文章不存在或无权限']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量操作(仅限自己的文章)
|
||||
*/
|
||||
public function batch()
|
||||
{
|
||||
$action = Request::post('action');
|
||||
$ids = Request::post('ids/a');
|
||||
if (empty($ids)) {
|
||||
return json(['code' => 0, 'msg' => '请选择文章']);
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'delete':
|
||||
$list = ArticleModel::whereIn('id', $ids)->where('uid', $this->uid())->select();
|
||||
foreach ($list as $a) {
|
||||
$a->tags()->detach();
|
||||
$a->delete();
|
||||
}
|
||||
return json(['code' => 1, 'msg' => '批量删除成功']);
|
||||
case 'publish':
|
||||
ArticleModel::whereIn('id', $ids)->where('uid', $this->uid())->update(['status' => 1]);
|
||||
return json(['code' => 1, 'msg' => '批量发布成功']);
|
||||
case 'draft':
|
||||
ArticleModel::whereIn('id', $ids)->where('uid', $this->uid())->update(['status' => 0]);
|
||||
return json(['code' => 1, 'msg' => '批量设为草稿成功']);
|
||||
default:
|
||||
return json(['code' => 0, 'msg' => '无效操作']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析标签:逗号分隔的名称 -> 标签 id 数组(不存在则自动创建)
|
||||
*/
|
||||
protected function parseTags($raw)
|
||||
{
|
||||
$tagIds = [];
|
||||
if (empty($raw)) {
|
||||
return $tagIds;
|
||||
}
|
||||
$names = is_array($raw) ? $raw : explode(',', $raw);
|
||||
foreach ($names as $name) {
|
||||
$name = trim($name);
|
||||
if ($name === '') {
|
||||
continue;
|
||||
}
|
||||
$tag = TagModel::where('name', $name)->find() ?: TagModel::create(['name' => $name]);
|
||||
$tagIds[] = $tag->id;
|
||||
}
|
||||
return $tagIds;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\member;
|
||||
|
||||
use addon\blog\model\BlogCategory as CategoryModel;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use think\Validate;
|
||||
use ywxapp\controller\MemberBase;
|
||||
|
||||
/**
|
||||
* 会员中心「我的分类」管理(用户自建分类)
|
||||
* 每个会员只能管理自己 uid 下的分类;全局共享分类(uid=0)由后台管理。
|
||||
*/
|
||||
class Category extends MemberBase
|
||||
{
|
||||
protected $noNeedLogin = [];
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
// 当前登录会员 id(会员主键为 uid)
|
||||
|
||||
protected function uid()
|
||||
{
|
||||
return $this->auth->model->uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 我的分类列表(仅当前会员自己的分类)
|
||||
* 以 layui treeTable 所需的扁平结构返回(带 pid / is_parent),由前端 treeTable 自动成树。
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$categories = CategoryModel::where('uid', $this->uid())
|
||||
->withCount('articles')
|
||||
->order('sort', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 计算哪些分类拥有子分类(用于 treeTable 的 is_parent 标记)
|
||||
$parentIds = array_unique(array_filter(array_column($categories, 'pid')));
|
||||
|
||||
$treeData = [];
|
||||
foreach ($categories as $c) {
|
||||
$treeData[] = [
|
||||
'id' => $c['id'],
|
||||
'pid' => $c['pid'],
|
||||
'title' => $c['name'], // treeTable 节点显示名称
|
||||
'name' => $c['name'],
|
||||
'description' => $c['description'] ?? '',
|
||||
'sort' => $c['sort'],
|
||||
'status' => $c['status'],
|
||||
'is_parent' => in_array($c['id'], $parentIds, true) ? 1 : 0,
|
||||
'articles_count' => $c['articles_count'] ?? 0,
|
||||
];
|
||||
}
|
||||
|
||||
return View::fetch('category/index', [
|
||||
'treeData' => json_encode($treeData, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加分类页面
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$categories = CategoryModel::where(function ($query) {
|
||||
$query->where('uid', 0)->whereOr('uid', $this->uid());
|
||||
})->order('sort', 'asc')->select();
|
||||
return View::fetch('category/create', ['categories' => $categories]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存分类(归属当前会员)
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$data = Request::post();
|
||||
$data['uid'] = $this->uid();
|
||||
$data['pid'] = (int) Request::post('pid', 0);
|
||||
$data['status'] = 1;
|
||||
$data['create_at'] = time();
|
||||
$data['update_at'] = time();
|
||||
|
||||
$validate = new Validate([
|
||||
'name|分类名称' => 'require|unique:BlogCategory,name,0,id,uid,' . $this->uid(),
|
||||
'pid|上级分类' => 'number',
|
||||
'sort|排序' => 'number|between:0,999',
|
||||
]);
|
||||
if (! $validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
CategoryModel::create($data);
|
||||
return json(['code' => 1, 'msg' => '分类添加成功', 'url' => '/blog/member/category']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑分类页面(仅自己的分类)
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
$category = CategoryModel::where('id', $id)
|
||||
->where('uid', $this->uid())
|
||||
->find();
|
||||
if (! $category) {
|
||||
return redirect('/blog/member/category');
|
||||
}
|
||||
|
||||
$categories = CategoryModel::where(function ($query) {
|
||||
$query->where('uid', 0)->whereOr('uid', $this->uid());
|
||||
})->order('sort', 'asc')->select();
|
||||
return View::fetch('category/edit', ['category' => $category, 'categories' => $categories]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新分类(仅自己的分类)
|
||||
*/
|
||||
public function update($id)
|
||||
{
|
||||
$category = CategoryModel::where('id', $id)
|
||||
->where('uid', $this->uid())
|
||||
->find();
|
||||
if (! $category) {
|
||||
return json(['code' => 0, 'msg' => '分类不存在或无权限']);
|
||||
}
|
||||
|
||||
if ((int) Request::post('pid', 0) === (int) $id) {
|
||||
return json(['code' => 0, 'msg' => '上级分类不能选择自己']);
|
||||
}
|
||||
|
||||
$data = Request::post();
|
||||
$data['pid'] = (int) Request::post('pid', 0);
|
||||
$data['update_at'] = time();
|
||||
|
||||
$validate = new Validate([
|
||||
'name|分类名称' => 'require|unique:BlogCategory,name,' . $id . ',id,uid,' . $this->uid(),
|
||||
'pid|上级分类' => 'number',
|
||||
'sort|排序' => 'number|between:0,999',
|
||||
]);
|
||||
if (! $validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
$category->save($data);
|
||||
return json(['code' => 1, 'msg' => '分类更新成功', 'url' => '/blog/member/category']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除分类(仅自己的分类,有文章时禁止删除)
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$category = CategoryModel::where('id', $id)
|
||||
->where('uid', $this->uid())
|
||||
->find();
|
||||
if (! $category) {
|
||||
return json(['code' => 0, 'msg' => '分类不存在或无权限']);
|
||||
}
|
||||
if ($category->children()->count() > 0) {
|
||||
return json(['code' => 0, 'msg' => '该分类下有子分类,请先删除子分类']);
|
||||
}
|
||||
if ($category->articles()->count() > 0) {
|
||||
return json(['code' => 0, 'msg' => '该分类下有文章,无法删除']);
|
||||
}
|
||||
$category->delete();
|
||||
return json(['code' => 1, 'msg' => '分类已删除']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量操作(仅限自己的分类)
|
||||
*/
|
||||
public function batch()
|
||||
{
|
||||
$action = Request::post('action');
|
||||
$ids = Request::post('ids/a');
|
||||
if (empty($ids)) {
|
||||
return json(['code' => 0, 'msg' => '请选择分类']);
|
||||
}
|
||||
|
||||
if ($action === 'delete') {
|
||||
$list = CategoryModel::whereIn('id', $ids)
|
||||
->where('uid', $this->uid())
|
||||
->select();
|
||||
foreach ($list as $c) {
|
||||
if ($c->articles()->count() > 0) {
|
||||
continue; // 跳过有文章的分类
|
||||
}
|
||||
if (CategoryModel::where('pid', $c->id)->whereNotIn('id', $ids)->count() > 0) {
|
||||
continue; // 跳过有外部子分类的分类
|
||||
}
|
||||
$c->delete();
|
||||
}
|
||||
return json(['code' => 1, 'msg' => '批量删除成功']);
|
||||
}
|
||||
|
||||
return json(['code' => 0, 'msg' => '无效操作']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\member;
|
||||
|
||||
use think\facade\View;
|
||||
use ywxapp\controller\MemberBase;
|
||||
|
||||
/**
|
||||
* 会员中心入口页(addon/blog/controller/member/Index.php)
|
||||
*
|
||||
* 继承 MemberBase,登录校验统一由 MemberBase::_initialize() 调用 Auth::verifyAuth 处理
|
||||
* (本控制器设 $noNeedLogin = [] 表示强制会员登录,未登录跳 /user/login)。
|
||||
* 容器 auth 默认即为 \ywxapp\library\Auth,故 $this->auth->info 即当前会员。
|
||||
*/
|
||||
class Index extends MemberBase
|
||||
{
|
||||
// 会员中心强制登录(由 MemberBase 的 verifyAuth 统一校验)
|
||||
protected $noNeedLogin = [];
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 会员中心首页
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$user = $this->auth->info ?? null;
|
||||
|
||||
return View::fetch('index/index', [
|
||||
'member' => $user,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人资料
|
||||
*/
|
||||
public function profile()
|
||||
{
|
||||
$user = $this->auth->info ?? null;
|
||||
|
||||
return View::fetch('index/profile', [
|
||||
'member' => $user,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user