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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user