SpecialController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\admin\controller\content;
  3. use app\controller\Curd;
  4. use app\model\Content;
  5. use support\exception\BusinessException;
  6. use support\Request;
  7. use support\Response;
  8. class SpecialController extends Curd
  9. {
  10. public function __construct()
  11. {
  12. $this->model = new Content();
  13. }
  14. public function select(Request $request): Response
  15. {
  16. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  17. $where['content_category'] = 'SPECIAL';
  18. $order = $request->get('order', 'desc');
  19. $field = $field ?? 'content_addtimes';
  20. $query = $this->doSelect($where, $field, $order);
  21. return $this->doFormat($query, $format, $limit);
  22. }
  23. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  24. {
  25. $model = $this->model->with([
  26. 'category' => function ($query) {
  27. $query->select('category_id', 'category_name');
  28. }
  29. ]);
  30. foreach ($where as $column => $value) {
  31. if (is_array($value)) {
  32. if ($value[0] === 'like' || $value[0] === 'not like') {
  33. $model = $model->where($column, $value[0], "%$value[1]%");
  34. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  35. $model = $model->where($column, $value[0], $value[1]);
  36. } elseif ($value[0] == 'in' && !empty($value[1])) {
  37. $valArr = $value[1];
  38. if (is_string($value[1])) {
  39. $valArr = explode(",", trim($value[1]));
  40. }
  41. $model = $model->whereIn($column, $valArr);
  42. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  43. $valArr = $value[1];
  44. if (is_string($value[1])) {
  45. $valArr = explode(",", trim($value[1]));
  46. }
  47. $model = $model->whereNotIn($column, $valArr);
  48. } elseif ($value[0] == 'null') {
  49. $model = $model->whereNull($column);
  50. } elseif ($value[0] == 'not null') {
  51. $model = $model->whereNotNull($column);
  52. } elseif ($value[0] !== '' || $value[1] !== '') {
  53. $model = $model->whereBetween($column, $value);
  54. }
  55. } else {
  56. $model = $model->where($column, $value);
  57. }
  58. }
  59. if ($field) {
  60. $model = $model->orderBy($field, $order);
  61. }
  62. return $model;
  63. }
  64. protected function afterQuery($items)
  65. {
  66. foreach ($items as &$item) {
  67. if (!empty($item->content_config_json)) {
  68. $configJson = json_decode($item->content_config_json, true);
  69. if (isset($configJson['cover'])) {
  70. $item->cover = getenv('STORAGE_DOMAIN') . $configJson['cover'];
  71. }
  72. }
  73. }
  74. return $items;
  75. }
  76. protected function insertInput(Request $request): array
  77. {
  78. $data = $this->inputFilter($request->post());
  79. $data['content_config_json'] = [];
  80. if (!empty($request->post('cover'))) {
  81. $configJson = !empty($model->content_config_json) ? json_decode($model->content_config_json, true) : [];
  82. $configJson['cover'] = str_replace(getenv("STORAGE_DOMAIN"), '', $request->post('cover'));
  83. $data['content_config_json'] = json_encode($configJson);
  84. }
  85. return $data;
  86. }
  87. protected function updateInput(Request $request): array
  88. {
  89. $primary_key = $this->model->getKeyName();
  90. $id = $request->post($primary_key);
  91. $data = $this->inputFilter($request->post());
  92. $model = $this->model->find($id);
  93. if (!$model) {
  94. throw new BusinessException('记录不存在', 2);
  95. }
  96. if (!empty($request->post('cover'))) {
  97. $configJson = !empty($model->content_config_json) ? json_decode($model->content_config_json, true) : [];
  98. $configJson['cover'] = str_replace(getenv("STORAGE_DOMAIN"), '', $request->post('cover'));
  99. $model->content_config_json = json_encode($configJson);
  100. }
  101. unset($data[$primary_key]);
  102. return [$id, $data];
  103. }
  104. }