Car.php
5.3 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/5/11
* Time: 11:41
*/
namespace app\index\controller;
use app\common\controller\Frontend;
use app\common\controller\HomeBase;
use app\index\model\Store;
use think\Db;
class Car extends Frontend
{
protected $noNeedLogin = [''];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
$this->view->assign('is_search', 0);
$this->view->assign('is_active', 0);
$this->view->assign('title', '购物车');
}
/**
* 购物车页面
* @return mixed
*/
public function index(){
$user_id = $this->auth->id;
$carModel = new \app\index\model\Car();
$car1 = $carModel->where(['user_id'=>$user_id])->group('store_id')->select();
$store_ids = [];
foreach($car1 as $key => $c1){
$store_ids[] = $c1['store_id'];
}
$goodsModel = new \app\index\model\Goods();
$storeModel = new Store();
$store = $storeModel->selectData(['id'=>['in',$store_ids]]);
$store = collection($store)->toArray();
foreach($store as $key1 => $s){
$car = $carModel->selectData(['store_id'=>$s['id'],'user_id'=>$user_id]);
foreach($car as $key2 => $c){
$goods = $goodsModel->findData(['g.id'=>$c['goods_id']]);
$car[$key2]['goods'] = $goods;
}
$store[$key1]['car'] = collection($car)->toArray();
}
$this->assign('data',$store);
$this->assign('title','购物车');
return $this->fetch();
}
/**
* 更新购物车
*/
public function add(){
$user_id = $this->auth->id;
$goods_id = $this->request->param('goods_id',0,'intval');
$number = $this->request->param('number',1,'intval');
$type = $this->request->param('type',0,'intval');//1增,2减
$province_id = $this->request->param('province_id',0,'intval');
if(empty($goods_id) || empty($number) || empty($type) || empty($province_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('该商品已下架');
}
$storeModel = new Store();
$store = $storeModel->findData(['id'=>$data['store_id']]);
//判断该区域是否为会员
if($store['is_svip'] != '1'){
if($store['is_vip'] != '1'){
$this->error('该商品不可以进行该操作~');
}else{
if(!in_array($province_id,$store['province_ids'])){
$this->error('该商品不可以进行该操作~');
}
}
}
$carModel = new \app\index\model\Car();
$arr['user_id'] = $user_id;
$arr['goods_id'] = $goods_id;
$arr['store_id'] = $data['store_id'];
$arr['number'] = $number;
$car = $carModel->findData(['user_id'=>$user_id,'goods_id'=>$goods_id]);
if(empty($car)){
//新增
//判断库存是否充足
if($data['inventory'] < 1){
$this->error("商品 【$data[goodsname]】 库存不足~");
}
$arr['createtime'] = time();
$result = $carModel->insertData($arr);
}else{
//更新
$arr['updatetime'] = time();
if($type == 1){
//增加数量
//判断库存是否充足
if($data['inventory'] < 1){
$this->error("商品 【$data[goodsname]】 库存不足~");
}
$result = $carModel->where(['id'=>$car['id']])->setInc('number',$number);
}else{
//减少数量
if($car['number'] <= 1){
$this->error('不能再减了~');
}
$result = $carModel->where(['id'=>$car['id']])->setDec('number',$number);
}
}
if(empty($result)){
$this->error('sql执行失败');
}
$car = $carModel->findData(['user_id'=>$user_id,'goods_id'=>$goods_id]);
$this->success('SUCCESS','',['number'=>$car['number']]);
}
/**
* 删除购物车
*/
public function del(){
$car_ids = $this->request->param('car_ids');
$car_ids = explode(',',$car_ids);
if(empty($car_ids)){
$this->error('缺少必要参数');
}
$carModel = new \app\index\model\Car();
$result = $carModel->where(['id'=>['in',$car_ids]])->delete();
$this->success('SUCCESS');
}
/**
* 气泡
*/
public function get_bubble(){
$user_id = $this->auth->id;
$carModel = new \app\index\model\Car();
$total = $carModel->countData(['user_id'=>$user_id]);
$this->success('SUCCESS','',['total'=>$total]);
}
public function selected(){
$data = $this->request->param('data/a');
foreach($data as $key => $vo){
Db::name('car')->where(['id'=>$vo['car_id']])->update(['is_selected'=>$vo['is_selected']]);
}
}
}