Block.php 6.7 KB

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