EnterpriseNews.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\store\model\enterprise;
  4. use app\common\model\enterprise\EnterpriseNews as EnModel;
  5. use app\store\model\store\User;
  6. /**
  7. * 官网新闻
  8. * Class EnterpriseNews
  9. * @package app\common\model
  10. */
  11. class EnterpriseNews extends EnModel
  12. {
  13. public function getContentsAttr($data)
  14. {
  15. return htmlspecialchars_decode($data);
  16. }
  17. /**
  18. * 新增功能
  19. * @param $form
  20. * @return bool
  21. */
  22. public function addOne($form)
  23. {
  24. $record = new self();
  25. $record->title = $form['title'];
  26. $record->author = $form['author'] ?? '';
  27. $record->sources = $form['sources'] ?? '';
  28. $record->thumb_img = $form['thumb_img'] ?? 0;
  29. $record->admin_id = $form['admin_id'];
  30. $record->keywords = $form['keywords'] ?? '';
  31. $record->desc = $form['desc'];
  32. $record->contents = $form['contents'];
  33. return $record->save();
  34. }
  35. public function getItem($id)
  36. {
  37. return $this->with(['file'])->where('id', $id)
  38. ->where('is_delete', 0)
  39. ->find();
  40. }
  41. /**
  42. * 新增功能
  43. * @param $form
  44. * @return bool
  45. */
  46. public function updateOne($form)
  47. {
  48. $record['title'] = $form['title'];
  49. $record['author'] = $form['author'] ?? '';
  50. $record['sources'] = $form['sources'] ?? '';
  51. $record['thumb_img'] = $form['thumb_img'];
  52. $record['update_admin_id'] = $form['update_admin_id'];
  53. $record['keywords'] = $form['keywords'] ?? '';
  54. $record['desc'] = $form['desc'];
  55. $record['contents'] = $form['contents'];
  56. try {
  57. $this->where('id', $form['id'])->update($record);
  58. } catch (\Exception $e) {
  59. $this->error = $e->getMessage();
  60. log_record($e->getMessage(), 'error');
  61. return false;
  62. }
  63. return true;
  64. }
  65. /**
  66. * 发布中/下线
  67. * @param $form
  68. * @return bool
  69. */
  70. public function publishOne($form)
  71. {
  72. try {
  73. $this->where('id', $form['id'])->update(['pub_status' => $form['pub_status']]);
  74. } catch (\Exception $e) {
  75. $this->error = $e->getMessage();
  76. log_record($e->getMessage(), 'error');
  77. return false;
  78. }
  79. return true;
  80. }
  81. /**
  82. * 是否推荐到首页
  83. * @param $form
  84. * @return bool
  85. */
  86. public function isTopOne($form)
  87. {
  88. try {
  89. $this->where('id', $form['id'])->update(['is_top' => $form['is_top']]);
  90. } catch (\Exception $e) {
  91. $this->error = $e->getMessage();
  92. log_record($e->getMessage(), 'error');
  93. return false;
  94. }
  95. return true;
  96. }
  97. /**
  98. * 删除新闻
  99. * @param $form
  100. * @return bool
  101. */
  102. public function deleteOne($form)
  103. {
  104. try {
  105. $this->where('id', $form['id'])->update(['is_delete' => 1]);
  106. } catch (\Exception $e) {
  107. $this->error = $e->getMessage();
  108. log_record($e->getMessage(), 'error');
  109. return false;
  110. }
  111. return true;
  112. }
  113. public function getItems($isTop = 0)
  114. {
  115. $model = $this->with(['file'])->where('is_delete', 0)
  116. ->where('pub_status', 0);
  117. if ($isTop) {
  118. $model = $model->where('is_top', 1);
  119. }
  120. return $model->order('id desc')->field('id,title,admin_id,sources,desc,thumb_img,create_time,author,contents')
  121. ->order('id desc')
  122. ->select()
  123. ->each(function (&$item) {
  124. $item['img'] = $item['file']['preview_url'];
  125. unset($item['file']);
  126. })
  127. ->toArray();
  128. }
  129. /**
  130. * 后台新闻列表
  131. * @return \think\Paginator
  132. * @throws \think\db\exception\DbException
  133. */
  134. public function tableItems()
  135. {
  136. return $this->with(['creator', 'updator'])
  137. ->where('is_delete', 0)
  138. ->field('id,title,admin_id,update_admin_id,sources,desc,thumb_img,sources,pub_status,is_top,create_time,update_time')
  139. ->order('id desc')
  140. ->paginate(15);
  141. }
  142. public function creator()
  143. {
  144. return $this->belongsTo(User::class, 'admin_id', 'store_user_id')
  145. ->field('store_user_id,user_name,real_name');
  146. }
  147. public function updator()
  148. {
  149. return $this->belongsTo(User::class, 'update_admin_id', 'store_user_id')
  150. ->field('store_user_id,user_name,real_name');
  151. }
  152. }