Block.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace addons\blog\model;
  3. use think\Model;
  4. /**
  5. * 区块模型
  6. */
  7. class Block extends Model
  8. {
  9. protected $name = "blog_block";
  10. // 开启自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = '';
  14. protected $updateTime = '';
  15. // 追加属性
  16. protected $append = [
  17. ];
  18. protected static $config = [];
  19. protected static function init()
  20. {
  21. $config = get_addon_config('blog');
  22. self::$config = $config;
  23. }
  24. public function getImageAttr($value, $data)
  25. {
  26. $value = $value ? $value : '/assets/addons/blog/img/thumb.png';
  27. return cdnurl($value);
  28. }
  29. /**
  30. * 获取区块列表
  31. * @param $params
  32. * @return false|\PDOStatement|string|\think\Collection
  33. */
  34. public static function getBlockList($params)
  35. {
  36. $name = empty($params['name']) ? '' : $params['name'];
  37. $condition = empty($params['condition']) ? '' : $params['condition'];
  38. $field = empty($params['field']) ? '*' : $params['field'];
  39. $row = empty($params['row']) ? 10 : (int)$params['row'];
  40. $orderby = empty($params['orderby']) ? 'id' : $params['orderby'];
  41. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  42. $limit = empty($params['limit']) ? $row : $params['limit'];
  43. $cache = !isset($params['cache']) ? true : (int)$params['cache'];
  44. $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
  45. $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
  46. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  47. $cache = !$cache ? false : $cache;
  48. $where = ['status' => 'normal'];
  49. if ($name !== '') {
  50. $where['name'] = $name;
  51. }
  52. $order = $orderby == 'rand' ? 'rand()' : (in_array($orderby, ['name', 'id', 'createtime', 'updatetime', 'weigh']) ? "{$orderby} {$orderway}" : "id {$orderway}");
  53. $list = self::where($where)
  54. ->where($condition)
  55. ->field($field)
  56. ->order($order)
  57. ->limit($limit)
  58. ->cache($cache)
  59. ->select();
  60. self::render($list, $imgwidth, $imgheight);
  61. return $list;
  62. }
  63. public static function render(&$list, $imgwidth, $imgheight)
  64. {
  65. $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
  66. $height = $imgheight ? 'height="' . $imgheight . '"' : '';
  67. foreach ($list as $k => &$v) {
  68. $v['hasimage'] = $v->getData('image') ? true : false;
  69. $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['title'] . '</a>';
  70. $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' /></a>';
  71. $v['img'] = '<img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' />';
  72. }
  73. return $list;
  74. }
  75. public static function getBlockContent($params)
  76. {
  77. $field = isset($params['id']) ? 'id' : 'name';
  78. $value = isset($params[$field]) ? $params[$field] : '';
  79. $cache = !isset($params['cache']) ? true : (int)$params['cache'];
  80. $row = self::where($field, $value)
  81. ->where('status', 'normal')
  82. ->cache($cache)
  83. ->find();
  84. $result = '';
  85. if ($row) {
  86. if ($row['content']) {
  87. $result = $row['content'];
  88. } elseif ($row['image']) {
  89. $result = '<img src="' . $row['image'] . '" class="img-responsive"/>';
  90. } else {
  91. $result = $row['title'];
  92. }
  93. if ($row['url'] && !$row['content']) {
  94. $result = $row['url'] ? '<a href="' . (preg_match("/^https?:\/\/(.*)/i", $row['url']) ? $row['url'] : url($row['url'])) . '">' . $result . '</a>' : $result;
  95. }
  96. }
  97. return $result;
  98. }
  99. }