更新代码

This commit is contained in:
2025-05-19 17:34:40 +08:00
parent 1587524031
commit 7bd072add9
32 changed files with 1465 additions and 139 deletions
@@ -3,16 +3,25 @@
* 后台管理系统-文章管理
*/
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Article\Article;
use app\admin\model\Article\ArticleCategory;
use app\admin\model\Article\Articles;
use app\admin\model\Article\ArticlesCategory;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\admin\controller\Log;
use app\admin\controller\BaseController;
class Articles extends Base
class Article extends BaseController
{
/**
* 获取控制器名称
* @return string
*/
public function getControllerName()
{
return 'Article';
}
// 文章列表
public function articlelist()
{
@@ -23,13 +32,13 @@ class Articles extends Base
$title = input('post.title');
$author = input('post.author');
$query = Article::where('delete_time', null)
$query = Articles::where('delete_time', null)
->where('status', '<>', 3);
// 分类筛选
if (!empty($category)) {
// 先获取分类ID
$cateInfo = ArticleCategory::where('name', $category)
$cateInfo = ArticlesCategory::where('name', $category)
->where('delete_time', null)
->where('status', 1)
->find();
@@ -58,7 +67,7 @@ class Articles extends Base
->select()
->each(function ($item) {
// 获取分类信息
$cateInfo = ArticleCategory::where('id', $item['cate'])
$cateInfo = ArticlesCategory::where('id', $item['cate'])
->field('name, image')
->find();
@@ -85,7 +94,7 @@ class Articles extends Base
]);
} else {
// 获取所有分类并构建父子结构
$allCategories = ArticleCategory::where('delete_time', null)
$allCategories = ArticlesCategory::where('delete_time', null)
->where('status', 1)
->order('sort asc, id asc')
->select()
@@ -127,7 +136,7 @@ class Articles extends Base
'create_time' => time()
];
$insert = Article::insert($data);
$insert = Articles::insert($data);
if (empty($insert)) {
Log::record('添加文章', 0, '添加文章失败', '文章管理');
return json(['code' => 1, 'msg' => '添加失败', 'data' => []]);
@@ -135,7 +144,7 @@ class Articles extends Base
Log::record('添加文章', 1, '', '文章管理');
return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
} else {
$lists = Article::order('id DESC')
$lists = Articles::order('id DESC')
->select()
->each(function ($item, $key) {
$item['create_time'] = time();
@@ -164,7 +173,7 @@ class Articles extends Base
'update_time' => time()
];
$update = Article::where('id', $id)->update($data);
$update = Articles::where('id', $id)->update($data);
if ($update === false) {
Log::record('编辑文章', 0, '编辑文章失败', '文章管理');
return json(['code' => 1, 'msg' => '更新失败', 'data' => []]);
@@ -173,12 +182,12 @@ class Articles extends Base
return json(['code' => 0, 'msg' => '更新成功', 'data' => []]);
} else {
$id = input('get.id');
$info = Article::where('id', $id)->find();
$info = Articles::where('id', $id)->find();
if ($info === null) {
return json(['code' => 1, 'msg' => '文章不存在', 'data' => []]);
}
$cates = ArticleCategory::where('delete_time', null)
$cates = ArticlesCategory::where('delete_time', null)
->where('status', 1)
->order('sort asc, id asc')
->select()
@@ -186,7 +195,7 @@ class Articles extends Base
$info['content'] = !empty($info['content']) ? htmlspecialchars_decode(str_replace(["\r\n", "\r", "\n"], '', addslashes($info['content']))) : '';
$currentCate = ArticleCategory::where('id', $info['cate'])
$currentCate = ArticlesCategory::where('id', $info['cate'])
->where('delete_time', null)
->where('status', 1)
->find();
@@ -207,7 +216,7 @@ class Articles extends Base
$data = [
'delete_time' => time(),
];
$delete = Article::where('id', $id)->update($data);
$delete = Articles::where('id', $id)->update($data);
if ($delete === false) {
Log::record('删除文章', 0, '删除文章失败', '文章管理');
return json(['code' => 1, 'msg' => '删除失败', 'data' => []]);
@@ -220,7 +229,7 @@ class Articles extends Base
public function articlecate()
{
if (Request::isPost()) {
$lists = ArticleCategory::where('delete_time', null)
$lists = ArticlesCategory::where('delete_time', null)
->where('status', 1)
->order('sort asc, id asc')
->select()
@@ -262,7 +271,7 @@ class Articles extends Base
public function getcate()
{
// 获取所有分类
$lists = ArticleCategory::where('delete_time', null)
$lists = ArticlesCategory::where('delete_time', null)
->where('status', 1)
->order('sort asc, id asc')
->select()
@@ -304,7 +313,7 @@ class Articles extends Base
'create_time' => time()
];
$insert = ArticleCategory::insert($data);
$insert = ArticlesCategory::insert($data);
if (empty($insert)) {
Log::record('添加文章分类', 0, '添加文章分类失败', '文章分类');
return json(['code' => 1, 'msg' => '添加失败', 'data' => []]);
@@ -313,7 +322,7 @@ class Articles extends Base
return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
} else {
// 获取所有可选的父级分类
$parentCategories = ArticleCategory::where('delete_time', null)
$parentCategories = ArticlesCategory::where('delete_time', null)
->where('status', 1)
->where('cid', 0)
->field('id, name')
@@ -344,7 +353,7 @@ class Articles extends Base
'update_time' => time()
];
$update = ArticleCategory::where('id', $data['id'])
$update = ArticlesCategory::where('id', $data['id'])
->update($data);
if ($update === false) {
@@ -355,10 +364,10 @@ class Articles extends Base
return json(['code' => 0, 'msg' => '更新成功', 'data' => []]);
} else {
$id = input('get.id');
$info = ArticleCategory::where('id', $id)->find();
$info = ArticlesCategory::where('id', $id)->find();
// 获取所有可选的父级分类
$parentCategories = ArticleCategory::where('delete_time', null)
$parentCategories = ArticlesCategory::where('delete_time', null)
->where('status', 1)
->where('id', '<>', $id) // 排除自己
->where(function ($query) use ($id) {
@@ -397,7 +406,7 @@ class Articles extends Base
$id = input('post.id');
// 检查是否有子分类
$hasChildren = ArticleCategory::where('cid', $id)
$hasChildren = ArticlesCategory::where('cid', $id)
->where('delete_time', null)
->find();
@@ -406,7 +415,7 @@ class Articles extends Base
return json(['code' => 1, 'msg' => '该分类下有子分类,无法删除', 'data' => []]);
}
$delete = ArticleCategory::where('id', $id)
$delete = ArticlesCategory::where('id', $id)
->update(['delete_time' => time()]);
if ($delete === false) {
@@ -419,7 +428,7 @@ class Articles extends Base
//统计文章数量
public function counts() {
$total = Article::where('delete_time', null)
$total = Articles::where('delete_time', null)
->where('status', '<>', 3)
->count();
+131
View File
@@ -0,0 +1,131 @@
<?php
declare (strict_types = 1);
namespace app\admin\controller;
use think\App;
use think\exception\ValidateException;
use think\Validate;
/**
* 后台控制器基础类
*/
abstract class BaseController extends Base
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
parent::__construct();
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize($app);
}
/**
* 初始化
* @access public
* @param App $app 应用对象
*/
public function initialize(App $app)
{
// 注册控制器映射
$this->registerControllerMap();
}
/**
* 注册控制器映射
*/
protected function registerControllerMap()
{
// 获取当前控制器类名
$className = get_class($this);
// 获取不带命名空间的类名
$shortName = substr($className, strrpos($className, '\\') + 1);
// 移除Controller后缀
$mapName = str_replace('Controller', '', $shortName);
// 调试信息
trace("Controller Mapping: {$mapName} => {$className}", 'debug');
// 注册控制器映射
$this->app->route->setControllerMap($mapName, $className);
}
/**
* 获取控制器名称(移除Controller后缀)
* @return string
*/
public function getControllerName()
{
$className = get_class($this);
$className = substr($className, strrpos($className, '\\') + 1);
return str_replace('Controller', '', $className);
}
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
[$validate, $scene] = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}
}
+2 -2
View File
@@ -3,7 +3,7 @@
* 后台管理系统-资源管理
*/
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\controller\BaseController;
use app\admin\model\Resource\Resource;
use app\admin\model\Resource\ResourceCategory;
use think\facade\View;
@@ -12,7 +12,7 @@ use think\facade\Db;
use app\admin\controller\Log;
use think\App;
class Resources extends Base
class Resources extends BaseController
{
// 资源列表
public function lists()
-13
View File
@@ -1,13 +0,0 @@
<?php
namespace app\admin\model\Article;
use think\Model;
class Article extends Model
{
// 设置当前模型对应的数据表名称(不含前缀)
protected $name = 'article';
// 设置主键
protected $pk = 'id';
}
@@ -1,13 +0,0 @@
<?php
namespace app\admin\model\Article;
use think\Model;
class ArticleCategory extends Model
{
// 设置当前模型对应的数据表名称(不含前缀)
protected $name = 'article_category';
// 设置主键
protected $pk = 'id';
}
+8
View File
@@ -0,0 +1,8 @@
<?php
namespace app\admin\model\Article;
use think\Model;
class Articles extends Model
{
}
@@ -0,0 +1,8 @@
<?php
namespace app\admin\model\Article;
use think\Model;
class ArticlesCategory extends Model
{
}
@@ -322,7 +322,7 @@
// 初始化分类列表
that.initCategoryList = function () {
$.ajax({
url: '/admin/articles/articlecate',
url: '/admin/article/articlecate',
type: 'POST',
success: function (res) {
if (res.code === 0) {
@@ -387,7 +387,7 @@
// 加载分类信息
that.loadCategoryInfo = function (id) {
$.get('/admin/articles/cateedit?id=' + id, function (res) {
$.get('/admin/article/cateedit?id=' + id, function (res) {
if (res.code === 0) {
that.showCategoryForm(0, res.data);
}
@@ -413,7 +413,7 @@
$select.empty().append('<option value="0">顶级分类</option>');
// 获取所有分类作为父级选项
$.ajax({
url: '/admin/articles/articlecate',
url: '/admin/article/articlecate',
type: 'POST',
async: false,
success: function (res) {
@@ -472,7 +472,7 @@
// 监听表单提交
form.on('submit(saveCategory)', function (data) {
var url = data.field.id ? '/admin/articles/cateedit' : '/admin/articles/cateadd';
var url = data.field.id ? '/admin/article/cateedit' : '/admin/article/cateadd';
$.post(url, data.field, function (res) {
if (res.code === 0) {
layer.msg(res.msg, { icon: 1 });
@@ -490,7 +490,7 @@
if (!id) return;
layer.confirm('确定要删除该分类吗?', function (index) {
$.post('/admin/articles/catedel', { id: id }, function (res) {
$.post('/admin/article/catedel', { id: id }, function (res) {
if (res.code === 0) {
layer.msg(res.msg, { icon: 1 });
that.initCategoryList();
@@ -87,7 +87,7 @@
// 初始化表格
table.render({
elem: '#articleTable',
url: '/admin/articles/articlelist',
url: '/admin/article/articlelist',
method: 'post',
cols: [[
{ field: 'id', title: 'ID', align: 'center', width: 80 },
@@ -174,18 +174,18 @@
}
function add() {
window.location.href = '/admin/articles/add';
window.location.href = '/admin/article/add';
}
function edit(id) {
window.location.href = '/admin/articles/edit?id=' + id;
window.location.href = '/admin/article/edit?id=' + id;
}
function del(id) {
layer.confirm('确定要删除该文章吗?', {
btn: ['确定', '取消']
}, function () {
$.post('/admin/articles/delete', { id: id }, function (res) {
$.post('/admin/article/delete', { id: id }, function (res) {
if (res.code == 0) {
layer.msg(res.msg, { icon: 1 });
setTimeout(function () {