dev_oa/app/home/controller/Article.php

273 lines
10 KiB
PHP
Raw Normal View History

<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.gougucms.com
*/
2021-11-18 00:05:13 +08:00
declare (strict_types = 1);
namespace app\home\controller;
use app\home\BaseController;
use app\home\model\Article as ArticleList;
use app\home\model\ArticleCate;
use app\home\validate\ArticleCateCheck;
use app\home\validate\ArticleCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
class Article extends BaseController
{
public function cate()
{
if (request()->isAjax()) {
$cate = Db::name('ArticleCate')->order('create_time asc')->select();
return to_assign(0, '', $cate);
2021-11-18 00:05:13 +08:00
} else {
return view();
2021-11-18 00:05:13 +08:00
}
}
2021-11-18 00:05:13 +08:00
//文章分类添加&编辑
public function cate_add()
{
2021-11-18 00:05:13 +08:00
$param = get_params();
if (request()->isAjax()) {
if (!empty($param['id']) && $param['id'] > 0) {
2021-11-23 23:09:15 +08:00
try {
validate(ArticleCateCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
2021-11-18 00:05:13 +08:00
}
2021-11-23 23:09:15 +08:00
$note_array = get_article_son($param['id']);
if (in_array($param['pid'], $note_array)) {
return to_assign(1, '父级分类不能是该分类本身或其子分类');
} else {
$param['update_time'] = time();
$res = ArticleCate::strict(false)->field(true)->update($param);
if($res){
add_log('edit', $param['id'], $param);
return to_assign();
}
}
} else {
try {
validate(ArticleCateCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['create_time'] = time();
$insertId = ArticleCate::strict(false)->field(true)->insertGetId($param);
if ($insertId) {
add_log('add', $insertId, $param);
}
return to_assign();
}
2021-11-18 12:19:57 +08:00
} else {
2021-11-23 23:09:15 +08:00
$id = isset($param['id']) ? $param['id'] : 0;
2021-11-18 00:05:13 +08:00
$pid = isset($param['pid']) ? $param['pid'] : 0;
2021-11-23 23:09:15 +08:00
if ($id > 0) {
$detail = Db::name('ArticleCate')->where(['id' => $id])->find();
View::assign('detail', $detail);
}
View::assign('id', $id);
2021-11-18 00:05:13 +08:00
View::assign('pid', $pid);
return view();
}
}
2021-11-18 00:05:13 +08:00
//删除文章分类
public function cate_delete()
{
$id = get_params("id");
$cate_count = Db::name('ArticleCate')->where(["pid" => $id])->count();
if ($cate_count > 0) {
return to_assign(1, "该分类下还有子分类,无法删除");
}
$content_count = Db::name('Article')->where(["article_cate_id" => $id])->count();
if ($content_count > 0) {
return to_assign(1, "该分类下还有文章,无法删除");
}
if (Db::name('ArticleCate')->delete($id) !== false) {
add_log('delete', $id);
return to_assign(0, "删除分类成功");
} else {
return to_assign(1, "删除失败");
}
}
public function index()
{
if (request()->isAjax()) {
$param = get_params();
$where = array();
if (!empty($param['keywords'])) {
2021-11-18 12:19:57 +08:00
$where[] = ['a.id|a.title|a.keywords|a.desc|a.content|c.title', 'like', '%' . $param['keywords'] . '%'];
}
if (!empty($param['article_cate_id'])) {
$where[] = ['a.article_cate_id', '=', $param['article_cate_id']];
}
$where[] = ['a.status', '>=', 0];
$where[] = ['a.is_share', '=', 1];
$rows = empty($param['limit']) ? get_config(app . page_size) : $param['limit'];
$content = ArticleList::where($where)
->field('a.*,c.id as cate_id,a.id as id,c.title as cate_title,a.title as title,d.title as department,u.name as user')
->alias('a')
->join('article_cate c', 'a.article_cate_id = c.id')
->join('admin u', 'a.uid = u.id','LEFT')
->join('department d', 'a.did = d.id','LEFT')
->order('a.create_time desc')
->paginate($rows, false, ['query' => $param]);
return table_assign(0, '', $content);
} else {
return view();
}
}
public function list()
{
if (request()->isAjax()) {
$param = get_params();
$where = array();
if (!empty($param['keywords'])) {
$where[] = ['a.id|a.title|a.keywords|a.desc|a.content|c.title', 'like', '%' . $param['keywords'] . '%'];
}
if (!empty($param['article_cate_id'])) {
$where[] = ['a.article_cate_id', '=', $param['article_cate_id']];
}
$where[] = ['a.status', '>=', 0];
2021-11-18 12:19:57 +08:00
$where[] = ['a.uid', '=', $this->uid];
$rows = empty($param['limit']) ? get_config(app . page_size) : $param['limit'];
$content = ArticleList::where($where)
2021-11-18 12:19:57 +08:00
->field('a.*,c.id as cate_id,a.id as id,c.title as cate_title,a.title as title')
->alias('a')
2021-11-18 12:19:57 +08:00
->join('article_cate c', 'a.article_cate_id = c.id')
->order('a.create_time desc')
->paginate($rows, false, ['query' => $param]);
return table_assign(0, '', $content);
2021-11-18 00:05:13 +08:00
} else {
return view();
2021-11-18 00:05:13 +08:00
}
}
//文章添加&&编辑
public function add()
{
2021-11-18 00:05:13 +08:00
$param = get_params();
if (request()->isAjax()) {
2021-11-18 00:05:13 +08:00
$DbRes = false;
if (!empty($param['id']) && $param['id'] > 0) {
try {
validate(ArticleCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
2021-11-18 00:05:13 +08:00
$param['update_time'] = time();
Db::startTrans();
try {
2021-11-18 00:05:13 +08:00
$res = ArticleList::strict(false)->field(true)->update($param);
$aid = $param['id'];
if ($res) {
//关联关键字
2021-11-18 00:05:13 +08:00
if (isset($param['keyword_names']) && $param['keyword_names']) {
Db::name('ArticleKeywords')->where(['aid' => $aid])->delete();
$keywordArray = explode(',', $param['keyword_names']);
$res_keyword = (new ArticleList())->insertKeyword($keywordArray, $aid);
} else {
$res_keyword == true;
}
2021-11-18 00:05:13 +08:00
if ($res_keyword !== false) {
add_log('edit', $param['id'], $param);
Db::commit();
2021-11-18 00:05:13 +08:00
$DbRes = true;
}
2021-11-18 00:05:13 +08:00
} else {
Db::rollback();
}
2021-11-18 00:05:13 +08:00
} catch (\Exception $e) { ##这里参数不能删除($e错误信息)
Db::rollback();
}
} else {
try {
validate(ArticleCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['create_time'] = time();
2021-11-18 12:19:57 +08:00
$param['uid'] = $this->uid;
$param['did'] = get_login_admin('did');
Db::startTrans();
try {
2021-11-18 00:05:13 +08:00
if (empty($param['desc'])) {
$param['desc'] = get_desc_content($param['content'], 100);
}
$aid = ArticleList::strict(false)->field(true)->insertGetId($param);
if ($aid) {
//关联关键字
2021-11-18 00:05:13 +08:00
if (isset($param['keyword_names']) && $param['keyword_names']) {
$keywordArray = explode(',', $param['keyword_names']);
$res_keyword = (new ArticleList())->insertKeyword($keywordArray, $aid);
} else {
$res_keyword == true;
}
2021-11-18 00:05:13 +08:00
if ($res_keyword !== false) {
add_log('add', $aid, $param);
Db::commit();
2021-11-18 00:05:13 +08:00
$DbRes = true;
}
} else {
2021-11-18 00:05:13 +08:00
Db::rollback();
}
2021-11-18 00:05:13 +08:00
} catch (\Exception $e) { ##这里参数不能删除($e错误信息)
Db::rollback();
}
}
2021-11-18 00:05:13 +08:00
if ($DbRes) {
return to_assign();
} else {
return to_assign(1, '操作失败');
}
2021-11-18 12:19:57 +08:00
} else {
2021-11-18 00:05:13 +08:00
$id = isset($param['id']) ? $param['id'] : 0;
View::assign('id', $id);
if ($id > 0) {
$article = (new ArticleList())->detail($id);
View::assign('article', $article);
return view('edit');
}
return view();
}
}
2021-11-18 12:19:57 +08:00
//查看文章
public function view()
{
$id = get_params("id");
$detail = (new ArticleList())->detail($id);
// read 字段加 1
Db::name('article')->where('id', $id)->inc('read')->update();
View::assign('detail', $detail);
return view();
}
//删除文章
public function delete()
{
$id = get_params("id");
$data['status'] = '-1';
$data['id'] = $id;
$data['update_time'] = time();
if (Db::name('Article')->update($data) !== false) {
add_log('delete', $id);
return to_assign(0, "删除成功");
} else {
return to_assign(1, "删除失败");
}
}
}