1], ]; } public function save($params = []) { try { $type = (int)$params['type']; 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' => '商品不存在或已下架', ]; } //判断会员是否可以购买 // if (!empty($goods->buy_type_info)) { // $buy_type_info = json_decode($goods->buy_type_info, true); // if ((int)$buy_type_info['type'] === 1) { //会员购买 // if ((int)$buy_type_info['member_level'] !== 0) { // $user = User::findOne($this->user_id); // if ((int)$user->level !== (int)$buy_type_info['member_level']) { // $level = Level::findOne(['level' => $buy_type_info['member_level']])->name ?? "普通会员"; // return [ // 'code' => 1, // 'msg' => '当前商品仅支持' . $level . '购买', // ]; // } // } // } // if ((int)$buy_type_info['type'] === 2) { //新用户购买 // $order = Order::findOne(['user_id' => $this->user_id]); // if (!empty($order)) { // 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 = Cart::findOne([ 'type'=>$type, 'store_id' => $this->store_id, 'goods_id' => $this->goods_id, 'user_id' => $this->user_id, 'md_id' => get_md_id(), 'is_delete' => 0, 'attr' => $attr, ]); if (!$cart) { $cart = new Cart(); $cart->type = $type; $cart->store_id = $this->store_id; $cart->goods_id = $this->goods_id; $cart->mch_id = $goods->mch_id; $cart->user_id = $this->user_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() ]; } } }