Comment.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\admin\model\ask;
  3. use addons\ask\library\Askdown;
  4. use addons\ask\library\Service;
  5. use addons\ask\model\User;
  6. use think\Exception;
  7. use think\Model;
  8. use traits\model\SoftDelete;
  9. class Comment extends Model
  10. {
  11. use SoftDelete;
  12. // 表名
  13. protected $name = 'ask_comment';
  14. // 自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = 'deletetime';
  20. protected $auto = ['content_fmt'];
  21. // 追加属性
  22. protected $append = [
  23. 'type_text',
  24. 'deletetime_text',
  25. 'status_text'
  26. ];
  27. protected static $config = [];
  28. //自定义初始化
  29. protected static function init()
  30. {
  31. $config = get_addon_config('ask');
  32. self::$config = $config;
  33. self::afterDelete(function ($row) use ($config) {
  34. $data = $row->withTrashed()->where('id', $row->id)->find();
  35. if ($data) {
  36. $model = Service::getModelByType($row['type'], $row['source_id']);
  37. if ($model) {
  38. try {
  39. $model->setDec("comments");
  40. } catch (Exception $e) {
  41. }
  42. }
  43. User::decrease('comments', 1, $row->user_id);
  44. //减少积分
  45. $config['score']['postcomment'] && \app\common\model\User::score(-$config['score']['postcomment'], $row->user_id, '删除评论');
  46. }
  47. });
  48. self::afterUpdate(function ($row) use ($config) {
  49. $changedData = $row->getChangedData();
  50. if (array_key_exists('deletetime', $changedData) && is_null($changedData['deletetime'])) {
  51. $model = Service::getModelByType($row['type'], $row['source_id']);
  52. if ($model) {
  53. try {
  54. $model->setInc("comments");
  55. } catch (Exception $e) {
  56. }
  57. }
  58. User::increase('comments', 1, $row->user_id);
  59. $config['score']['postcomment'] && \app\common\model\User::score($config['score']['postcomment'], $row->user_id, '恢复评论');
  60. }
  61. });
  62. }
  63. public function setContentFmtAttr($data)
  64. {
  65. $content = Askdown::instance()->format($this->data['content']);
  66. return $content;
  67. }
  68. public function getTypeList()
  69. {
  70. return ['question' => __('Question'), 'article' => __('Article'), 'answer' => __('Answer')];
  71. }
  72. public function getStatusList()
  73. {
  74. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  75. }
  76. public function getTypeTextAttr($value, $data)
  77. {
  78. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  79. $list = $this->getTypeList();
  80. return isset($list[$value]) ? $list[$value] : '';
  81. }
  82. public function getDeletetimeTextAttr($value, $data)
  83. {
  84. $value = $value ? $value : (isset($data['deletetime']) ? $data['deletetime'] : '');
  85. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  86. }
  87. public function getStatusTextAttr($value, $data)
  88. {
  89. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  90. $list = $this->getStatusList();
  91. return isset($list[$value]) ? $list[$value] : '';
  92. }
  93. protected function setDeletetimeAttr($value)
  94. {
  95. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  96. }
  97. public function user()
  98. {
  99. return $this->belongsTo("\app\common\model\User", 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  100. }
  101. }