<?php

namespace app\controller\v2;

use app\extra\basic\Base;
use app\extra\service\blue\TaskService;
use app\middleware\WxMiddleware;
use app\model\blue\BlueBanner;
use app\model\blue\BlueCategory;
use DI\Attribute\Inject;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Middleware;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;


#[Controller(prefix: "/wx_v2/portal"),Middleware(WxMiddleware::class)]
class Portal extends Base
{


    protected array $noNeedLogin = ['getPortal'];

    #[Inject]
    protected TaskService $service;

    /**
     * 小程序首页信息
     * @param Request $request
     * @return Response
     */
    #[Route(path: "home",methods: "get")]
    public function getPortal(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "category.default"  => 0,
                "size.default"      => 10,
                "page.default"      => 1,
            ],$request->method());
            $banner = (new BlueBanner)->where("status",1)->field("title,img,path")->select()->toArray();
            $category = (new BlueCategory)->where("type",1)->field("name,icon,small_icon,sign")->append(['id'])->withAttr(['id' => function($data,$resp){
                return $resp['sign'];
            }])->select()->toArray();
            if (empty($param['category']) || $param['category'] == 0) {
                $param['category'] = $category[0]['id'];
            }
            $share = [
                "image" => sConf("wechat.share"),
                "title" => sConf("wechat.share_title")
            ];
            $rank = [];
            $star = [];
            $list = $this->service->getHomeList(['category' => $param['category'],'pageSize' => $param['size'],'page' => $param['page']]);
            $task = pageFormat($list);
            return success("ok",compact("banner","category",'task','share','rank','star'));
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

}