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
+98
View File
@@ -0,0 +1,98 @@
<?php
/*
* @Author: YwxApp <ywx@ywxapp.cn>
* @Date: 2026-08-10 15:31:07
* @LastEditors: YwxApp <ywx@ywxapp.cn>
* @LastEditTime: 2026-08-13 17:07:28
* @Description:
* @FilePath: \ywxapp_dev\addon\articles\controller\Article.php
* @CustomString: Copyright (c) 2026 YwxApp
*/
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | 文章CMS前台文章详情 / 搜索 / 评论
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\articles\controller;
use think\facade\Db;
use addon\articles\model\Article as ArticleModel;
use addon\articles\model\ArticleComment;
class Article extends ArticlesFrontend
{
public function read($id)
{
$article = ArticleModel::getDetail((int) $id);
if (!$article) {
$this->result->error('文章不存在或已删除');
}
// 浏览量 +1
ArticleModel::where('id', $id)->inc('view_count')->update();
$commentsPage = ArticleComment::getByArticle((int) $id, (int) input('page', 1), 10);
$comments = [
'data' => $commentsPage->items(),
'render' => $commentsPage->render(),
];
$this->view->assign([
'article' => $article,
'prev' => ArticleModel::prev($article->id, $article->cid),
'next' => ArticleModel::next($article->id, $article->cid),
'related' => ArticleModel::related($article->id, $article->cid, 5),
'hot' => ArticleModel::hot(10),
'comments'=> $comments,
'nav_active' => $article->cid,
]);
return $this->fetch('article/detail');
}
// 兼容 route: articles/article/detail/:id
public function detail($id)
{
return $this->read($id);
}
public function search()
{
$keyword = trim(input('keyword', ''));
$page = (int) input('page', 1);
$list = ArticleModel::listArticles(['keyword' => $keyword], $page, 10);
$this->view->assign([
'keyword' => $keyword,
'list' => $list->items(),
'total' => $list->total(),
'page' => $list->render(),
'nav_active' => '',
]);
return $this->fetch('search/index');
}
public function comment()
{
if (!$this->auth || !$this->auth->isLogin) {
$this->result->error('请先登录后再评论');
}
$aid = (int) input('aid', 0);
$content = trim(input('content', ''));
if (!$aid || !$content) {
$this->result->error('参数错误');
}
$uid = $this->auth->info['uid'] ?? 0;
ArticleComment::create([
'uid' => $uid,
'aid' => $aid,
'pid' => 0,
'content' => $content,
'status' => 1,
'create_at' => time(),
'update_at' => time(),
]);
ArticleModel::where('id', $aid)->inc('comment_count')->update();
$this->result->success('评论成功');
}
}
@@ -0,0 +1,50 @@
<?php
/*
* @Author: YwxApp <ywx@ywxapp.cn>
* @Date: 2026-08-10 15:30:57
* @LastEditors: YwxApp <ywx@ywxapp.cn>
* @LastEditTime: 2026-08-13 17:38:27
* @Description:
* @FilePath: \ywxapp_dev\addon\articles\controller\ArticlesFrontend.php
* @CustomString: Copyright (c) 2026 YwxApp
*/
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | 文章CMS前台基类:统一注入侧边栏/分类/标签等共享数据
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\articles\controller;
use ywxapp\controller\FrontendBase;
use addon\articles\model\Article;
use addon\articles\model\ArticleCategory;
use addon\articles\model\ArticleTag;
use addon\articles\library\ArticleSchema;
class ArticlesFrontend extends FrontendBase
{
protected $noNeedLogin = ['*'];
protected $noNeedVerify = ['*'];
protected function initialize()
{
parent::initialize();
// articles 前台页面自带完整 <!DOCTYPE html> 结构,清空 layout_name
// 避免被 common/layout 包裹导致双层 <html>、标签外露、排版错乱。
//$this->view->config(['layout_name' => '']);
ArticleSchema::ensure();
$isLogin = $this->auth && $this->auth->isLogin;
$loginUser = $isLogin ? $this->auth->info : null;
$this->view->assign([
'categories' => ArticleCategory::allEnable(),
'tags' => ArticleTag::where('delete_at', 0)->order('id', 'desc')->limit(20)->select(),
'hot' => Article::hot(10),
'is_login' => $isLogin,
'member' => $loginUser,
'nav_active' => '',
]);
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | 文章CMS前台分类列表
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\articles\controller;
use addon\articles\model\Article;
use addon\articles\model\ArticleCategory;
class Category extends ArticlesFrontend
{
public function index($id)
{
$category = ArticleCategory::getById((int) $id);
if (!$category) {
$this->result->error('分类不存在');
}
$page = (int) input('page', 1);
$list = Article::listArticles(['cid' => $id], $page, 10);
$this->view->assign([
'category' => $category,
'list' => $list->items(),
'page' => $list->render(),
'nav_active' => $id,
]);
return $this->fetch('category/index');
}
}
+47
View File
@@ -0,0 +1,47 @@
<?php
/*
* @Author: YwxApp <ywx@ywxapp.cn>
* @Date: 2026-08-10 15:31:01
* @LastEditors: YwxApp <ywx@ywxapp.cn>
* @LastEditTime: 2026-08-10 20:38:46
* @Description:
* @FilePath: \ywxapp_dev\addon\articles\controller\Index.php
* @CustomString: Copyright (c) 2026 YwxApp
*/
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | 文章CMS前台首页
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\articles\controller;
use addon\articles\model\Article;
use addon\articles\model\ArticleCategory;
class Index extends ArticlesFrontend
{
public function index()
{
// dump($this->app->route);die;
$page = (int) input('page', 1);
$list = Article::listArticles([], $page, 10);
// 轮播:取推荐或最新的 5 篇
$slides = Article::scope('normal')
->order('is_top desc, is_recommend desc, create_at desc')
->limit(5)
->field('id,title,cover_image')
->select();
$this->view->assign([
'list' => $list->items(),
'page' => $list->render(),
'slides' => $slides,
'nav_active' => 'index',
]);
return $this->fetch();
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | 文章CMS前台标签列表
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\articles\controller;
use addon\articles\model\Article;
use addon\articles\model\ArticleTag;
class Tag extends ArticlesFrontend
{
public function index($id)
{
$tag = ArticleTag::where('delete_at', 0)->find($id);
if (!$tag) {
$this->result->error('标签不存在');
}
$page = (int) input('page', 1);
$list = Article::listArticles(['tid' => $id], $page, 10);
$this->view->assign([
'tag' => $tag,
'list' => $list->items(),
'page' => $list->render(),
]);
return $this->fetch('tag/index');
}
}
@@ -0,0 +1,201 @@
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | 文章CMS后台文章管理
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\articles\controller\backend;
use think\facade\Request;
use think\facade\View;
use think\Validate;
use ywxapp\controller\BackendBase;
use addon\articles\model\Article as ArticleModel;
use addon\articles\model\ArticleCategory;
use addon\articles\model\ArticleTag;
use addon\articles\model\ArticleArticleTag;
use addon\articles\library\ArticleSchema;
class Article extends BackendBase
{
protected $noNeedVerify = ['*'];
protected function initialize()
{
parent::initialize();
ArticleSchema::ensure();
}
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']);
if ($status !== 'all') {
$query->where('status', $status);
}
if ($keyword) {
$query->where('title', 'like', "%{$keyword}%");
}
$paginator = $query->order('is_top', 'desc')
->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,
'is_recommend'=> $a->is_recommend,
'category' => $a->category->name ?? '-',
'status' => $a->status,
'view_count' => (int) $a->view_count,
'comment_count' => (int) $a->comment_count,
'create_at' => $a->create_at,
'edit_url' => (string) url('articles/backend.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 save()
{
if (Request::isPost()) {
return $this->update(0);
}
$categories = ArticleCategory::where('delete_at', 0)->where('status', 1)->select();
$tags = ArticleTag::where('delete_at', 0)->select();
return View::fetch('article/edit', [
'article' => null,
'categories' => $categories,
'tags' => $tags,
'articleTags' => [],
'tagNamesStr' => '',
]);
}
public function edit($id = null)
{
$article = ArticleModel::with(['tags'])->find($id);
if (!$article) {
return redirect('/articles/backend/article');
}
$categories = ArticleCategory::where('delete_at', 0)->where('status', 1)->select();
$tags = ArticleTag::where('delete_at', 0)->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 = 0)
{
$data = Request::post();
$id = $id ?: (int) ($data['id'] ?? 0);
$data['update_at'] = time();
$data['is_top'] = isset($data['is_top']) ? 1 : 0;
$data['is_recommend']= isset($data['is_recommend']) ? 1 : 0;
$validate = new Validate([
'title|标题' => 'require|max:255',
'content|内容' => 'require',
'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 = ArticleTag::where('delete_at', 0)->where('name', $name)->find()
?: ArticleTag::create(['name' => $name, 'alias' => $name, 'create_at' => time(), 'update_at' => time()]);
$tagIds[] = $tag->id;
}
}
if ($id) {
$article = ArticleModel::find($id);
if (!$article) {
return json(['code' => 0, 'msg' => '文章不存在']);
}
$article->save($data);
} else {
if (empty($data['uid'])) {
$data['uid'] = $this->auth->info['uid'] ?? 0;
}
if (empty($data['author'])) {
$data['author'] = $this->auth->info['nickname'] ?? '';
}
$data['create_at'] = time();
$article = ArticleModel::create($data);
$id = $article->id;
}
ArticleArticleTag::syncTags($id, $tagIds);
return json(['code' => 1, 'msg' => '保存成功', 'url' => '/articles/backend/article']);
}
public function delete($id = 0)
{
$id = $id ?: (int) Request::post('id', 0);
$article = ArticleModel::find($id);
if ($article) {
$article->delete();
return json(['code' => 1, 'msg' => '已删除']);
}
return json(['code' => 0, 'msg' => '文章不存在']);
}
public function recyclebin()
{
$page = Request::param('page', 1);
$limit = Request::param('limit', 15);
if (Request::isAjax()) {
$list = ArticleModel::onlyTrashed()
->order('delete_at', 'desc')
->paginate(['list_rows' => $limit, 'page' => $page]);
$rows = array_map(function ($a) {
return [
'id' => $a->id,
'title' => $a->title,
'delete_at' => $a->delete_at,
];
}, $list->items());
return json(['code' => 0, 'msg' => '', 'count' => $list->total(), 'data' => $rows]);
}
return View::fetch('article/recyclebin');
}
public function restore($id = 0)
{
$id = $id ?: (int) Request::post('id', 0);
$article = ArticleModel::onlyTrashed()->find($id);
if ($article) {
$article->restore();
return json(['code' => 1, 'msg' => '已恢复']);
}
return json(['code' => 0, 'msg' => '记录不存在']);
}
}
@@ -0,0 +1,128 @@
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | 文章CMS后台分类管理
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace addon\articles\controller\backend;
use think\facade\Request;
use think\facade\View;
use think\Validate;
use ywxapp\controller\BackendBase;
use addon\articles\model\ArticleCategory;
use addon\articles\library\ArticleSchema;
class Category extends BackendBase
{
/**
* Summary of needLogin
* @var array
*/
protected $noNeedLogin = [];
/**
* Summary of needRight
* @var array
*/
protected $noNeedVerify = [];
protected function initialize()
{
parent::initialize();
ArticleSchema::ensure();
}
/**
* 显示资源列表
*
* @param \think\Request $request
* @return \think\Response
*/
public function index()
{
$page = $this->request->param('page', 1);
$size = $this->request->param('size', 15);
if ($this->request->isAjax()) {
$data = ArticleCategory::paginate([
'page' => $page,
'list_rows' => $size,
]);
$this->result->success($data->items());
}
$this->view->assign('title', '登录');
return $this->view->fetch('category/index');
}
public function save()
{
if (Request::isPost()) {
$data = Request::post();
$data['status'] = isset($data['status']) ? 1 : 0;
$validate = new Validate([
'name|名称' => 'require|max:50',
'alias|别名' => 'require|max:50',
]);
if (!$validate->check($data)) {
$this->result->error($validate->getError());
}
ArticleCategory::create($data);
$this->result->success();
}
}
public function edit($id = null)
{
$category = ArticleCategory::find($id);
if (!$category) {
return redirect('/articles/backend/category');
}
return View::fetch('category/edit', ['category' => $category]);
}
public function update($id = 0)
{
$data = Request::post();
$id = $id ?: (int) ($data['id'] ?? 0);
$data['update_at'] = time();
$data['status'] = isset($data['status']) ? 1 : 0;
$validate = new Validate([
'name|名称' => 'require|max:50',
'alias|别名' => 'require|max:50',
]);
if (!$validate->check($data)) {
return json(['code' => 0, 'msg' => $validate->getError()]);
}
if ($id) {
$category = ArticleCategory::find($id);
if (!$category) {
return json(['code' => 0, 'msg' => '分类不存在']);
}
$category->save($data);
} else {
$data['create_at'] = time();
ArticleCategory::create($data);
}
return json(['code' => 0, 'msg' => '保存成功', 'url' => '/articles/backend/category']);
}
public function delete($id = 0)
{
$id = $id ?: (int) Request::post('id', 0);
$category = ArticleCategory::find($id);
if ($category) {
if (ArticleCategory::hasArticles($id)) {
return json(['code' => 0, 'msg' => '该分类下还有文章,无法删除']);
}
$category->delete();
return json(['code' => 0, 'msg' => '已删除']);
}
return json(['code' => 0, 'msg' => '分类不存在']);
}
}
@@ -0,0 +1,85 @@
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | 文章CMS后台评论管理
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\articles\controller\backend;
use think\facade\Request;
use think\facade\View;
use ywxapp\controller\BackendBase;
use addon\articles\model\ArticleComment;
use addon\articles\model\Article;
use addon\articles\library\ArticleSchema;
class Comment extends BackendBase
{
protected $noNeedVerify = ['*'];
protected function initialize()
{
parent::initialize();
ArticleSchema::ensure();
}
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 = ArticleComment::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) {
$rows[] = [
'id' => $c->id,
'nickname' => $c->user->nickname ?? '游客',
'article' => $c->article->title ?? '-',
'content' => $c->content,
'status' => $c->status,
'create_at' => $c->create_at,
];
}
return json(['code' => 0, 'msg' => '', 'count' => $paginator->total(), 'data' => $rows]);
}
return View::fetch('comment/index', ['status' => $status, 'keyword' => $keyword]);
}
public function audit($id = 0)
{
$id = $id ?: (int) Request::post('id', 0);
$comment = ArticleComment::find($id);
if ($comment) {
$comment->status = 1;
$comment->save();
Article::where('id', $comment->aid)->inc('comment_count')->update();
return json(['code' => 1, 'msg' => '审核通过']);
}
return json(['code' => 0, 'msg' => '评论不存在']);
}
public function delete($id = 0)
{
$id = $id ?: (int) Request::post('id', 0);
$comment = ArticleComment::find($id);
if ($comment) {
$comment->delete();
return json(['code' => 1, 'msg' => '已删除']);
}
return json(['code' => 0, 'msg' => '评论不存在']);
}
}
+101
View File
@@ -0,0 +1,101 @@
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | 文章CMS后台标签管理
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\articles\controller\backend;
use think\facade\Request;
use think\facade\View;
use think\Validate;
use ywxapp\controller\BackendBase;
use addon\articles\model\ArticleTag;
use addon\articles\library\ArticleSchema;
class Tag extends BackendBase
{
protected $noNeedVerify = ['*'];
protected function initialize()
{
parent::initialize();
ArticleSchema::ensure();
}
public function index()
{
if (Request::isAjax()) {
$list = ArticleTag::where('delete_at', 0)->order('id', 'desc')->select();
$rows = array_map(function ($t) {
return [
'id' => $t->id,
'name' => $t->name,
'alias' => $t->alias,
'description' => $t->description,
'create_at' => $t->create_at,
'edit_url' => (string) url('articles/backend.tag/edit', ['id' => $t->id]),
];
}, $list->toArray());
return json(['code' => 0, 'msg' => '', 'count' => count($rows), 'data' => $rows]);
}
return View::fetch('tag/index');
}
public function save()
{
if (Request::isPost()) {
return $this->update(0);
}
return View::fetch('tag/edit', ['tag' => null]);
}
public function edit($id = null)
{
$tag = ArticleTag::find($id);
if (!$tag) {
return redirect('/articles/backend/tag');
}
return View::fetch('tag/edit', ['tag' => $tag]);
}
public function update($id = 0)
{
$data = Request::post();
$id = $id ?: (int) ($data['id'] ?? 0);
$data['update_at'] = time();
$data['alias'] = $data['alias'] ?? $data['name'];
$validate = new Validate([
'name|名称' => 'require|max:50',
]);
if (!$validate->check($data)) {
return json(['code' => 0, 'msg' => $validate->getError()]);
}
if ($id) {
$tag = ArticleTag::find($id);
if (!$tag) {
return json(['code' => 0, 'msg' => '标签不存在']);
}
$tag->save($data);
} else {
$data['create_at'] = time();
ArticleTag::create($data);
}
return json(['code' => 1, 'msg' => '保存成功', 'url' => '/articles/backend/tag']);
}
public function delete($id = 0)
{
$id = $id ?: (int) Request::post('id', 0);
$tag = ArticleTag::find($id);
if ($tag) {
$tag->delete();
return json(['code' => 1, 'msg' => '已删除']);
}
return json(['code' => 0, 'msg' => '标签不存在']);
}
}