Goodsclass.php 29 KB

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