Category.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Service;
  4. use app\common\model\Attribute as AttributeModel;
  5. use think\Cache;
  6. use think\Db;
  7. use think\Model;
  8. /**
  9. * 分类模型
  10. */
  11. class Category extends Model
  12. {
  13. protected $name = 'shop_category';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. // 追加属性
  20. protected $append = [
  21. 'url',
  22. 'flag_text',
  23. ];
  24. protected static $config = [];
  25. protected static $tagCount = 0;
  26. protected static $parentIds = null;
  27. protected static $outlinkParentIds = null;
  28. protected static $navParentIds = null;
  29. protected static function init()
  30. {
  31. $config = get_addon_config('shop');
  32. self::$config = $config;
  33. self::afterInsert(function ($row) {
  34. $row->save(['weigh' => $row['id']]);
  35. });
  36. }
  37. public function setFlagAttr($value, $data)
  38. {
  39. return is_array($value) ? implode(',', $value) : $value;
  40. }
  41. public function getUrlAttr($value, $data)
  42. {
  43. return $this->buildUrl($value, $data);
  44. }
  45. public function getFullurlAttr($value, $data)
  46. {
  47. return $this->buildUrl($value, $data, true);
  48. }
  49. private function buildUrl($value, $data, $domain = false)
  50. {
  51. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  52. $cateid = $data['id'] ?? 0;
  53. $catename = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : 'all';
  54. $time = $data['createtime'] ?? time();
  55. $vars = [
  56. ':id' => $data['id'],
  57. ':diyname' => $diyname,
  58. ':category' => $cateid,
  59. ':catename' => $catename,
  60. ':cateid' => $cateid,
  61. ':year' => date("Y", $time),
  62. ':month' => date("m", $time),
  63. ':day' => date("d", $time)
  64. ];
  65. if (isset($data['type']) && isset($data['outlink']) && $data['type'] == 'link') {
  66. return $this->getAttr('outlink');
  67. }
  68. $suffix = static::$config['moduleurlsuffix']['category'] ?? static::$config['urlsuffix'];
  69. return addon_url('shop/category/index', $vars, $suffix, $domain);
  70. }
  71. public function getImageAttr($value, $data)
  72. {
  73. $value = $value ? $value : self::$config['default_category_img'];
  74. return cdnurl($value, true);
  75. }
  76. public function getFlagList()
  77. {
  78. return ['hot' => __('Hot'), 'index' => __('Index'), 'recommend' => __('Recommend')];
  79. }
  80. public function getOutlinkAttr($value, $data)
  81. {
  82. $indexUrl = $view_replace_str = config('view_replace_str.__PUBLIC__');
  83. $indexUrl = rtrim($indexUrl, '/');
  84. return str_replace('__INDEX__', $indexUrl, $value ?: '');
  85. }
  86. public function getFlagTextAttr($value, $data)
  87. {
  88. $value = $value ?: ($data['flag'] ?? '');
  89. $valueArr = explode(',', $value);
  90. $list = $this->getFlagList();
  91. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  92. }
  93. /**
  94. * 判断是否拥有子列表
  95. * @param $value
  96. * @param $data
  97. * @return bool|mixed
  98. */
  99. public function getHasChildAttr($value, $data)
  100. {
  101. static $checked = [];
  102. if (isset($checked[$data['id']])) {
  103. return $checked[$data['id']];
  104. }
  105. if (is_null(self::$parentIds)) {
  106. self::$parentIds = self::where('pid', '>', 0)->cache(true, null, 'shop')->where('status', 'normal')->column('pid');
  107. }
  108. if (self::$parentIds && in_array($data['id'], self::$parentIds)) {
  109. return true;
  110. }
  111. return false;
  112. }
  113. /**
  114. * 判断导航是否拥有子列表
  115. * @param $value
  116. * @param $data
  117. * @return bool|mixed
  118. */
  119. public function getHasNavChildAttr($value, $data)
  120. {
  121. static $checked = [];
  122. if (isset($checked[$data['id']])) {
  123. return $checked[$data['id']];
  124. }
  125. if (is_null(self::$navParentIds)) {
  126. self::$navParentIds = self::where('pid', '>', 0)->cache(true, null, 'shop')->where('status', 'normal')->where('isnav', 1)->column('pid');
  127. }
  128. if (self::$navParentIds && in_array($data['id'], self::$navParentIds)) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. public static function getIndexCategoryList()
  134. {
  135. $categoryList = self::where('status', 'normal')
  136. ->where('pid', 0)
  137. ->where("FIND_IN_SET('index',`flag`)")
  138. ->limit(9)
  139. ->order('weigh desc,id asc')
  140. ->cache(false)
  141. ->select();
  142. $categoryList = collection($categoryList)->toArray();
  143. return $categoryList;
  144. }
  145. /**
  146. * 获取面包屑导航
  147. * @param array $category
  148. * @param array $goods
  149. * @param array $page
  150. * @return array
  151. */
  152. public static function getBreadcrumb($category, $goods = [], $page = [])
  153. {
  154. $list = [];
  155. $list[] = ['name' => __('Home'), 'url' => addon_url('shop/index/index', [], false)];
  156. if ($category) {
  157. if ($category['pid']) {
  158. $categoryList = self::where('status', 'normal')
  159. ->order('weigh desc,id desc')
  160. ->field('id,name,pid,diyname')
  161. ->cache(true, null, 'shop')
  162. ->select();
  163. //获取栏目的所有上级栏目
  164. $parents = \fast\Tree::instance()->init(collection($categoryList)->toArray(), 'pid')->getParents($category['id']);
  165. foreach ($parents as $k => $v) {
  166. $list[] = ['name' => $v['name'], 'url' => $v['url']];
  167. }
  168. }
  169. $list[] = ['name' => $category['name'], 'url' => $category['url']];
  170. }
  171. if ($goods) {
  172. //$list[] = ['name' => $goods['title'], 'url' => $goods['url']];
  173. }
  174. if ($page && $category['url'] != $page['url']) {
  175. $list[] = ['name' => $page['title'], 'url' => $page['url']];
  176. }
  177. return $list;
  178. }
  179. /**
  180. * 获取导航分类列表HTML
  181. * @param $category
  182. * @param array $tag
  183. * @return mixed|string
  184. */
  185. public static function getNav($category, $tag = [])
  186. {
  187. $config = get_addon_config('shop');
  188. $condition = empty($tag['condition']) ? '' : $tag['condition'];
  189. $maxLevel = !isset($tag['maxlevel']) ? 0 : $tag['maxlevel'];
  190. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('nav', $tag);
  191. $cacheName = 'shop-nav-' . md5(serialize($tag));
  192. $result = Cache::tag('shop')->get($cacheName);
  193. if ($result === false) {
  194. $categoryList = Category::where($condition)
  195. ->where('status', 'normal')
  196. ->order('weigh desc,id desc')
  197. ->cache($cacheKey, $cacheExpire, 'shop')
  198. ->select();
  199. $tree = \fast\Tree::instance();
  200. $tree->init(collection($categoryList)->toArray(), 'pid');
  201. $result = self::getTreeUl($tree, 0, $category ? $category['id'] : '', '', 1, $maxLevel);
  202. Cache::tag('shop')->set($cacheName, $result);
  203. }
  204. return $result;
  205. }
  206. /**
  207. * 获取栏目所有子级的ID
  208. * @param mixed $ids 栏目ID或集合ID
  209. * @param bool $withself 是否包含自身
  210. * @return array
  211. */
  212. public static function getCategoryChildrenIds($ids, $withself = true)
  213. {
  214. $cacheName = 'shop-childrens-' . $ids . '-' . $withself;
  215. $result = Cache::get($cacheName);
  216. if ($result === false) {
  217. $categoryList = Category::where('status', 'normal')
  218. ->order('weigh desc,id desc')
  219. ->cache(true, null, 'shop')
  220. ->select();
  221. $result = [];
  222. $tree = \fast\Tree::instance();
  223. $tree->init(collection($categoryList)->toArray(), 'pid');
  224. $CategoryIds = is_array($ids) ? $ids : explode(',', $ids);
  225. foreach ($CategoryIds as $index => $CategoryId) {
  226. $result = array_merge($result, $tree->getChildrenIds($CategoryId, $withself));
  227. }
  228. Cache::set($cacheName, $result);
  229. }
  230. return $result;
  231. }
  232. /**
  233. * 获取分类列表
  234. * @param $tag
  235. * @return false|\PDOStatement|string|\think\Collection
  236. */
  237. public static function getCategoryList($tag)
  238. {
  239. $config = get_addon_config('shop');
  240. $type = empty($tag['type']) ? '' : $tag['type'];
  241. $typeid = !isset($tag['typeid']) ? '' : $tag['typeid'];
  242. $condition = empty($tag['condition']) ? '' : $tag['condition'];
  243. $field = empty($tag['field']) ? '*' : $tag['field'];
  244. $row = empty($tag['row']) ? 10 : (int)$tag['row'];
  245. $flag = empty($tag['flag']) ? '' : $tag['flag'];
  246. $orderby = empty($tag['orderby']) ? 'weigh' : $tag['orderby'];
  247. $orderway = empty($tag['orderway']) ? 'desc' : strtolower($tag['orderway']);
  248. $limit = empty($tag['limit']) ? $row : $tag['limit'];
  249. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  250. $paginate = !isset($tag['paginate']) ? false : $tag['paginate'];
  251. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('categorylist', $tag);
  252. $where = ['status' => 'normal'];
  253. self::$tagCount++;
  254. if ($type === 'top') {
  255. //顶级分类
  256. $where['pid'] = 0;
  257. } elseif ($type === 'brother') {
  258. $subQuery = self::where('id', 'in', $typeid)->field('pid')->buildSql();
  259. //同级
  260. $where['pid'] = ['exp', Db::raw(' IN ' . '(' . $subQuery . ')')];
  261. } elseif ($type === 'son') {
  262. $subQuery = self::where('pid', 'in', $typeid)->field('id')->buildSql();
  263. //子级
  264. $where['id'] = ['exp', Db::raw(' IN ' . '(' . $subQuery . ')')];
  265. } elseif ($type === 'sons') {
  266. //所有子级
  267. $where['id'] = ['in', self::getCategoryChildrenIds($typeid)];
  268. } else {
  269. if ($typeid !== '') {
  270. $where['id'] = ['in', $typeid];
  271. }
  272. }
  273. //如果有设置标志,则拆分标志信息并构造condition条件
  274. if ($flag !== '') {
  275. if (stripos($flag, '&') !== false) {
  276. $arr = [];
  277. foreach (explode('&', $flag) as $k => $v) {
  278. $arr[] = "FIND_IN_SET('{$v}', flag)";
  279. }
  280. if ($arr) {
  281. $condition .= "(" . implode(' AND ', $arr) . ")";
  282. }
  283. } else {
  284. $condition .= ($condition ? ' AND ' : '');
  285. $arr = [];
  286. foreach (explode(',', str_replace('|', ',', $flag)) as $k => $v) {
  287. $arr[] = "FIND_IN_SET('{$v}', flag)";
  288. }
  289. if ($arr) {
  290. $condition .= "(" . implode(' OR ', $arr) . ")";
  291. }
  292. }
  293. }
  294. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  295. $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
  296. $CategoryModel = self::where($where)
  297. ->where($condition)
  298. ->field($field)
  299. ->orderRaw($order);
  300. if ($paginate) {
  301. $paginateArr = explode(',', $paginate);
  302. $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
  303. $config = [];
  304. $config['var_page'] = $paginateArr[2] ?? 'cpage' . self::$tagCount;
  305. $config['path'] = $paginateArr[3] ?? '';
  306. $config['fragment'] = $paginateArr[4] ?? '';
  307. $config['query'] = request()->get();
  308. $list = $CategoryModel->paginate($listRows, ($paginateArr[1] ?? false), $config);
  309. } else {
  310. $list = $CategoryModel->limit($limit)->cache($cacheKey, $cacheExpire, 'shop')->select();
  311. }
  312. return $list;
  313. }
  314. public static function getFilterList($category, $filter, $params = [], $multiple = false)
  315. {
  316. $filterList = [];
  317. $attributeList = (new AttributeModel())->with(['AttributeValue'])->where('category_id', $category['id'])->where('is_search', 1)->select();
  318. foreach ($attributeList as $k => $v) {
  319. $v['title'] = $v['name'];
  320. $v['name'] = 'f_' . $v['id'];
  321. $content = [];
  322. $valueList = ['' => __('All')];
  323. foreach ($v['attribute_value'] as $index => $item) {
  324. $valueList[$item['id']] = $item['name'];
  325. }
  326. foreach ($valueList as $m => $n) {
  327. $filterArr = isset($filter[$v['name']]) && $filter[$v['name']] !== '' ? ($multiple ? explode(',', $filter[$v['name']]) : [$filter[$v['name']]]) : [];
  328. $active = ($m === '' && !$filterArr) || ($m !== '' && in_array($m, $filterArr)) ? true : false;
  329. if ($active) {
  330. $current = implode(',', array_diff($filterArr, [$m]));
  331. } else {
  332. $current = $multiple ? implode(',', array_merge($filterArr, [$m])) : $m;
  333. }
  334. $prepare = $m === '' ? array_diff_key($filter, [$v['name'] => $m]) : array_merge($filter, [$v['name'] => $current]);
  335. $url = '?' . str_replace(['%2C', '%3B'], [',', ';'], http_build_query(array_merge($prepare, array_intersect_key($params, array_flip(['orderby', 'orderway', 'multiple'])))));
  336. $content[] = ['value' => $m, 'title' => $n, 'active' => $active, 'url' => $url];
  337. }
  338. $filterList[] = [
  339. 'name' => $v['name'],
  340. 'title' => $v['title'],
  341. 'values' => $content,
  342. ];
  343. }
  344. foreach ($filter as $index => &$item) {
  345. $item = is_array($item) ? $item : explode(',', str_replace(';', ',', $item));
  346. }
  347. return $filterList;
  348. }
  349. public static function getCategorySubList($id)
  350. {
  351. return self::where('pid', $id)->where('status', 'normal')->order('weigh desc,id asc')->select();
  352. }
  353. public static function getCategorySubIds($id)
  354. {
  355. return self::where('pid', $id)->where('status', 'normal')->column('id');
  356. }
  357. public function Goods()
  358. {
  359. return $this->hasMany('Goods', 'category_id', 'id');
  360. }
  361. }