Column.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\cms\model;
  10. use think\Model as ThinkModel;
  11. use util\Tree;
  12. /**
  13. * 栏目模型
  14. * @package app\cms\model
  15. */
  16. class Column extends ThinkModel
  17. {
  18. // 设置当前模型对应的完整数据表名称
  19. protected $name = 'cms_column';
  20. // 自动写入时间戳
  21. protected $autoWriteTimestamp = true;
  22. /**
  23. * 标题获取器
  24. * @param $value
  25. * @param $data
  26. * @author 蔡伟明 <314013107@qq.com>
  27. */
  28. protected function getTitleAttr($value, $data) {
  29. switch ($data['type']) {
  30. case 0: // 栏目
  31. break;
  32. case 1: // 单页
  33. break;
  34. }
  35. }
  36. /**
  37. * 获取栏目列表
  38. * @author 蔡伟明 <314013107@qq.com>
  39. * @return array|mixed
  40. */
  41. public static function getList()
  42. {
  43. $data_list = cache('cms_column_list');
  44. if (!$data_list) {
  45. $data_list = self::where('status', 1)->column(true, 'id');
  46. // 非开发模式,缓存数据
  47. if (config('develop_mode') == 0) {
  48. cache('cms_column_list', $data_list);
  49. }
  50. }
  51. return $data_list;
  52. }
  53. /**
  54. * 获取树状栏目
  55. * @param int $id 需要隐藏的栏目id
  56. * @param string $default 默认第一个节点项,默认为“顶级栏目”,如果为false则不显示,也可传入其他名称
  57. * @author 蔡伟明 <314013107@qq.com>
  58. * @return array|mixed
  59. */
  60. public static function getTreeList($id = 0, $default = '')
  61. {
  62. $result[0] = '顶级栏目';
  63. // 排除指定节点及其子节点
  64. $where = [
  65. ['status', '=', 1]
  66. ];
  67. if ($id !== 0) {
  68. $hide_ids = array_merge([$id], self::getChildsId($id));
  69. $where[] = ['id', 'not in', $hide_ids];
  70. }
  71. $data_list = Tree::config(['title' => 'name'])->toList(self::where($where)->order('pid,id')->column('id,pid,name'));
  72. foreach ($data_list as $item) {
  73. $result[$item['id']] = $item['title_display'];
  74. }
  75. // 设置默认节点项标题
  76. if ($default != '') {
  77. $result[0] = $default;
  78. }
  79. // 隐藏默认节点项
  80. if ($default === false) {
  81. unset($result[0]);
  82. }
  83. return $result;
  84. }
  85. /**
  86. * 获取所有子栏目id
  87. * @param int $pid 父级id
  88. * @author 蔡伟明 <314013107@qq.com>
  89. * @return array
  90. */
  91. public static function getChildsId($pid = 0)
  92. {
  93. $ids = self::where('pid', $pid)->column('id');
  94. foreach ($ids as $value) {
  95. $ids = array_merge($ids, self::getChildsId($value));
  96. }
  97. return $ids;
  98. }
  99. /**
  100. * 获取指定栏目数据
  101. * @param int $cid 栏目id
  102. * @author 蔡伟明 <314013107@qq.com>
  103. * @return mixed|static
  104. */
  105. public static function getInfo($cid = 0)
  106. {
  107. $result = cache('cms_column_info_'. $cid);
  108. if (!$result) {
  109. $result = self::get($cid);
  110. // 非开发模式,缓存数据
  111. if (config('develop_mode') == 0) {
  112. cache('cms_column_info_'. $cid, $result);
  113. }
  114. }
  115. return $result;
  116. }
  117. }