GoodsdetailsController.php 12.4 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 EasyWeChat\Foundation\Application;
use think\Db;

class GoodsdetailsController extends WeChatBaseController{

    /**
     * 商品详情页
     */
    public function Goods_details(){

        $goods_id =  $this -> request -> param();
        $uid = cmf_get_current_user_id();
        //判断跳转过来的链接
        if(!empty($goods_id['status']) && !empty($goods_id['id'])){
            //判断上级权限是否存在
            $my_user = Db::name('my_user') -> where('id',$goods_id['id']) -> find();
            if(empty($my_user)){
                $this->error('查无此人','','','');
            }
            if($my_user['status'] != $goods_id['status']){
                $this->error('分享人身份有改动','','','');
            }
            $share_user = Db::name('my_user') -> where('uid',$uid) -> find();
            //判断用户是否存在
            if(!empty($share_user)){
//                dump($share_user);
                //判断用户是不是自由人
                if($share_user['status'] == 0){
                    //如果是自由人 那就绑定关系
                    $share_data['status'] = 4;
                    $share_data['pid'] = $goods_id['id'];
                    $share_data['bind_time'] = time();
                    $share_data['bind_status'] = 1;
                    Db::name('my_user') -> where("uid",$uid) -> update($share_data);
                }
            }else{
                //用户不存在的话 如果是老师分享过来的
                if($goods_id['status'] == 3 || $goods_id['status'] == 4){
                    //判断这个用户是否存在
                    if(empty($share_user)){
                        $share_data['status'] = 4;
                        $share_data['uid'] = $uid;
                        $share_data['create_time'] = time();
                        $share_data['is_pro'] = 0;
                        $share_data['pid'] = $goods_id['id'];
                        $share_data['bind_time'] = time();
                        $share_data['bind_status'] = 1;
                        Db::name('my_user') -> insert($share_data);
                    }
                }
            }

        }
        $data = Db::name('goods') -> alias('a') -> field("a.*,b.name") -> join('classification b','a.classify_id = b.id','LEFT') -> where('a.id',$goods_id['goods_id']) -> find();
        //当商品不存在时跳转到首页
        if(empty($data)){
            $this -> redirect("Index/index");
        }
        $this -> assign('price',$data['price']);
        $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);
        $data['instr'] = cmf_replace_content_file_url(htmlspecialchars_decode($data['instr']));
        $data['instr'] = explode(' ',$data['instr']);
      /*  $data['det_img'] = explode(',',$data['det_img']);*/
        $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 is_out = 1 and type = 1") -> limit(3) -> select();
            $this -> assign('data_recomm',$data_recomm);
            $this -> assign('is_recomm',1);
        }else{
            $this -> assign('is_recomm',2);
        }
        //添加浏览记录
        $uid = cmf_get_current_user_id();
        $data_browsing_history = Db::name('browsing_history') -> where("uid = ".$uid." and goods_id =".$goods_id['goods_id']) -> find();
        if(empty($data_browsing_history)){
            $browsing_history['uid'] = $uid;
            $browsing_history['goods_id'] = $goods_id['goods_id'];
            Db::name('browsing_history') -> insert($browsing_history);
        }
        //判断订单收藏
        $collect = Db::name('collect') -> where('goods_id',$goods_id['goods_id']) -> where('uid',$uid) -> find();
        if($collect){
            //存在显示2
           $this -> assign('is_collect',2);
        }else{
            //不存在显示1
            $this -> assign('is_collect',1);
        }


        //商品详情分享
        $options=config('wechat_config');
        $app = new Application($options);
        $js = $app->js;
        $jssdk=$js->config(array('onMenuShareAppMessage', 'onMenuShareTimeline'), false,false,true);
        $this->assign('jssdk',$jssdk);
        //判断用户身份
        $my_user = Db::name('my_user') -> where('uid',$uid) -> find();
        //如果是业务员身份
        if($my_user['status'] == 2 || $my_user['status'] == 0 || $my_user['status'] == 1 || $my_user['status'] == 5 || $my_user['status'] == 6){
            $url = "http://xkeasy.w.bronet.cn/portal/goodsdetails/goods_details/goods_id/".$goods_id['goods_id'];
        }
        //如果是老师身份
        if($my_user['status'] == 3){
            if(empty($goods_id['id'])){
                $url = "http://xkeasy.w.bronet.cn/portal/goodsdetails/goods_details/goods_id/".$goods_id['goods_id']."/status/2/id/".$my_user['id'];
            }else{
                $url = "http://xkeasy.w.bronet.cn/portal/goodsdetails/goods_details/goods_id/".$goods_id['goods_id']."/status/2/id/".$goods_id['id'];
            }

        }
        //学生身份
        if($my_user['status'] == 4){
            $teacher_my_user = Db::name('my_user') -> where('id',$my_user['pid']) -> find();
            if(empty($goods_id['id'])){
                $url = "http://xkeasy.w.bronet.cn/portal/goodsdetails/goods_details/goods_id/".$goods_id['goods_id']."/status/3/id/".$teacher_my_user['id'];
            }else{
                $url = "http://xkeasy.w.bronet.cn/portal/goodsdetails/goods_details/goods_id/".$goods_id['goods_id']."/status/3/id/".$goods_id['id'];
            }

        }
        $this -> assign('url',$url);
        return $this -> fetch();
    }

    /**
     * 点击收藏
     */
    public function goods_collection(){

        $goods_id = $_POST['goods_id'];
        $uid = cmf_get_current_user_id();
        $arr['goods_id'] = $goods_id;
        $arr['uid'] = $uid;
        $collect = Db::name('collect') -> where('goods_id',$arr['goods_id']) -> where('uid',$uid) -> find();
        if($collect){
         return 3;
        }else{
            $data = Db::name('collect') -> insert($arr);
            if($data){
                return true;
            }else{
                return false;
            }
        }

    }

    /**
     * 点击取消收藏
     */
    public function cancel_goods_collection(){

        $uid = cmf_get_current_user_id();
        $goods_id = $_POST['goods_id'];
        $data = Db::name('collect') -> where("uid=".$uid." and goods_id=".$goods_id) -> delete();
        if($data){
            return true;
        }else{
            return false;
        }

    }














    /**
     * 立即支付判断
     */
    public function goodsdetails_go_pay(){

        $uid = cmf_get_current_user_id();
        $my_user = Db::name('my_user') -> where('uid',$uid) -> find();
        if($my_user['status'] == 2){
            $ret['type'] = 20;
            return json_encode($ret);
        }
        if(in_array(intval($my_user['status']),[1,5,6])){
            $ret['type'] = 21;
            return json_encode($ret);
        }
        $goods_id = $_POST['id'];
        $data[] = Db::name('goods') -> where('id',$goods_id) -> find();
        $data[0]['book_num'] = 1;

        foreach ($data as $key => $val){
            if($data[0]['type'] == $data[$key]['type']){
            }else{
                $ret['type'] = 3;
                return json_encode($ret);
            }
        }
        if($data[0]['type'] == 1){

            $indent['uid'] = $uid;
            $indent['state'] = 4;
            $indent['order_number'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
            $indent['money'] = $_POST['myprice'];
            $indent['create_time'] = time();
            $indent['indent_type'] = 1;
            $indent['logistic_name'] = 'YTO';
            $book_num = 0;
            foreach ($data as $key => $val){
                $book_num += $val['book_num'];
            }
            $indent['book_num'] = $book_num;
            $indet_id = Db::name('indent') -> insertGetId($indent);
            foreach ($data as $key => $val){
                $indent_goods['book_name'] = $val['book_name'];
                $indent_goods['pricing'] = $val['pricing'];
                $indent_goods['price'] = $val['price'];
                $indent_goods['number'] = $val['book_num'];
                $indent_goods['thumbnail'] = $val['show_img'];
                $indent_goods['indent_id'] = $indet_id;
                $indent_goods['commission'] = $val['money'];
                $indent_goods['goods_id'] = $val['id'];
                Db::name('indent_goods ') -> insert($indent_goods);
            }
            $ret['type'] = 1;
            $ret['indet_id'] = $indet_id;
            $res = json_encode($ret);
            return $res;
        }elseif ($data[0]['type'] == 2){
            $indent['uid'] = $uid;
            $indent['state'] = 4;
            $indent['order_number'] = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
            $indent['money'] = $_POST['myprice'];
            $indent['indent_type'] = 2;
            $indent['create_time'] = time();
            $book_num = 0;
            foreach ($data as $key => $val){
                $book_num += $val['book_num'];
            }
            $indent['book_num'] = $book_num;
            $indent['salesman_uid'] = $data[0]['uid'];
            $indet_id = Db::name('indent') -> insertGetId($indent);
            foreach ($data as $key => $val){
                $indent_goods['book_name'] = $val['book_name'];
                $indent_goods['pricing'] = $val['pricing'];
                $indent_goods['price'] = $val['price'];
                $indent_goods['number'] = $val['book_num'];
                $indent_goods['thumbnail'] = $val['show_img'];
                $indent_goods['indent_id'] = $indet_id;
                $indent_goods['commission'] = $val['money'];
                $indent_goods['goods_id'] = $val['id'];
                Db::name('indent_goods ') -> insert($indent_goods);
            }
            $ret['type'] = 2;
            $ret['indet_id'] = $indet_id;
            $res = json_encode($ret);
            return $res;
        }

    }

    /**
     * 判断购买的商品是不是自己上一级业务员卖的商品
     */
    public function is_salesmang_goods(){

        $uid = cmf_get_current_user_id();
        $goods_uid = Db::name('goods') -> where("id",$_POST['goods_id']) -> find();
        $my_user = Db::name('my_user') -> where('uid',$uid) -> find();
        if($goods_uid['type'] == 1){
            return true;
        }else{
            if($my_user['status'] == 3){
                $salesman_user = Db::name('my_user') -> where('id',$my_user['pid']) -> find();
            }
            if($my_user['status'] == 4){
                $teacher_user = Db::name('my_user') -> where('id',$my_user['pid']) -> find();
                $salesman_user = Db::name('my_user') -> where('id',$teacher_user['pid']) -> find();
            }
            if(!empty($salesman_user)){
                if($goods_uid['uid'] == $salesman_user['uid']){
                    return true;
                }else{
                    return false;
                }
            }else{
                return true;
            }
        }

    }












}