* @return mixed */ public function index() { // 保存模块排序 if ($this->request->isPost()) { $modules = $this->request->post('sort/a'); if ($modules) { foreach ($modules as $key => $module) { $data[] = [ 'id' => $module, 'sort' => $key + 1 ]; } $ProductCategoryModel = new ProductCategoryModel(); if (false !== $ProductCategoryModel->saveAll($data)) { return $this->success('保存成功'); } else { return $this->error('保存失败'); } } } $data_list = ProductCategoryModel::getMenusByGroup(); $max_level = $this->request->get('max', 0); $this->assign('menus', $this->getNestMenu($data_list, $max_level)); $this->assign('page_title', '类别管理'); return $this->fetch(); } /** * 新增类别 * @author loomis <2477365162@qq.com> */ public function add($module = 'admin', $pid = '') { // 保存数据 if ($this->request->isPost()) { $data = $this->request->post(); // 验证 $result = $this->validate($data, 'Category'); // 验证失败 输出错误信息 if(true !== $result) return $this->error($result); $data['UID'] = UID; if ($menu = ProductCategoryModel::create($data)) { return $this->success('新增成功',url('index')); } else { return $this->error('新增失败'); } } else { $category_list = ProductCategoryModel::getMenuTree(0, '', $module); // 使用ZBuilder快速创建表单 return ZBuilder::make('form') ->setPageTitle('新增类别') ->addFormItems([ ['image', 'thumb', '缩略图'], ['select', 'pid', '所属类别', '所属上级类别', $category_list, $pid], ['text', 'title', '类别标题'], ['radio', 'is_index', '是否首页显示', '',[0=>'不显示',1=>'显示'],0], ]) ->addIcon('icon', '图标', '导航图标') ->addText('sort', '排序', '', 100) ->layout(['pid' => '8', 'title' => '8', 'icon' => '8', 'sort' => '8']) ->fetch(); } } /** * 编辑类别 * @param int $id 类别ID * @author loomis <2477365162@qq.com> * @return mixed */ public function edit($id = 0) { if ($id === 0) return $this->error('缺少参数'); // 保存数据 if ($this->request->isPost()) { $data = $this->request->post(); // 验证 $result = $this->validate($data, 'Category'); // 验证失败 输出错误信息 if(true !== $result) return $this->error($result); if (ProductCategoryModel::update($data)) { return $this->success('编辑成功', url('index')); } else { return $this->error('编辑失败'); } } // 获取数据 $info = ProductCategoryModel::get($id); // 使用ZBuilder快速创建表单 return ZBuilder::make('form') ->setPageTitle('编辑类别') ->addFormItem('hidden', 'id') ->addFormItem('image', 'thumb', '缩略图') ->addFormItem('select', 'pid', '所属类别', '所属上级类别', ProductCategoryModel::getMenuTree(0, '', $info['module'])) ->addFormItem('text', 'title', '类别标题') ->addFormItem('radio', 'is_index', '是否首页显示','', [0=>'不显示',1=>'显示']) ->addIcon('icon', '图标', '导航图标') ->addText('sort', '排序', '', 100) ->layout(['pid' => '8', 'title' => '8', 'icon' => '8', 'sort' => '8']) ->setFormData($info) ->fetch(); } /** * 删除类别 * @return mixed */ public function delete($record = []) { $id = $this->request->param('id'); $menu = ProductCategoryModel::where('id', $id)->find(); if ($menu['system_menu'] == '1') return $this->error('系统类别,禁止删除'); // 获取该类别的所有后辈类别id $menu_childs = ProductCategoryModel::getChildsId($id); // 要删除的所有类别id $all_ids = array_merge([(int)$id], $menu_childs); // 删除类别 if (ProductCategoryModel::destroy($all_ids)) { return $this->success('删除成功'); } else { return $this->error('删除失败'); } } /** * 保存类别排序 * @return mixed */ public function save() { if ($this->request->isPost()) { $data = $this->request->post(); if (!empty($data)) { $menus = $this->parseMenu($data['menus']); foreach ($menus as $menu) { if ($menu['pid'] == 0) { ProductCategoryModel::update(array('sort'=>$menu['sort']),array('id'=>$menu['id'])); }else{ ProductCategoryModel::update(array('pid'=>$menu['pid'],'sort'=>$menu['sort']),array('id'=>$menu['id'])); } } return $this->success('保存成功'); } else { return $this->error('没有需要保存的类别'); } } return $this->error('非法请求'); } /** * 递归解析类别 * @param array $menus 类别数据 * @param int $pid 上级类别id * @return array 解析成可以写入数据库的格式 */ private function parseMenu($menus = [], $pid = 0) { $sort = 1; $result = []; foreach ($menus as $menu) { $result[] = [ 'id' => (int)$menu['id'], 'pid' => (int)$pid, 'sort' => $sort, ]; if (isset($menu['children'])) { $result = array_merge($result, $this->parseMenu($menu['children'], $menu['id'])); } $sort ++; } return $result; } /** * 获取嵌套式类别 * @param array $lists 原始类别数组 * @param int $pid 父级id * @param int $max_level 最多返回多少层,0为不限制 * @param int $curr_level 当前层数 */ private function getNestMenu($lists = [], $max_level = 0, $pid = 0, $curr_level = 1) { $result = ''; foreach ($lists as $key => $value) { if ($value['pid'] == $pid) { $disable = $value['status'] == 0 ? 'dd-disable' : ''; $isindex = $value['is_index']==1?'(首页显示)':null; // 组合类别 $result .= '
  • '; $result .= '
    拖拽
    '.$value['title'].$isindex; if ($value['url_value'] != '') { $result .= ' '.$value['url_value'].''; } $result .= '
    '; $result .= ''; if ($value['status'] == 0) { // 启用 $result .= ''; } else { // 禁用 $result .= ''; } $result .= '
    '; $result .= '
    '; if ($max_level == 0 || $curr_level != $max_level) { unset($lists[$key]); // 下级类别 $children = $this->getNestMenu($lists, $max_level, $value['id'], $curr_level + 1); if ($children != '') { $result .= '
      '.$children.'
    '; } } $result .= '
  • '; } } return $result; } /** * 启用类别 */ public function enable($record = []) { $id = input('param.ids'); $menu = ProductCategoryModel::where('id', $id)->update(['status'=>1]); $this->success('操作成功'); } /** * 禁用类别 */ public function disable($record = []) { $id = input('param.ids'); $menu = ProductCategoryModel::where('id', $id)->update(['status'=>0]); $this->success('操作成功'); } }