BlogPost.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\admin\model;
  3. use addons\blog\library\FulltextSearch;
  4. use think\Model;
  5. class BlogPost 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. 'fullurl',
  18. 'flag_text',
  19. 'status_text'
  20. ];
  21. protected static function init()
  22. {
  23. $config = get_addon_config('blog');
  24. self::afterInsert(function ($row) {
  25. $row->save(['weigh' => $row['id']]);
  26. });
  27. self::afterWrite(function ($row) use ($config) {
  28. $changedData = $row->getChangedData();
  29. if (isset($changedData['status']) && $changedData['status'] == 'normal') {
  30. if ($config['baidupush']) {
  31. //推送到熊掌号+百度站长
  32. $urls = [$row->fullurl];
  33. \think\Hook::listen("baidupush", $urls);
  34. }
  35. }
  36. if ($config['searchtype'] == 'xunsearch') {
  37. //更新全文搜索
  38. FulltextSearch::update($row->id);
  39. }
  40. });
  41. self::afterDelete(function ($row) use ($config) {
  42. if ($config['searchtype'] == 'xunsearch') {
  43. //更新全文搜索
  44. FulltextSearch::del($row);
  45. }
  46. });
  47. }
  48. public function getUrlAttr($value, $data)
  49. {
  50. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  51. $catename = isset($this->category) && $this->category ? $this->category->diyname : 'all';
  52. $cateid = isset($this->category) && $this->category ? $this->category->id : 0;
  53. return addon_url('blog/post/index', [':id' => $data['id'], ':diyname' => $diyname, ':catename' => $catename, ':cateid' => $cateid]);
  54. }
  55. public function getFullurlAttr($value, $data)
  56. {
  57. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  58. $catename = isset($this->category) && $this->category ? $this->category->diyname : 'all';
  59. $cateid = isset($this->category) && $this->category ? $this->category->id : 0;
  60. return addon_url('blog/post/index', [':id' => $data['id'], ':diyname' => $diyname, ':catename' => $catename, ':cateid' => $cateid], true, true);
  61. }
  62. public function getFlagList()
  63. {
  64. return ['hot' => __('Hot'), 'index' => __('Index'), 'recommend' => __('Recommend')];
  65. }
  66. public function getStatusList()
  67. {
  68. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  69. }
  70. public function getFlagTextAttr($value, $data)
  71. {
  72. $value = $value ? $value : $data['flag'];
  73. $valueArr = explode(',', $value);
  74. $list = $this->getFlagList();
  75. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  76. }
  77. public function getStatusTextAttr($value, $data)
  78. {
  79. $value = $value ? $value : $data['status'];
  80. $list = $this->getStatusList();
  81. return isset($list[$value]) ? $list[$value] : '';
  82. }
  83. protected function setFlagAttr($value)
  84. {
  85. return is_array($value) ? implode(',', $value) : $value;
  86. }
  87. public function category()
  88. {
  89. return $this->belongsTo('BlogCategory', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
  90. }
  91. }