Skip to content

Commit

Permalink
appserver:产品页面加入购物车和支付部分
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyecommerce committed Sep 14, 2017
1 parent c93336a commit c47652a
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/appserver/config/modules/Checkout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
return [
'checkout' => [
'class' => '\fecshop\app\appserver\modules\Checkout\Module',
'params'=> [
],
]
];
2 changes: 2 additions & 0 deletions app/appserver/modules/AppserverController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function init()
{
parent::init();
Yii::$app->user->enableSession = false;
// 如果用户登录,会在header中传递access-token,这个函数就会登录用户。
Yii::$service->customer->loginByAccessToken();
}

public function behaviors()
Expand Down
31 changes: 30 additions & 1 deletion app/appserver/modules/Catalog/controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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(),
]
]
Expand Down
41 changes: 41 additions & 0 deletions app/appserver/modules/Checkout/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appserver\modules\Checkout;
use fecshop\app\appserver\modules\AppserverModule;
use Yii;
/**
* @author Terry Zhao <2358269014@qq.com>
* @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();
}
}
216 changes: 216 additions & 0 deletions app/appserver/modules/Checkout/controllers/CartController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appserver\modules\Checkout\controllers;
use fecshop\app\appserver\modules\AppserverController;
use Yii;
/**
* @author Terry Zhao <2358269014@qq.com>
* @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();
}
}
}

0 comments on commit c47652a

Please sign in to comment.