-1, 'name' => '不选择', ); private $search_default = array ( 'value' => -1, 'name' => '分类', ); /** * 获取分类数据 * * @return mixed */ public function get_api() { # 关闭语言包 Dever::config('base')->lang = false; # 联动总数 $level_total = 100; # 当前联动级别 $level_num = Dever::input('level_num'); # 一般为id $level_id = Dever::input('level_id'); # 传入的参数 $level_param = Dever::input('level_param'); if ($level_param) { parse_str($level_param, $param); $where['id'] = $param['id']; } # 是否是搜索列表页 $level_search = Dever::input('level_search'); if ($level_search) { $default = $this->search_default; $default['name'] = $level_num . '级' . $default['name']; } else { $default = $this->default; } $data = array(); # 三级联动 if ($level_num == 1) { $where['parent_id'] = -1; } elseif($level_id > 0) { $where['parent_id'] = $level_id; } if (isset($where['parent_id'])) { $data = Dever::db('product/category')->getAll($where); } $type = Dever::input('type'); if ($data) { if ($type && $type == 1) { if ($level_num == 1) { array_unshift($data, $default); } } else { array_unshift($data, $default); } } if (!$data) { Dever::alert('error'); } $result['level_total'] = $level_total; $result['list'] = $data; return $result; } /** * 根据分类id转成名称 * * @return mixed */ public function string($cate, $im = ',') { if ($cate) { $cate = explode(',', $cate); $result = array(); foreach ($cate as $k => $v) { $result[$k] = $this->getName($v); } $result = implode($im, $result); return $result; $table[] = $result; return Dever::table($table); } return ''; } /** * 获取顶级分类 * * @return mixed */ public function getTop() { $where = array(); $result = Dever::db('product/category')->getTop($where); return $result; } /** * 获取所有信息 * * @return mixed */ public function getAll($parent = false) { $where = array(); if ($parent) { $where['parent_id'] = $parent; } $result = Dever::db('product/category')->getList($where); return $result; } /** * 获取详细信息 * * @return mixed */ public function getInfo($cate) { if ($cate) { $cate = explode(',', $cate); $result = array(); foreach ($cate as $k => $v) { $result[$k] = $this->getName($v, true); } return $result; } return array(); } private function getName($id, $state = false) { $name = ''; if ($id > 0) { $data = Dever::db('product/category')->getOne($id); if ($state) { return $data; } if ($data) { $name = $data['name']; } } return $name; } # 根据上级分类获取下级分类 按照category_id进行索引 public function getChild($parent_id) { if (is_numeric($parent_id)) { $where['parent_id'] = $parent_id; } else { $where['parent_id_in'] = $parent_id; } $data = Dever::db('product/category')->getChild($where); return $data; } # 根据上级分类获取下级分类 public function getList($parent_id) { if (is_numeric($parent_id)) { $where['parent_id'] = $parent_id; } else { $where['parent_id_in'] = $parent_id; } $data = Dever::db('product/category')->getList($where); return $data; } # 根据顶级分类获取最低级的分类 public function getChildByTop($top_parent_id, $level = -1) { $where['top_parent_id'] = $top_parent_id; $where['level'] = $level; $data = Dever::db('product/category')->getList($where); return $data; } }