123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- namespace addons\unishop\controller;
- use addons\unishop\extend\Hashids;
- use addons\unishop\model\Config;
- use addons\unishop\model\Product;
- use addons\unishop\model\Cart as CartModel;
- use think\Exception;
- class Cart extends Base
- {
-
- protected $frequently = ['number_change','index', 'choose_change'];
-
- public function index()
- {
- $carts = (new CartModel)->where(['user_id' => $this->auth->id])
- ->with([
- 'product' => function ($query) {
- $query->field(['id', 'image', 'title', 'specTableList','sales','real_sales','market_price','sales_price','stock','use_spec', 'switch']);
- }
- ])
- ->order(['createtime' => 'desc'])
- ->select();
- if (!$carts) {
- $result = [
- 'choose_price_total' => '0',
- 'choose_number' => 0,
- 'list' => [],
- ];
- $this->success('', $result);
- }
- $data = [];
- $productExtend = new \addons\unishop\extend\Product;
- $choose_price_total = '0';
- $choose_number = 0;
- foreach ($carts as $item) {
- if (empty($item['product'])) {
- continue;
-
- } else {
- $productData = $item['product']->getData();
- $tempData = $productExtend->getBaseData($productData, $item['spec'] ?? '');
- $tempData['title'] = $item['product']['title'];
- $tempData['choose'] = $item['choose'];
- $tempData['isset'] = true;
- if ($productData['switch'] == Product::SWITCH_OFF) {
- continue;
- $tempData['isset'] = false;
- $tempData['choose'] = 0;
- }
- }
- $tempData['cart_id'] = $item['id'];
- $tempData['spec'] = $item['spec'];
- $tempData['number'] = $item['number'];
- $tempData['image'] = Config::getImagesFullUrl($tempData['image']);
- $tempData['oldPrice'] = round($tempData['sales_price'], 2);
- $tempData['nowPrice'] = round($tempData['sales_price'], 2);
-
- $tempData['Price_total'] = bcmul($tempData['sales_price'],$tempData['number'],2);
-
- if($item['choose'] == 1){
- $choose_price_total = bcadd($choose_price_total,$tempData['Price_total'],2);
- $choose_number += $tempData['number'];
- }
- $tempData['product_id'] = Hashids::encodeHex($item['product_id']);
- $data[] = $tempData;
- }
- $result = [
- 'choose_price_total' => $choose_price_total,
- 'choose_number' => $choose_number,
- 'list' => $data,
- ];
- $this->success('', $result);
- }
-
- public function add()
- {
- $id = input('id', 0);
- $number = input('number',1);
- $id = \addons\unishop\extend\Hashids::decodeHex($id);
- $product = (new Product)->where(['id' => $id, 'switch' => Product::SWITCH_ON])->find();
- if (!$product) {
- $this->error('产品不存在或已下架');
- }
- $spec = input('spec', '');
- $productBase = (new \addons\unishop\extend\Product())->getBaseData($product->getData(), $spec);
- if (!$productBase['stock'] || $productBase['stock'] <= 0) {
- $this->error('库存不足');
- }
- $user_id = $this->auth->id;
- $cartModel = new \addons\unishop\model\Cart();
- $cartModel->where(['user_id' => $user_id, 'product_id' => $id]);
- $spec && $cartModel->where('spec', $spec);
- $oldCart = $cartModel->find();
- if ($oldCart) {
-
- $oldCart->number += $number;
- $result = $oldCart->save();
- } else {
- $cartModel->user_id = $user_id;
- $cartModel->product_id = $id;
- $spec && $cartModel->spec = $spec;
- $cartModel->number = $number;
- $result = $cartModel->save();
- }
- if ($result) {
- $this->success('添加成功', 1);
- } else {
- $this->error('添加失败');
- }
- }
-
- public function delete()
- {
- $id = input('id', 0);
- $userId = $this->auth->id;
- $result = CartModel::destroy(function ($query) use ($id, $userId) {
- $query->whereIn('id', $id)->where(['user_id' => $userId]);
- });
- if ($result) {
- $this->success('删除成功', 1);
- } else {
- $this->error('删除失败', 0);
- }
- }
-
- public function number_change()
- {
- $cart_id = input('id', 0);
- $number = input('number', 1);
- $cart = CartModel::get(['id' => $cart_id, 'user_id' => $this->auth->id]);
- if (empty($cart)) {
- $this->error('此商品不存在购物车');
- }
- $cart->number = $number;
- $result = $cart->save();
- if ($result) {
- $this->success('更改成功', $number);
- } else {
- $this->error('更改失败', $number);
- }
- }
-
- public function choose_change()
- {
- $trueArr = input('trueArr', false);
- $falseArr = input('falseArr', false);
- $user_id = $this->auth->id;
- try {
- $cart = new CartModel();
- if ($trueArr) {
- $cart->save(['choose' => CartModel::CHOOSE_ON], function ($query) use ($user_id, $trueArr) {
- $query->where('user_id', $user_id)->where('id', 'IN', $trueArr);
- });
- }
- if ($falseArr) {
- $cart->save(['choose' => CartModel::CHOOSE_OFF], function ($query) use ($user_id, $falseArr) {
- $query->where('user_id', $user_id)->where('id', 'IN', $falseArr);
- });
- }
- } catch (Exception $e) {
- $this->error('更新失败', 0);
- }
- $this->success('', 1);
- }
- }
|