Article.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\admin\model\article;
  3. use app\admin\model\CategoryLangs;
  4. use app\common\model\Category;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\ModelNotFoundException;
  7. use think\Exception;
  8. use think\exception\DbException;
  9. use think\Model;
  10. use traits\model\SoftDelete;
  11. class Article extends Model
  12. {
  13. use SoftDelete;
  14. // 表名
  15. protected $name = 'article';
  16. // 自动写入时间戳字段
  17. protected $autoWriteTimestamp = 'int';
  18. // 定义时间戳字段名
  19. protected $createTime = 'createtime';
  20. protected $updateTime = 'updatetime';
  21. protected $deleteTime = 'deletetime';
  22. // 追加属性
  23. protected $append = [
  24. 'status_text'
  25. ];
  26. public static function validWhere($alias = '')
  27. {
  28. $str = '';
  29. if ($alias) {
  30. $str = $alias . '.';
  31. }
  32. return self::where($str . 'status', '1')->where($str . 'showswitch', 1);
  33. }
  34. protected static function init()
  35. {
  36. self::afterInsert(function ($row) {
  37. $pk = $row->getPk();
  38. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  39. });
  40. }
  41. public function getStatusList()
  42. {
  43. return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
  44. }
  45. public function getStatusTextAttr($value, $data)
  46. {
  47. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  48. $list = $this->getStatusList();
  49. return isset($list[$value]) ? $list[$value] : '';
  50. }
  51. /**
  52. * @param $where
  53. * @return array
  54. * @throws Exception
  55. * @throws DataNotFoundException
  56. * @throws ModelNotFoundException
  57. * @throws DbException
  58. */
  59. public static function getValidArticleList($where)
  60. {
  61. $model = self::validWhere();
  62. if (isset($where['title']) && $where['title'] != '') {
  63. $ids = ArticleLangs::where('title|synopsis', 'like', "%{$where['title']}%")->column('article_id');
  64. $ids2 = self::where('title|synopsis', 'like', "%{$where['title']}%")->column('id');
  65. $all_ids = array_merge($ids, $ids2);
  66. $model->where('id', 'in', $all_ids);
  67. }
  68. if (isset($where['category_id']) && $where['category_id'] != '') {
  69. $model->where('find_in_set(' . $where['category_id'] . ',category_ids)');
  70. }
  71. $count = $model->count();
  72. $model = self::validWhere();
  73. if (isset($where['title']) && $where['title'] != '') {
  74. $ids = ArticleLangs::where('title|synopsis', 'like', "%{$where['title']}%")->column('article_id');
  75. $ids2 = self::where('title|synopsis', 'like', "%{$where['title']}%")->column('id');
  76. $all_ids = array_merge($ids, $ids2);
  77. $model->where('id', 'in', $all_ids);
  78. }
  79. if (isset($where['category_id']) && $where['category_id'] != '') {
  80. $model->where('find_in_set(' . $where['category_id'] . ',category_ids)');
  81. }
  82. $model = $model->order('weigh', 'desc')
  83. ->field('id,title,coverimage,category_ids,visit,synopsis,author,createtime,updatetime,content');
  84. $list = $model->page((int)($where['page'] ?? 1), (int)($where['limit'] ?? 10))
  85. ->select();
  86. // var_dump($list);
  87. //var_dump(self::getLastSql());
  88. foreach ($list as &$item) {
  89. $cate = function ($item) use ($where) {
  90. $cates = Category::where('id', 'in', $item['category_ids'])->column('id');
  91. $cate_item = [];
  92. foreach ($cates as $v) {
  93. $cate = Category::where('id', $v)->value('name');
  94. if (isset($where['lang']) && $where['lang']) {
  95. $info = CategoryLangs::where('category_id', $v)->where('langlist', $where['lang'])->find();
  96. if ($info) {
  97. $cate = $info['name'];
  98. }
  99. }
  100. $cate_item[] = $cate;
  101. }
  102. $item['cate'] = $cate_item;
  103. if (isset($where['lang']) && $where['lang']) {
  104. $info = ArticleLangs::where('article_id', $item['id'])->where('langlist', $where['lang'])->find();
  105. if ($info) {
  106. $item['title'] = $info['title'];
  107. $item['synopsis'] = $info['synopsis'];
  108. $item['content'] = $info['content'];
  109. }
  110. }
  111. };
  112. $cate($item);
  113. }
  114. return compact('list', 'count');
  115. }
  116. /**
  117. * @param $id
  118. * @param string $lang
  119. * @param bool $add_visit
  120. * @return array|false|\PDOStatement|string|Model
  121. * @throws DataNotFoundException
  122. * @throws DbException
  123. * @throws ModelNotFoundException
  124. */
  125. public static function getValidArticle($id, $lang = '', $add_visit = false)
  126. {
  127. $model = self::validWhere();
  128. $info = $model->where('id', $id)->find();
  129. if ($info && $add_visit) {
  130. $info->hidden(['showswitch', 'weigh', 'status', 'admin_id', 'deletetime']);
  131. $info['visit'] = $info['visit'] + 1;
  132. $info->save();
  133. $cates = Category::where('id', 'in', $info['category_ids'])->column('id');
  134. $info_cate = [];
  135. foreach ($cates as $v) {
  136. $cate = Category::where('id', $v)->value('name');
  137. if ($lang) {
  138. $lang_info = CategoryLangs::where('category_id', $v)->where('langlist', $lang)->find();
  139. if ($lang_info) {
  140. $cate = $lang_info['name'];
  141. }
  142. }
  143. $info_cate[] = $cate;
  144. }
  145. $info['cate'] = $info_cate;
  146. if ($lang) {
  147. $lang_info = ArticleLangs::where('article_id', $info['id'])->where('langlist', $lang)->find();
  148. if ($lang_info) {
  149. $info['title'] = $lang_info['title'];
  150. $info['synopsis'] = $lang_info['synopsis'];
  151. $info['content'] = $lang_info['content'];
  152. }
  153. }
  154. }
  155. return $info;
  156. }
  157. }