Goods.php 15.0 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/6
 * Time: 14:43
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use app\index\model\Evaluate;
use app\index\model\Goodscollect;
use app\index\model\Goodstype;
use app\index\model\Pic;
use app\index\model\Province;
use app\index\model\Store;
use think\Db;

class Goods extends Frontend
{
    protected $noNeedLogin = ['index','detail','goods_list','get_all','get_one','search'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->view->assign('is_search', 1);
        $this->view->assign('is_active', 2);
        $this->view->assign('title', '采购中心');
    }

    /**
     * 采购中心页面
     * @return mixed
     */
    public function index(){
        //获取分类
        $goodstypeModel = new Goodstype();
        $goodstype_one = $goodstypeModel->selectData(['pid'=>0]);//一级
        foreach($goodstype_one as $key => $g_o){
            $goodstype_two = $goodstypeModel->selectData(['pid'=>$g_o['id']]);//二级
            $goodstype_one[$key]['goodstype_two'] = $goodstype_two;
        }
        //幻灯片
        $picModel = new Pic();
        $slide = $picModel->selectData([]);
        $this->assign('goodstype_one',$goodstype_one);
        $this->assign('slide',$slide);
        $this->assign('clearance_goods',[]);
        $this->assign('goods',[]);
        $this->assign('title','采购中心');
        return $this->fetch();
    }

    /**
     * 商品详情页
     * @return mixed
     */
    public function detail(){
        $user_id = 0;
        if($this->auth->isLogin()) {
            $user_id = $this->auth->id;
        }
        $goods_id = $this->request->param('goods_id',0,'intval');
        if(empty($goods_id)){
            $this->error('缺少必要参数');
        }
        $goodsModel = new \app\index\model\Goods();
        $data = $goodsModel->findData(['g.id'=>$goods_id]);
        if(empty($data)){
            $this->error('查询为空');
        }
        if($data['status'] != '1'){
            $this->error('该商品已下架');
        }
        //收藏
        $is_collect = "2";
        if(!empty($user_id)){
            $collectModel = new Goodscollect();
            $collect = $collectModel->findData(['user_id'=>$user_id,'goods_id'=>$goods_id]);
            if(!empty($collect)){
                $is_collect = "1";
            }
            /*$userModel = new \app\index\model\User();
            $encrypt = '2';
            $user = $userModel->findData(['id'=>$user_id]);
            if(!empty($user)){
                if($user['is_svip'] == '1' || ($user['is_vip'] == '1' && in_array($user['province_id'],$user['province_ids']))){
                    $encrypt = '1';
                }
            }
            //当前用户若不是会员加密部分信息
            if($encrypt == '2'){
                $data['store_phone'] = "***********";
                $data['store_address'] = "***********";
            }*/
        }
        $data['is_collect'] = $is_collect;
        //获取评论
        $evaluateModel = new Evaluate();
        $comments_count1 = $evaluateModel->countData(['goods_id'=>$goods_id]);//全部评论数量
        $comments_count2 = $evaluateModel->countData(['goods_id'=>$goods_id,'level'=>['in',[4,5]]]);//好评评论数量
        $comments_count3 = $evaluateModel->countData(['goods_id'=>$goods_id,'level'=>['in',[3]]]);//中评评论数量
        $comments_count4 = $evaluateModel->countData(['goods_id'=>$goods_id,'level'=>['in',[1,2]]]);//差评评论数量
        $comments1 = $evaluateModel->selectData(['goods_id'=>$goods_id]);//全部评论
        $comments2 = $evaluateModel->selectData(['goods_id'=>$goods_id,'level'=>['in',[4,5]]]);//好评评论
        $comments3 = $evaluateModel->selectData(['goods_id'=>$goods_id,'level'=>['in',[3]]]);//中评评论
        $comments4 = $evaluateModel->selectData(['goods_id'=>$goods_id,'level'=>['in',[1,2]]]);//差评评论
        //计算好评、中评、差评率
        $probability1 = !empty($comments_count2) ? round(($comments_count2/$comments_count1)*100,2) : 0;//好评率
        $probability2 = !empty($comments_count3) ? round(($comments_count3/$comments_count1)*100,2) : 0;//中评率
        $probability3 = !empty($comments_count4) ? round(($comments_count4/$comments_count1)*100,2) : 0;//差评率
        $this->assign('data',$data);
        $this->assign('comments_count1',$comments_count1);
        $this->assign('comments_count2',$comments_count2);
        $this->assign('comments_count3',$comments_count3);
        $this->assign('comments_count4',$comments_count4);
        $this->assign('comments1',$comments1);
        $this->assign('comments2',$comments2);
        $this->assign('comments3',$comments3);
        $this->assign('comments4',$comments4);
        $this->assign('probability1',$probability1);
        $this->assign('probability2',$probability2);
        $this->assign('probability3',$probability3);
        $this->assign('title',$data['goodsname']);
        return $this->fetch();
    }

