审查视图

app/portal/controller/ShopcartController.php 6.9 KB
anyv authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?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 ShopcartController extends WeChatBaseController{

    /**
     * 显示购物车页
     */
    public function shop_cart(){
5  
anyv authored
21 22 23 24
        $uid = cmf_get_current_user_id();
        $goods_id = Db::name('shopping_cart') -> where("uid",$uid) -> select() -> toArray();
        if(!empty($goods_id)){
            foreach ($goods_id as $key => $val){
4  
anyv authored
25 26
                $goods_data[$key] = Db::name('goods') -> where("id",$val['goods_id']) -> find();
                $goods_data[$key]['carid'] = $val['id'];
5  
anyv authored
27
                $goods_data[$key]['book_num'] = $val['book_num'];
5  
anyv authored
28 29 30 31 32 33
            }
            foreach ($goods_data as $key => $val){
                $price = explode('.',$goods_data[$key]['price']); ;
                $goods_data[$key]['price0'] = $price[0];
                $goods_data[$key]['price1'] = $price[1];
            }
2  
anyv authored
34 35
        }else{
            $goods_data = null;
5  
anyv authored
36
        }
5  
anyv authored
37 38
        $num = count($goods_id);
        $this -> assign('num',$num);
5  
anyv authored
39 40
        $this -> assign('goods_data',$goods_data);
anyv authored
41 42 43 44
        return $this -> fetch();

    }
5  
anyv authored
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    /**
     * 添加购物车
     */
    public function add_cart(){

        $goods_id = $_POST['goods_id'];
        $uid = cmf_get_current_user_id();
        $data = Db::name('shopping_cart') -> where("uid = ".$uid." and goods_id = ".$goods_id) -> find();
        if($data){
            return 2;
        }else{
            $arr['goods_id'] = $goods_id;
            $arr['uid'] = $uid;
            $insert = Db::name('shopping_cart') -> insert($arr);
            if($insert){
                return 1;
            }else{
                return 3;
            }
        }
5  
anyv authored
65
5  
anyv authored
66
    }
anyv authored
67
4  
anyv authored
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    /**
     *购物车删除
     */
    public function shop_cart_del(){

        $id = explode(',',$_POST['goods_id']);
        $data = Db::name('shopping_cart') -> delete($id);
        if($data){
           return true;
        }else{
            return false;
        }

    }
2  
anyv authored
83 84 85 86 87 88 89 90
    /**
     * 商品数量加一
     */
    public function shop_cart_numadd(){

        $id = $_POST['id'];
        $data = Db::name('shopping_cart') -> where('id',$id) -> find();
        if($data['book_num']<200){
anyv authored
91
            $res = Db::name('shopping_cart') -> where('id',$id) -> setInc('book_num');
2  
anyv authored
92 93 94 95
        }
        return true;

    }
anyv authored
96
2  
anyv authored
97 98 99 100 101 102 103 104 105 106 107 108 109
    /**
     * 商品数量减一
     */
    public function shop_cart_numdec(){

        $id = $_POST['id'];
        $data = Db::name('shopping_cart') -> where('id',$id) -> find();
        if($data['book_num']>1){
            Db::name('shopping_cart') -> where('id',$id) -> setDec('book_num');
        }
        return true;

    }
anyv authored
110
5  
anyv authored
111 112 113 114 115
    /**
     * 去支付判断
     */
    public function shop_cart_pay(){
4  
anyv authored
116
        $uid = cmf_get_current_user_id();
5  
anyv authored
117 118 119 120 121 122
        $cart_id = explode(',',$_POST['id']);
        foreach ($cart_id as $key => $val){
            $goods_id[] = Db::name('shopping_cart') -> where('id',$val) -> find();
        }
        foreach ($goods_id as $key => $val){
            $data[] = Db::name('goods') -> where('id',$val['goods_id']) -> find();
4  
anyv authored
123
            $data[$key]['book_num'] = $goods_id[$key]['book_num'];
5  
anyv authored
124 125 126 127
        }
        foreach ($data as $key => $val){
            if($data[0]['type'] == $data[$key]['type']){
            }else{
anyv authored
128 129
                $ret['type'] = 3;
                return json_encode($ret);
5  
anyv authored
130 131
            }
        }
4  
anyv authored
132 133 134 135 136
        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);
5  
anyv authored
137 138 139
            $indent['money'] = $_POST['myprice'];
            $indent['create_time'] = time();
            $indent['indent_type'] = 1;
anyv authored
140
            $indent['logistic_name'] = 'YTO';
4  
anyv authored
141
            $indent['is_courier'] = 1;
5  
anyv authored
142 143 144 145 146 147 148 149 150 151 152 153 154
            $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;
anyv authored
155
                $indent_goods['commission'] = $val['money'];
anyv authored
156
                $indent_goods['goods_id'] = $val['id'];
5  
anyv authored
157 158
                Db::name('indent_goods ') -> insert($indent_goods);
            }
4  
anyv authored
159
            $ret['type'] = 1;
5  
anyv authored
160 161 162
            $ret['indet_id'] = $indet_id;
            $res = json_encode($ret);
            return $res;
4  
anyv authored
163
        }elseif ($data[0]['type'] == 2){
anyv authored
164 165 166 167 168 169 170 171 172 173 174
            $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;
anyv authored
175 176 177 178 179 180 181 182 183
            $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;
anyv authored
184
                $indent_goods['commission'] = $val['money'];
anyv authored
185
                $indent_goods['goods_id'] = $val['id'];
anyv authored
186 187 188 189 190 191
                Db::name('indent_goods ') -> insert($indent_goods);
            }
            $ret['type'] = 2;
            $ret['indet_id'] = $indet_id;
            $res = json_encode($ret);
            return $res;
4  
anyv authored
192
        }
5  
anyv authored
193
anyv authored
194
5  
anyv authored
195
    }
anyv authored
196
4  
anyv authored
197 198 199 200 201 202 203 204 205 206 207 208 209 210
    /**
     * 业务员不能买书
     */
    public function is_to_pay(){

        $uid = cmf_get_current_user_id();
        $my_user = Db::name('my_user') -> where('uid',$uid) -> find();
        if($my_user['status'] == 2){
            return false;
        }else{
            return true;
        }

    }
anyv authored
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230



















}