StoreCategory.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\store;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use service\UtilService;
  15. /**
  16. * Class StoreCategory
  17. * @package app\admin\model\store
  18. */
  19. class StoreCategory extends ModelBasic
  20. {
  21. use ModelTrait;
  22. /*
  23. * 异步获取分类列表
  24. * @param $where
  25. * @return array
  26. */
  27. public static function CategoryList($where)
  28. {
  29. $data = ($data = self::systemPage($where, true)->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  30. foreach ($data as &$item) {
  31. if ($item['pid']) $item['pid_name'] = self::where('id', $item['pid'])->value('cate_name');
  32. else $item['pid_name'] = '顶级';
  33. }
  34. $count = self::systemPage($where, true)->count();
  35. return compact('count', 'data');
  36. }
  37. /**
  38. * @param $where
  39. * @return array
  40. */
  41. public static function systemPage($where, $isAjax = false)
  42. {
  43. $model = new self;
  44. if ($where['pid'] != '') $model = $model->where('pid', $where['pid']);
  45. else if ($where['pid'] == '' && $where['cate_name'] == '') $model = $model->where('pid', 0);
  46. if ($where['is_show'] != '') $model = $model->where('is_show', $where['is_show']);
  47. if ($where['cate_name'] != '') $model = $model->where('cate_name', 'LIKE', "%$where[cate_name]%");
  48. if (isset($where['mer_id']) && $where['mer_id']) $model->where('mer_id', $where['mer_id']);
  49. if ($isAjax === true) {
  50. if (isset($where['order']) && $where['order'] != '') {
  51. $model = $model->order(self::setOrder($where['order']));
  52. } else {
  53. $model = $model->order('sort DESC,id DESC');
  54. }
  55. return $model;
  56. }
  57. return self::page($model, function ($item) {
  58. if ($item['pid']) {
  59. $item['pid_name'] = self::where('id', $item['pid'])->value('cate_name');
  60. } else {
  61. $item['pid_name'] = '顶级';
  62. }
  63. }, $where);
  64. }
  65. /**
  66. * 获取顶级分类
  67. * @return array
  68. */
  69. public static function getCategory($field = 'id,cate_name')
  70. {
  71. return self::where('is_show', 1)->column($field);
  72. }
  73. /**
  74. * 分级排序列表
  75. * @param null $model
  76. * @return array
  77. */
  78. public static function getTierList($model = null)
  79. {
  80. if ($model === null) $model = new self();
  81. return UtilService::sortListTier($model->select()->toArray());
  82. }
  83. public static function delCategory($id)
  84. {
  85. $count = self::where('pid', $id)->count();
  86. if ($count)
  87. return false;
  88. else {
  89. return self::del($id);
  90. }
  91. }
  92. }