You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.0 KiB
105 lines
3.0 KiB
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\api\model;
|
|
|
|
use think\Model;
|
|
|
|
/**
|
|
* @mixin think\Model
|
|
*/
|
|
class Cate extends CmsCommon
|
|
{
|
|
|
|
public $table = 'bmz_cate';
|
|
|
|
|
|
/**
|
|
* 列表数据模型
|
|
* @return array|\think\Collection
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function index()
|
|
{
|
|
// $_op = config('app.app_debug') ?: $_op = $this->where('is_dev', 0); //开发者模式
|
|
|
|
$module_id = $this->request->param('module_id', 0);
|
|
$_op = $this->field('*');
|
|
|
|
if($module_id){
|
|
$_op = $this->where('module_id',$module_id);
|
|
}
|
|
|
|
$data = parent::parentLists($_op, 0);
|
|
|
|
$mudule = new Module();
|
|
$data_module = $mudule->parentLists(null,0);
|
|
$module_arr = [];
|
|
foreach ($data_module['data'] as $v){
|
|
$module_arr[$v['id']] = $v['module_name'];
|
|
}
|
|
|
|
foreach ($data['data'] as &$item){
|
|
if(isset($module_arr[$item['module_id']])){
|
|
$item['module_name'] = $module_arr[$item['module_id']];
|
|
}else{
|
|
$item['module_name'] = '';
|
|
}
|
|
}
|
|
$data['data'] = $this->getHasChild($data['data']);
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
function getHasChild($data){
|
|
|
|
foreach ($data as &$item){
|
|
$item['has_child'] = 0;
|
|
foreach ($data as $val){
|
|
if($val['parent_id'] == $item['id']){
|
|
$item['has_child'] = 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return $data;
|
|
|
|
}
|
|
/**
|
|
* 新增或修改前
|
|
* @param Model $model
|
|
* @return mixed|void
|
|
*/
|
|
public static function onBeforeWrite(Model $model)
|
|
{
|
|
//设定child_list
|
|
if (empty($model->getAttr('parent_id'))) {
|
|
//parent_id = 0 一级栏目填充
|
|
$child_list = self::where('parent_id', 0)->max('child_list');
|
|
$child_list = !empty($child_list) ? (int)$child_list + 1 : 100; //如果不存在设置默认值 100起始
|
|
$model->setAttr('child_list', $child_list);
|
|
} else {
|
|
//子栏目填充
|
|
$child_list = self::where('parent_id', $model->getAttr('parent_id'))->max('child_list'); //同级最大
|
|
$parent_child_list = self::where('id', $model->getAttr('parent_id'))->value('child_list'); //父级初始
|
|
$child_list = !empty($child_list) ? (int)$child_list + 1 : $parent_child_list . '100';
|
|
$model->setAttr('child_list', $child_list);
|
|
}
|
|
parent::onBeforeWrite($model);
|
|
}
|
|
|
|
|
|
public static function onAfterDelete(Model $model)
|
|
{
|
|
//删除父级栏目下所有子栏目
|
|
if ($model->getAttr('child_list')) {
|
|
self::where('child_list', 'like', $model->getAttr('child_list') . '%')->useSoftDelete('delete_time', time())->delete();
|
|
}
|
|
parent::onAfterDelete($model);
|
|
}
|
|
|
|
}
|
|
|