ShoppingController.php
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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 app\portal\model\ShoppingModel;
use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
use think\Session;
//购物车
class ShoppingController extends CommentController
{
// 加入购物车
public function add(){
$data = $this->request->param();
$model = new ShoppingModel();
// 判断是否已添加至购物车
$where_shopping['user_id'] = Session::get('uid');
$where_shopping['vehicle_id'] = $data['vehicle_id'];
$where_shopping['status'] = array('neq',9);
$is_set = $model->where($where_shopping)->find();
if($is_set){
$add['id'] = $is_set['id'];
$add['num'] = $is_set['num'] + 1;
$res = $model->isUpdate(true)->save($add);
}else{
$add = $data;
$add['user_id'] = Session::get('uid');
$add['num'] = 1;
$res = $model->save($add);
}
if($res){
$where_count['user_id'] = Session::get('uid');
$where_count['status'] = array('neq',9);
$cart = $model->where($where_count)->count();
$this->apiResponse('1','成功',$cart);
}else{
$this->apiResponse('0','加入购物车失败');
}
}
// 购物车列表
public function index(){
$model = new ShoppingModel();
$where_shopping['user_id'] = Session::get('uid');
$where_shopping['status'] = array('neq',9);
$cart = $model->where($where_shopping)->select()->toArray();
$this->assign('cart',$cart);
return $this->fetch();
}
}