hahn-admin/app/controller/IndexController.php

114 lines
4.0 KiB
PHP

<?php
namespace app\controller;
use plugin\admin\app\model\Link;
use plugin\admin\app\model\Option;
use plugin\admin\app\model\Product;
use plugin\admin\app\model\ProductCate;
use support\Redis;
use support\Request;
use support\Response;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\ServerSentEvents;
use Workerman\Timer;
class IndexController extends BaseController
{
//首页seo
public function index(): Response
{
$config = Option::where(['name' => 'system_config'])->value('value');
$config = json_decode($config, true);
return view("index", ['logo' => $config['logo']]);
}
public function header(Request $request): Response
{
$connection = $request->connection;
$id = Timer::add(1, function () use ($connection, &$id) {
if ($connection->getStatus() != TcpConnection::STATUS_ESTABLISHED) {
Timer::del($id);
}
$connection->send(new ServerSentEvents(["data" => json_encode(['date'=>date('Y-m-d H:i:s')])]));
});
return \response("", 200, [
'Content-Type' => 'text/event-stream',
'Cache-Control' => 'no-cache',
'Connection' => 'keep-alive',
]);
}
public function sse()
{
return view("sse");
}
//获取配置参数
public function getOptions(): Response
{
$options = Option::where(['name' => 'system_config'])->value('value');
$config = json_decode($options, true);
$logo = $config['logo'];
unset($logo["title"]);
$config['banner'] = ImgSrcByArr($config['banner']);
foreach ($config['banner'] as &$banner) {
$banner = explode(",", $banner);
}
$link = Link::orderBy("id", "asc")->select(["title", "url"])->get();
return Success(['options' => ImgSrcByArr($logo), 'banner' => $config['banner'], 'link' => $link]);
}
//产品分类 && 重磅新品
public function getProductCate(): Response
{
$cate = ProductCate::where(['pid' => 0])->orderBy("id")->select(["id", "title", "e_title"])->get()->toArray();
$firstCate = reset($cate);
$productList = Product::where(['cid' => $firstCate['id']])->select(["cover", "name", "label", "new", "url", "property", "corner_mark"])->get();
$childrenCate = ProductCate::whereIn("pid", array_column($cate, 'id'))->select(["id", "title", "pid", "e_title"])->orderBy("id")->get();
$childrenList = [];
foreach ($childrenCate as $item) {
$childrenList[$item['pid']][] = $item;
}
foreach ($productList as $product) {
$cover = explode(",", $product->cover);
foreach ($cover as &$item) {
$item = ImgSrc($item);
}
$product->cover = $cover;
}
foreach ($cate as &$c) {
$c['children'] = $childrenList[$c['id']] ?? [];
}
return Success(['cate' => $cate, 'productList' => $productList]);
}
//产品列表0
public function getProductList(Request $request): Response
{
$cid = $request->get('cid', 0);
$cate = ProductCate::where(['id' => $cid])->first();
if (!$cate) return Error("参数异常!");
$cateList = [];
if ($cate->pid == 0) {
$cateList = ProductCate::where(['pid' => $cate->id])->pluck("id")->toArray();
}
$cateList[] = $cate->id;
$projectList = Product::whereIn("cid", $cateList)->select(["cover", "name", "label", "new", "url", "property", "corner_mark"])->orderBy("id", "desc")->paginate($request->get("per_page", 6));
foreach ($projectList->items() as &$item) {
$item->cover = ImgSrc($item->cover);
$item->cover = explode(",", $item->cover);
}
return Success([
'data' => $projectList->items(),
'total' => $projectList->total(),
'current_page' => $projectList->currentPage(),
'last_page' => $projectList->lastPage(),
]);
}
}