CartController.php
2.5 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
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/9/28
* Time: 16:12
*/
namespace app\cart\controller;
use app\cart\model\CartModel;
use cmf\controller\HomeBaseController;
use think\Db;
class CartController extends HomeBaseController
{
public function addCart()
{
$cart = new CartModel;
$request = request();
if ($request->isAjax()) {
$gid = $request->param('gid');
$num = $request->param('num');
if ($cart->addToCart(session('user.id'), $gid, $num)) {
$this->success('', '', true);
}
}
}
//直接购买
public function purchase() {
$request = request();
if ($request->isAjax()) {
session('cart.id', null);
session('goods.id', $request->param('gid'));
$this->success('', url('cart/zj_cart/orderConfirm'), true);
}
}
public function editCart()
{
$request = request();
if ($request->isAjax()) {
$cart = [
'id' => $request->param('id'),
'num' => $request->param('num')
];
Db::name('cart')->update($cart);
}
}
public function delCart()
{
$request = request();
if ($request->isAjax()) {
if (Db::name('cart')->delete($request->param('id'))) {
$this->success('', '', true);
}
}
}
//检查购物车中勾选的商品一级大分类是否相同
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.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('cart.id', $str);
$this->success('成功', url('cart/Cart/confirm'), $status);
} else {
$this->success('请购买同分类的商品', null, $status);
}
}
}
}