SpecialController.php 4.2 KB

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