BlogCategory.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\admin\model;
  3. use addons\blog\library\FulltextSearch;
  4. use think\Model;
  5. class BlogCategory extends Model
  6. {
  7. // 表名
  8. protected $name = 'blog_category';
  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. });
  37. }
  38. public function getUrlAttr($value, $data)
  39. {
  40. $diyname = $data['diyname'] ? $data['diyname'] : $data['id'];
  41. return addon_url('blog/category/index', [':id' => $data['id'], ':diyname' => $diyname], true);
  42. }
  43. public function getFullurlAttr($value, $data)
  44. {
  45. $diyname = $data['diyname'] ? $data['diyname'] : $data['id'];
  46. return addon_url('blog/category/index', [':id' => $data['id'], ':diyname' => $diyname], true, true);
  47. }
  48. public function getFlagList()
  49. {
  50. return ['hot' => __('Hot'), 'index' => __('Index'), 'recommend' => __('Recommend')];
  51. }
  52. public function getFlagTextAttr($value, $data)
  53. {
  54. $value = $value ? $value : $data['flag'];
  55. $valueArr = explode(',', $value);
  56. $list = $this->getFlagList();
  57. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  58. }
  59. public function getStatusList()
  60. {
  61. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  62. }
  63. public function getStatusTextAttr($value, $data)
  64. {
  65. $value = $value ? $value : $data['status'];
  66. $list = $this->getStatusList();
  67. return isset($list[$value]) ? $list[$value] : '';
  68. }
  69. }