ActivityController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 ActivityController 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'] = 'ACTIVITY';
  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('category');
  27. foreach ($where as $column => $value) {
  28. if (is_array($value)) {
  29. if ($value[0] === 'like' || $value[0] === 'not like') {
  30. $model = $model->where($column, $value[0], "%$value[1]%");
  31. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  32. $model = $model->where($column, $value[0], $value[1]);
  33. } elseif ($value[0] == 'in' && !empty($value[1])) {
  34. $valArr = $value[1];
  35. if (is_string($value[1])) {
  36. $valArr = explode(",", trim($value[1]));
  37. }
  38. $model = $model->whereIn($column, $valArr);
  39. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  40. $valArr = $value[1];
  41. if (is_string($value[1])) {
  42. $valArr = explode(",", trim($value[1]));
  43. }
  44. $model = $model->whereNotIn($column, $valArr);
  45. } elseif ($value[0] == 'null') {
  46. $model = $model->whereNull($column);
  47. } elseif ($value[0] == 'not null') {
  48. $model = $model->whereNotNull($column);
  49. } elseif ($value[0] !== '' || $value[1] !== '') {
  50. $model = $model->whereBetween($column, $value);
  51. }
  52. } else {
  53. $model = $model->where($column, $value);
  54. }
  55. }
  56. if ($field) {
  57. $model = $model->orderBy($field, $order);
  58. }
  59. return $model;
  60. }
  61. protected function afterQuery($items)
  62. {
  63. foreach ($items as &$item) {
  64. if (!empty($item->content_config_json)) {
  65. $configJson = json_decode($item->content_config_json, true);
  66. if (isset($configJson['cover'])) {
  67. $item->cover = getenv('STORAGE_DOMAIN') . $configJson['cover'];
  68. }
  69. }
  70. $item->content_main = htmlspecialchars_decode($item->content_main);
  71. }
  72. return $items;
  73. }
  74. protected function insertInput(Request $request): array
  75. {
  76. $data = $this->inputFilter($request->post());
  77. if (!empty($request->post('cover'))) {
  78. $configJson['cover'] = str_replace(getenv("STORAGE_DOMAIN"), '', $request->post('cover'));
  79. $data['content_config_json'] = json_encode($configJson);
  80. }
  81. $data['content_updatetimes'] = time();
  82. $data['creator_user_id'] = JwtToken::getCurrentId();
  83. return $data;
  84. }
  85. protected function updateInput(Request $request): array
  86. {
  87. $primary_key = $this->model->getKeyName();
  88. $id = $request->post($primary_key);
  89. $data = $this->inputFilter($request->post());
  90. $data['content_updatetimes'] = time();
  91. $model = $this->model->find($id);
  92. if (!$model) {
  93. throw new BusinessException('记录不存在', 2);
  94. }
  95. if (!empty($request->post('cover'))) {
  96. $configJson = !empty($model->content_config_json) ? json_decode($model->content_config_json, true) : [];
  97. $configJson['cover'] = str_replace(getenv("STORAGE_DOMAIN"), '', $request->post('cover'));
  98. $data['content_config_json'] = json_encode($configJson);
  99. }
  100. unset($data[$primary_key]);
  101. return [$id, $data];
  102. }
  103. }