123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace Product\Lib;
- use Dever;
- class Category
- {
- private $default = array
- (
- 'value' => -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;
- }
- }
|