Article.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\admin\model\ask;
  3. use addons\ask\library\Askdown;
  4. use addons\ask\library\FulltextSearch;
  5. use addons\ask\model\Taggable;
  6. use addons\ask\model\User;
  7. use addons\pay\library\Service;
  8. use think\Exception;
  9. use think\Model;
  10. use traits\model\SoftDelete;
  11. class Article extends Model
  12. {
  13. use SoftDelete;
  14. // 表名
  15. protected $name = 'ask_article';
  16. // 自动写入时间戳字段
  17. protected $autoWriteTimestamp = 'int';
  18. // 定义时间戳字段名
  19. protected $createTime = 'createtime';
  20. protected $updateTime = 'updatetime';
  21. protected $deleteTime = 'deletetime';
  22. protected $auto = ['content_fmt'];
  23. // 追加属性
  24. protected $append = [
  25. 'url',
  26. 'flag_text',
  27. 'deletetime_text',
  28. 'status_text'
  29. ];
  30. protected static $config = [];
  31. protected static function init()
  32. {
  33. $config = get_addon_config('ask');
  34. self::$config = $config;
  35. self::afterDelete(function ($row) use ($config) {
  36. $data = $row->withTrashed()->where('id', $row->id)->find();
  37. if ($data) {
  38. //删除相关评论
  39. $commentList = Comment::where('type', 'article')->where('source_id', $row['id'])->select();
  40. foreach ($commentList as $index => $item) {
  41. $item->delete();
  42. }
  43. User::decrease('articles', 1, $row->user_id);
  44. //更新Tags信息
  45. Tag::where('articles', '>', '0')->where('id', 'in', $row->tags_ids)->setDec("articles");
  46. //减少积分
  47. $config['score']['postarticle'] && \app\common\model\User::score(-$config['score']['postarticle'], $row->user_id, '删除文章');
  48. if ($config['searchtype'] == 'xunsearch') {
  49. FulltextSearch::del($row);
  50. }
  51. } else {
  52. //删除相关评论
  53. $commentList = Comment::withTrashed()->where('type', 'article')->where('source_id', $row['id'])->select();
  54. foreach ($commentList as $index => $item) {
  55. $item->delete(true);
  56. }
  57. //删除相关标签
  58. $tagableList = Taggable::where('type', 'article')->where('source_id', $row['id'])->select();
  59. foreach ($tagableList as $index => $item) {
  60. $item->delete();
  61. }
  62. }
  63. });
  64. self::afterUpdate(function ($row) use ($config) {
  65. $changedData = $row->getChangedData();
  66. if (array_key_exists('deletetime', $changedData) && is_null($changedData['deletetime'])) {
  67. //更新Tags信息
  68. Tag::where('id', 'in', $row->tags_ids)->setInc("articles");
  69. User::increase('articles', 1, $row->user_id);
  70. $config['score']['postarticle'] && \app\common\model\User::score($config['score']['postarticle'], $row->user_id, '恢复文章');
  71. if ($config['searchtype'] == 'xunsearch') {
  72. FulltextSearch::update($row);
  73. }
  74. }
  75. });
  76. }
  77. public function getUrlAttr($value, $data)
  78. {
  79. return addon_url('ask/article/show', [':id' => $data['id']], static::$config['urlsuffix']);
  80. }
  81. public function setContentFmtAttr($data)
  82. {
  83. $content = Askdown::instance()->format($this->data['content']);
  84. return $content;
  85. }
  86. public function getTagsAttr($value, $data)
  87. {
  88. if (isset($this->data['tags'])) {
  89. return $this->data['tags'];
  90. }
  91. $tags = Tag::getTags('article', $data['id']);
  92. return $tags;
  93. }
  94. public function getTagsIdsAttr($value, $data)
  95. {
  96. if (isset($this->data['tags_ids'])) {
  97. return $this->data['tags_ids'];
  98. }
  99. $tagsArr = [];
  100. $tagsList = $this->getTagsAttr($value, $data);
  101. foreach ($tagsList as $index => $item) {
  102. $tagsArr[] = $item->id;
  103. }
  104. return $tagsArr;
  105. }
  106. public function getFlagList()
  107. {
  108. return ['index' => __('Index'), 'hot' => __('Hot'), 'recommend' => __('Recommend'), 'top' => __('Top')];
  109. }
  110. public function getStatusList()
  111. {
  112. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  113. }
  114. public function getFlagTextAttr($value, $data)
  115. {
  116. $value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
  117. $valueArr = explode(',', $value);
  118. $list = $this->getFlagList();
  119. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  120. }
  121. public function getDeletetimeTextAttr($value, $data)
  122. {
  123. $value = $value ? $value : (isset($data['deletetime']) ? $data['deletetime'] : '');
  124. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  125. }
  126. public function getStatusTextAttr($value, $data)
  127. {
  128. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  129. $list = $this->getStatusList();
  130. return isset($list[$value]) ? $list[$value] : '';
  131. }
  132. protected function setFlagAttr($value)
  133. {
  134. return is_array($value) ? implode(',', $value) : $value;
  135. }
  136. protected function setDeletetimeAttr($value)
  137. {
  138. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  139. }
  140. public function user()
  141. {
  142. return $this->belongsTo("\app\common\model\User", 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  143. }
  144. public function category()
  145. {
  146. return $this->belongsTo("Category", 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
  147. }
  148. }