From c47652a934d051bb8d8562e46d34a3179ba527f7 Mon Sep 17 00:00:00 2001 From: Terry <2358269014@qq.com> Date: Thu, 14 Sep 2017 14:53:00 +0800 Subject: [PATCH] =?UTF-8?q?appserver=EF=BC=9A=E4=BA=A7=E5=93=81=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E5=8A=A0=E5=85=A5=E8=B4=AD=E7=89=A9=E8=BD=A6=E5=92=8C?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/appserver/config/modules/Checkout.php | 15 ++ app/appserver/modules/AppserverController.php | 2 + .../Catalog/controllers/ProductController.php | 31 ++- app/appserver/modules/Checkout/Module.php | 41 ++++ .../Checkout/controllers/CartController.php | 216 ++++++++++++++++++ 5 files changed, 304 insertions(+), 1 deletion(-) create mode 100644 app/appserver/config/modules/Checkout.php create mode 100644 app/appserver/modules/Checkout/Module.php create mode 100644 app/appserver/modules/Checkout/controllers/CartController.php diff --git a/app/appserver/config/modules/Checkout.php b/app/appserver/config/modules/Checkout.php new file mode 100644 index 000000000..610e10c71 --- /dev/null +++ b/app/appserver/config/modules/Checkout.php @@ -0,0 +1,15 @@ + [ + 'class' => '\fecshop\app\appserver\modules\Checkout\Module', + 'params'=> [ + ], + ] +]; \ No newline at end of file diff --git a/app/appserver/modules/AppserverController.php b/app/appserver/modules/AppserverController.php index a554288d2..790c04b36 100644 --- a/app/appserver/modules/AppserverController.php +++ b/app/appserver/modules/AppserverController.php @@ -31,6 +31,8 @@ public function init() { parent::init(); Yii::$app->user->enableSession = false; + // 如果用户登录,会在header中传递access-token,这个函数就会登录用户。 + Yii::$service->customer->loginByAccessToken(); } public function behaviors() diff --git a/app/appserver/modules/Catalog/controllers/ProductController.php b/app/appserver/modules/Catalog/controllers/ProductController.php index ef89a2182..973366bba 100644 --- a/app/appserver/modules/Catalog/controllers/ProductController.php +++ b/app/appserver/modules/Catalog/controllers/ProductController.php @@ -42,6 +42,35 @@ class ProductController extends AppserverController protected $_currentSpuAttrValArr; protected $_spuAttrShowAsImgArr; + public function actionFavorite(){ + if(Yii::$app->user->isGuest){ + return [ + 'code' => 400, + 'content' => 'no login', + ]; + } + $product_id = Yii::$app->request->get('product_id'); + $identity = Yii::$app->user->identity; + $user_id = $identity->id; + + $addStatus = Yii::$service->product->favorite->add($product_id, $user_id); + if (!$addStatus) { + $errors = Yii::$service->helper->errors->get(); + return [ + 'code' => 401, + 'content' => $errors, + ]; + }else{ + return [ + 'code' => 200, + 'content' => 'success', + ]; + } + // 收藏失败,需要登录 + + + } + // 网站信息管理 public function actionIndex() { @@ -121,7 +150,7 @@ public function actionIndex() 'options' => $this->getSameSpuInfo(), 'custom_option' => $custom_option, 'description' => Yii::$service->store->getStoreAttrVal($this->_product['description'], 'description'), - '_id' => $this->_product['_id'], + '_id' => (string)$this->_product['_id'], 'buy_also_buy' => $this->getProductBySkus(), ] ] diff --git a/app/appserver/modules/Checkout/Module.php b/app/appserver/modules/Checkout/Module.php new file mode 100644 index 000000000..9a61d5d2f --- /dev/null +++ b/app/appserver/modules/Checkout/Module.php @@ -0,0 +1,41 @@ + + * @since 1.0 + * Checkout Module 模块 + */ +class Module extends AppserverModule +{ + public $blockNamespace; + public function init() + { + // 以下代码必须指定 + $nameSpace = __NAMESPACE__; + // 如果 Yii::$app 对象是由类\yii\web\Application 实例化出来的。 + if (Yii::$app instanceof \yii\web\Application) { + // 设置模块 controller namespace的文件路径 + $this->controllerNamespace = $nameSpace . '\\controllers'; + // 设置模块block namespace的文件路径 + $this->blockNamespace = $nameSpace . '\\block'; + // console controller + //} elseif (Yii::$app instanceof \yii\console\Application) { + // $this->controllerNamespace = $nameSpace . '\\console\\controllers'; + // $this->blockNamespace = $nameSpace . '\\console\\block'; + } + //$this->_currentDir = __DIR__ ; + //$this->_currentNameSpace = __NAMESPACE__; + // 设置该模块的view(theme)的默认layout文件。 + Yii::$service->page->theme->layoutFile = 'one_step_checkout.php'; + parent::init(); + } +} \ No newline at end of file diff --git a/app/appserver/modules/Checkout/controllers/CartController.php b/app/appserver/modules/Checkout/controllers/CartController.php new file mode 100644 index 000000000..411586b07 --- /dev/null +++ b/app/appserver/modules/Checkout/controllers/CartController.php @@ -0,0 +1,216 @@ + + * @since 1.0 + */ +class CartController extends AppserverController +{ + public $enableCsrfValidation = false; + public function actionIndex() + { + $data = $this->getBlock()->getLastData(); + return $this->render($this->action->id, $data); + } + /** + * ѲƷ뵽ﳵ. + */ + public function actionAdd() + { + //echo 1;exit; + $custom_option = Yii::$app->request->get('custom_option'); + $product_id = Yii::$app->request->get('product_id'); + $qty = Yii::$app->request->get('qty'); + //$custom_option = \Yii::$service->helper->htmlEncode($custom_option); + $product_id = \Yii::$service->helper->htmlEncode($product_id); + $qty = \Yii::$service->helper->htmlEncode($qty); + $qty = abs(ceil((int) $qty)); + $return = []; + $code = 400; + if ($qty && $product_id) { + if ($custom_option) { + $custom_option_sku = json_decode($custom_option, true); + } + if (empty($custom_option_sku)) { + $custom_option_sku = null; + } + $item = [ + 'product_id' => $product_id, + 'qty' => $qty, + 'custom_option_sku' => $custom_option_sku, + ]; + $innerTransaction = Yii::$app->db->beginTransaction(); + try { + $addToCart = Yii::$service->cart->addProductToCart($item); + if ($addToCart) { + $return = [ + 'status' => 'success', + 'items_count' => Yii::$service->cart->quote->getCartItemCount(), + ]; + $code = 200; + $innerTransaction->commit(); + } else { + $errors = Yii::$service->helper->errors->get(','); + $return = [ + 'status' => 'fail', + 'content'=> $errors, + //'items_count' => Yii::$service->cart->quote->getCartItemCount(), + ]; + $code = 400; + $innerTransaction->rollBack(); + } + } catch (Exception $e) { + $innerTransaction->rollBack(); + } + } + + return [ + 'code' => $code , + 'content' => $return , + ]; + } + /** + * ﳵŻȯ. + */ + public function actionAddcoupon() + { + if (Yii::$app->user->isGuest) { + // һµ¼ɹعﳵҳ + $cartUrl = Yii::$service->url->getUrl('checkout/cart'); + Yii::$service->customer->setLoginSuccessRedirectUrl($cartUrl); + echo json_encode([ + 'status' => 'fail', + 'content'=> 'nologin', + ]); + exit; + } + $coupon_code = trim(Yii::$app->request->post('coupon_code')); + $coupon_code = \Yii::$service->helper->htmlEncode($coupon_code); + if ($coupon_code) { + $innerTransaction = Yii::$app->db->beginTransaction(); + try { + if (Yii::$service->cart->coupon->addCoupon($coupon_code)) { + $innerTransaction->commit(); + } else { + $innerTransaction->rollBack(); + } + } catch (Exception $e) { + $innerTransaction->rollBack(); + } + $error_arr = Yii::$service->helper->errors->get(true); + if (!empty($error_arr)) { + $error_str = implode(',', $error_arr); + echo json_encode([ + 'status' => 'fail', + 'content'=> $error_str, + ]); + exit; + } else { + echo json_encode([ + 'status' => 'success', + 'content'=> 'add coupon success', + ]); + exit; + } + } else { + echo json_encode([ + 'status' => 'fail', + 'content'=> 'coupon is empty', + ]); + exit; + } + } + /** + * ﳵȡŻȯ. + */ + public function actionCancelcoupon() + { + if (Yii::$app->user->isGuest) { + // һµ¼ɹعﳵҳ + $cartUrl = Yii::$service->url->getUrl('checkout/cart'); + Yii::$service->customer->setLoginSuccessRedirectUrl($cartUrl); + echo json_encode([ + 'status' => 'fail', + 'content'=> 'nologin', + ]); + exit; + } + $coupon_code = trim(Yii::$app->request->post('coupon_code')); + if ($coupon_code) { + $innerTransaction = Yii::$app->db->beginTransaction(); + try { + $cancelStatus = Yii::$service->cart->coupon->cancelCoupon($coupon_code); + if (!$cancelStatus) { + echo json_encode([ + 'status' => 'fail', + 'content'=> 'coupon is not exist;', + ]); + $innerTransaction->rollBack(); + exit; + } + $error_arr = Yii::$service->helper->errors->get(true); + if (!empty($error_arr)) { + $error_str = implode(',', $error_arr); + echo json_encode([ + 'status' => 'fail', + 'content'=> $error_str, + ]); + $innerTransaction->rollBack(); + exit; + } else { + echo json_encode([ + 'status' => 'success', + 'content'=> 'cacle coupon success', + ]); + $innerTransaction->commit(); + exit; + } + } catch (Exception $e) { + $innerTransaction->rollBack(); + } + } else { + echo json_encode([ + 'status' => 'fail', + 'content'=> 'coupon is empty', + ]); + exit; + } + } + public function actionUpdateinfo() + { + $item_id = Yii::$app->request->get('item_id'); + $up_type = Yii::$app->request->get('up_type'); + $innerTransaction = Yii::$app->db->beginTransaction(); + try { + if ($up_type == 'add_one') { + $status = Yii::$service->cart->addOneItem($item_id); + } elseif ($up_type == 'less_one') { + $status = Yii::$service->cart->lessOneItem($item_id); + } elseif ($up_type == 'remove') { + $status = Yii::$service->cart->removeItem($item_id); + } + if ($status) { + echo json_encode([ + 'status' => 'success', + ]); + $innerTransaction->commit(); + } else { + echo json_encode([ + 'status' => 'fail', + ]); + $innerTransaction->rollBack(); + } + } catch (Exception $e) { + $innerTransaction->rollBack(); + } + } +} \ No newline at end of file