Block.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace addons\cms\model;
  3. use addons\cms\library\Service;
  4. use think\Db;
  5. use think\Model;
  6. use think\View;
  7. /**
  8. * 区块模型
  9. */
  10. class Block extends Model
  11. {
  12. protected $name = "cms_block";
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = '';
  17. protected $updateTime = '';
  18. // 追加属性
  19. protected $append = [
  20. ];
  21. protected static $config = [];
  22. protected static $tagCount = 0;
  23. protected static function init()
  24. {
  25. $config = get_addon_config('cms');
  26. self::$config = $config;
  27. }
  28. public function getImageAttr($value, $data)
  29. {
  30. $value = $value ? $value : self::$config['default_block_img'];
  31. return cdnurl($value);
  32. }
  33. public function getContentAttr($value, $data)
  34. {
  35. if (isset($data['parsetpl']) && $data['parsetpl']) {
  36. $view = View::instance();
  37. $view->engine->layout(false);
  38. return $view->display($data['content']);
  39. }
  40. return $data['content'];
  41. }
  42. public function getHasimageAttr($value, $data)
  43. {
  44. return $this->getData("image") ? true : false;
  45. }
  46. /**
  47. * 获取区块列表
  48. * @param $params
  49. * @return false|\PDOStatement|string|\think\Collection
  50. */
  51. public static function getBlockList($params)
  52. {
  53. $config = get_addon_config('cms');
  54. $name = empty($params['name']) ? '' : $params['name'];
  55. $condition = empty($params['condition']) ? '' : $params['condition'];
  56. $field = empty($params['field']) ? '*' : $params['field'];
  57. $row = empty($params['row']) ? 10 : (int)$params['row'];
  58. $orderby = empty($params['orderby']) ? 'id' : $params['orderby'];
  59. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  60. $limit = empty($params['limit']) ? $row : $params['limit'];
  61. $cache = !isset($params['cache']) ? $config['cachelifetime'] === 'true' ? true : (int)$config['cachelifetime'] : (int)$params['cache'];
  62. $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
  63. $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
  64. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  65. $paginate = !isset($params['paginate']) ? false : $params['paginate'];
  66. $cache = !$cache ? false : $cache;
  67. self::$tagCount++;
  68. $where = ['status' => 'normal'];
  69. if ($name !== '') {
  70. $where['name'] = $name;
  71. }
  72. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  73. $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
  74. $blockModel = self::where($where)
  75. ->where($condition)
  76. ->field($field)
  77. ->orderRaw($order);
  78. if ($paginate) {
  79. $paginateArr = explode(',', $paginate);
  80. $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
  81. $config = [];
  82. $config['var_page'] = isset($paginateArr[2]) ? $paginateArr[2] : 'bpage' . self::$tagCount;
  83. $config['path'] = isset($paginateArr[3]) ? $paginateArr[3] : '';
  84. $config['fragment'] = isset($paginateArr[4]) ? $paginateArr[4] : '';
  85. $config['query'] = request()->get();
  86. $list = $blockModel->paginate($listRows, (isset($paginateArr[1]) ? $paginateArr[1] : false), $config);
  87. } else {
  88. $list = $blockModel->limit($limit)->cache($cache)->select();
  89. }
  90. $fieldsContentList = Fields::getFieldsContentList('block');
  91. foreach ($list as $index => $item) {
  92. Service::appendTextAttr($fieldsContentList, $item);
  93. }
  94. self::render($list, $imgwidth, $imgheight);
  95. return $list;
  96. }
  97. public static function render(&$list, $imgwidth, $imgheight)
  98. {
  99. $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
  100. $height = $imgheight ? 'height="' . $imgheight . '"' : '';
  101. $time = time();
  102. foreach ($list as $k => &$v) {
  103. if (($v['begintime'] && $time < $v['begintime']) || ($v['endtime'] && $time > $v['endtime'])) {
  104. unset($list[$k]);
  105. continue;
  106. }
  107. $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['title'] . '</a>';
  108. $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' /></a>';
  109. $v['img'] = '<img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' />';
  110. }
  111. return $list;
  112. }
  113. /**
  114. * 获取区块内容
  115. * @param $params
  116. * @return mixed|string
  117. */
  118. public static function getBlockContent($params)
  119. {
  120. $fieldName = isset($params['id']) ? 'id' : 'name';
  121. $value = isset($params[$fieldName]) ? $params[$fieldName] : '';
  122. $field = isset($params['field']) ? $params['field'] : '';
  123. $cache = !isset($params['cache']) ? true : (int)$params['cache'];
  124. $cache = !$cache ? false : $cache;
  125. $row = self::where($fieldName, $value)
  126. ->where('status', 'normal')
  127. ->cache($cache)
  128. ->find();
  129. $result = '';
  130. if ($row) {
  131. $fieldsContentList = Fields::getFieldsContentList('block');
  132. Service::appendTextAttr($fieldsContentList, $row);
  133. $time = time();
  134. if (($row['begintime'] && $time < $row['begintime']) || ($row['endtime'] && $time > $row['endtime'])) {
  135. return $result;
  136. }
  137. if ($field && isset($row[$field])) {
  138. return $row[$field];
  139. }
  140. if ($row['content']) {
  141. $result = $row['content'];
  142. } elseif ($row['image']) {
  143. $result = '<img src="' . $row['image'] . '" class="img-responsive"/>';
  144. } else {
  145. $result = $row['title'];
  146. }
  147. if ($row['url'] && !$row['content']) {
  148. $result = $row['url'] ? '<a href="' . (preg_match("/^https?:\/\/(.*)/i", $row['url']) ? $row['url'] : url($row['url'])) . '">' . $result . '</a>' : $result;
  149. }
  150. }
  151. return $result;
  152. }
  153. }