Article.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\store\model;
  3. use app\common\model\Article as ArticleModel;
  4. /**
  5. * 文章模型
  6. * Class Article
  7. * @package app\store\model
  8. */
  9. class Article extends ArticleModel
  10. {
  11. /**
  12. * 获取文章列表
  13. * @return \think\Paginator
  14. * @throws \think\exception\DbException
  15. */
  16. public function getList()
  17. {
  18. return $this->with(['image', 'category'])
  19. ->where('is_delete', '=', 0)
  20. ->order(['article_sort' => 'asc', 'create_time' => 'desc'])
  21. ->paginate(15, false, [
  22. 'query' => request()->request()
  23. ]);
  24. }
  25. /**
  26. * 新增记录
  27. * @param $data
  28. * @return false|int
  29. */
  30. public function add($data)
  31. {
  32. if (empty($data['image_id'])) {
  33. $this->error = '请上传封面图';
  34. return false;
  35. }
  36. if (empty($data['article_content'])) {
  37. $this->error = '请输入文章内容';
  38. return false;
  39. }
  40. $data['wxapp_id'] = self::$wxapp_id;
  41. return $this->allowField(true)->save($data);
  42. }
  43. /**
  44. * 更新记录
  45. * @param $data
  46. * @return bool|int
  47. */
  48. public function edit($data)
  49. {
  50. if (empty($data['image_id'])) {
  51. $this->error = '请上传封面图';
  52. return false;
  53. }
  54. if (empty($data['article_content'])) {
  55. $this->error = '请输入文章内容';
  56. return false;
  57. }
  58. return $this->allowField(true)->save($data) !== false;
  59. }
  60. /**
  61. * 软删除
  62. * @return false|int
  63. */
  64. public function setDelete()
  65. {
  66. return $this->save(['is_delete' => 1]);
  67. }
  68. /**
  69. * 获取文章总数量
  70. * @param array $where
  71. * @return int|string
  72. */
  73. public static function getArticleTotal($where = [])
  74. {
  75. $model = new static;
  76. !empty($where) && $model->where($where);
  77. return $model->where('is_delete', '=', 0)->count();
  78. }
  79. }