    /**
     * 产品热卖商品列表页面
     * @return mixed
     */
    public function goods_list(){
        $category_id = $this->request->param('category_id',0,'intval');
        $category = [];
        $where = [];
        $goodstypeModel = new Goodstype();
        if(!empty($category_id)){
            $category = $goodstypeModel->findData(['id'=>$category_id]);
            $where['goodstype_id'] = ['eq',$category_id];
        }
        //获取分类
        $goodstype_one = $goodstypeModel->selectData(['pid'=>0]);//一级
        foreach($goodstype_one as $key => $g_o){
            $goodstype_two = $goodstypeModel->selectData(['pid'=>$g_o['id']]);//二级
            $goodstype_one[$key]['goodstype_two'] = $goodstype_two;
        }
        //幻灯片
        $picModel = new Pic();
        $slide = $picModel->selectData([]);
        $this->assign('goodstype_one',$goodstype_one);
        $this->assign('slide',$slide);
        $this->assign('category',$category);
        $this->assign('title','产品热卖');
        return $this->fetch();
    }

    /**
     * 搜索结果页面
     * @return mixed
     */
    public function search(){
        $keyword = $this->request->param('keyword');
        $this->assign('keyword',$keyword);
        $this->assign('title','搜索');
        return $this->fetch();
    }

