Category.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace Product\Lib;
  3. use Dever;
  4. class Category
  5. {
  6. private $default = array
  7. (
  8. 'value' => -1,
  9. 'name' => '不选择',
  10. );
  11. private $search_default = array
  12. (
  13. 'value' => -1,
  14. 'name' => '分类',
  15. );
  16. /**
  17. * 获取分类数据
  18. *
  19. * @return mixed
  20. */
  21. public function get_api()
  22. {
  23. # 关闭语言包
  24. Dever::config('base')->lang = false;
  25. # 联动总数
  26. $level_total = 100;
  27. # 当前联动级别
  28. $level_num = Dever::input('level_num');
  29. # 一般为id
  30. $level_id = Dever::input('level_id');
  31. # 传入的参数
  32. $level_param = Dever::input('level_param');
  33. if ($level_param) {
  34. parse_str($level_param, $param);
  35. $where['id'] = $param['id'];
  36. }
  37. # 是否是搜索列表页
  38. $level_search = Dever::input('level_search');
  39. if ($level_search) {
  40. $default = $this->search_default;
  41. $default['name'] = $level_num . '级' . $default['name'];
  42. } else {
  43. $default = $this->default;
  44. }
  45. $data = array();
  46. # 三级联动
  47. if ($level_num == 1) {
  48. $where['parent_id'] = -1;
  49. } elseif($level_id > 0) {
  50. $where['parent_id'] = $level_id;
  51. }
  52. if (isset($where['parent_id'])) {
  53. $data = Dever::db('product/category')->getAll($where);
  54. }
  55. $type = Dever::input('type');
  56. if ($data) {
  57. if ($type && $type == 1) {
  58. if ($level_num == 1) {
  59. array_unshift($data, $default);
  60. }
  61. } else {
  62. array_unshift($data, $default);
  63. }
  64. }
  65. if (!$data) {
  66. Dever::alert('error');
  67. }
  68. $result['level_total'] = $level_total;
  69. $result['list'] = $data;
  70. return $result;
  71. }
  72. /**
  73. * 根据分类id转成名称
  74. *
  75. * @return mixed
  76. */
  77. public function string($cate, $im = ',')
  78. {
  79. if ($cate) {
  80. $cate = explode(',', $cate);
  81. $result = array();
  82. foreach ($cate as $k => $v) {
  83. $result[$k] = $this->getName($v);
  84. }
  85. $result = implode($im, $result);
  86. return $result;
  87. $table[] = $result;
  88. return Dever::table($table);
  89. }
  90. return '';
  91. }
  92. /**
  93. * 获取顶级分类
  94. *
  95. * @return mixed
  96. */
  97. public function getTop()
  98. {
  99. $where = array();
  100. $result = Dever::db('product/category')->getTop($where);
  101. return $result;
  102. }
  103. /**
  104. * 获取所有信息
  105. *
  106. * @return mixed
  107. */
  108. public function getAll($parent = false)
  109. {
  110. $where = array();
  111. if ($parent) {
  112. $where['parent_id'] = $parent;
  113. }
  114. $result = Dever::db('product/category')->getList($where);
  115. return $result;
  116. }
  117. /**
  118. * 获取详细信息
  119. *
  120. * @return mixed
  121. */
  122. public function getInfo($cate)
  123. {
  124. if ($cate) {
  125. $cate = explode(',', $cate);
  126. $result = array();
  127. foreach ($cate as $k => $v) {
  128. $result[$k] = $this->getName($v, true);
  129. }
  130. return $result;
  131. }
  132. return array();
  133. }
  134. private function getName($id, $state = false)
  135. {
  136. $name = '';
  137. if ($id > 0) {
  138. $data = Dever::db('product/category')->getOne($id);
  139. if ($state) {
  140. return $data;
  141. }
  142. if ($data) {
  143. $name = $data['name'];
  144. }
  145. }
  146. return $name;
  147. }
  148. # 根据上级分类获取下级分类 按照category_id进行索引
  149. public function getChild($parent_id)
  150. {
  151. if (is_numeric($parent_id)) {
  152. $where['parent_id'] = $parent_id;
  153. } else {
  154. $where['parent_id_in'] = $parent_id;
  155. }
  156. $data = Dever::db('product/category')->getChild($where);
  157. return $data;
  158. }
  159. # 根据上级分类获取下级分类
  160. public function getList($parent_id)
  161. {
  162. if (is_numeric($parent_id)) {
  163. $where['parent_id'] = $parent_id;
  164. } else {
  165. $where['parent_id_in'] = $parent_id;
  166. }
  167. $data = Dever::db('product/category')->getList($where);
  168. return $data;
  169. }
  170. # 根据顶级分类获取最低级的分类
  171. public function getChildByTop($top_parent_id, $level = -1)
  172. {
  173. $where['top_parent_id'] = $top_parent_id;
  174. $where['level'] = $level;
  175. $data = Dever::db('product/category')->getList($where);
  176. return $data;
  177. }
  178. }