chore: 重写初始提交(清空历史,整理后全量提交)

This commit is contained in:
ywxapp
2026-08-16 16:54:14 +08:00
commit 6c1a106bc1
1808 changed files with 238144 additions and 0 deletions
+248
View File
@@ -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 : '')]);
}
}
+217
View File
@@ -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]);
}
}
+181
View File
@@ -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,
]);
}
}
+44
View File
@@ -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,
]);
}
}
+117
View File
@@ -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);
}
}
+136
View File
@@ -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');
}
}
+171
View File
@@ -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' => '批量删除成功']);
}
}
+158
View File
@@ -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' => '批量删除成功']);
}
}