增加文章发布功能

This commit is contained in:
2025-05-09 17:35:43 +08:00
parent f879d27fac
commit e7bcaf29f5
35 changed files with 26474 additions and 61 deletions
+279
View File
@@ -0,0 +1,279 @@
<?php
/**
* 后台管理系统-文章管理
*/
namespace app\admin\controller;
use app\admin\controller\Base;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
class Article extends Base
{
// 文章列表
public function articlelist()
{
if (Request::isPost()) {
$lists = Db::table('yz_article')
->where('delete_time', null)
->where('status', '<>', 3)
->order('id DESC')
->select()
->each(function ($item) {
$item['create_time'] = date('Y-m-d H:i:s', strtotime($item['create_time']));
$item['publishdate'] = $item['publishdate'] ? date('Y-m-d H:i:s', strtotime($item['publishdate'])) : '';
return $item;
});
return json(['code' => 0, 'msg' => '获取成功', 'data' => $lists]);
} else {
$lists = Db::table('yz_article')
->where('delete_time', null)
->where('status', '<>', 3)
->order('id DESC')
->select()
->each(function ($item) {
$item['create_time'] = date('Y-m-d H:i:s', strtotime($item['create_time']));
return $item;
});
View::assign(['lists' => $lists]);
return View::fetch();
}
}
// 添加文章
public function add()
{
if (Request::isPost()) {
$data = [
'title' => input('post.title'),
'cate' => input('post.cate'),
'image' => input('post.image'),
'content' => input('post.content'),
'author' => input('post.author'),
'desc' => input('post.desc'),
'status' => input('post.status', 2),
'publishdate' => date('Y-m-d H:i:s', time()),
'create_time' => date('Y-m-d H:i:s', time())
];
$insert = Db::table('yz_article')->insert($data);
if (empty($insert)) {
return json(['code' => 1, 'msg' => '添加失败', 'data' => []]);
}
return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
} else {
$lists = Db::table('yz_article')
->order('id DESC')
->select()
->each(function ($item, $key) {
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
return $item;
});
View::assign([
'lists' => $lists
]);
return View::fetch();
}
}
// 编辑文章
public function edit()
{
if (Request::isPost()) {
$article_id = input('post.article_id');
$data = [
'title' => input('post.title'),
'content' => input('post.content'),
'author' => input('post.author'),
'status' => input('post.status', 1),
'update_time' => time()
];
$update = Db::table('yz_article')
->where('article_id', $article_id)
->update($data);
if ($update === false) {
return json(['code' => 1, 'msg' => '更新失败', 'data' => []]);
}
return json(['code' => 0, 'msg' => '更新成功', 'data' => []]);
} else {
$article_id = input('get.article_id');
$info = Db::table('yz_article')->where('article_id', $article_id)->find();
View::assign([
'info' => $info
]);
return View::fetch();
}
}
// 删除文章
public function delete()
{
$id = input('post.id');
$data = [
'delete_time' => date('Y-m-d H:i:s', time()),
];
$delete = Db::table('yz_article')->where('id', $id)->update($data);
if ($delete === false) {
return json(['code' => 1, 'msg' => '删除失败', 'data' => []]);
}
return json(['code' => 0, 'msg' => '删除成功', 'data' => []]);
}
// 文章分类
public function articlecate()
{
if (Request::isPost()) {
$count = Db::table('yz_article_category')
->where('delete_time', null)
->where('status', 1)
->count();
$page = (int) input('post.page', 1);
$limit = (int) input('post.limit', 10);
$lists = Db::table('yz_article_category')
->where('delete_time', null)
->where('status', 1)
->order('id asc')
->page($page, $limit)
->select();
return json(['code' => 0, 'msg' => '获取成功', 'data' => $lists]);
} else {
// 获取分类列表
$lists = Db::table('yz_article_category')
->where('delete_time', null)
->where('status', 1)
->order('id asc')
->select();
View::assign([
'lists' => $lists
]);
return View::fetch();
}
}
//获取分类结构
public function getcate()
{
// 获取所有分类
$lists = Db::table('yz_article_category')
->where('delete_time', null)
->where('status', 1)
->order('sort asc, id asc')
->select()
->toArray();
// 构建父子结构
$tree = [];
foreach ($lists as $item) {
if ($item['cid'] == 0) {
// 顶级分类
$tree[] = $item;
} else {
// 子分类
foreach ($tree as &$parent) {
if ($parent['id'] == $item['cid']) {
if (!isset($parent['children'])) {
$parent['children'] = [];
}
$parent['children'][] = $item;
break;
}
}
}
}
return json(['code' => 0, 'msg' => '获取成功', 'data' => $tree]);
}
// 添加文章分类
public function cateadd()
{
if (Request::isPost()) {
// 将时间戳转换为 'Y-m-d H:i:s' 格式
$currentDateTime = date('Y-m-d H:i:s');
$data = [
'name' => input('post.name'),
'cid' => input('post.cid'),
'sort' => input('post.sort', 0),
'status' => input('post.status', 1),
'create_time' => $currentDateTime
];
$insert = Db::table('yz_article_category')->insert($data);
if (empty($insert)) {
return json(['code' => 1, 'msg' => '添加失败', 'data' => []]);
}
return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
} else {
$lists = Db::table('yz_article_category')
->order('id DESC')
->select()
->each(function ($item, $key) {
$item['create_time'] = date('Y-m-d H:i:s', strtotime($item['create_time']));
return $item;
});
View::assign([
'lists' => $lists
]);
return View::fetch();
}
}
//编辑文章分类
public function cateedit()
{
if (Request::isPost()) {
$data = [
'id' => input('post.id'),
'name' => input('post.name'),
'cid' => input('post.cid'),
'sort' => input('post.sort', 0),
'status' => input('post.status', 1),
'update_time' => date('Y-m-d H:i:s')
];
$update = Db::table('yz_article_category')
->where('id', $data['id'])
->update($data);
if ($update === false) {
return json(['code' => 1, 'msg' => '更新失败', 'data' => []]);
}
return json(['code' => 0, 'msg' => '更新成功', 'data' => []]);
} else {
$id = input('get.id');
$info = Db::table('yz_article_category')->where('id', $id)->find();
View::assign([
'info' => $info
]);
return View::fetch();
}
}
//删除文章分类
public function catedel()
{
$id = input('post.id');
// 检查是否有子分类
$hasChildren = Db::table('yz_article_category')
->where('cid', $id)
->where('delete_time', null)
->find();
if ($hasChildren) {
return json(['code' => 1, 'msg' => '该分类下有子分类,无法删除', 'data' => []]);
}
$delete = Db::table('yz_article_category')
->where('id', $id)
->update(['delete_time' => date('Y-m-d H:i:s')]);
if ($delete === false) {
return json(['code' => 1, 'msg' => '删除失败', 'data' => []]);
}
return json(['code' => 0, 'msg' => '删除成功', 'data' => []]);
}
}
+1 -1
View File
@@ -62,7 +62,7 @@ class Base{
if($code == 0){
$arr = array(
'code'=>$code,
'msg'=>'成功',
'msg'=>'操作成功',
'count'=> $count,
'data' => $data
);
+2 -2
View File
@@ -198,9 +198,9 @@ class Index extends Base{
$a = delete_dir_file(Env::get('runtime_path').'cache/');
$b = delete_dir_file(Env::get('runtime_path').'temp/');
if ($a || $b) {
$this->returnCode(0);
$this->returnCode(0, '清除缓存成功');
} else {
$this->returnCode('91000006');
$this->returnCode(1, '清除缓存失败');
}
}
}
+6 -6
View File
@@ -31,11 +31,11 @@ class Login
if (Request::isPost()) {
$account = trim(input('post.account'));
if (empty($account)) {
$this->returnCode('90000001');
$this->returnCode(1, '账号不能为空');
}
$pattern = "/^([0-9A-Za-z-_.]+)@([0-9a-z]+.[a-z]{2,3}(.[a-z]{2})?)$/i";
if (!preg_match($pattern, $account)) {
$this->returnCode('90000006');
$this->returnCode(1, '邮箱格式不正确');
}
$password = trim(input('post.password'));
if (empty($password)) {
@@ -50,13 +50,13 @@ class Login
}
$aUser = Db::table('yz_admin_user')->where('account', $account)->find();
if (empty($aUser)) {
$this->returnCode('90000029');
$this->returnCode(1, '账号不存在');
}
if ($aUser['status'] != 1) {
$this->returnCode('90000030');
$this->returnCode(1, '账号已被禁用');
}
if ($aUser['password'] != md5($password)) {
$this->returnCode('90000031');
$this->returnCode(1, '密码错误');
}
$remember = input('post.remember');
if (!empty($remember)) {
@@ -69,7 +69,7 @@ class Login
Db::table('yz_admin_user')->where('uid', $aUser['uid'])->update(
['login_count' => $aUser['login_count'] + 1, 'update_time' => time()]
);
$this->returnCode(0, [], '登成功');
$this->returnCode(0, [], '登成功');
}
}
+65 -40
View File
@@ -19,38 +19,63 @@ class Yunzer extends Base{
return View::fetch();
}
# 菜单添加
public function menuadd(){
public function menuadd()
{
$req = request();
if($req->isPost()){
if ($req->isPost()) {
$data['label'] = trim(input('post.label'));
if(empty($data['label'])){
View::returnCode('90000009');
if (empty($data['label'])) {
$this->returnCode(1, '请输入菜单名称');
}
$data['icon_class'] = trim(input('post.icon_class'));
$data['sort'] = (int)trim(input('post.sort'));
$data['status'] = (int)trim(input('post.status'));
$data['type'] = (int)trim(input('post.type',0));
if($data['type'] == 1){
$data['sort'] = (int) trim(input('post.sort'));
$data['status'] = (int) trim(input('post.status'));
$data['type'] = (int) trim(input('post.type', 0));
if ($data['type'] == 1) {
$data['src'] = trim(input('post.src1'));
if(empty($data['src'])){
$this->returnCode('90000012');
if (empty($data['src'])) {
$this->returnCode(1, '请输入内部跳转地址');
}
}else if($data['type'] == 2){
} else if ($data['type'] == 2) {
$data['src'] = trim(input('post.src2'));
if(empty($data['src'])){
$this->returnCode('90000013');
if (empty($data['src'])) {
$this->returnCode(1, '请输入超链接地址');
}
}else{
} else {
$data['src'] = '';
}
// 保存用户
// 保存菜单
$res = Db::table('yz_admin_sys_menu')->insert($data);
if(!$res){
$this->returnCode('91000001');
if (!$res) {
$this->returnCode(1, '添加菜单失败');
}
// 获取新插入的菜单ID
$newMenuId = Db::table('yz_admin_sys_menu')->getLastInsID();
// 获取所有管理员用户组
$adminGroups = Db::table('yz_admin_user_group')->select();
foreach ($adminGroups as $group) {
// 获取现有权限
$rights = [];
if (!empty($group['rights'])) {
$rights = json_decode($group['rights'], true);
}
// 添加新菜单ID到权限列表
if (!in_array($newMenuId, $rights)) {
$rights[] = $newMenuId;
// 更新用户组权限
Db::table('yz_admin_user_group')
->where('group_id', $group['group_id'])
->update(['rights' => json_encode($rights)]);
}
}
$this->returnCode(0);
}else{
$iconfont = Db::table('yz_z_iconfont')->where('status',1)->select();
} else {
$iconfont = Db::table('yz_z_iconfont')->where('status', 1)->select();
View::assign([
'iconfont' => $iconfont
]);
@@ -64,7 +89,7 @@ class Yunzer extends Base{
$smid = (int)input('post.smid');
$data['label'] = trim(input('post.label'));
if(!$data['label']){
$this->returnCode('90000009');
$this->returnCode(1, '请输入菜单名称');
}
$data['icon_class'] = trim(input('post.icon_class'));
$data['sort'] = (int)trim(input('post.sort'));
@@ -73,12 +98,12 @@ class Yunzer extends Base{
if($data['type'] == 1){
$data['src'] = trim(input('post.src1'));
if(empty($data['src'])){
$this->returnCode('90000012');
$this->returnCode(1, '请输入内部跳转地址');
}
}else if($data['type'] == 2){
$data['src'] = trim(input('post.src2'));
if(empty($data['src'])){
$this->returnCode('90000013');
$this->returnCode(1, '请输入超链接地址');
}
}else{
$data['src'] = '';
@@ -86,7 +111,7 @@ class Yunzer extends Base{
// 保存用户
$res = Db::table('yz_admin_sys_menu')->where('smid',$smid)->update($data);
if(!$res){
$this->returnCode('91000002');
$this->returnCode(1, '修改菜单失败');
}
$this->returnCode(0);
}else{
@@ -105,11 +130,11 @@ class Yunzer extends Base{
$smid = (int)input('post.smid');
$count = Db::table('yz_admin_sys_menu')->where('parent_id',$smid)->count();
if($count > 0){
$this->returnCode('90000010');
$this->returnCode(1, '该菜单下还有子菜单,不能删除');
}
$res = Db::table('yz_admin_sys_menu')->where('smid',$smid)->delete();
if(empty($res)){
$this->returnCode('91000003');
$this->returnCode(1, '删除菜单失败');
}
$this->returnCode(0);
}
@@ -148,30 +173,30 @@ class Yunzer extends Base{
$smid = (int)input('post.smid');
$data['label'] = trim(input('post.label'));
if(!$data['label']){
$this->returnCode('90000015');
$this->returnCode(1, '请输入按钮名称');
}
$data['icon_class'] = trim(input('post.icon_class'));
$data['sort'] = (int)trim(input('post.sort'));
$data['status'] = (int)trim(input('post.status'));
$data['type'] = (int)trim(input('post.type'));
if(empty($data['type'])){
$this->returnCode('90000011');
$this->returnCode(1, '请选择按钮类型');
}
if($data['type'] == 1){
$data['src'] = trim(input('post.src1'));
if(empty($data['src'])){
$this->returnCode('90000012');
$this->returnCode(1, '请输入内部跳转地址');
}
}else if($data['type'] == 2){
$data['src'] = trim(input('post.src2'));
if(empty($data['src'])){
$this->returnCode('90000013');
$this->returnCode(1, '请输入超链接地址');
}
}
$data['parent_id'] = $smid;
$res = Db::table('yz_admin_sys_menu')->insert($data);
if(!$res){
$this->returnCode('91000001');
$this->returnCode(1, '添加按钮失败');
}
$this->returnCode(0);
}else{
@@ -191,29 +216,29 @@ class Yunzer extends Base{
$smid = (int)input('post.smid');
$data['label'] = trim(input('post.label'));
if(!$data['label']){
$this->returnCode('90000015');
$this->returnCode(1, '请输入按钮名称');
}
$data['icon_class'] = trim(input('post.icon_class'));
$data['sort'] = (int)trim(input('post.sort'));
$data['status'] = (int)trim(input('post.status'));
$data['type'] = (int)trim(input('post.type'));
if(empty($data['type'])){
$this->returnCode('90000011');
$this->returnCode(1, '请选择按钮类型');
}
if($data['type'] == 1){
$data['src'] = trim(input('post.src1'));
if(empty($data['src'])){
$this->returnCode('90000012');
$this->returnCode(1, '请输入内部跳转地址');
}
}else if($data['type'] == 2){
$data['src'] = trim(input('post.src2'));
if(empty($data['src'])){
$this->returnCode('90000013');
$this->returnCode(1, '请输入超链接地址');
}
}
$res = Db::table('yz_admin_sys_menu')->where('smid',$smid)->update($data);
if(!$res){
$this->returnCode('91000002');
$this->returnCode(1, '修改按钮失败');
}
$this->returnCode(0);
}else{
@@ -232,7 +257,7 @@ class Yunzer extends Base{
$smid = (int)input('post.smid');
$res = Db::table('yz_admin_sys_menu')->where('smid',$smid)->delete();
if(empty($res)){
$this->returnCode('91000003');
$this->returnCode(1, '删除按钮失败');
}
$this->returnCode(0);
}
@@ -267,7 +292,7 @@ class Yunzer extends Base{
$data['config_sort'] = trim(input('post.config_sort'));
$res = Db::table('yz_admin_config')->insert($data);
if(empty($res)){
$this->returnCode('91000001');
$this->returnCode(1, '添加配置失败');
}
$this->returnCode(0);
}else{
@@ -296,7 +321,7 @@ class Yunzer extends Base{
$data['config_sort'] = trim(input('post.config_sort'));
$res = Db::table('yz_admin_config')->where('config_id',$config_id)->update($data);
if(empty($res)){
$this->returnCode('91000002');
$this->returnCode(1, '修改配置失败');
}
$this->returnCode(0);
}else{
@@ -316,7 +341,7 @@ class Yunzer extends Base{
}
$res = Db::table('yz_admin_config')->where('config_id',$config_id)->delete();
if(empty($res)){
$this->returnCode('91000003');
$this->returnCode(1, '删除配置失败');
}
$this->returnCode(0);
}
@@ -331,7 +356,7 @@ class Yunzer extends Base{
$oConfig = new YzAdminConfig();
$updateAll = $oConfig->updateAll($post);
if(empty($updateAll)){
$this->returnCode('91000002');
$this->returnCode(1, '更新配置值失败');
}
$this->returnCode(0);
}else{