IndexController.php 14.8 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 IndexController extends WeChatBaseController
{
    /**
     * 显示首页
     */
    public function index(){

        //显示分类
        $data_classif= Db::name('classification') -> select();
        $this -> assign('data_classif',$data_classif);
        //热门推荐
        $data_goods_hot = Db::name('goods') -> alias('a') -> field('a.*,b.name') -> join('classification b','a.classify_id = b.id','LEFT') -> where("recommend_hot=1 and type = 1") -> select() -> toArray();
        if(!empty($data_goods_hot)){
            foreach($data_goods_hot as $key => $val){
                $price = explode('.',$data_goods_hot[$key]['price']);
                $data_goods_hot[$key]['price0'] = $price[0];
                $data_goods_hot[$key]['price1'] = $price[1];
            }
        }
        //判断用户是否存在
        $uid = cmf_get_current_user_id();
        $data_my_user = Db::name('my_user') -> where('uid',$uid) -> find();
        if($data_my_user){
            //用户是否为自由人 是自由人则绑定 不是自由人判断是不是老师
            if($data_my_user['status'] == 0 || $data_my_user['status'] == 1 || $data_my_user['status'] == 5 || $data_my_user['status'] == 6){
                $data_id = $this -> request -> param();
                //判断跳转链接是否存在数据 存在则绑定关系
                if(!empty($data_id)){
                    //判断数据是老师还是业务员
                    if($data_id['status_id'] == 2){
                        //业务员分享 更改用户身份为老师
                        $my_user_phone = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                        $data_update['status'] = 3;
                        $data_update['inviter_phone'] = $my_user_phone['phone'];
                        $data_update['is_pro'] = 1;
                        $data_update['pid'] = $my_user_phone['id'];
                        $data_update['bind_time'] = time();
                        $data_update['bind_status'] = 1;
                        Db::name('my_user') -> where('uid',$uid) -> update($data_update);
                        $data_goods = Db::name('goods') -> where('uid',$my_user_phone['uid']) -> select();
                    }elseif ($data_id['status_id'] == 3){
                        //老师分享 更改用户身份为学生
                        $my_user_phone = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                        $data_update['status'] = 4;
                        $data_update['inviter_phone'] = $my_user_phone['phone'];
                        $data_update['is_pro'] = 0;
                        $data_update['pid'] = $my_user_phone['id'];
                        $data_update['bind_time'] = time();
                        $data_update['bind_status'] = 1;
                        Db::name('my_user') -> where('uid',$uid) -> update($data_update);
                        $salesman_id = Db::name('my_user') -> where('id',$my_user_phone['pid']) -> find();
                        $data_goods = Db::name('goods') -> where('uid',$salesman_id['uid']) -> select();
                    }
                }
            }else{
                $data_id = $this -> request -> param();
                //判断跳转链接是否存在数据
                if(!empty($data_id)){
                    //判断用户是不是老师
                    if($data_my_user['status'] == 3){
                        //是老师则判断老师绑定状态
                        if($data_my_user['bind_status'] = 2){
                            //判断是否为业务员分享的链接
                            if($data_id['status_id'] == 2){
                                //是业务员分享则更新老师的pid
                                $my_user_phone = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                                $data_update['pid'] = $my_user_phone['id'];
                                $data_update['bind_time'] = time();
                                $data_update['bind_status'] = 1;
                                Db::name('my_user') -> where('uid',$uid) -> update($data_update);
                                $data_goods = Db::name('goods') -> where('uid',$my_user_phone['uid']) -> select();
                            }else{
                                $salesman_id = Db::name('my_user') -> where('id',$data_my_user['pid']) -> find();
                                $data_goods = Db::name('goods') -> where('uid',$salesman_id['uid']) -> select();
                            }
                        }else{
                            $salesman_id = Db::name('my_user') -> where('id',$data_my_user['pid']) -> find();
                            $data_goods = Db::name('goods') -> where('uid',$salesman_id['uid']) -> select();
                        }
                    }else{
                        //当为业务员时显示的数据
                        if($data_my_user['status'] == 2){
                            $data_goods = Db::name('goods') -> where('uid',$data_my_user['uid']) -> select();
                        }
                        //当为学生时显示的数据
                        if($data_my_user['status'] == 4){
                            $teacher_id = Db::name('my_user') -> where('id',$data_my_user['pid']) -> find();
                            $salesman_id = Db::name('my_user') -> where('id',$teacher_id['pid']) -> find();
                            $data_goods = Db::name('goods') -> where('uid',$salesman_id['uid']) -> select();
                        }
                    }
                }else{
                    //当为业务员时显示的数据
                    if($data_my_user['status'] == 2){
                        $data_goods = Db::name('goods') -> where('uid',$data_my_user['uid']) -> select();
                    }
                    //当为老师时显示的数据
                    if($data_my_user['status'] == 3){
                        $salesman_id = Db::name('my_user') -> where('id',$data_my_user['pid']) -> find();
                        $data_goods = Db::name('goods') -> where('uid',$salesman_id['uid']) -> select();
                    }
                    //当为学生时显示的数据
                    if($data_my_user['status'] == 4){
                        $teacher_id = Db::name('my_user') -> where('id',$data_my_user['pid']) -> find();
                        $salesman_id = Db::name('my_user') -> where('id',$teacher_id['pid']) -> find();
                        $data_goods = Db::name('goods') -> where('uid',$salesman_id['uid']) -> select();
                    }
                }


            }
        }else{
            $data_id = $this -> request -> param();
            //判断跳转链接是否存在数据 如果不存在添加一条自由人用户
            if(!empty($data_id)){
                //判断数据是老师还是业务员
                if($data_id['status_id'] == 2){
                    //业务员分享 添加老师
                    $my_user_phone = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                    $data_inser['status'] = 3;
                    $data_inser['uid'] = $uid;
                    $data_inser['inviter_phone'] = $my_user_phone['phone'];
                    $data_inser['create_time'] = time();
                    $data_inser['is_pro'] = 1;
                    $data_inser['pid'] = $my_user_phone['id'];
                    $data_inser['bind_time'] = time();
                    $data_inser['bind_status'] = 1;
                    Db::name('my_user') -> insert($data_inser);
                    $data_goods = Db::name('goods') -> where('uid',$my_user_phone['uid']) -> select();
                }elseif ($data_id['status_id'] == 3){
                    //老师分享 添加学生
                    $my_user_phone = Db::name('my_user') -> where('id',$data_id['my_user_id']) -> find();
                    $data_inser['status'] = 4;
                    $data_inser['uid'] = $uid;
                    $data_inser['inviter_phone'] = $my_user_phone['phone'];
                    $data_inser['create_time'] = time();
                    $data_inser['is_pro'] = 0;
                    $data_inser['pid'] = $my_user_phone['id'];
                    $data_inser['bind_time'] = time();
                    $data_inser['bind_status'] = 1;
                    Db::name('my_user') -> insert($data_inser);
                    $salesman_id = Db::name('my_user') -> where('id',$my_user_phone['pid']) -> find();
                    $data_goods = Db::name('goods') -> where('uid',$salesman_id['uid']) -> select();
                }
            }else{
                $data_inser['status'] = 0;
                $data_inser['uid'] = $uid;
                $data_inser['create_time'] = time();
                $data_inser['is_pro'] = 0;
                Db::name('my_user') -> insert($data_inser);
            }

        }
        if(empty($data_goods)) {
            $data_goods = null;
        }
        $this -> assign('data_goods',$data_goods);
        //推荐新品
        $data_goods_recommend_new = Db::name('goods') -> alias('a') -> field('a.*,b.name') -> join('classification b','a.classify_id = b.id','LEFT') -> where("recommend_new=1 and type = 1") -> select() -> toArray();
        $this -> assign('data_goods_recommend_new',$data_goods_recommend_new);
        $this -> assign('data_goods_hot',$data_goods_hot);
        return $this -> fetch();

    }

    /**
     * 首页搜索
     */
    public function search_salesman_goods(){

        $book_name = $_POST['book_name'];
        $data_salesman_goods = Db::name('goods') -> alias('a') -> field("a.*,b.name") -> join('classification b','a.classify_id = b.id','LEFT') -> where("type =2 ") -> where('book_name','like',"%".$book_name."%") -> select() -> toArray();
        if(!empty($data_salesman_goods)){
            foreach ($data_salesman_goods as $key => $val){
                $price = explode('.',$data_salesman_goods[$key]['price']);
                $data_salesman_goods[$key]['price0'] = $price[0];
                $data_salesman_goods[$key]['price1'] = $price[1];
            }
        }
        $this -> assign('search_content',$book_name);
        $this -> assign('data_salesman_goods',$data_salesman_goods);
        return $this -> fetch();

    }

    /**
     * 首页搜索结果页 销量排序
     */
    public function search_sales_order(){

        $search_content = $_POST['search_content'];
        $search_content_data = Db::name('goods') -> alias('a') -> field("a.*,b.name") -> join('classification b','a.classify_id = b.id','LEFT') -> where("type =2 ") -> where('book_name','like',"%".$search_content."%") -> order('sales desc') -> select() -> toArray();
        if(!empty($search_content_data)){
            foreach ($search_content_data as $key => $val){
                $price = explode('.',$search_content_data[$key]['price']);
                $search_content_data[$key]['price0'] = $price[0];
                $search_content_data[$key]['price1'] = $price[1];
                $search_content_data[$key]['show_img'] = cmf_get_image_url($search_content_data[$key]['show_img']);
            }
        }
        return json_encode($search_content_data);

    }

    /**
     * 首页搜索结果页 价格排序
     */
    public function search_price_order(){

        $search_content = $_POST['search_content'];
        $search_content_data = Db::name('goods') -> alias('a') -> field("a.*,b.name") -> join('classification b','a.classify_id = b.id','LEFT') -> where("type =2 ") -> where('book_name','like',"%".$search_content."%") -> order('price desc') -> select() -> toArray();
        if(!empty($search_content_data)){
            foreach ($search_content_data as $key => $val){
                $price = explode('.',$search_content_data[$key]['price']);
                $search_content_data[$key]['price0'] = $price[0];
                $search_content_data[$key]['price1'] = $price[1];
                $search_content_data[$key]['show_img'] = cmf_get_image_url($search_content_data[$key]['show_img']);
            }
        }
        return json_encode($search_content_data);

    }

    /**
     * 首页搜索结果页 综合
     */
    public function search_synthesis_order(){

        $book_name = $_POST['search_content'];
        $data_salesman_goods = Db::name('goods') -> alias('a') -> field("a.*,b.name") -> join('classification b','a.classify_id = b.id','LEFT') -> where("type =2 ") -> where('book_name','like',"%".$book_name."%") -> select() -> toArray();
        if(!empty($data_salesman_goods)){
            foreach ($data_salesman_goods as $key => $val){
                $price = explode('.',$data_salesman_goods[$key]['price']);
                $data_salesman_goods[$key]['price0'] = $price[0];
                $data_salesman_goods[$key]['price1'] = $price[1];
                $data_salesman_goods[$key]['show_img'] = cmf_get_image_url($data_salesman_goods[$key]['show_img']);
            }
        }
        return json_encode($data_salesman_goods);

    }

    /**
     * 搜索跳转详情页
     */
    public function search_goods_details(){

        $goods_id =  $this -> request -> param();
        $data = Db::name('goods') -> alias('a') -> join('classification b','a.classify_id = b.id','LEFT') -> where('a.id',$goods_id['goods_id']) -> find();
        $price = explode('.',$data['price']);
        $pricing = explode('.',$data['pricing']);
        $data['price0'] = $price[0];
        $data['price1'] = $price[1];
        $data['pricing0'] = $pricing[0];
        $data['pricing1'] = $pricing[1];
        $data['det_img'] = json_decode($data['det_img'],true);
        $this -> assign('det_img',$data['det_img']);
        $this -> assign('data',$data);
        $data_label = Db::name('label') -> alias('a') -> field("a.*,b.goods_id,b.label_id") -> join('goods_label b','a.id=b.label_id','LEFT') -> where("b.goods_id = ".$goods_id['goods_id']) -> select();
        $this -> assign('data_label',$data_label);
        if($data['type'] == 1){
            //相关推荐
            $data_recomm = Db::name('goods') -> where("classify_id =".$data['classify_id']." and type = 1") -> limit(3) -> select();
            $this -> assign('data_recomm',$data_recomm);
            $this -> assign('is_recomm',1);
        }else{
            $this -> assign('is_recomm',2);
        }
        return $this -> fetch();

    }




























}