ArticleController.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\validate\sys_manage\AdvValidate;
  4. use app\admin\validate\sys_manage\ArticleValidate;
  5. use app\admin\validate\sys_manage\ContentValidate;
  6. use app\controller\Curd;
  7. use app\model\Adv;
  8. use app\model\Article;
  9. use app\model\Content;
  10. use support\Request;
  11. use support\Response;
  12. class ArticleController extends Curd
  13. {
  14. public function __construct()
  15. {
  16. $this->model = new Content();
  17. $this->validate = true;
  18. $this->validateClass = new ContentValidate();
  19. }
  20. /** 列表
  21. * @Desc
  22. * @Author Gorden
  23. * @Date 2024/3/5 10:00
  24. *
  25. * @param Request $request
  26. * @return Response
  27. * @throws \support\exception\BusinessException
  28. */
  29. public function select(Request $request): Response
  30. {
  31. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  32. $where['content_category'] = 'ARTICLE';
  33. $order = $request->get('order', 'desc');
  34. $field = $field ?? 'content_addtimes';
  35. $query = $this->doSelect($where, $field, $order);
  36. return $this->doFormat($query, $format, $limit);
  37. }
  38. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  39. {
  40. $model = $this->model->with('category');
  41. foreach ($where as $column => $value) {
  42. if (is_array($value)) {
  43. if ($value[0] === 'like' || $value[0] === 'not like') {
  44. $model = $model->where($column, $value[0], "%$value[1]%");
  45. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  46. $model = $model->where($column, $value[0], $value[1]);
  47. } elseif ($value[0] == 'in' && !empty($value[1])) {
  48. $valArr = $value[1];
  49. if (is_string($value[1])) {
  50. $valArr = explode(",", trim($value[1]));
  51. }
  52. $model = $model->whereIn($column, $valArr);
  53. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  54. $valArr = $value[1];
  55. if (is_string($value[1])) {
  56. $valArr = explode(",", trim($value[1]));
  57. }
  58. $model = $model->whereNotIn($column, $valArr);
  59. } elseif ($value[0] == 'null') {
  60. $model = $model->whereNull($column);
  61. } elseif ($value[0] == 'not null') {
  62. $model = $model->whereNotNull($column);
  63. } elseif ($value[0] !== '' || $value[1] !== '') {
  64. $model = $model->whereBetween($column, $value);
  65. }
  66. } else {
  67. $model = $model->where($column, $value);
  68. }
  69. }
  70. if ($field) {
  71. $model = $model->orderBy($field, $order);
  72. }
  73. return $model;
  74. }
  75. /**
  76. * @Desc
  77. * @Author Gorden
  78. * @Date 2024/3/27 10:24
  79. *
  80. * @param Request $request
  81. * @return array
  82. * @throws \support\exception\BusinessException
  83. */
  84. protected function insertInput(Request $request): array
  85. {
  86. $data = $this->inputFilter($request->post());
  87. $data['content_category'] = 'ARTICLE';
  88. return $data;
  89. }
  90. }