ClassifyController.php 6.0 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\WeChatBaseController;
use think\Db;

class ClassifyController extends WeChatBaseController{

    /**
     * 显示分类页
     */
    public function classify(){

        //所有分类
        $data_classify = Db::name('classification') -> select();
        //所有标签
        $data_label = Db::name('label') -> select();
        $classify_id = $this -> request -> param();
        if($classify_id){
            //获取指定分类商品
            $data_goods = Db::name('goods') -> alias('a') -> field("a.*,b.name") -> join('classification b','a.classify_id = b.id','LEFT') -> where("a.type = 1 and a.is_out = 1 and a.classify_id =".$classify_id['classify_id']) -> select() -> toArray();
            $this -> assign('class',$classify_id['classify_id']);
        }else{
            //获取默认分类商品
            $data_goods = Db::name('goods') -> alias('a') -> field("a.*,b.name") -> join('classification b','a.classify_id = b.id','LEFT') -> where("a.type = 1 and a.is_out = 1 and a.classify_id =".$data_classify[0]['id']) -> select() -> toArray();
            $this -> assign('class',$data_classify[0]['id']);
        }
        foreach($data_goods as $key => $val){
            $price = explode('.',$data_goods[$key]['price']);
            $data_goods[$key]['price0'] = $price[0];
            $data_goods[$key]['price01'] = $price[1];
        }
        $this -> assign('data_goods',$data_goods);
        $this -> assign('data_label',$data_label);
        $this -> assign('data_classify',$data_classify);
        return $this -> fetch();

    }

    /**
     * 分类下的商品
     */
    public function classify_goods(){

            $classify_id = $_POST['classify_id'];
            $data_classify_goods = Db::name('goods') -> alias('a') -> field("a.*,b.name") -> join('classification b','a.classify_id = b.id','LEFT') -> where("a.type = 1 and a.is_out = 1 and a.classify_id =".$classify_id) -> select() -> toArray();
        foreach($data_classify_goods as $key => $val){
            $price = explode('.',$data_classify_goods[$key]['price']);
            $data_classify_goods[$key]['price0'] = $price[0];
            $data_classify_goods[$key]['price01'] = $price[1];
            $data_classify_goods[$key]['show_img'] = cmf_get_image_url($data_classify_goods[$key]['show_img']);
        }
        return json_encode($data_classify_goods);

    }

    /**
     * 分类标签下的商品
     */
    public function classify_lable_goods(){

        $label_id = $_POST['label_id'];
        $classify_id = $_POST['classify_id'];
        $data_classify_lable_goods = Db::name('goods') -> alias('a') -> field("a.*") -> join('goods_label b','a.id = b.goods_id','LEFT') -> where("a.type = 1 and a.is_out = 1 and a.classify_id =".$classify_id." and b.label_id =".$label_id) -> select() -> toArray();
        $data_classify_name = Db::name('classification') -> where('id',$classify_id) -> find();
        foreach($data_classify_lable_goods as $key => $val){
            $price = explode('.',$data_classify_lable_goods[$key]['price']);
            $data_classify_lable_goods[$key]['price0'] = $price[0];
            $data_classify_lable_goods[$key]['price01'] = $price[1];
            $data_classify_lable_goods[$key]['show_img'] = cmf_get_image_url($data_classify_lable_goods[$key]['show_img']);
            $data_classify_lable_goods[$key]['name'] = $data_classify_name['name'];
        }
        return json_encode($data_classify_lable_goods);

    }

    /**
     * 销量排序
     */
    public function classify_sales(){

        $where = [
            'a.type' => 1,
            'a.is_out' => 1
        ];
        if(!empty($_POST['classify_id'])){
            $where['a.classify_id'] = $_POST['classify_id'];
        }
        if(!empty($_POST['lable_id'])){
            $where['b.label_id'] = $_POST['lable_id'];
        }
        $data = Db::name('goods') -> alias('a') -> field("a.*") -> join('goods_label b','a.id = b.goods_id','LEFT') -> where($where) -> order('a.sales desc') -> select() -> toArray();
        $data_classify_name = Db::name('classification') -> where('id',$_POST['classify_id']) -> find();
        foreach($data as $key => $val){
            $price = explode('.',$data[$key]['price']);
            $data[$key]['price0'] = $price[0];
            $data[$key]['price01'] = $price[1];
            $data[$key]['show_img'] = cmf_get_image_url($data[$key]['show_img']);
            $data[$key]['name'] = $data_classify_name['name'];
        }
        return json_encode($data);

    }

    /**
     * 价格排序
     */
    public function classify_price(){

        $where = [
            'a.type' => 1,
            'a.is_out' => 1
        ];
        if(!empty($_POST['classify_id'])){
            $where['classify_id'] = $_POST['classify_id'];
        }
        if(!empty($_POST['lable_id'])){
            $where['label_id'] = $_POST['lable_id'];
        }
        $data = Db::name('goods') -> alias('a') -> field("a.*") -> join('goods_label b','a.id = b.goods_id','LEFT') -> where($where) -> order('a.price desc') -> select() -> toArray();
        $data_classify_name = Db::name('classification') -> where('id',$_POST['classify_id']) -> find();
        foreach($data as $key => $val){
            $price = explode('.',$data[$key]['price']);
            $data[$key]['price0'] = $price[0];
            $data[$key]['price01'] = $price[1];
            $data[$key]['show_img'] = cmf_get_image_url($data[$key]['show_img']);
            $data[$key]['name'] = $data_classify_name['name'];
        }
        return json_encode($data);

    }

















}