1], [['cart_id_list','attr'],'string'] ]; } public function save() { try { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } $goods = Goods::findOne([ 'id' => $this->goods_id, //'store_id' => $this->store_id, 'is_delete' => 0, 'status' => 1, ]); if (!$goods) { return [ 'code' => 1, 'msg' => '商品不存在或已下架', ]; } $store = \app\models\Store::findOne(['id' => $goods->store_id, 'is_delete' => 0, 'business_model' => 2]); if (!$store) { return [ 'code' => 1, 'msg' => '店铺不存在或非平台运营', ]; } $this->attr = json_decode($this->attr, true); $attr = []; foreach ($this->attr as $item) { if (!empty($item['attr_id'])) { $attr[] = intval($item['attr_id']); } } sort($attr); $attr = json_encode($attr, JSON_UNESCAPED_UNICODE); $cart = BusinessCart::findOne([ 'store_id' => $goods->store_id, 'goods_id' => $this->goods_id, 'saas_id' => $this->saas_id, //'md_id' => get_md_id(), 'is_delete' => 0, 'attr' => $attr, ]); if (!$cart) { $cart = new BusinessCart(); $cart->store_id = $goods->store_id; $cart->goods_id = $this->goods_id; $cart->saas_id = $this->saas_id; $cart->num = 0; $cart->created_at = time(); $cart->is_delete = 0; $cart->attr = $attr; // if (get_md_id()) { // $cart->md_id = get_md_id(); // } } $cart->num += $this->num; if ($cart->save()) { return [ 'code' => 0, 'msg' => '添加购物车成功', ]; } else { return ['code' => 1, 'msg' => $cart->errors[0]]; } } catch (\Exception $e) { return [ 'code' => 0, 'msg' => $e->getMessage() . 'line=' . $e->getLine() ]; } } public function del() { if (!$this->validate()) { return $this->errorResponse; } try { $this->cart_id_list = json_decode($this->cart_id_list, true); foreach ($this->cart_id_list as $cart_id) { $cart = BusinessCart::findOne([ //'store_id' => $this->store_id, 'is_delete' => 0, 'saas_id' => $this->saas_id, 'id' => $cart_id, ]); if (!$cart) { continue; } $cart->is_delete = 1; $cart->save(); } return [ 'code' => 0, 'msg' => '删除完成', ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() . $e->getLine(), ]; } } public function clear() { try { $store_id = $this->store_id; $saas_id = $this->saas_id; $cart = BusinessCart::findOne([ 'is_delete' => 0, 'saas_id' => $saas_id, 'store_id' => $store_id, ]); if (!$cart) { throw new \Exception('购物车列表为空'); } BusinessCart::updateAll(['is_delete' => 1], [ 'is_delete' => 0, 'saas_id' => $saas_id, 'store_id' => $store_id, ]); return [ 'code' => 0, 'msg' => '清空完成', ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() . $e->getLine(), ]; } } }