request->query('created_at', [date('Y-m-d', strtotime('-1 month')), date('Y-m-d', time())]); $title = $this->request->input('title', ''); foreach ($createdFilter as $index => &$item) { if ($index == 0) { $item = strtotime($item . ' 00:00:00'); } if ($index == 1) { $item = strtotime($item . ' 23:59:59'); } } $ids = []; $query = AppNews::query() ->where('is_delete', 0) ->select(['id']) ->when($title, fn($q) => $q->where('title', 'like', "%{$title}%")) ->whereBetween('created_at', $createdFilter) ->orderBy('id', 'desc'); $pagination = $query->paginate($this->request->input('limit', 10), page: $this->request->input('page')); foreach ($pagination->items() as $item) { $ids[] = $item->id; } $value = AppNews::query()->whereIn('id', $ids)->orderBy('id', 'desc')->get()->toArray(); // $value = AppNews::find($ids, ['title', 'is_record'])->toArray(); return ['code' => 0, 'msg' => 'ok', 'count' => $pagination->total(), 'data' => $value]; } /** * 查看文章详情信息 * @url /admin/api/news/view */ #[RequestMapping(path:'view', methods: 'get')] public function view(): \Psr\Http\Message\ResponseInterface { $query = AppNews::query() ->where('id', $this->request->query('id')); return $this->response->json(['code' => 0, 'msg' => 'ok', 'data' => $query->first()]); } /** * 更新文章内容 * @url /admin/api/news/update */ #[RequestMapping(path:'update', methods: 'post')] public function update() { $model = new NewsFormModel(); $model->setAttributes($this->request->post()); $model->update(); return $this->response->json(['code' => 0, 'msg' => 'ok']); } /** * 新增新新闻接口 * @url /admin/api/news/insert */ #[RequestMapping(path:'insert', methods: 'post')] public function insert() { $model = new NewsFormModel(); $model->setAttributes($this->request->post(), ['title', 'keywords', 'description', 'cover', 'content', 'platform']); $model->insert(); return $this->response->json(['code' => 0, 'msg' => 'ok']); } /** * 导入 * [{title:'',desc:'',keywords:'',content:''}] * @url /admin/api/news/insert */ #[RequestMapping(path:'import', methods: 'post')] public function import() { $content = $this->request->post('content', null); $platform = $this->request->post('platform', 0); $column = $this->request->post('column', 0); // 随机图片 $directory = env('COVER_ROOT'); // 文件夹路径 $images = glob($directory . '/*.{jpg,jpeg,png,gif,bmp}', GLOB_BRACE); // 获取图片文件 $imagesArr = []; if (count($images) > 0) { foreach ($images as $image) { $imagesArr[] = basename($image); } } if (!$imagesArr) { return $this->response->json(['code' => 400, 'msg' => 'not content.']); } if (!$content) { return $this->response->json(['code' => 400, 'msg' => 'not content.']); } $content = json_decode($content, true); foreach ($content as $item) { $randIndex = mt_rand(1,count($imagesArr)); $cover = $imagesArr[$randIndex]; $model = new AppNews(); $model->title = is_array($item['title']) ? current($item['title']) : $item['title']; $model->description = $item['desc']; $model->keywords = $item['keywords']; $model->platform = $platform; $model->cover = env('APP_DOMAIN') . '/uploads/cover/' . $cover; $model->content = strtr($item['content'], [ '' => <<

EOF ]); $model->column_tag = $column; $model->save(); } return $this->response->json(['code' => 0, 'msg' => 'ok']); } /** * 删除新新闻接口 * @url /admin/api/news/delete */ #[RequestMapping(path:'delete', methods: 'post')] public function delete(): \Psr\Http\Message\ResponseInterface { $id = $this->request->post('id'); $query = AppNews::find($id); $query->is_delete = 1; $query->deleted_at = time(); $query->save(); return $this->response->json(['code' => 0, 'msg' => 'ok']); } }