Article.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\model;
  13. use app\common\model\Article as ArticleModel;
  14. /**
  15. * 文章模型
  16. * Class Article
  17. * @package app\store\model
  18. */
  19. class Article extends ArticleModel
  20. {
  21. /**
  22. * 获取列表
  23. * @param array $param
  24. * @return \think\Paginator
  25. * @throws \think\db\exception\DbException
  26. */
  27. public function getList(array $param = []): \think\Paginator
  28. {
  29. // 查询参数
  30. $params = $this->setQueryDefaultValue($param, [
  31. 'title' => '', // 文章标题
  32. 'categoryId' => 0, // 文章分类id
  33. 'status' => -1, // 文章状态
  34. ]);
  35. // 检索查询条件
  36. $filter = [];
  37. // 文章标题
  38. !empty($params['title']) && $filter[] = ['title', 'like', "%{$params['title']}%"];
  39. // 文章分类id
  40. $params['categoryId'] > 0 && $filter[] = ['category_id', '=', $params['categoryId']];
  41. // 文章状态
  42. $params['status'] > -1 && $filter[] = ['status', '=', $params['status']];
  43. // 查询列表数据
  44. return $this->with(['image', 'category'])
  45. ->withoutField(['content'])
  46. ->where($filter)
  47. ->where('is_delete', '=', 0)
  48. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  49. ->paginate(15);
  50. }
  51. /**
  52. * 新增记录
  53. * @param array $data
  54. * @return bool
  55. */
  56. public function add(array $data): bool
  57. {
  58. if (empty($data['image_id'])) {
  59. $this->error = '请上传封面图';
  60. return false;
  61. }
  62. if (empty($data['content'])) {
  63. $this->error = '请输入文章内容';
  64. return false;
  65. }
  66. $data['store_id'] = self::$storeId;
  67. return $this->save($data);
  68. }
  69. /**
  70. * 更新记录
  71. * @param array $data
  72. * @return bool
  73. */
  74. public function edit(array $data): bool
  75. {
  76. if (empty($data['image_id'])) {
  77. $this->error = '请上传封面图';
  78. return false;
  79. }
  80. if (empty($data['content'])) {
  81. $this->error = '请输入文章内容';
  82. return false;
  83. }
  84. return $this->save($data) !== false;
  85. }
  86. /**
  87. * 软删除
  88. * @return bool
  89. */
  90. public function setDelete(): bool
  91. {
  92. return $this->save(['is_delete' => 1]);
  93. }
  94. /**
  95. * 获取文章总数量
  96. * @param array $where
  97. * @return int
  98. */
  99. public static function getArticleTotal(array $where = []): int
  100. {
  101. return (new static)->where($where)->where('is_delete', '=', 0)->count();
  102. }
  103. }