Article.php 2.1 KB

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