update
This commit is contained in:
@ -39,4 +39,14 @@ class NewsController extends AbstractController
|
||||
{
|
||||
return $render->render('news/insert');
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入新闻
|
||||
* @url /admin/news/insert
|
||||
*/
|
||||
#[RequestMapping(path: 'import', methods: 'get')]
|
||||
public function import(RenderInterface $render): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
return $render->render('news/import');
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ use App\Model\AppBrand;
|
||||
use App\Model\AppNews;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
use function Hyperf\Support\env;
|
||||
|
||||
#[Controller(prefix: 'admin/api/news')]
|
||||
class NewsController extends AbstractController
|
||||
@ -92,6 +93,57 @@ class NewsController extends AbstractController
|
||||
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'], [
|
||||
'<imageSlot>' => <<<EOF
|
||||
<p style="text-align:center"><img style="aspect-ratio: 4 / 3;max-width: 56%;" class="ue-image" src="$model->cover"></p>
|
||||
EOF
|
||||
]);
|
||||
$model->column_tag = $column;
|
||||
$model->save();
|
||||
}
|
||||
return $this->response->json(['code' => 0, 'msg' => 'ok']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除新新闻接口
|
||||
* @url /admin/api/news/delete
|
||||
|
Reference in New Issue
Block a user