CRMEB/backend/app/adminapi/controller/cms/ArticleCategoryController.php
2026-09-23 15:15:02 +08:00

180 lines
4.9 KiB
PHP
Raw 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
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\adminapi\controller\cms;
use app\adminapi\controller\AuthController;
use app\services\article\ArticleCategoryServices;
use crmeb\services\CacheService;
use think\facade\App;
/**
* 文章分类管理
* Class ArticleCategory
* @package app\adminapi\controller\cms
*/
class ArticleCategoryController extends AuthController
{
/**
* @var ArticleCategoryServices
*/
protected $service;
/**
* ArticleCategory constructor.
* @param App $app
* @param ArticleCategoryServices $service
*/
public function __construct(App $app, ArticleCategoryServices $service)
{
parent::__construct($app);
$this->service = $service;
}
/**
* 获取分类列表
* @return mixed
*/
public function index()
{
$where = $this->request->getMore([
['status', ''],
['title', ''],
['type', 0]
]);
$type = $where['type'];
unset($where['type']);
$data = $this->service->getList($where);
if ($type == 1) $data = $data['list'];
return app('json')->success($data);
}
/**
* 创建新增表单
* @return mixed
* @throws \FormBuilder\Exception\FormBuilderException
*/
public function create()
{
return app('json')->success($this->service->createForm(0));
}
/**
* 保存新建分类
* @return mixed
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function save()
{
$data = $this->request->postMore([
['title', ''],
['pid', 0],
['intr', ''],
['image', ''],
['sort', 0],
['status', 0]
]);
if (!$data['title']) {
return app('json')->fail('请填写分类名称');
}
$data['add_time'] = time();
$this->service->save($data);
CacheService::delete('ARTICLE_CATEGORY');
CacheService::delete('ARTICLE_CATEGORY_PC');
return app('json')->success('添加成功');
}
/**
* 创建修改表单
* @param int $id
* @return mixed
* @throws \FormBuilder\Exception\FormBuilderException
*/
public function edit($id = 0)
{
if (!$id) return app('json')->fail('参数错误');
return app('json')->success($this->service->createForm($id));
}
/**
* 保存修改分类
* @param $id
* @return mixed
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function update($id)
{
$data = $this->request->postMore([
['id', 0],
['title', ''],
['pid', 0],
['intr', ''],
['image', ''],
['sort', 0],
['status', 0]
]);
$this->service->update($data);
CacheService::delete('ARTICLE_CATEGORY');
CacheService::delete('ARTICLE_CATEGORY_PC');
return app('json')->success('修改成功');
}
/**
* 删除文章分类
* @param $id
* @return mixed
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function delete($id)
{
if (!$id) return app('json')->fail('参数错误');
$this->service->del($id);
CacheService::delete('ARTICLE_CATEGORY');
CacheService::delete('ARTICLE_CATEGORY_PC');
return app('json')->success('删除成功');
}
/**
* 修改文章分类状态
* @param int $id
* @param int $status
* @return mixed
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function setStatus($id, $status)
{
if ($status === '' || $status === null || empty($id)) return app('json')->fail('参数错误');
$this->service->setStatus($id, $status);
CacheService::delete('ARTICLE_CATEGORY');
CacheService::delete('ARTICLE_CATEGORY_PC');
return app('json')->success('设置成功');
}
/**
* 获取文章分类
* @return mixed
*/
public function categoryList()
{
return app('json')->success($this->service->getArticleTwoCategory());
}
/**
* 树形列表
* @return mixed
* @throws \ReflectionException
*/
public function getTreeList()
{
$list = $this->service->getTreeList();
return app('json')->success($list);
}
}