NewsController.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 NewsController 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'] = ['in',['NEWS','FINANCE','APARTMENT']];
  33. $where['content_category'] = 'NEWS';
  34. $order = $request->get('order', 'desc');
  35. $field = $field ?? 'content_addtimes';
  36. $query = $this->doSelect($where, $field, $order);
  37. return $this->doFormat($query, $format, $limit);
  38. }
  39. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  40. {
  41. $model = $this->model->with('category');
  42. foreach ($where as $column => $value) {
  43. if (is_array($value)) {
  44. if ($value[0] === 'like' || $value[0] === 'not like') {
  45. $model = $model->where($column, $value[0], "%$value[1]%");
  46. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  47. $model = $model->where($column, $value[0], $value[1]);
  48. } elseif ($value[0] == 'in' && !empty($value[1])) {
  49. $valArr = $value[1];
  50. if (is_string($value[1])) {
  51. $valArr = explode(",", trim($value[1]));
  52. }
  53. $model = $model->whereIn($column, $valArr);
  54. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  55. $valArr = $value[1];
  56. if (is_string($value[1])) {
  57. $valArr = explode(",", trim($value[1]));
  58. }
  59. $model = $model->whereNotIn($column, $valArr);
  60. } elseif ($value[0] == 'null') {
  61. $model = $model->whereNull($column);
  62. } elseif ($value[0] == 'not null') {
  63. $model = $model->whereNotNull($column);
  64. } elseif ($value[0] !== '' || $value[1] !== '') {
  65. $model = $model->whereBetween($column, $value);
  66. }
  67. } else {
  68. $model = $model->where($column, $value);
  69. }
  70. }
  71. if ($field) {
  72. $model = $model->orderBy($field, $order);
  73. }
  74. return $model;
  75. }
  76. /**
  77. * @Desc
  78. * @Author Gorden
  79. * @Date 2024/3/27 10:24
  80. *
  81. * @param Request $request
  82. * @return array
  83. * @throws \support\exception\BusinessException
  84. */
  85. protected function insertInput(Request $request): array
  86. {
  87. $data = $this->inputFilter($request->post());
  88. $data['content_category'] = 'NEWS';
  89. return $data;
  90. }
  91. }