89 lines
2.2 KiB
PHP
89 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\logic\PostCategoryLogic;
|
|
use app\common\model\NavItems;
|
|
use app\common\validate\PostCategoryValidate;
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
use app\common\model\PostCategory;
|
|
use support\Request;
|
|
|
|
class PostCategoryService
|
|
{
|
|
|
|
use CrudServiceTrait;
|
|
|
|
protected $logic;
|
|
|
|
/**
|
|
* 指定当前要操作的模型
|
|
*/
|
|
protected string $modelClass = PostCategory::class;
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected $validator = PostCategoryValidate::class;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->logic = (new PostCategoryLogic());
|
|
}
|
|
|
|
/**
|
|
* 获取验证器
|
|
*/
|
|
public function getValidator()
|
|
{
|
|
return new $this->validator;
|
|
}
|
|
|
|
/**
|
|
* 获取系统list
|
|
*/
|
|
public function getList($params)
|
|
{
|
|
$where = [];
|
|
if(isset($params['parent_id']) && $params['parent_id']) {
|
|
$where['parent_id'] = $params['parent_id'];
|
|
}else{
|
|
$where['parent_id'] = 0;
|
|
}
|
|
$result = $this->logic->getList($where)->toArray();
|
|
foreach ($result as $key => $value) {
|
|
$where = [];
|
|
$where['parent_id'] = $value['id'];
|
|
$result[$key]['children'] = $this->logic->getList($where)->toArray();
|
|
foreach ( $result[$key]['children'] as $k => $v) {
|
|
$where = [];
|
|
$where['parent_id'] = $v['id'];
|
|
$result[$key]['children'][$k]['children'] = $this->logic->getList($where)->toArray();
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 添加前置处理
|
|
*/
|
|
public function beforeServiceStore($data, Request $request)
|
|
{
|
|
$brand_id = $data['parent_id'] ?? 0;
|
|
if($brand_id == 0) {
|
|
$data['level'] = 1;
|
|
$data['path'] = '';
|
|
return $data;
|
|
}
|
|
$parent = PostCategory::where('id', $data['parent_id'])->first();
|
|
$parent['path'] = !empty($parent['path'])?:'/';
|
|
$data['level'] = $parent['level']+1;
|
|
$data['path'] = $parent['path'].$parent['id'].'/';
|
|
|
|
if($data['level'] > 3) {
|
|
throw new \Exception("分类层级不能超过三层");
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
} |