Category.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Scm_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('scm_product/category')->getAll($where);
  54. }
  55. /*
  56. $type = Dever::input('type');
  57. if ($data) {
  58. if ($type && $type == 1) {
  59. if ($level_num == 1) {
  60. array_unshift($data, $default);
  61. }
  62. } else {
  63. array_unshift($data, $default);
  64. }
  65. }*/
  66. if ($level_num == 1) {
  67. array_unshift($data, $default);
  68. }
  69. if (!$data) {
  70. Dever::alert('error');
  71. }
  72. $result['level_total'] = $level_total;
  73. $result['list'] = $data;
  74. return $result;
  75. }
  76. /**
  77. * 根据分类id转成名称
  78. *
  79. * @return mixed
  80. */
  81. public function string($cate, $im = ',')
  82. {
  83. if ($cate) {
  84. $cate = explode(',', $cate);
  85. $result = array();
  86. foreach ($cate as $k => $v) {
  87. $result[$k] = $this->getName($v);
  88. }
  89. $result = implode($im, $result);
  90. return $result;
  91. $table[] = $result;
  92. return Dever::table($table);
  93. }
  94. return '';
  95. }
  96. /**
  97. * 获取顶级分类
  98. *
  99. * @return mixed
  100. */
  101. public function getTop()
  102. {
  103. $where = array();
  104. $result = Dever::db('scm_product/category')->getTop($where);
  105. return $result;
  106. }
  107. /**
  108. * 获取所有信息
  109. *
  110. * @return mixed
  111. */
  112. public function getAll($parent = false)
  113. {
  114. $where = array();
  115. if ($parent) {
  116. $where['parent_id'] = $parent;
  117. }
  118. $result = Dever::db('scm_product/category')->getList($where);
  119. return $result;
  120. }
  121. /**
  122. * 获取详细信息
  123. *
  124. * @return mixed
  125. */
  126. public function getInfo($cate)
  127. {
  128. if ($cate) {
  129. $cate = explode(',', $cate);
  130. $result = array();
  131. foreach ($cate as $k => $v) {
  132. $result[$k] = $this->getName($v, true);
  133. }
  134. return $result;
  135. }
  136. return array();
  137. }
  138. private function getName($id, $state = false)
  139. {
  140. $name = '';
  141. if ($id > 0) {
  142. $data = Dever::db('scm_product/category')->getOne($id);
  143. if ($state) {
  144. return $data;
  145. }
  146. if ($data) {
  147. $name = $data['name'];
  148. }
  149. }
  150. return $name;
  151. }
  152. # 根据上级分类获取下级分类 按照category_id进行索引
  153. public function getChild($parent_id)
  154. {
  155. if (is_numeric($parent_id)) {
  156. $where['parent_id'] = $parent_id;
  157. } else {
  158. $where['parent_id_in'] = $parent_id;
  159. }
  160. $data = Dever::db('scm_product/category')->getChild($where);
  161. return $data;
  162. }
  163. # 根据上级分类获取下级分类
  164. public function getList($parent_id)
  165. {
  166. if (is_numeric($parent_id)) {
  167. $where['parent_id'] = $parent_id;
  168. } else {
  169. $where['parent_id_in'] = $parent_id;
  170. }
  171. $data = Dever::db('scm_product/category')->getList($where);
  172. return $data;
  173. }
  174. # 根据顶级分类获取最低级的分类
  175. public function getChildByTop($top_parent_id, $level = -1)
  176. {
  177. $where['top_parent_id'] = $top_parent_id;
  178. $where['level'] = $level;
  179. $data = Dever::db('scm_product/category')->getList($where);
  180. return $data;
  181. }
  182. }