Files

249 lines
8.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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 : '')]);
}
}