Post.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace addons\blog\model;
  3. use think\Cache;
  4. use think\Model;
  5. class Post extends Model
  6. {
  7. // 表名
  8. protected $name = 'blog_post';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'url',
  17. 'create_date',
  18. ];
  19. protected static function init()
  20. {
  21. self::afterInsert(function ($row) {
  22. $row->save(['weigh' => $row['id']]);
  23. });
  24. }
  25. public function getCreateDateAttr($value, $data)
  26. {
  27. return human_date($data['createtime']);
  28. }
  29. public function getThumbAttr($value, $data)
  30. {
  31. $value = $value ? $value : '/assets/addons/blog/img/thumb.png';
  32. return cdnurl($value, true);
  33. }
  34. public function getImageAttr($value, $data)
  35. {
  36. $value = $value ? $value : '/assets/addons/blog/img/thumb.png';
  37. return cdnurl($value, true);
  38. }
  39. public function getStatusList()
  40. {
  41. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  42. }
  43. public function getStatusTextAttr($value, $data)
  44. {
  45. $value = $value ? $value : $data['status'];
  46. $list = $this->getStatusList();
  47. return isset($list[$value]) ? $list[$value] : '';
  48. }
  49. public function getUrlAttr($value, $data)
  50. {
  51. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  52. $catename = isset($this->category) && $this->category ? $this->category->diyname : 'all';
  53. $cateid = isset($this->category) && $this->category ? $this->category->id : 0;
  54. return addon_url('blog/post/index', [':id' => $data['id'], ':diyname' => $diyname, ':catename' => $catename, ':cateid' => $cateid]);
  55. }
  56. public function getFullurlAttr($value, $data)
  57. {
  58. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  59. $catename = isset($this->category) && $this->category ? $this->category->diyname : 'all';
  60. $cateid = isset($this->category) && $this->category ? $this->category->id : 0;
  61. return addon_url('blog/post/index', [':id' => $data['id'], ':diyname' => $diyname, ':catename' => $catename, ':cateid' => $cateid], true, true);
  62. }
  63. /**
  64. * 获取日志列表
  65. * @param $tag
  66. * @return array|false|\PDOStatement|string|\think\Collection
  67. */
  68. public static function getPostList($tag)
  69. {
  70. $category = !isset($tag['category']) ? '' : $tag['category'];
  71. $condition = empty($tag['condition']) ? '' : $tag['condition'];
  72. $field = empty($params['field']) ? '*' : $params['field'];
  73. $flag = empty($tag['flag']) ? '' : $tag['flag'];
  74. $row = empty($tag['row']) ? 10 : (int)$tag['row'];
  75. $orderby = empty($tag['orderby']) ? 'createtime' : $tag['orderby'];
  76. $orderway = empty($tag['orderway']) ? 'desc' : strtolower($tag['orderway']);
  77. $limit = empty($tag['limit']) ? $row : $tag['limit'];
  78. $cache = !isset($tag['cache']) ? true : (int)$tag['cache'];
  79. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  80. $cache = !$cache ? false : $cache;
  81. $where = ['status' => 'normal'];
  82. if ($category !== '') {
  83. $where['category_id'] = ['in', $category];
  84. }
  85. //如果有设置标志,则拆分标志信息并构造condition条件
  86. if ($flag !== '') {
  87. if (stripos($flag, '&') !== false) {
  88. $arr = [];
  89. foreach (explode('&', $flag) as $k => $v) {
  90. $arr[] = "FIND_IN_SET('{$v}', flag)";
  91. }
  92. if ($arr) {
  93. $condition .= "(" . implode(' AND ', $arr) . ")";
  94. }
  95. } else {
  96. $condition .= ($condition ? ' AND ' : '');
  97. $arr = [];
  98. foreach (explode(',', str_replace('|', ',', $flag)) as $k => $v) {
  99. $arr[] = "FIND_IN_SET('{$v}', flag)";
  100. }
  101. if ($arr) {
  102. $condition .= "(" . implode(' OR ', $arr) . ")";
  103. }
  104. }
  105. }
  106. $order = $orderby == 'rand' ? 'rand()' : (in_array($orderby, ['createtime', 'updatetime', 'views', 'weigh', 'id']) ? "{$orderby} {$orderway}" : "createtime {$orderway}");
  107. $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
  108. $postModel = self::with('category');
  109. $list = $postModel
  110. ->where($where)
  111. ->where($condition)
  112. ->field($field)
  113. ->cache($cache)
  114. ->order($order)
  115. ->limit($limit)
  116. ->select();
  117. //$list = collection($list)->toArray();
  118. return $list;
  119. }
  120. /**
  121. * 获取SQL查询结果
  122. */
  123. public static function getQueryList($tag)
  124. {
  125. $sql = isset($tag['sql']) ? $tag['sql'] : '';
  126. $bind = isset($tag['bind']) ? $tag['bind'] : [];
  127. $cache = !isset($tag['cache']) ? true : (int)$tag['cache'];
  128. $name = md5("sql-" . $tag['sql']);
  129. $list = Cache::get($name);
  130. if (!$list) {
  131. $list = \think\Db::query($sql, $bind);
  132. Cache::set($name, $list, $cache);
  133. }
  134. return $list;
  135. }
  136. public function category()
  137. {
  138. return $this->belongsTo('addons\blog\model\Category')->setEagerlyType(1);
  139. }
  140. }