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
|
||||
|
@ -56,15 +56,4 @@ class ArticlesController extends AbstractController
|
||||
'message' => 'ok'
|
||||
];
|
||||
}
|
||||
|
||||
#[RequestMapping(path:'brand-search', methods: 'get')]
|
||||
public function brandSearch(): array
|
||||
{
|
||||
$input = $this->request->input('brand');
|
||||
$query = Model::query()->select(['name', 'cn_name', 'id'])->where('name', 'like', "%$input%")
|
||||
->orWhere('cn_name', 'like', "%$input%")
|
||||
->get()->toArray();
|
||||
|
||||
return ['code' => 0, 'msg' => 'ok', 'data' => $query];
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ namespace App\Model;
|
||||
* @property int $is_delete
|
||||
* @property string $source_url
|
||||
* @property string $source_platform
|
||||
* @property int $column_tag
|
||||
* @property-read mixed $created_at
|
||||
*/
|
||||
class AppNews extends Model
|
||||
@ -40,12 +41,14 @@ class AppNews extends Model
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['id' => 'integer', 'created_at' => 'datetime', 'created_by' => 'integer', 'updated_at' => 'datetime', 'updated_by' => 'integer', 'deleted_at' => 'integer', 'deleted_by' => 'integer', 'platform' => 'integer', 'is_record' => 'integer', 'is_delete' => 'integer'];
|
||||
protected array $casts = ['id' => 'integer', 'created_at' => 'datetime', 'created_by' => 'integer', 'updated_at' => 'datetime', 'updated_by' => 'integer', 'deleted_at' => 'integer', 'deleted_by' => 'integer', 'platform' => 'integer', 'is_record' => 'integer', 'is_delete' => 'integer', 'column_tag' => 'integer'];
|
||||
|
||||
protected ?string $dateFormat = 'U';
|
||||
|
||||
public function getCreatedAtAttribute($value)
|
||||
{
|
||||
return date('Y-m-d', intval($value));
|
||||
return $this->format('created_at', function (bool $isFormat) use ($value) {
|
||||
return $isFormat ? date('Y-m-d', intval($value)) : $value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace App\Rpc\v1;
|
||||
|
||||
use App\Model\AppNews;
|
||||
use App\Model\AppNewsColumn;
|
||||
use App\Rpc\BaseService;
|
||||
use Hyperf\RpcServer\Annotation\RpcService;
|
||||
|
||||
@ -28,6 +29,12 @@ class NewsService extends BaseService
|
||||
if (!$query) {
|
||||
return $this->getResponse()->setCode(404)->send();
|
||||
}
|
||||
|
||||
$query['created_at'] = date('Y-m-d', $query['created_at']);
|
||||
$columnTag = AppNewsColumn::find($query['column_tag']);
|
||||
$query['column_tag'] = $columnTag->name ?? '';
|
||||
$query['column_tag_url'] = $columnTag->url ?? '';
|
||||
|
||||
// 相关文章
|
||||
$query['about'] = AppNews::formatQuery(['created_at'])
|
||||
->where('platform', $isRelation ? current(self::RELATION[$app_id]) : $app_id)
|
||||
@ -55,7 +62,7 @@ class NewsService extends BaseService
|
||||
*/
|
||||
public function index(int $id, int $limit = 30, int $page = 1): array
|
||||
{
|
||||
$query = AppNews::formatQuery(['created_at'])
|
||||
$query = AppNews::formatQuery(['created_at', 'column_tag'])
|
||||
->where('is_delete', 0);
|
||||
|
||||
if (isset(self::RELATION[$id])) {
|
||||
@ -74,14 +81,22 @@ class NewsService extends BaseService
|
||||
|
||||
$value = AppNews::query()->whereIn('id', $ids)->orderBy('id', 'desc')->get()->toArray();
|
||||
|
||||
$about = AppNews::formatQuery(['created_at'])
|
||||
foreach ($value as &$item) {
|
||||
$item['created_at'] = date('Y-m-d', $item['created_at']);
|
||||
$columnTag = AppNewsColumn::find($item['column_tag']);
|
||||
$item['column_tag'] = $columnTag->name ?? '';
|
||||
$item['column_tag_url'] = $columnTag->url ?? '';
|
||||
}
|
||||
|
||||
$hot = AppNews::formatQuery(['created_at'])
|
||||
->where('platform', $id)
|
||||
->where('is_delete', 0)
|
||||
->select(['title', 'id', 'cover'])
|
||||
->limit(10)
|
||||
->limit(5)
|
||||
->orderBy('id', 'desc')
|
||||
->get()
|
||||
->toArray();
|
||||
return $this->getResponse()->setExtra('about', $about)->setExtra('total', ceil($total / $limit))->setData($value)->setCode(0)->send();
|
||||
|
||||
return $this->getResponse()->setExtra('hot', $hot)->setExtra('total', ceil($total / $limit))->setData($value)->setCode(0)->send();
|
||||
}
|
||||
}
|
173
storage/view/news/import.blade.php
Normal file
173
storage/view/news/import.blade.php
Normal file
@ -0,0 +1,173 @@
|
||||
<?php
|
||||
$platforms = call_user_func(function () {
|
||||
return \App\Model\AppWebsiteConfig::query()->where('is_delete', 0)->get()->toArray();
|
||||
});
|
||||
|
||||
$columnTag = call_user_func(function () {
|
||||
return \App\Model\AppNewsColumn::query()->get()->toArray();
|
||||
});
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-cn">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>导入</title>
|
||||
<link rel="stylesheet" href="/component/pear/css/pear.css" />
|
||||
<link rel="stylesheet" href="/admin/css/reset.css" />
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form class="layui-form" action="">
|
||||
<div class="mainBox">
|
||||
<div class="main-container mr-5">
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label required">发布平台</label>
|
||||
<div class="layui-input-block">
|
||||
<select name="platform" lay-search="">
|
||||
<option value="">请选择</option>
|
||||
<?php foreach ($platforms ?: [] as $platform): ?>
|
||||
<option value="<?= $platform['id'] ?>"><?= $platform['app_name'] ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label required">栏目</label>
|
||||
<div class="layui-input-block">
|
||||
<select name="column" lay-search="">
|
||||
<option value="">请选择</option>
|
||||
<?php foreach ($columnTag ?: [] as $tag): ?>
|
||||
<option value="<?= $tag['id'] ?>"><?= $tag['name'] ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="layui-form-item layui-form-text">
|
||||
<label class="layui-form-label">json数据</label>
|
||||
<div class="layui-input-block">
|
||||
<textarea name="content" placeholder="请输入内容" class="layui-textarea"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<div class="button-container">
|
||||
<button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="submit">
|
||||
提交
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<script src="/component/layui/layui.js?v=2.8.12"></script>
|
||||
<script src="/component/pear/pear.js"></script>
|
||||
<script>
|
||||
// api
|
||||
const SAVE_API = "/admin/api/news/import"; // 插入新闻
|
||||
const UPLOAD_API = '/upload/image' // 上传图片
|
||||
|
||||
layui.use(['upload', 'jquery', 'popup'], function(){
|
||||
const upload = layui.upload;
|
||||
const $ = layui.jquery;
|
||||
layui.form.on("submit(submit)", function (data) {
|
||||
layui.$.ajax({
|
||||
url: SAVE_API,
|
||||
type: "POST",
|
||||
dateType: "json",
|
||||
data: data.field,
|
||||
success: function (res) {
|
||||
|
||||
if (res.code) {
|
||||
return layui.popup.failure(res.msg);
|
||||
}
|
||||
return layui.popup.success("操作成功", function () {
|
||||
parent.refreshTable();
|
||||
parent.layer.close(parent.layer.getFrameIndex(window.name));
|
||||
});
|
||||
}
|
||||
});
|
||||
return false;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<style>
|
||||
#editor—wrapper {
|
||||
border: 1px solid #ccc;
|
||||
z-index: 100; /* 按需定义 */
|
||||
}
|
||||
#toolbar-container {
|
||||
border-bottom: 1px solid #ccc;
|
||||
}
|
||||
#editor-container {
|
||||
height: 500px;
|
||||
}
|
||||
</style>
|
||||
<style>
|
||||
.res-options {
|
||||
padding: 0 10px;
|
||||
line-height: 36px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
.layui-upload-list {
|
||||
margin-top: 20px;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, 120px);
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
.img-item {
|
||||
position: relative;
|
||||
border: 1px solid #e6e6e6;
|
||||
border-radius: 4px;
|
||||
cursor: move;
|
||||
}
|
||||
|
||||
.img-preview {
|
||||
width: 100%;
|
||||
height: 120px;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
.img-operate {
|
||||
position: absolute;
|
||||
top: 5px;
|
||||
right: 5px;
|
||||
opacity: 0;
|
||||
transition: .3s;
|
||||
}
|
||||
|
||||
.img-item:hover .img-operate {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
#images-tpl-container .layui-icon-close,
|
||||
#images-tpl-container .layui-icon-util {
|
||||
color: #fff;
|
||||
background: #ff5722;
|
||||
border-radius: 50%;
|
||||
padding: 2px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.layui-upload-list {
|
||||
/* 保持原有样式 */
|
||||
margin-top: 20px;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, 120px);
|
||||
gap: 15px;
|
||||
}
|
||||
/* 新增显示限制 */
|
||||
.img-item:nth-child(n+31) {
|
||||
display: none;
|
||||
}
|
||||
.load-more-btn {
|
||||
grid-column: 1 / -1;
|
||||
text-align: center;
|
||||
padding: 10px 0;
|
||||
}
|
||||
</style>
|
||||
</body>
|
||||
</html>
|
@ -63,9 +63,9 @@
|
||||
<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
|
||||
<i class="layui-icon layui-icon-add-1"></i>新增
|
||||
</button>
|
||||
{{-- <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">--}}
|
||||
{{-- <i class="layui-icon layui-icon-delete"></i>删除--}}
|
||||
{{-- </button>--}}
|
||||
<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="import">
|
||||
<i class="layui-icon layui-icon-file"></i>导入
|
||||
</button>
|
||||
</script>
|
||||
|
||||
<!-- 表格行工具栏 -->
|
||||
@ -97,6 +97,7 @@
|
||||
const VIEW_API = "/admin/news/view";
|
||||
const INSERT_API = "/admin/api/news/insert";
|
||||
const INSERT_URL = "/admin/news/insert";
|
||||
const IMPORT_URL = "/admin/news/import";
|
||||
const DELETE_API = "/admin/api/news/delete";
|
||||
|
||||
// 字段 创建时间 created_at
|
||||
@ -191,8 +192,8 @@
|
||||
add();
|
||||
} else if (obj.event === "refresh") {
|
||||
refreshTable();
|
||||
} else if (obj.event === "batchRemove") {
|
||||
batchRemove(obj);
|
||||
} else if (obj.event === "import") {
|
||||
importNews(obj);
|
||||
}
|
||||
});
|
||||
|
||||
@ -237,6 +238,17 @@
|
||||
});
|
||||
}
|
||||
|
||||
// 导入数据
|
||||
let importNews = function () {
|
||||
layer.open({
|
||||
type: 2,
|
||||
title: "导入",
|
||||
shade: 0.1,
|
||||
area: [common.isModile() ? "100%" : "98%", common.isModile() ? "100%" : "95%"],
|
||||
content: IMPORT_URL
|
||||
});
|
||||
}
|
||||
|
||||
// 表格编辑数据
|
||||
let edit = function (obj) {
|
||||
let value = obj.data['id'];
|
||||
|
Reference in New Issue
Block a user