Comment.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace app\admin\model\cms;
  3. use app\common\model\User;
  4. use think\Db;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. class Comment extends Model
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'cms_comment';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = 'deletetime';
  18. // 追加属性
  19. protected $append = [
  20. 'type_text',
  21. 'status_text'
  22. ];
  23. protected static function init()
  24. {
  25. $config = get_addon_config('cms');
  26. self::afterWrite(function ($row) use ($config) {
  27. $changedData = $row->getChangedData();
  28. if (isset($changedData['status']) && isset($config['score']['postcomment'])) {
  29. if ($changedData['status'] == 'normal') {
  30. User::score($config['score']['postcomment'], $row['user_id'], '发表评论');
  31. } else {
  32. User::score(-$config['score']['postcomment'], $row['user_id'], '删除评论');
  33. }
  34. }
  35. self::refreshSourceComments($row['id']);
  36. });
  37. self::afterDelete(function ($row) use ($config) {
  38. $data = Comment::withTrashed()->where('id', $row['id'])->find();
  39. if ($data) {
  40. if ($data['status'] == 'normal' && isset($config['score']['postcomment'])) {
  41. User::score(-$config['score']['postcomment'], $row['user_id'], '删除评论');
  42. }
  43. }
  44. self::refreshSourceComments($row['id']);
  45. });
  46. }
  47. /**
  48. * 刷新评论
  49. * @param int $id 评论ID
  50. * @return bool
  51. */
  52. public static function refreshSourceComments($id)
  53. {
  54. $row = self::withTrashed()->where('id', $id)->find();
  55. if (!$row) {
  56. return false;
  57. }
  58. $model = $row->source;
  59. if ($model && $model instanceOf \think\Model) {
  60. $comments = self::where('type', $row['type'])->where('aid', $row['aid'])->where('status', 'normal')->count();
  61. $model->save(['comments' => $comments]);
  62. }
  63. return true;
  64. }
  65. public function getTypeList()
  66. {
  67. return ['archives' => __('Archives'), 'page' => __('Page'), 'special' => __('Special')];
  68. }
  69. public function getStatusList()
  70. {
  71. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  72. }
  73. public function getTypeTextAttr($value, $data)
  74. {
  75. $value = $value ? $value : $data['type'];
  76. $list = $this->getTypeList();
  77. return isset($list[$value]) ? $list[$value] : '';
  78. }
  79. public function getStatusTextAttr($value, $data)
  80. {
  81. $value = $value ? $value : $data['status'];
  82. $list = $this->getStatusList();
  83. return isset($list[$value]) ? $list[$value] : '';
  84. }
  85. /**
  86. * 根据类型和ID删除
  87. */
  88. public static function deleteByType($type, $aid, $force = false)
  89. {
  90. if (!$force) {
  91. //删除评论
  92. $commentList = Comment::where(['type' => $type, 'aid' => $aid])->select();
  93. foreach ($commentList as $index => $item) {
  94. $item->delete();
  95. }
  96. } else {
  97. //强制删除评论
  98. $commentList = Comment::withTrashed()->where(['type' => $type, 'aid' => $aid])->select();
  99. foreach ($commentList as $index => $item) {
  100. $item->delete(true);
  101. }
  102. }
  103. }
  104. public function user()
  105. {
  106. return $this->belongsTo('\app\common\model\User', 'user_id', '', [], 'LEFT')->setEagerlyType(0);
  107. }
  108. /**
  109. * 关联文档模型
  110. */
  111. public function archives()
  112. {
  113. return $this->belongsTo('Archives', 'aid', '', [], 'LEFT')->setEagerlyType(0);
  114. }
  115. /**
  116. * 关联单页模型
  117. */
  118. public function spage()
  119. {
  120. return $this->belongsTo("addons\cms\model\Page", 'aid', '', [], 'LEFT')->setEagerlyType(0);
  121. }
  122. /**
  123. * 关联专题模型
  124. */
  125. public function special()
  126. {
  127. return $this->belongsTo("addons\cms\model\Special", 'aid', '', [], 'LEFT')->setEagerlyType(0);
  128. }
  129. /**
  130. * 关联模型
  131. */
  132. public function source()
  133. {
  134. $type = $this->getData('type');
  135. $modelArr = ['page' => 'Page', 'archives' => 'Archives', 'special' => 'Special'];
  136. $model = isset($modelArr[$type]) ? $modelArr[$type] : $modelArr['archives'];
  137. return $this->belongsTo($model, "aid");
  138. }
  139. }