Answer.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Answer extends Model
  10. {
  11. use SoftDelete;
  12. // 表名
  13. protected $name = 'ask_answer';
  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. 'question_url',
  24. 'deletetime_text',
  25. 'adopttime_text',
  26. 'status_text'
  27. ];
  28. protected static $config;
  29. public 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. $commentList = Comment::where('type', 'answer')->where('source_id', $row['id'])->select();
  37. foreach ($commentList as $index => $item) {
  38. $item->delete();
  39. }
  40. $model = Service::getModelByType('question', $row['question_id']);
  41. if ($model) {
  42. try {
  43. $model->setDec("answers");
  44. } catch (Exception $e) {
  45. }
  46. }
  47. User::decrease('answers', 1, $row->user_id);
  48. $config['score']['postanswer'] && \app\common\model\User::score(-$config['score']['postanswer'], $row->user_id, '删除答案');
  49. } else {
  50. //永久删除
  51. $commentList = Comment::where('type', 'answer')->where('source_id', $row['id'])->select();
  52. foreach ($commentList as $index => $item) {
  53. $item->delete(true);
  54. }
  55. }
  56. });
  57. self::afterUpdate(function ($row) use ($config) {
  58. $changedData = $row->getChangedData();
  59. if (array_key_exists('deletetime', $changedData) && is_null($changedData['deletetime'])) {
  60. $model = Service::getModelByType('question', $row['question_id']);
  61. if ($model) {
  62. try {
  63. $model->setInc("answers");
  64. } catch (Exception $e) {
  65. echo $e->getMessage();
  66. }
  67. }
  68. User::increase('answers', 1, $row->user_id);
  69. $config['score']['postanswer'] && \app\common\model\User::score($config['score']['postanswer'], $row->user_id, '恢复答案');
  70. }
  71. });
  72. parent::init();
  73. }
  74. public function getQuestionUrlAttr($value, $data)
  75. {
  76. return addon_url('ask/question/show', [':id' => $data['question_id']], static::$config['urlsuffix']);
  77. }
  78. public function setContentFmtAttr($data)
  79. {
  80. $content = Askdown::instance()->format($this->data['content']);
  81. return $content;
  82. }
  83. public function getStatusList()
  84. {
  85. return ['normal' => __('Normal'), 'hidden' => __('Hidden'), 'closed' => __('Closed')];
  86. }
  87. public function getDeletetimeTextAttr($value, $data)
  88. {
  89. $value = $value ? $value : (isset($data['deletetime']) ? $data['deletetime'] : '');
  90. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  91. }
  92. public function getAdopttimeTextAttr($value, $data)
  93. {
  94. $value = $value ? $value : (isset($data['adopttime']) ? $data['adopttime'] : '');
  95. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  96. }
  97. public function getStatusTextAttr($value, $data)
  98. {
  99. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  100. $list = $this->getStatusList();
  101. return isset($list[$value]) ? $list[$value] : '';
  102. }
  103. protected function setDeletetimeAttr($value)
  104. {
  105. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  106. }
  107. protected function setAdopttimeAttr($value)
  108. {
  109. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  110. }
  111. public function user()
  112. {
  113. return $this->belongsTo("\app\common\model\User", 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  114. }
  115. public function question()
  116. {
  117. return $this->belongsTo("Question", 'question_id', 'id', [], 'LEFT')->setEagerlyType(0);
  118. }
  119. }