    public function get_all(){
//        dump($this->auth->id);
        $param = $this->request->param();
        $where['g.status'] = ['eq','1'];
        if(!empty($param['province_id'])){
            $storeModel = new Store();
            $store = $storeModel->selectData([]);
            $store_ids = [];
            foreach($store as $key => $s){
                $store_ids[] = $s['id'];
                /*//判断该区域是否为会员
                if($s['is_svip'] == '1'){
                    $store_ids[] = $s['id'];
                    continue;
                }
                if($s['is_vip'] == '1' && in_array($param['province_id'],$s['province_ids'])){
                    $store_ids[] = $s['id'];
                    continue;
                }*/
            }
            $where['g.store_id'] = ['in',$store_ids];
        }
        if(!empty($param['is_clearance'])){
            $where['g.is_clearance'] = ['eq',$param['is_clearance']];
        }
        if(!empty($param['keyword'])){
            $where['t.name|s.name|g.goodsname|g.brand|g.keyword'] = ['like',"%$param[keyword]%"];
        }
        if(!empty($param['category_id'])){
            $where['g.goodstype_id'] = ['eq',$param['category_id']];
        }
        if(!empty($param['store_id'])){
            $where['g.store_id'] = ['eq',$param['store_id']];
        }
        $order = 'g.weigh desc';
        if(!empty($param['sort'])){
            if($param['sort'] == 1){
                //销量降序
                $order = "g.sale desc";
            }else if($param['sort'] == 2){
                //评分降序
                $order = "";
            }else if($param['sort'] == 3){
                //发布时间降序
                $order = "g.createtime desc";
            }else if($param['sort'] == 4){
                //库存降序
                $order = "g.inventory desc";
            }else if($param['sort'] == 5){
                //价格升序
                $order = "g.price asc";
            }else if($param['sort'] == 6){
                //价格降序
                $order = "g.price desc";
            }else if($param['sort'] == 7){
                //店铺属性筛选
                $where['s.property'] = ['like',"%供应商%"];
            }else if($param['sort'] == 8){
                //店铺属性筛选
                $where['s.property'] = ['like',"%一级代理%"];
            }else if($param['sort'] == 9){
                //店铺属性筛选
                $where['s.property'] = ['like',"%厂家直销%"];
            }
        }
        $goodsModel = new \app\index\model\Goods();
        $ids = [];
        if(!empty($param['lng']) && !empty($param['lat'])){
            $data = $goodsModel
                ->alias('g')
                ->field('g.*,t.name as goodstype_name,s.lng,s.lat,s.name as store_name,s.address as store_address,s.property,s.type,s.content as store_content,s.thumbnail as store_thumbnail,s.money as store_money')
                ->join('sto_goodstype t','t.id = g.goodstype_id')
                ->join('sto_store s','s.id = g.store_id')
                ->where($where)
                ->order($order)
                ->select();
            foreach($data as $key => $vo){
                //计算距离
                $distance = distance($param,$vo);
//                if($distance <= 50){
                    $ids[] = $vo['id'];
//                }
            }
            $where['g.id'] = ['in',$ids];
        }
        $data = $goodsModel
            ->alias('g')
            ->field('g.*,t.name as goodstype_name,s.lng,s.lat,s.name as store_name,s.phone as store_phone,s.address as store_address,s.property,s.type,s.content as store_content,s.thumbnail as store_thumbnail,s.money as store_money')
            ->join('sto_goodstype t','t.id = g.goodstype_id')
            ->join('sto_store s','s.id = g.store_id')
            ->where($where)
            ->order($order);
        if(!empty($param['paginate'])){
            $data = $data->paginate($param['pageNum'],'',['page'=>$param['page']]);
        }else{
            $data = $data->select();
        }
        $user_id = $this->auth->id;
        $userModel = new \app\index\model\User();
        $encrypt = '2';
        $user = $userModel->findData(['id'=>$user_id]);
        if(!empty($user)){
            $province_ids = $user['province_ids'];
            if($user['is_svip'] == '1' || in_array($param['province_id'],$province_ids)){
                $encrypt = '1';
            }
        }
        $storeModel = new Store();
        $evaluateModel = new Evaluate();
        //店铺id
        $store_ids = [];
        foreach($data as $key => $vo){
            if(!in_array($vo['store_id'],$store_ids)){
                $store_ids[] = $vo['store_id'];
            }
            //判断店铺在该区域是否为会员
            $insurance = '2';
            $store = $storeModel->findData(['id'=>$vo['store_id']]);
            $store_user = $userModel->findData(['id'=>$store['user_id']]);
            $user_province = Db::name('user_province')->where(['user_id'=>$store['user_id'],'expiration_time'=>['gt',time()]])->select();
            $province_ids = [];
            foreach($user_province as $key2 => $u_p){
                $province_ids[] = $u_p['province_id'];
            }
            /*if($vo['id'] == 13){
                dump($store['is_svip']);
                dump($param['province_id2']);
                dump($province_ids);
            }*/
            if(!empty($param['province_id2'])){
                if($store['is_svip'] == '1' || in_array($param['province_id2'],$province_ids)){
                    $insurance = '1';
                }
            }
//            dump($insurance);
            $data[$key]['insurance'] = $insurance;
            if(empty($user_id)){
                $text = "(请您先登录)";
                $data[$key]['store_phone'] = substr_replace($vo['store_phone'],'****',3,4).$text;
                $data[$key]['store_address'] = substr_replace($vo['store_address'],'********',3).$text;
            }
            //当前用户若不是会员加密部分信息
            if($insurance == '2'){
                $text = '';
                if(empty($user_id)){
                    $text = "(请您先登录)";
                }
                $data[$key]['store_phone'] = substr_replace($vo['store_phone'],'****',3,4).$text;
                $data[$key]['store_address'] = substr_replace($vo['store_address'],'********',3).$text;
            }
            //计算平均分
            $score_count = $evaluateModel->where(['goods_id'=>$vo['id']])->count();
            $score_sum = $evaluateModel->where(['goods_id'=>$vo['id']])->sum('level');
            $average_score = !empty($score_sum) ? round($score_sum/$score_count,2) : 0;
            $data[$key]['average_score'] = $average_score;
            //计算距离
            $distance = 0;
            if(!empty($param['lng']) && !empty($param['lat'])){
                $distance = distance($param,$vo);
                if($distance <= 50){
                    $ids[] = $vo['id'];
                }
            }
            $data[$key]['distance'] = $encrypt == '1' ? $distance : '';
        }
        if(!empty($param['sort'])){
            if($param['sort'] == 2){
                $sort = array_column($data->items(),"average_score");
                array_multisort($sort,SORT_DESC,$data->items());
            }
        }
        $store = Db::name('store')->where(['id'=>['in',$store_ids]])->select();
        $this->success('SUCCESS','',$data,$store);
    }
    public function get_one(){
        $param = $this->request->param();
        if(empty($param['goods_id']) || empty($param['province_id'])){
            $this->error('缺少必要参数');
        }
        $goodsModel = new \app\index\model\Goods();
        $data = $goodsModel->findData(['g.id'=>$param['goods_id']]);
        if(empty($data)){
            $this->error('查询为空','',['result'=>2]);
        }
        $storeModel = new Store();
        $store = $storeModel->findData(['id'=>$data['store_id']]);
        $userModel = new \app\index\model\User();
        $store_user = $userModel->findData(['id'=>$store['user_id']]);
        $user_province = Db::name('user_province')->where(['user_id'=>$store['user_id'],'expiration_time'=>['gt',time()]])->select();
        $province_ids = [];
        foreach($user_province as $key => $u_p){
            $province_ids[] = $u_p['province_id'];
        }
        if(empty($store)){
            $this->error('查询为空','',['result'=>2]);
        }
        $result = 2;
        if($store['is_svip'] == '1' || in_array($param['province_id'],$province_ids)){
            $result = 1;
        }
        $this->success('SUCCESS','',['result'=>$result]);
    }
}