CRMEB/backend/app/services/diy/PageCategoryServices.php
2026-09-23 15:15:02 +08:00

135 lines
4.5 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>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\services\diy;
use crmeb\basic\BaseService;
use app\dao\diy\PageCategoryDao;
use crmeb\exceptions\AdminException;
use crmeb\services\CacheService;
use crmeb\utils\FormBuilder as Form;
use think\facade\Route as Url;
/**
* Class PageCategoryServices
* @package app\services\diy
*/
class PageCategoryServices extends BaseService
{
/**
* @var PageCategoryDao
*/
protected $dao;
protected $tree_page_category_key = 'tree_page_categroy';
/**
* PageCategoryServices constructor.
* @param PageCategoryDao $dao
*/
public function __construct(PageCategoryDao $dao)
{
$this->dao = $dao;
}
/**
* 获取分类列表
* @return bool|mixed|null
*/
public function getCategroyList()
{
// return CacheService::remember($this->tree_page_category_key, function () {
return $this->getSonCategoryList();
// }, 86400);
}
/**
* tree分类列表
* @param int $pid
* @param string $parent_name
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getSonCategoryList($pid = 0)
{
$list = $this->dao->getList(['pid' => $pid], 'id,pid,type,name');
$arr = [];
if ($list) {
foreach ($list as $item) {
$item['title'] = $item['name'];
$item['expand'] = true;
$item['children'] = $this->getSonCategoryList($item['id']);
$arr[] = $item;
}
}
return $arr;
}
public function getLinkCategoryForm($cate_id = 0, $pid = 1)
{
$info = $this->dao->get($cate_id);
$list = $this->dao->getList(['pid' => 1]);
$data = [['value' => 1, 'label' => '顶级分类']];
foreach ($list as $menu) {
$data[] = ['value' => $menu['id'], 'label' => $menu['name']];
}
$pid = isset($info['pid']) ? $info['pid'] : $pid;
$f[] = Form::hidden('id', $cate_id);
$f[] = Form::select('pid', '上级分类', (int)$pid)->setOptions($data)->filterable(true);
$f[] = Form::input('name', '分类名称', $info['name'] ?? '')->required();
$f[] = Form::input('type', '分类类型', $info['type'] ?? '')->required();
$f[] = Form::number('sort', '排序', (int)($info['sort'] ?? 0))->min(0)->precision(0);
$f[] = Form::radio('status', '状态', $info['status'] ?? 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]]);
return create_form($cate_id ? '修改分类' : '添加分类', $f, Url::buildUrl('/diy/link/category/save/' . $cate_id), 'POST');
}
public function getLinkCategorySave($cate_id, $data)
{
if ($cate_id) {
$res = $this->dao->update($cate_id, $data);
} else {
$data['add_time'] = time();
$res = $this->dao->save($data);
}
if (!$res) {
throw new AdminException('保存失败');
} else {
return true;
}
}
public function getLinkCategoryDel($cate_id)
{
$cate_id = (int)$cate_id;
if (!$cate_id || !$this->dao->get($cate_id)) {
throw new AdminException('分类不存在');
}
if ($this->dao->count(['pid' => $cate_id])) {
throw new AdminException('该分类有下级分类,无法删除');
}
/** @var PageLinkServices $pageLinkServices */
$pageLinkServices = app()->make(PageLinkServices::class);
if ($pageLinkServices->count(['cate_id' => $cate_id])) {
throw new AdminException('该分类下有链接,无法删除');
}
$res = $this->dao->delete($cate_id);
if (!$res) {
throw new AdminException('删除失败');
} else {
return true;
}
}
}