<?php

namespace app\api\controller;

use app\admin\model\CategoryLangs;
use app\common\controller\Api;
use app\common\model\Category;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $loopProduct = \app\admin\model\product\Product::validWhere()->where('loopswitch', 1)->select();
        $recommendProduct = \app\admin\model\product\Product::validWhere()->where('recommendswitch', 1)->select();
        $newProduct = \app\admin\model\product\Product::validWhere()->where('newswitch', 1)->select();
        $cases = \app\admin\model\cases\Cases::validWhere()->where('indexswitch', 1)->select();
        $banners = $this->site['banners'];
        $company_cover = $this->site['company_cover'];
        $about_us = $this->site['about_us'];
        $this->success('请求成功', compact('company_cover', 'about_us', 'banners', 'cases', 'loopProduct', 'newProduct', 'recommendProduct'));
    }

    public function cates($type = '')
    {
        $pid = $this->request->get('pid', 0);
        $list = Category::getCategoryArray($type, 'normal', $pid);
//        var_dump($list);
        if ($this->lang)
            foreach ($list as &$v) {
                $info = CategoryLangs::where('category_id', $v['id'])->where('langlist', $this->lang)->find();
                if ($info) {
                    $v['name'] = $info['name'];
                }
            }
        $this->success('请求成功', $list);
    }
}
