ZjCartController.php
7.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace app\cart\controller;
use cmf\controller\HomeBaseController;
use think\Db;
class ZjCartController extends HomeBaseController
{
/**
* 购物车
*/
public function cart()
{
$id = session('user.id');
$all = Db::name('zj_cart')
->alias('c')
->join('zj_goods g', 'c.gid=g.id')
->join('zj_category ca', 'g.cid=ca.id')
->where('c.uid', $id)
->where(['g.is_sta' => '1', 'g.delete_time' => '0'])
->field('c.id as cartid,c.gid,c.num,g.*,ca.cid as caid')
->select();
if (empty($all[0])) {
$you = 1;
} else {
$you = 2;
}
$this->assign('you', $you);
$this->assign('all', $all);
return $this->fetch();
}
/**
* 商品数量更改
*/
public function num()
{
if ($this->request->isAjax()) {
$param = input('param.');
if ($param['state'] == 1) {
$jia = Db::name('zj_cart')->where('id', $param['id'])->setDec('num', '1');
} else {
$jia = Db::name('zj_cart')->where('id', $param['id'])->setInc('num', '1');
}
if (empty($jia)) {
$this->error('NO');
} else {
$this->success('OK');
}
}
}
/**
* 删除选中商品
*/
public function del()
{
if ($this->request->isAjax()) {
$param = input('param.');
$del = Db::name('zj_cart')->where('id', 'in', $param['id'])->delete();
if (empty($del)) {
$this->error('NO');
} else {
$this->success('OK');
}
}
}
//检查购物车中勾选的商品一级大分类是否相同
public function checkCartGoods()
{
$request = request();
if ($request->isAjax()) {
$str = $request->param('id');
$cid = Db::name('zj_cart')->alias('c')
->join('zj_goods g', 'c.gid=g.id')
->join('zj_category t', 't.id=g.cid')
->field('t.cid')
->where(['c.uid' => session('user.id'), 'c.id' => ['in', $str]])
->select()
->toArray();
$temp = $cid[0]['cid'];
$status = true;
foreach ($cid as $k => $v) {
if ($k != 0) {
if ($temp != $v['cid']) {
$status = false;
break;
}
}
}
if ($status === true) {
session('goods.id', null);
session('cart.id', $str);
$this->success('成功', url('cart/ZjCart/orderConfirm'), $status);
} else {
$this->success('请购买同分类的商品', null, $status);
}
}
}
//订单确认页
public function orderConfirm()
{
if (session('cart.id') == null && session('goods.id') == null) {
$this->redirect(url('index/Index/index'));
} else {
$total_num = 0;
$whole = 0;
if (session('cart.id') != null && session('goods.id') == null) {
$data = Db::name('zj_cart')->alias('c')
->join('zj_goods g', 'c.gid=g.id')
->join('zj_category t', 't.id=g.cid')
->field('c.num,g.name,g.price,g.price_num,g.price_cash,g.thumb,g.is_type,g.intro,t.cid as caid,g.is_type')
->where(['c.uid' => session('user.id'), 'c.id' => ['in', session('cart.id')]])
->select();
} elseif (session('cart.id') == null && session('goods.id') != null) {
$data = Db::name('zj_goods')->alias('g')
->join('zj_category t', 't.id=g.cid')
->field('g.name,g.price,g.price_num,g.price_cash,g.thumb,g.is_type,g.intro,t.cid as caid,g.is_type')
->where(['g.id' => session('goods.id')])
->select();
} else {
return false;
}
foreach ($data as $k => $item) {
if (session('cart.id') == null && session('goods.id') != null) {
$item['num'] = 1;
}
if ($item['caid'] == 2) {
$item['integral'] = '购买可获得' . $item['price'] * $item['num'] * 2 . '积分';
} else {
$item['integral'] = '该商品无积分奖励';
}
$total_num += $item['num'];
$item['sum_price'] = $item['price'] * $item['num'];
$whole += $item['sum_price'];
$data[$k] = $item;
}
$address = Db::name('zj_user_place')
->field('id,name,province,city,county,mobile,place,is_sta')
->where(['uid' => session('user.id')])
->select()
->toArray();
$default_address_id = '';
$default_name = '请选择地址';
$default_mobile = '';
$default_address = '';
if (!empty($address)) {
foreach ($address as $item) {
if ($item['is_sta'] == 2) {
$default_address_id = $item['id'];
$default_name = $item['name'];
$default_mobile = $item['mobile'];
$default_address = $item['province'] . $item['city'] . $item['county'] . $item['place'];
break;
}
}
}
require_once EXTEND_PATH . '/WeChatCommon.php';
$wx = new \WeChatCommon();
//attach额外附加金额积分关系字段
if ($data[0]['caid'] == 2) {
$min_cash = 0; //该笔订单所需的最低金额
$min_num = 0; //该笔订单所需的最低积分
$total_price = 0; //该笔订单所需的总价格
foreach ($data as $k => $v) {
$min_cash += $v['price_cash'] * $v['num'];
$min_num += $v['price_num'] * $v['num'];
$total_price += $v['price'] * $v['num'];
}
$attach = [
'min_cash' => $min_cash,
'min_num' => $min_num,
'total_price' => $total_price
];
} else {
$attach = [
'min_cash' => '',
'min_num' => '',
'total_price' => ''
];
}
return $this->fetch('order_confirm', [
'data' => $data,
'address' => $address,
'default_address_id' => $default_address_id,
'default_name' => $default_name,
'default_mobile' => $default_mobile,
'default_address' => $default_address,
'total_num' => $total_num,
'total_price' => $whole,
'caid' => $data[0]['caid'],
'js_sdk' => $wx->js_sdk(),
'uid' => session('user.id'),
'attach' => $attach,
'balance' => Db::name('user')->where(['id'=>session('user.id')])->value('balance')
]);
}
}
}