Comment.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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'])) {
  29. if ($changedData['status'] == 'normal') {
  30. Db::name("cms_{$row['type']}")->where('id', $row['aid'])->setInc("comments");
  31. User::score($config['score']['postcomment'], $row['user_id'], '发表评论');
  32. } else {
  33. Db::name("cms_{$row['type']}")->where('id', $row['aid'])->where('comments', '>', 0)->setDec("comments");
  34. User::score(-$config['score']['postcomment'], $row['user_id'], '删除评论');
  35. }
  36. }
  37. });
  38. self::afterDelete(function ($row) use ($config) {
  39. $data = Comment::withTrashed()->where('id', $row['id'])->find();
  40. if ($data) {
  41. if ($data['status'] == 'normal') {
  42. Db::name("cms_{$row['type']}")->where('id', $row['aid'])->where('comments', '>', 0)->setDec("comments");
  43. User::score(-$config['score']['postcomment'], $row['user_id'], '删除评论');
  44. }
  45. }
  46. });
  47. }
  48. public function getTypeList()
  49. {
  50. return ['archives' => __('Archives'), 'page' => __('Page'), 'special' => __('Special')];
  51. }
  52. public function getStatusList()
  53. {
  54. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  55. }
  56. public function getTypeTextAttr($value, $data)
  57. {
  58. $value = $value ? $value : $data['type'];
  59. $list = $this->getTypeList();
  60. return isset($list[$value]) ? $list[$value] : '';
  61. }
  62. public function getStatusTextAttr($value, $data)
  63. {
  64. $value = $value ? $value : $data['status'];
  65. $list = $this->getStatusList();
  66. return isset($list[$value]) ? $list[$value] : '';
  67. }
  68. /**
  69. * 根据类型和ID删除
  70. */
  71. public static function deleteByType($type, $aid, $force = false)
  72. {
  73. if (!$force) {
  74. //删除评论
  75. $commentList = Comment::where(['type' => $type, 'aid' => $aid])->select();
  76. foreach ($commentList as $index => $item) {
  77. $item->delete();
  78. }
  79. } else {
  80. //强制删除评论
  81. $commentList = Comment::withTrashed()->where(['type' => $type, 'aid' => $aid])->select();
  82. foreach ($commentList as $index => $item) {
  83. $item->delete(true);
  84. }
  85. }
  86. }
  87. public function user()
  88. {
  89. return $this->belongsTo('\app\common\model\User', 'user_id', '', [], 'LEFT')->setEagerlyType(0);
  90. }
  91. /**
  92. * 关联文档模型
  93. */
  94. public function archives()
  95. {
  96. return $this->belongsTo('Archives', 'aid', '', [], 'LEFT')->setEagerlyType(0);
  97. }
  98. /**
  99. * 关联单页模型
  100. */
  101. public function spage()
  102. {
  103. return $this->belongsTo("addons\cms\model\Page", 'aid', '', [], 'LEFT')->setEagerlyType(0);
  104. }
  105. /**
  106. * 关联专题模型
  107. */
  108. public function special()
  109. {
  110. return $this->belongsTo("addons\cms\model\Special", 'aid', '', [], 'LEFT')->setEagerlyType(0);
  111. }
  112. }