Goodsclass.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 数据层模型
  14. */
  15. class Goodsclass extends BaseModel
  16. {
  17. /**
  18. * 缓存数据
  19. */
  20. protected $cachedData;
  21. /**
  22. * 缓存数据 原H('goods_class')形式
  23. */
  24. protected $gcForCacheModel;
  25. /**
  26. * 获取缓存数据
  27. * @access public
  28. * @author csdeshang
  29. * @return array
  30. * array(
  31. * 'data' => array(
  32. * // Id => 记录
  33. * ),
  34. * 'parent' => array(
  35. * // 子Id => 父Id
  36. * ),
  37. * 'children' => array(
  38. * // 父Id => 子Id数组
  39. * ),
  40. * 'children2' => array(
  41. * // 1级Id => 3级Id数组
  42. * ),
  43. * )
  44. */
  45. public function getCache()
  46. {
  47. if ($this->cachedData) {
  48. return $this->cachedData;
  49. }
  50. $data = rkcache('gc_class');
  51. if (!$data) {
  52. $data = array();
  53. foreach ((array)$this->getGoodsclassList(array()) as $v) {
  54. $id = $v['gc_id'];
  55. $pid = $v['gc_parent_id'];
  56. $data['data'][$id] = $v;
  57. $data['parent'][$id] = $pid;
  58. $data['children'][$pid][] = $id;
  59. }
  60. foreach ((array)@$data['children'][0] as $id) {
  61. if (!empty($data['children'][$id])) {
  62. foreach ((array)$data['children'][$id] as $cid) {
  63. if (!empty($data['children'][$cid])) {
  64. foreach ((array)$data['children'][$cid] as $ccid) {
  65. $data['children2'][$id][] = $ccid;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. wkcache('gc_class', $data);
  72. }
  73. return $this->cachedData = $data;
  74. }
  75. /**
  76. * 删除缓存数据
  77. * @access public
  78. * @author csdeshang
  79. * @return bool
  80. */
  81. public function dropCache()
  82. {
  83. $this->cachedData = null;
  84. $this->gcForCacheModel = null;
  85. dkcache('gc_class');
  86. dkcache('all_categories');
  87. }
  88. /**
  89. * 类别列表
  90. * @access public
  91. * @author csdeshang
  92. * @param array $condition 检索条件
  93. * @param string $field 字段
  94. * @return array 返回二位数组
  95. */
  96. public function getGoodsclassList($condition, $field = '*')
  97. {
  98. $result = Db::name('goodsclass')->field($field)->where($condition)->order('gc_parent_id asc,gc_sort asc,gc_id asc')->select()->toArray();
  99. return $result;
  100. }
  101. /**
  102. * 从缓存获取全部分类
  103. * @access public
  104. * @author csdeshang
  105. * @return type
  106. */
  107. public function getGoodsclassListAll()
  108. {
  109. $data = $this->getCache();
  110. return array_values((array)@$data['data']);
  111. }
  112. /**
  113. * 从缓存获取全部分类 分类id作为数组的键
  114. * @access public
  115. * @author csdeshang
  116. * @return array
  117. */
  118. public function getGoodsclassIndexedListAll()
  119. {
  120. $data = $this->getCache();
  121. if($data){
  122. return (array)$data['data'];
  123. }
  124. return '';
  125. }
  126. /**
  127. * 从缓存获取分类 通过分类id数组
  128. * @access public
  129. * @author csdeshang
  130. * @param array $ids 分类id数组
  131. * @return array
  132. */
  133. public function getGoodsclassListByIds($ids)
  134. {
  135. $data = $this->getCache();
  136. $ret = array();
  137. foreach ((array)$ids as $i) {
  138. if (isset($data['data'][$i]) && $data['data'][$i]) {
  139. $ret[] = $data['data'][$i];
  140. }
  141. }
  142. return $ret;
  143. }
  144. /**
  145. * 从缓存获取分类 通过上级分类id
  146. * @access public
  147. * @author csdeshang
  148. * @param type $pid 上级分类id 若传0则返回1级分类
  149. * @return type
  150. */
  151. public function getGoodsclassListByParentId($pid)
  152. {
  153. $data = $this->getCache();
  154. $ret = array();
  155. if (!empty($data['children'][$pid])) {
  156. foreach ((array)$data['children'][$pid] as $i) {
  157. if ($data['data'][$i]) {
  158. $ret[] = $data['data'][$i];
  159. }
  160. }
  161. }
  162. return $ret;
  163. }
  164. /**
  165. * 从缓存获取分类 通过分类id
  166. * @access public
  167. * @author csdeshang
  168. * @param type $id 分类id
  169. * @return type
  170. */
  171. public function getGoodsclassInfoById($id)
  172. {
  173. $data = $this->getCache();
  174. return @$data['data'][$id];
  175. }
  176. /**
  177. * 返回缓存数据 原H('goods_class')形式
  178. * @access public
  179. * @author csdeshang
  180. * @return type
  181. */
  182. public function getGoodsclassForCacheModel()
  183. {
  184. if ($this->gcForCacheModel)
  185. return $this->gcForCacheModel;
  186. $data = $this->getCache();
  187. $r = isset($data['data'])?$data['data']:'';
  188. $p = isset($data['parent'])?$data['parent']:'';
  189. $c = isset($data['children'])?$data['children']:'';
  190. $c2 = empty($data['children2']) ? '' : $data['children2'];
  191. if(!empty($r)) {
  192. $r = (array)$r;
  193. foreach ($r as $k => & $v) {
  194. if ((string)$p[$k] == '0') {
  195. $v['depth'] = 1;
  196. if (!empty($data['children'][$k])) {
  197. $v['child'] = implode(',', $c[$k]);
  198. }
  199. if (!empty($data['children2'][$k])) {
  200. $v['childchild'] = implode(',', $c2[$k]);
  201. }
  202. }
  203. else if (isset($p[$p[$k]]) && (string)$p[$p[$k]] == '0') {
  204. $v['depth'] = 2;
  205. if (isset($data['children'][$k])) {
  206. $v['child'] = implode(',', $c[$k]);
  207. }
  208. }
  209. else if (isset($p[$p[$k]]) && isset($p[$p[$p[$k]]]) && (string)$p[$p[$p[$k]]] == '0') {
  210. $v['depth'] = 3;
  211. }
  212. }
  213. }
  214. return $this->gcForCacheModel = $r;
  215. }
  216. /**
  217. * 更新信息
  218. * @access public
  219. * @author csdeshang
  220. * @param type $data 更新数据
  221. * @param type $condition 条件
  222. * @return bool
  223. */
  224. public function editGoodsclass($data = array(), $condition = array())
  225. {
  226. // 删除缓存
  227. $this->dropCache();
  228. return Db::name('goodsclass')->where($condition)->update($data);
  229. }
  230. /**
  231. * 取得店铺绑定的分类
  232. * @access public
  233. * @author csdeshang
  234. * @param number $store_id 店铺id
  235. * @param number $pid 父级分类id
  236. * @param number $deep 深度
  237. * @return array 二维数组
  238. */
  239. public function getGoodsclass($store_id, $pid = 0, $deep = 1)
  240. {
  241. // 读取商品分类
  242. $gc_list_o = $gc_list = $this->getGoodsclassListByParentId($pid);
  243. // 如果不是自营店铺或者自营店铺未绑定全部商品类目,读取绑定分类
  244. if (!check_platform_store_bindingall_goodsclass()) {
  245. $gc_list = array_under_reset($gc_list, 'gc_id');
  246. $storebindclass_model = model('storebindclass');
  247. $condition = array();
  248. $condition[] = array('store_id','=',$store_id);
  249. $condition[] = array('storebindclass_state','in',array(1, 2));
  250. $gcid_array = $storebindclass_model->getStorebindclassList($condition, '', "class_{$deep} asc", "distinct class_{$deep}");
  251. if (!empty($gcid_array)) {
  252. $tmp_gc_list = array();
  253. foreach ($gcid_array as $value) {
  254. if ($value["class_{$deep}"] == 0)
  255. return $gc_list_o;
  256. if (isset($gc_list[$value["class_{$deep}"]])) {
  257. $tmp_gc_list[] = $gc_list[$value["class_{$deep}"]];
  258. }
  259. }
  260. $gc_list = $tmp_gc_list;
  261. }
  262. else {
  263. return array();
  264. }
  265. }
  266. return $gc_list;
  267. }
  268. /**
  269. * 删除商品分类
  270. * @access public
  271. * @author csdeshang
  272. * @param array $condition 条件
  273. * @return boolean
  274. */
  275. public function delGoodsclass($condition)
  276. {
  277. // 删除缓存
  278. $this->dropCache();
  279. return Db::name('goodsclass')->where($condition)->delete();
  280. }
  281. /**
  282. * 删除商品分类
  283. * @access public
  284. * @author csdeshang
  285. * @param array $gcids 分类ID
  286. * @return boolean
  287. */
  288. public function delGoodsclassByGcIdString($gcids)
  289. {
  290. $gcids = explode(',', $gcids);
  291. if (empty($gcids)) {
  292. return false;
  293. }
  294. $goods_class = $this->getGoodsclassForCacheModel();
  295. $gcid_array = array();
  296. foreach ($gcids as $gc_id) {
  297. $child = (!empty($goods_class[$gc_id]['child'])) ? explode(',', $goods_class[$gc_id]['child']) : array();
  298. $childchild = (!empty($goods_class[$gc_id]['childchild'])) ? explode(',', $goods_class[$gc_id]['childchild']) : array();
  299. $gcid_array = array_merge($gcid_array, array($gc_id), $child, $childchild);
  300. }
  301. // 删除商品分类
  302. $this->delGoodsclass(array(array('gc_id','in', $gcid_array)));
  303. // 删除常用商品分类
  304. model('goodsclassstaple')->delGoodsclassstaple(array(array('gc_id_1|gc_id_2|gc_id_3','in', $gcid_array)));
  305. // 删除分类tag表
  306. model('goodsclasstag')->delGoodsclasstag(array(array('gc_id_1|gc_id_2|gc_id_3','in', $gcid_array)));
  307. // 删除店铺绑定分类
  308. model('storebindclass')->delStorebindclass(array(array('class_1|class_2|class_3','in', $gcid_array)));
  309. // 商品下架
  310. model('goods')->editProducesLockUp(array('goods_stateremark' => '商品分类被删除,需要重新选择分类'), array(array('gc_id','in', $gcid_array)));
  311. return true;
  312. }
  313. /**
  314. * 前台头部的商品分类
  315. * @access public
  316. * @author csdeshang
  317. * @param number $update_all 更新
  318. * @return array 数组
  319. */
  320. public function get_all_category($update_all = 0)
  321. {
  322. // 不存在时更新或者强制更新时执行
  323. if ($update_all == 1 || !($gc_list = rkcache('all_categories'))) {
  324. $class_list = $this->getTreeClassList(3);
  325. $gc_list = [];
  326. $type_ids = array(); //第2级分类关联类型
  327. if (is_array($class_list) && !empty($class_list)) {
  328. //遍历一级商品分类
  329. foreach ($class_list as $k => $v) {
  330. if ($v[ 'deep' ] == 1) {
  331. $nav_info = $this->_getGoodsclassnavById($v['gc_id']);//一级分类推荐品牌
  332. $gc_list[$v['gc_id']] = array_merge($v, $nav_info);
  333. unset($class_list[ $k ]);
  334. }
  335. }
  336. $class_list = array_values($class_list);
  337. //遍历二级商品分类
  338. foreach ($class_list as $k => $v) {
  339. foreach ($gc_list as $ck => $cv) {
  340. if ($v[ 'deep' ] == 2 && $cv[ 'gc_id' ] == $v[ 'gc_parent_id' ]) {
  341. $gc_list[$cv['gc_id']][ 'class2' ][$v['gc_id']] = $v;
  342. $type_ids[] = $cv['type_id'];
  343. unset($class_list[ $k ]);
  344. }
  345. }
  346. }
  347. $class_list = array_values($class_list);
  348. //遍历三级商品分类
  349. foreach ($class_list as $k => $v) {
  350. foreach ($gc_list as $ck => $cv) {
  351. if (!empty($cv[ 'class2' ])) {
  352. foreach ($cv[ 'class2' ] as $third_k => $third_v) {
  353. if ($v[ 'deep' ] == 3 && $third_v[ 'gc_id' ] == $v[ 'gc_parent_id' ]) {
  354. $gc_list[$cv['gc_id']][ 'class2' ][$third_v['gc_id']][ 'class3' ][] = $v;
  355. unset($class_list[ $k ]);
  356. }
  357. }
  358. }
  359. }
  360. }
  361. $type_brands = $this->get_type_brands($type_ids); //类型关联品牌
  362. foreach ($gc_list as $key => $value) {
  363. $class2s = isset($value['class2']) ? $value['class2'] : '';
  364. if (is_array($class2s) && !empty($class2s)) {//第2级关联品牌
  365. foreach ($class2s as $k2 => $v2) {
  366. $p_id = $v2['gc_parent_id'];
  367. $gc_id = $v2['gc_id'];
  368. $type_id = $v2['type_id'];
  369. $gc_list[$p_id]['class2'][$gc_id]['brands'] = isset($type_brands[$type_id]) ? $type_brands[$type_id] : '';
  370. }
  371. }
  372. }
  373. }
  374. wkcache('all_categories', $gc_list);
  375. }
  376. return $gc_list;
  377. }
  378. /**
  379. * 类型关联品牌
  380. * @access public
  381. * @author csdeshang
  382. * @param array $type_ids 类型
  383. * @return array 数组
  384. */
  385. public function get_type_brands($type_ids = array())
  386. {
  387. $brands = array(); //品牌
  388. $type_brands = array(); //类型关联品牌
  389. if (is_array($type_ids) && !empty($type_ids)) {
  390. $type_ids = array_unique($type_ids);
  391. $type_list = Db::name('typebrand')->where('type_id','in', $type_ids)->limit(10000)->select()->toArray();
  392. if (is_array($type_list) && !empty($type_list)) {
  393. $brand_mod=model('brand');
  394. $brand_list = $brand_mod->getBrandList(array('brand_apply' => 1),'brand_id,brand_name,brand_pic',10000);
  395. if (is_array($brand_list) && !empty($brand_list)) {
  396. foreach ($brand_list as $key => $value) {
  397. $brand_id = $value['brand_id'];
  398. $brands[$brand_id] = $value;
  399. }
  400. foreach ($type_list as $key => $value) {
  401. $type_id = $value['type_id'];
  402. $brand_id = $value['brand_id'];
  403. if(isset($brands[$brand_id])){
  404. $brand = $brands[$brand_id];
  405. if (is_array($brand) && !empty($brand)) {
  406. $type_brands[$type_id][$brand_id] = $brand;
  407. }
  408. }
  409. }
  410. }
  411. }
  412. }
  413. return $type_brands;
  414. }
  415. /**
  416. * 获取商品分类导航
  417. * @access public
  418. * @author csdeshang
  419. * @param int $gc_id 商品分类id
  420. * @return type
  421. */
  422. private function _getGoodsclassnavById($gc_id)
  423. {
  424. $classnav_model = model('goodsclassnav');
  425. $brand_model = model('brand');
  426. $nav_info = $classnav_model->getGoodsclassnavInfoByGcId($gc_id);
  427. if (empty($nav_info)) {
  428. return array();
  429. }
  430. if (!empty($nav_info['goodscn_pic'])) {
  431. $nav_info['goodscn_pic'] = ds_get_pic( ATTACH_GOODS_CLASS , $nav_info['goodscn_pic']);
  432. }
  433. else {
  434. unset($nav_info['goodscn_pic']);
  435. }
  436. if (!empty($nav_info['goodscn_adv1'])) {
  437. $nav_info['goodscn_adv1'] = ds_get_pic( ATTACH_GOODS_CLASS , $nav_info['goodscn_adv1']);
  438. }
  439. else {
  440. unset($nav_info['goodscn_adv1']);
  441. }
  442. if (!empty($nav_info['goodscn_adv2'])) {
  443. $nav_info['goodscn_adv2'] = ds_get_pic( ATTACH_GOODS_CLASS , $nav_info['goodscn_adv2']);
  444. }
  445. else {
  446. unset($nav_info['goodscn_adv2']);
  447. }
  448. if ($nav_info['goodscn_brandids'] != '') {
  449. $nav_info['cn_brands'] = $brand_model->getBrandList(array(array('brand_id','in', $nav_info['goodscn_brandids'])));
  450. unset($nav_info['goodscn_brandids']);
  451. }
  452. if ($nav_info['goodscn_classids'] != '') {
  453. $nav_info['cn_classs'] = $this->getGoodsclassList(array(array('gc_id','in', $nav_info['goodscn_classids'])));
  454. unset($nav_info['goodscn_classids']);
  455. }
  456. if ($nav_info['goodscn_alias'] != '') {
  457. $nav_info['gc_name'] = $nav_info['goodscn_alias'];
  458. unset($nav_info['goodscn_alias']);
  459. }
  460. return $nav_info;
  461. }
  462. /**
  463. * 新增商品分类
  464. * @access public
  465. * @author csdeshang
  466. * @param array $data 参数数据
  467. * @return boolean
  468. */
  469. public function addGoodsclass($data)
  470. {
  471. // 删除缓存
  472. $this->dropCache();
  473. return Db::name('goodsclass')->insertGetId($data);
  474. }
  475. /**
  476. * 取分类列表,最多为三级
  477. * @access public
  478. * @author csdeshang
  479. * @param int $show_deep 显示深度
  480. * @param array $condition 检索条件
  481. * @return array 数组类型的返回结果
  482. */
  483. public function getTreeClassList($show_deep = '3', $condition = array())
  484. {
  485. $class_list = $this->getGoodsclassList($condition);
  486. $goods_class = array(); //分类数组
  487. if (is_array($class_list) && !empty($class_list)) {
  488. $show_deep = intval($show_deep);
  489. if ($show_deep == 1) {//只显示第一级时用循环给分类加上深度deep号码
  490. foreach ($class_list as $val) {
  491. if ($val['gc_parent_id'] == 0) {
  492. $val['deep'] = 1;
  493. $goods_class[] = $val;
  494. }
  495. else {
  496. break; //父类编号不为0时退出循环
  497. }
  498. }
  499. }
  500. else {//显示第二和三级时用递归
  501. $goods_class = $this->_getTreeClassList($show_deep, $class_list);
  502. }
  503. }
  504. return $goods_class;
  505. }
  506. /**
  507. * 递归 整理分类
  508. * @access public
  509. * @author csdeshang
  510. * @param int $show_deep 显示深度
  511. * @param array $class_list 类别内容集合
  512. * @param int $deep 深度
  513. * @param int $parent_id 父类编号
  514. * @param int $i 上次循环编号
  515. * @return array $show_class 返回数组形式的查询结果
  516. */
  517. private function _getTreeClassList($show_deep, $class_list, $deep = 1, $parent_id = 0, $i = 0)
  518. {
  519. static $show_class = array(); //树状的平行数组
  520. if (is_array($class_list) && !empty($class_list)) {
  521. $size = count($class_list);
  522. if ($i == 0)
  523. $show_class = array(); //从0开始时清空数组,防止多次调用后出现重复
  524. for ($i; $i < $size; $i++) {//$i为上次循环到的分类编号,避免重新从第一条开始
  525. $val = $class_list[$i];
  526. $gc_id = $val['gc_id'];
  527. $gc_parent_id = $val['gc_parent_id'];
  528. if ($gc_parent_id == $parent_id) {
  529. $val['deep'] = $deep;
  530. $show_class[] = $val;
  531. if ($deep < $show_deep && $deep < 3) {//本次深度小于显示深度时执行,避免取出的数据无用
  532. $this->_getTreeClassList($show_deep, $class_list, $deep + 1, $gc_id, 1);
  533. }
  534. }
  535. if ($gc_parent_id > $parent_id)
  536. break; //当前分类的父编号大于本次递归的时退出循环
  537. }
  538. }
  539. return $show_class;
  540. }
  541. /**
  542. * 取指定分类ID下的所有子类
  543. * @access public
  544. * @author csdeshang
  545. * @staticvar type $_cache
  546. * @param int $parent_id 父ID 可以单一可以为数组
  547. * @return array 返回数组形式的查询结果
  548. */
  549. public function getChildClass($parent_id)
  550. {
  551. static $_cache;
  552. if ($_cache !== null)
  553. return $_cache;
  554. $all_class = $this->getGoodsclassListAll();
  555. if (is_array($all_class)) {
  556. if (!is_array($parent_id)) {
  557. $parent_id = array($parent_id);
  558. }
  559. $result = array();
  560. foreach ($all_class as $k => $v) {
  561. $gc_id = $v['gc_id']; //返回的结果包括父类
  562. $gc_parent_id = $v['gc_parent_id'];
  563. if (in_array($gc_id, $parent_id) || in_array($gc_parent_id, $parent_id)) {
  564. $parent_id[] = $v['gc_id'];
  565. $result[] = $v;
  566. }
  567. }
  568. $return = $result;
  569. }
  570. else {
  571. $return = false;
  572. }
  573. return $_cache = $return;
  574. }
  575. /**
  576. * 取指定分类ID的导航链接
  577. * @access public
  578. * @author csdeshang
  579. * @param int $id 父类ID/子类ID
  580. * @param int $sign 1、0 1为最后一级不加超链接,0为加超链接
  581. * @return array $nav_link 返回数组形式类别导航连接
  582. */
  583. public function getGoodsclassnav($id = 0, $sign = 1)
  584. {
  585. if (intval($id) > 0) {
  586. $data = $this->getGoodsclassIndexedListAll();
  587. // 当前分类不加超链接
  588. if ($sign == 1) {
  589. if (isset($data[$id])) {
  590. $nav_link [] = array(
  591. 'title' => $data[$id]['gc_name']
  592. );
  593. }
  594. }
  595. else {
  596. if (isset($data[$id])) {
  597. $nav_link [] = array(
  598. 'title' => isset($data[$id]['gc_name']) ? $data[$id]['gc_name'] : '..',
  599. 'link' => (string)url('/home/Search/index', ['cate_id' => $data[$id]['gc_id']]),
  600. );
  601. }
  602. }
  603. if (isset($data[$id])) {
  604. // 最多循环4层
  605. for ($i = 1; $i < 5; $i++) {
  606. if ($data[$id]['gc_parent_id'] == '0') {
  607. break;
  608. }
  609. $id = $data[$id]['gc_parent_id'];
  610. if(!isset($data[$id])){
  611. break;
  612. }
  613. $nav_link[] = array(
  614. 'title' => $data[$id]['gc_name'],
  615. 'link' => (string)url('/home/Search/index', ['cate_id' => $data[$id]['gc_id']])
  616. );
  617. }
  618. }
  619. }
  620. else {
  621. // 加上 首页 商品分类导航
  622. $nav_link[] = array('title' => lang('goods_class_index_search_results'));
  623. }
  624. // 首页导航
  625. $nav_link[] = array('title' => lang('homepage'), 'link' => (string)url('home/Index/index'));
  626. krsort($nav_link);
  627. return $nav_link;
  628. }
  629. /**
  630. * 根据一级分类id取得所有三级分类
  631. * @access public
  632. * @author csdeshang
  633. * @param type $id 分类ID
  634. * @return type
  635. */
  636. public function getChildClassByFirstId($id)
  637. {
  638. $data = $this->getCache();
  639. $result = array();
  640. if (!empty($data['children2'][$id])) {
  641. foreach ($data['children2'][$id] as $val) {
  642. $child = $data['data'][$val];
  643. $result[$child['gc_parent_id']]['class'][$child['gc_id']] = $child['gc_name'];
  644. $result[$child['gc_parent_id']]['name'] = $data['data'][$child['gc_parent_id']]['gc_name'];
  645. }
  646. }
  647. return $result;
  648. }
  649. /**
  650. * 取指定分类ID的所有父级分类
  651. * @access public
  652. * @author csdeshang
  653. * @param int $id 父类ID/子类ID
  654. * @return array
  655. */
  656. public function getGoodsclassLineForTag($id = 0)
  657. {
  658. if (intval($id) > 0) {
  659. $gc_line = array();
  660. /**
  661. * 取当前类别信息
  662. */
  663. $class = $this->getGoodsclassInfoById(intval($id));
  664. $gc_line['gc_id'] = $class['gc_id'];
  665. $gc_line['type_id'] = $class['type_id'];
  666. $gc_line['gc_virtual'] = $class['gc_virtual'];
  667. $gc_line['gctag_name']='>';
  668. $gc_line['gctag_value']=',';
  669. /**
  670. * 是否是子类
  671. */
  672. if ($class['gc_parent_id'] != 0) {
  673. $parent_1 = $this->getGoodsclassInfoById($class['gc_parent_id']);
  674. if ($parent_1['gc_parent_id'] != 0) {
  675. $parent_2 = $this->getGoodsclassInfoById($parent_1['gc_parent_id']);
  676. $gc_line['gc_id_1'] = $parent_2['gc_id'];
  677. $gc_line['gctag_name'] = trim($parent_2['gc_name']) . ' >';
  678. $gc_line['gctag_value'] = trim($parent_2['gc_name']) . ',';
  679. }
  680. if (!isset($gc_line['gc_id_1'])) {
  681. $gc_line['gc_id_1'] = $parent_1['gc_id'];
  682. }
  683. else {
  684. $gc_line['gc_id_2'] = $parent_1['gc_id'];
  685. }
  686. $gc_line['gctag_name'] .= trim($parent_1['gc_name']) . ' >';
  687. $gc_line['gctag_value'] .= trim($parent_1['gc_name']) . ',';
  688. }
  689. if (!isset($gc_line['gc_id_1'])) {
  690. $gc_line['gc_id_1'] = $class['gc_id'];
  691. }
  692. else if (!isset($gc_line['gc_id_2'])) {
  693. $gc_line['gc_id_2'] = $class['gc_id'];
  694. }
  695. else {
  696. $gc_line['gc_id_3'] = $class['gc_id'];
  697. }
  698. $gc_line['gctag_name'] .= trim($class['gc_name']) . ' >';
  699. $gc_line['gctag_value'] .= trim($class['gc_name']) . ',';
  700. }
  701. $gc_line['gctag_name'] = trim($gc_line['gctag_name'], ' >');
  702. $gc_line['gctag_value'] = trim($gc_line['gctag_value'], ',');
  703. return $gc_line;
  704. }
  705. /**
  706. * 取得分类关键词,方便SEO
  707. * @access public
  708. * @author csdeshang
  709. * @param type $gc_id 商品分类ID
  710. * @return boolean
  711. */
  712. public function getKeyWords($gc_id = null)
  713. {
  714. if (empty($gc_id))
  715. return false;
  716. $keywrods = rkcache('goodsclassseo', true);
  717. if (empty($keywrods)) {
  718. return array(1 => '', 2 => trim('', ','), 3 => trim('', ','));
  719. }
  720. $seo_title = $keywrods[$gc_id]['title'];
  721. $seo_key = '';
  722. $seo_desc = '';
  723. if ($gc_id > 0) {
  724. if (isset($keywrods[$gc_id])) {
  725. $seo_key .= $keywrods[$gc_id]['key'] . ',';
  726. $seo_desc .= $keywrods[$gc_id]['desc'] . ',';
  727. }
  728. $goods_class = model('goodsclass')->getGoodsclassIndexedListAll();
  729. if (($gc_id = $goods_class[$gc_id]['gc_parent_id']) > 0) {
  730. if (isset($keywrods[$gc_id])) {
  731. $seo_key .= $keywrods[$gc_id]['key'] . ',';
  732. $seo_desc .= $keywrods[$gc_id]['desc'] . ',';
  733. }
  734. }
  735. if (($gc_id = $goods_class[$gc_id]['gc_parent_id']) > 0) {
  736. if (isset($keywrods[$gc_id])) {
  737. $seo_key .= $keywrods[$gc_id]['key'] . ',';
  738. $seo_desc .= $keywrods[$gc_id]['desc'] . ',';
  739. }
  740. }
  741. }
  742. return array(1 => $seo_title, 2 => trim($seo_key, ','), 3 => trim($seo_desc, ','));
  743. }
  744. /**
  745. * 获得商品分类缓存
  746. * @access public
  747. * @author csdeshang
  748. * @param int $choose_gcid 选择分类ID
  749. * @param int $show_depth 需要展示分类深度
  750. * @return array 返回分类数组和选择分类id数组
  751. */
  752. public function getGoodsclassCache($choose_gcid, $show_depth = 3)
  753. {
  754. $gc_list = $this->getGoodsclassForCacheModel();
  755. //获取需要展示的分类数组
  756. $show_gc_list = array();
  757. if(!empty($gc_list)) {
  758. foreach ((array)$gc_list as $k => $v) {
  759. if(isset($v['depth'])){
  760. if ($v['depth'] < $show_depth) {
  761. $show_gc_list[$v['gc_id']] = $v;
  762. }
  763. elseif ($v['depth'] == $show_depth) {
  764. unset($v['child'], $v['childchild']);
  765. $show_gc_list[$v['gc_id']] = $v;
  766. }
  767. }
  768. }
  769. }
  770. $choose_gcidarr = array();
  771. if ($choose_gcid > 0) {
  772. //遍历出选择商品分类的上下级ID
  773. if(isset($gc_list[$choose_gcid])){
  774. $gc_depth = $gc_list[$choose_gcid]['depth'];
  775. $parentid = $choose_gcid;
  776. for ($i = $gc_depth - 1; $i >= 0; $i--) {
  777. $choose_gcidarr[$i] = $parentid;
  778. $parentid = $gc_list[$parentid]['gc_parent_id'];
  779. }
  780. }
  781. }
  782. return array('showclass' => $show_gc_list, 'choose_gcid' => $choose_gcidarr);
  783. }
  784. }
  785. ?>