Block.php 6.7 KB

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