root
5 years ago
20 changed files with 934 additions and 68 deletions
-
15api/config/main.php
-
2api/config/params.php
-
47api/controllers/CartController.php
-
53api/controllers/CommonController.php
-
49api/controllers/GoodsController.php
-
55api/controllers/OrderController.php
-
18api/controllers/SiteController.php
-
9api/controllers/TestController.php
-
31api/controllers/UserController.php
-
144api/logic/CartLogic.php
-
114api/logic/Helper.php
-
349api/logic/OrderLogic.php
-
6backend/config/main.php
-
5backend/controllers/OrderController.php
-
1backend/controllers/SiteController.php
-
3backend/web/css/site.css
-
45common/models/ars/Order.php
-
21vendor/antgoods/goods/src/models/ars/Goods.php
-
1vendor/antgoods/goods/src/models/searchs/GoodsSearch.php
-
8vendor/iron/grid/GridView.php
@ -0,0 +1,47 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace api\controllers; |
||||
|
|
||||
|
use yii\db\ActiveRecord; |
||||
|
use yii\rest\ActiveController; |
||||
|
use Yii; |
||||
|
|
||||
|
/** |
||||
|
* @author iron |
||||
|
* @email weiriron@gmail.com |
||||
|
*/ |
||||
|
class CartController extends CommonController |
||||
|
{ |
||||
|
public $modelClass = 'common\models\ars\Cart'; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @return bool |
||||
|
* 更新购物车(增/删商品) |
||||
|
*/ |
||||
|
public function actionUpdate() |
||||
|
{ |
||||
|
$type = Yii::$app->request->post('type'); |
||||
|
return Yii::$app->cartLogic->addOrSubGoods($type); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @return ActiveRecord |
||||
|
* 创建商品 |
||||
|
*/ |
||||
|
public function actionCreate() |
||||
|
{ |
||||
|
$goodsId = Yii::$app->request->getBodyParam('goodsId'); |
||||
|
$count = Yii::$app->request->getBodyParam('count'); |
||||
|
$skuId = Yii::$app->request->getBodyParam('skuId'); |
||||
|
return Yii::$app->cartLogic->addGoods($goodsId, $count, $skuId); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 删除商品 |
||||
|
*/ |
||||
|
public function actionDelete() |
||||
|
{ |
||||
|
Yii::$app->cartLogic->delete(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,53 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace api\controllers; |
||||
|
|
||||
|
use yii\filters\auth\HttpBearerAuth; |
||||
|
use yii\helpers\ArrayHelper; |
||||
|
use yii\rest\ActiveController; |
||||
|
|
||||
|
/** |
||||
|
* @author iron |
||||
|
* @email weiriron@gmail.com |
||||
|
*/ |
||||
|
class CommonController extends ActiveController |
||||
|
{ |
||||
|
|
||||
|
// public function behaviors()
|
||||
|
// {
|
||||
|
// return ArrayHelper::merge(parent::behaviors(), [
|
||||
|
// 'authenticatior' => [
|
||||
|
// 'class' => HttpBearerAuth::className(),
|
||||
|
// 'except' => [],
|
||||
|
// ]
|
||||
|
// ]);
|
||||
|
// }
|
||||
|
|
||||
|
public function actions() |
||||
|
{ |
||||
|
$action = parent::actions(); |
||||
|
unset($action['delete']); |
||||
|
unset($action['create']); |
||||
|
unset($action['update']); |
||||
|
$action['index'] = [ |
||||
|
'class' => 'yii\rest\IndexAction', |
||||
|
'checkAccess' => [$this, 'checkAccess'], |
||||
|
'modelClass' => $this->modelClass, |
||||
|
'dataFilter' => $this->getFilter() |
||||
|
]; |
||||
|
return $action; |
||||
|
} |
||||
|
|
||||
|
protected function getFilter() |
||||
|
{ |
||||
|
$data = \Yii::$app->request->getBodyParams(); |
||||
|
$data['user'] = true; |
||||
|
\Yii::$app->request->setBodyParams($data); |
||||
|
$filter = ['class' => 'yii\data\ActiveDataFilter', |
||||
|
'filter' => ['user_id' => \Yii::$app->user->getId()], |
||||
|
'searchModel' => ['class' => 'antgoods\goods\models\searchs\GoodsSearch']]; |
||||
|
return $filter; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,49 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace api\controllers; |
||||
|
|
||||
|
use yii\filters\auth\HttpBearerAuth; |
||||
|
use yii\helpers\ArrayHelper; |
||||
|
use yii\rest\ActiveController; |
||||
|
|
||||
|
/** |
||||
|
* @author iron |
||||
|
* @email weiriron@gmail.com |
||||
|
*/ |
||||
|
class GoodsController extends ActiveController |
||||
|
{ |
||||
|
public $modelClass = 'antgoods\goods\models\ars\Goods'; |
||||
|
|
||||
|
public function actions() |
||||
|
{ |
||||
|
$action = parent::actions(); |
||||
|
unset($action['delete']); |
||||
|
unset($action['create']); |
||||
|
$action['index'] = [ |
||||
|
'class' => 'yii\rest\IndexAction', |
||||
|
'modelClass' => $this->modelClass, |
||||
|
'dataFilter' => $this->getFilter() |
||||
|
]; |
||||
|
return $action; |
||||
|
} |
||||
|
|
||||
|
protected function getFilter() |
||||
|
{ |
||||
|
$keyword = \Yii::$app->request->getBodyParam('keyword'); |
||||
|
$category = \Yii::$app->request->getBodyParam('category'); |
||||
|
if (empty($keyword) && empty($category)) { |
||||
|
return null; |
||||
|
} |
||||
|
$array = []; |
||||
|
if ($keyword) { |
||||
|
$array['name'] = ['like' => $keyword]; |
||||
|
} |
||||
|
if ($category) { |
||||
|
$array['cat_id'] = $category; |
||||
|
} |
||||
|
$filter = ['class' => 'yii\data\ActiveDataFilter', |
||||
|
'filter' => $array, |
||||
|
'searchModel' => ['class' => 'antgoods\goods\models\searchs\GoodsSearch']]; |
||||
|
return $filter; |
||||
|
} |
||||
|
} |
@ -0,0 +1,55 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace api\controllers; |
||||
|
|
||||
|
use yii\web\ForbiddenHttpException; |
||||
|
use Yii; |
||||
|
|
||||
|
/** |
||||
|
* @author iron |
||||
|
* @email weiriron@gmail.com |
||||
|
*/ |
||||
|
class OrderController extends CommonController |
||||
|
{ |
||||
|
public $modelClass = 'common\models\ars\Order'; |
||||
|
|
||||
|
|
||||
|
protected function getFilter() |
||||
|
{ |
||||
|
$status = Yii::$app->request->getBodyParam('status'); |
||||
|
$data = \Yii::$app->request->getBodyParams(); |
||||
|
$data['user'] = true; |
||||
|
\Yii::$app->request->setBodyParams($data); |
||||
|
$array = ['user_id' => Yii::$app->user->getId()]; |
||||
|
if ($status) { |
||||
|
$array['status'] = $status; |
||||
|
} |
||||
|
$filter = ['class' => 'yii\data\ActiveDataFilter', |
||||
|
'filter' => $array, |
||||
|
'searchModel' => ['class' => 'common\models\searchs\OrderSearch']]; |
||||
|
return $filter; |
||||
|
} |
||||
|
|
||||
|
public function actionCreate() |
||||
|
{ |
||||
|
$originId = Yii::$app->request->getBodyParam('goodsId'); |
||||
|
$count = Yii::$app->request->getBodyParam('count'); |
||||
|
$skuId = Yii::$app->request->getBodyParam('skuId'); |
||||
|
return Yii::$app->orderLogic->addGoods($originId, $count, $skuId); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @return mixed |
||||
|
* @throws \yii\base\InvalidConfigException |
||||
|
*/ |
||||
|
public function actionUpdate() |
||||
|
{ |
||||
|
$data = Yii::$app->request->getBodyParams(); |
||||
|
return Yii::$app->orderLogic->update($data); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,144 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace api\logic; |
||||
|
|
||||
|
use antgoods\goods\models\ars\Goods; |
||||
|
use common\models\ars\Cart; |
||||
|
use yii\base\Component; |
||||
|
use Yii; |
||||
|
use yii\db\ActiveRecord; |
||||
|
use yii\helpers\Url; |
||||
|
use yii\web\BadRequestHttpException; |
||||
|
use yii\web\NotFoundHttpException; |
||||
|
use yii\web\ServerErrorHttpException; |
||||
|
|
||||
|
/** |
||||
|
* @author iron |
||||
|
* @email weiriron@gmail.com |
||||
|
* Class CartLogic |
||||
|
* @package api\logic |
||||
|
*/ |
||||
|
class CartLogic extends Component |
||||
|
{ |
||||
|
public $viewAction = 'view'; |
||||
|
|
||||
|
const TYPE_ADD = 1; |
||||
|
const TYPE_SUB = -1; |
||||
|
|
||||
|
/** |
||||
|
* @param $goodsId |
||||
|
* @param $count |
||||
|
* @param $skuId |
||||
|
* @return array|\yii\db\ActiveRecord|null |
||||
|
* @throws BadRequestHttpException |
||||
|
* @throws NotFoundHttpException |
||||
|
* @throws ServerErrorHttpException |
||||
|
* 添加商品到购物车 |
||||
|
*/ |
||||
|
public function addGoods($goodsId, $count, $skuId) |
||||
|
{ |
||||
|
if (empty($goodsId) || empty($count)) { |
||||
|
throw new BadRequestHttpException('无效参数'); |
||||
|
} |
||||
|
//TODO 判断限购
|
||||
|
//判断库存
|
||||
|
Helper::checkStock($goodsId, $count, $skuId); |
||||
|
$cart = Cart::find()->where(['goods_id' => $goodsId]) |
||||
|
->andWhere(['user_id' => Yii::$app->user->getId()]) |
||||
|
->one(); |
||||
|
if ($cart) { |
||||
|
if (!$cart->updateCounters(['goods_count' => $count])) { |
||||
|
throw new ServerErrorHttpException('服务器添加购物车商品失败'); |
||||
|
} |
||||
|
} else { |
||||
|
$cart = $this->create($goodsId, $skuId, $count); |
||||
|
} |
||||
|
Helper::createdResponse($cart, $this->viewAction); |
||||
|
return $cart; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param int $type |
||||
|
* @return bool |
||||
|
* @throws BadRequestHttpException |
||||
|
* @throws NotFoundHttpException |
||||
|
*/ |
||||
|
public function addOrSubGoods($type) |
||||
|
{ |
||||
|
if (empty($type) || ($type != self::TYPE_SUB && $type != self::TYPE_ADD)) { |
||||
|
throw new BadRequestHttpException('无效参数'); |
||||
|
} |
||||
|
$cart = $this->findCart(); |
||||
|
if (!$cart) { |
||||
|
throw new NotFoundHttpException('未找到该购物车'); |
||||
|
} |
||||
|
if ($cart->goods_count == 1 && $type == self::TYPE_SUB) { |
||||
|
throw new BadRequestHttpException('购物商品需至少1件'); |
||||
|
} |
||||
|
return $cart->updateCounters(['goods_count' => $type]); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @throws NotFoundHttpException |
||||
|
* @throws ServerErrorHttpException |
||||
|
* @throws \Throwable |
||||
|
* @throws \yii\db\StaleObjectException |
||||
|
* 删除购物车 |
||||
|
*/ |
||||
|
public function delete() |
||||
|
{ |
||||
|
$cart = $this->findCart(); |
||||
|
if (!$cart) { |
||||
|
throw new NotFoundHttpException('未找到该购物车'); |
||||
|
} |
||||
|
if ($cart->delete()) { |
||||
|
Yii::$app->getResponse()->setStatusCode(204); |
||||
|
} else { |
||||
|
throw new ServerErrorHttpException('服务器无法删除购物车'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $goodsId |
||||
|
* @param $skuId |
||||
|
* @param $count |
||||
|
* @return Cart; |
||||
|
* 创建新购物车 |
||||
|
* @throws ServerErrorHttpException |
||||
|
* @throws NotFoundHttpException |
||||
|
*/ |
||||
|
private function create($goodsId, $skuId, $count) |
||||
|
{ |
||||
|
$goods = Goods::findOne($goodsId); |
||||
|
if (!$goods) { |
||||
|
throw new NotFoundHttpException('商品未找到'); |
||||
|
} |
||||
|
$cart = new Cart(); |
||||
|
$cart->goods_id = $goods->id; |
||||
|
$cart->user_id = Yii::$app->user->getId(); |
||||
|
$cart->goods_count = $count; |
||||
|
$cart->sku_id = $skuId ?: 0; |
||||
|
$cart->goods_img = $goods->image; |
||||
|
$cart->goods_name = $goods->name; |
||||
|
$cart->goods_price = Helper::countPrice($goodsId, $skuId); |
||||
|
if (!$cart->save()) { |
||||
|
throw new ServerErrorHttpException('服务器创建购物车失败'); |
||||
|
} |
||||
|
return $cart; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @return ActiveRecord|null|Cart |
||||
|
* 获取购物车实例 |
||||
|
*/ |
||||
|
private function findCart() |
||||
|
{ |
||||
|
$id = Yii::$app->request->getQueryParam('id'); |
||||
|
return Cart::find() |
||||
|
->where(['id' => $id]) |
||||
|
->andWhere(['user_id' => Yii::$app->user->getId()]) |
||||
|
->one(); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,114 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace api\logic; |
||||
|
|
||||
|
use antgoods\goods\models\ars\Goods; |
||||
|
use antgoods\goods\models\ars\GoodsAttr; |
||||
|
use antgoods\goods\models\ars\GoodsSku; |
||||
|
use yii\di\Instance; |
||||
|
use yii\helpers\Url; |
||||
|
use yii\web\BadRequestHttpException; |
||||
|
use yii\web\NotFoundHttpException; |
||||
|
use Yii; |
||||
|
|
||||
|
/** |
||||
|
* @author iron |
||||
|
* @email weiriron@gmail.com |
||||
|
* Class Helper |
||||
|
* @package api\logic |
||||
|
*/ |
||||
|
class Helper |
||||
|
{ |
||||
|
/** |
||||
|
* @param $array |
||||
|
* @return string |
||||
|
*/ |
||||
|
public static function errorMessageStr($array): string |
||||
|
{ |
||||
|
$data = []; |
||||
|
foreach ($array as $k => $v) { |
||||
|
$data[] = implode(' ', $v); |
||||
|
} |
||||
|
return implode(' ', $data); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $goodsId |
||||
|
* @param $count |
||||
|
* @param null $skuId |
||||
|
* @throws BadRequestHttpException |
||||
|
* @throws NotFoundHttpException |
||||
|
* 判断库存 |
||||
|
*/ |
||||
|
public static function checkStock($goodsId, $count, $skuId = null) |
||||
|
{ |
||||
|
$sku = GoodsSku::findOne($skuId); |
||||
|
$goods = Goods::findOne($goodsId); |
||||
|
if (!$goods) { |
||||
|
throw new NotFoundHttpException('商品未找到'); |
||||
|
} |
||||
|
if (!$sku && $goods->stock == Goods::UNLIMITED_STOCK) { |
||||
|
return; |
||||
|
} |
||||
|
$stock = $sku ? $sku->stock : $goods->stock; |
||||
|
if ($stock < $count) { |
||||
|
throw new BadRequestHttpException("商品{$goods->name}库存不足"); |
||||
|
} |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
public static function createdResponse($model, $view) |
||||
|
{ |
||||
|
$response = Yii::$app->getResponse()->setStatusCode(201); |
||||
|
$id = implode(',', array_values($model->getPrimaryKey(true))); |
||||
|
$response->getHeaders()->set('Location', Url::toRoute([$view, 'id' => $id], true)); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $goodsId |
||||
|
* @param $skuId |
||||
|
* @return int |
||||
|
* @throws NotFoundHttpException |
||||
|
* 计算商品价格 |
||||
|
*/ |
||||
|
public static function countPrice($goodsId, $skuId) |
||||
|
{ |
||||
|
$goods = Goods::findOne($goodsId); |
||||
|
if (!$goods) { |
||||
|
throw new NotFoundHttpException('商品未找到'); |
||||
|
} |
||||
|
$price = $goods->price; |
||||
|
$sku = GoodsSku::findOne($skuId); |
||||
|
if ($sku) { |
||||
|
$price = $sku->price; |
||||
|
} |
||||
|
return $price; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $skuId |
||||
|
* @return string |
||||
|
* @throws NotFoundHttpException |
||||
|
*/ |
||||
|
public static function skuValue($skuId) |
||||
|
{ |
||||
|
$sku = GoodsSku::findOne($skuId); |
||||
|
if ($sku) { |
||||
|
return ''; |
||||
|
} |
||||
|
$data = []; |
||||
|
$goodsAttr = array_filter(explode(',', $sku->goods_attr)); |
||||
|
if (empty($goodsAttr)) { |
||||
|
throw new NotFoundHttpException('sku无对应商品属性'); |
||||
|
} |
||||
|
foreach ($goodsAttr as $k => $v) { |
||||
|
$attr = GoodsAttr::findOne($v); |
||||
|
if (!$attr) { |
||||
|
throw new NotFoundHttpException('商品属性未找到'); |
||||
|
} |
||||
|
$data[] = $attr->attr_value; |
||||
|
} |
||||
|
return implode(',', $data); |
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,349 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace api\logic; |
||||
|
|
||||
|
use antgoods\goods\models\ars\Goods; |
||||
|
use antgoods\goods\models\ars\GoodsSku; |
||||
|
use common\models\ars\Address; |
||||
|
use common\models\ars\Cart; |
||||
|
use common\models\ars\Order; |
||||
|
use common\models\ars\OrderGoods; |
||||
|
use common\models\ars\TakingSite; |
||||
|
use yii\base\Component; |
||||
|
use Yii; |
||||
|
use yii\base\ErrorException; |
||||
|
use yii\db\ActiveRecord; |
||||
|
use yii\db\Exception; |
||||
|
use yii\helpers\Url; |
||||
|
use yii\web\BadRequestHttpException; |
||||
|
use yii\web\NotFoundHttpException; |
||||
|
use yii\web\ServerErrorHttpException; |
||||
|
|
||||
|
/** |
||||
|
* @author iron |
||||
|
* @email weiriron@gmail.com |
||||
|
* Class CartLogic |
||||
|
* @package api\logic |
||||
|
*/ |
||||
|
class OrderLogic extends Component |
||||
|
{ |
||||
|
/*创建途径类型*/ |
||||
|
const TYPE_ADD_GOODS_PURCHASE = 1; |
||||
|
const TYPE_ADD_GOODS_CART = 2; |
||||
|
/*订单操作类型*/ |
||||
|
const TYPE_CONFIRM = 1; |
||||
|
const TYPE_CANCEL = 2; |
||||
|
const TYPE_TAKE = 3; |
||||
|
const TYPE_CHANGE_SHIPPING_TYPE = 4; |
||||
|
const TYPE_CHANGE_ADDRESS = 5; |
||||
|
const TYPE_CHANGE_TAKING_SITE = 6; |
||||
|
/*仓库类型*/ |
||||
|
const TYPE_ADD_STOCK = 1; |
||||
|
const TYPE_SUB_STOCK = -1; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @param $data |
||||
|
* @return bool|\Exception|\yii\base\Exception |
||||
|
*/ |
||||
|
public function update($data) |
||||
|
{ |
||||
|
$type = Yii::$app->request->getQueryParam('type'); |
||||
|
$order = $this->findOrder(); |
||||
|
$tra = Yii::$app->db->beginTransaction(); |
||||
|
try { |
||||
|
switch ($type) { |
||||
|
case self::TYPE_CONFIRM : |
||||
|
$this->confirm($order, $data); |
||||
|
break; |
||||
|
case self::TYPE_CANCEL: |
||||
|
$this->cancel($order); |
||||
|
break; |
||||
|
case self::TYPE_TAKE: |
||||
|
$this->take($order); |
||||
|
break; |
||||
|
case self::TYPE_CHANGE_SHIPPING_TYPE: |
||||
|
$this->updateShippingType($order, $data); |
||||
|
break; |
||||
|
case self::TYPE_CHANGE_ADDRESS: |
||||
|
$this->changeAddress($order, $data); |
||||
|
break; |
||||
|
case self::TYPE_CHANGE_TAKING_SITE: |
||||
|
$this->changeTakingSite($order, $data); |
||||
|
break; |
||||
|
} |
||||
|
if (!$order->save()) { |
||||
|
throw new ServerErrorHttpException('服务器更新订单失败'); |
||||
|
} |
||||
|
$tra->commit(); |
||||
|
return true; |
||||
|
} catch (\yii\base\Exception $e) { |
||||
|
$tra->rollBack(); |
||||
|
return $e; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param Order $order |
||||
|
* @param array $data |
||||
|
* @throws BadRequestHttpException |
||||
|
* @throws NotFoundHttpException |
||||
|
*/ |
||||
|
private function changeAddress($order, $data) |
||||
|
{ |
||||
|
if (!isset($data['address_id'])) { |
||||
|
throw new BadRequestHttpException('无效参数'); |
||||
|
} |
||||
|
if ($order->shipping_type !== Order::SHIPPING_TYPE_EXPRESS) { |
||||
|
throw new BadRequestHttpException('配送方式异常'); |
||||
|
} |
||||
|
$address = Address::find() |
||||
|
->where(['id' => $data['address_id'], 'user_id' => Yii::$app->user->getId()]) |
||||
|
->one(); |
||||
|
if (!$address) { |
||||
|
throw new NotFoundHttpException('收货地址未找到'); |
||||
|
} |
||||
|
$order->consignee = $address->consignee; |
||||
|
$order->phone = $address->phone; |
||||
|
$order->city = $address->city; |
||||
|
$order->area = $address->area; |
||||
|
$order->province = $address->province; |
||||
|
$order->address = $address->address; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param Order $order |
||||
|
* @param array $data |
||||
|
* @throws BadRequestHttpException |
||||
|
* @throws NotFoundHttpException |
||||
|
*/ |
||||
|
private function changeTakingSite($order, $data) |
||||
|
{ |
||||
|
if (!isset($data['taking_site_id']) || empty($data['taking_site_id'])) { |
||||
|
throw new BadRequestHttpException('无效参数'); |
||||
|
} |
||||
|
if ($order->shipping_type !== Order::SHIPPING_TYPE_PICKED_UP) { |
||||
|
throw new BadRequestHttpException('配送方式异常'); |
||||
|
} |
||||
|
$site = TakingSite::findOne($data['taking_site_id']); |
||||
|
if (!$site) { |
||||
|
throw new NotFoundHttpException('自提点未找到'); |
||||
|
} |
||||
|
$order->taking_site = $data['taking_site_id']; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @param Order $order |
||||
|
* @param array $data |
||||
|
* @throws BadRequestHttpException |
||||
|
*/ |
||||
|
private function updateShippingType($order, $data) |
||||
|
{ |
||||
|
if (!isset($data['shipping_type'])) { |
||||
|
throw new BadRequestHttpException('无效参数'); |
||||
|
} |
||||
|
if ($order->status !== Order::STATUS_UNCONFIRMED) { |
||||
|
throw new BadRequestHttpException('订单状态异常'); |
||||
|
} |
||||
|
$order->shipping_type = $data['shipping_type']; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param Order $order |
||||
|
* @param array $data |
||||
|
* @throws BadRequestHttpException |
||||
|
* @throws NotFoundHttpException |
||||
|
* @throws ServerErrorHttpException |
||||
|
*/ |
||||
|
private function confirm($order, $data) |
||||
|
{ |
||||
|
if ($order->status !== Order::STATUS_UNCONFIRMED) { |
||||
|
throw new BadRequestHttpException('订单状态异常'); |
||||
|
} |
||||
|
if ($order->shipping_type === Order::SHIPPING_TYPE_PICKED_UP) { |
||||
|
if (!isset($data['consignee']) || !isset($data['phone']) |
||||
|
|| empty($data['consignee']) || empty($data['phone'])) { |
||||
|
throw new BadRequestHttpException('(联系人)参数无效'); |
||||
|
} |
||||
|
} |
||||
|
if (isset($data['remarks'])) { |
||||
|
$order->remarks = $data['remarks']; |
||||
|
} |
||||
|
$order->status = Order::STATUS_NONPAYMENT; |
||||
|
$this->updateStock($order, self::TYPE_SUB_STOCK); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param Order $order |
||||
|
* @throws BadRequestHttpException |
||||
|
* @throws NotFoundHttpException |
||||
|
* @throws ServerErrorHttpException |
||||
|
*/ |
||||
|
private function cancel($order) |
||||
|
{ |
||||
|
if ($order->status !== Order::STATUS_NONPAYMENT) { |
||||
|
throw new BadRequestHttpException('订单状态异常'); |
||||
|
} |
||||
|
$order->status = Order::STATUS_CANCEL; |
||||
|
$this->updateStock($order, self::TYPE_ADD_STOCK); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param Order $order |
||||
|
* @throws BadRequestHttpException |
||||
|
*/ |
||||
|
private function take($order) |
||||
|
{ |
||||
|
if ($order->status !== Order::STATUS_SHIPMENT_ALL) { |
||||
|
throw new BadRequestHttpException('订单状态异常'); |
||||
|
} |
||||
|
$order->status = Order::STATUS_FINISH; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $id |
||||
|
* @param $type |
||||
|
* @throws BadRequestHttpException |
||||
|
* @throws NotFoundHttpException |
||||
|
* @throws ServerErrorHttpException |
||||
|
*/ |
||||
|
private function updateStock($id, $type) |
||||
|
{ |
||||
|
$allOrderGoods = $this->findOrderGoods($id); |
||||
|
foreach ($allOrderGoods as $orderGoods) { |
||||
|
/*检查库存*/ |
||||
|
Helper::checkStock($orderGoods->goods_id, $orderGoods->goods_count, $orderGoods->sku_id); |
||||
|
if ($orderGoods->sku_id) { |
||||
|
$goods = GoodsSku::findOne($orderGoods->sku_id); |
||||
|
} else { |
||||
|
$goods = Goods::findOne($orderGoods->goods_id); |
||||
|
} |
||||
|
if ($type) { |
||||
|
$count = $orderGoods->goods_count; |
||||
|
} else { |
||||
|
$count = -$orderGoods->goods_count; |
||||
|
} |
||||
|
if (!$goods->updateCounters(['stock' => $count])) { |
||||
|
throw new ServerErrorHttpException('更新库存失败'); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $originId |
||||
|
* @param $count |
||||
|
* @param $skuId |
||||
|
* @return \Exception|bool; |
||||
|
* @throws BadRequestHttpException |
||||
|
* @throws NotFoundHttpException |
||||
|
* 创建订单并添加商品 |
||||
|
*/ |
||||
|
public function addGoods($originId, $count, $skuId) |
||||
|
{ |
||||
|
$type = Yii::$app->request->getQueryParam('type'); |
||||
|
if (($type === self::TYPE_ADD_GOODS_CART && empty($originId)) || |
||||
|
($type == self::TYPE_ADD_GOODS_PURCHASE && empty($count) && empty($originId))) { |
||||
|
throw new BadRequestHttpException('无效参数'); |
||||
|
} |
||||
|
if ($type === self::TYPE_ADD_GOODS_PURCHASE) { |
||||
|
$goodsId = $originId; |
||||
|
} else { |
||||
|
$cart = Cart::findOne($originId); |
||||
|
if (!$cart) { |
||||
|
throw new NotFoundHttpException('购物车未找到'); |
||||
|
} |
||||
|
$goodsId = $cart->goods_id; |
||||
|
$skuId = $cart->sku_id; |
||||
|
$count = $cart->goods_count; |
||||
|
} |
||||
|
/*检查库存*/ |
||||
|
Helper::checkStock($goodsId, $count, $skuId); |
||||
|
$tra = Yii::$app->db->beginTransaction(); |
||||
|
try { |
||||
|
$order = new Order(); |
||||
|
$order->type = Order::TYPE_SHOPPING; |
||||
|
if ($order->save()) { |
||||
|
throw new ServerErrorHttpException('服务器创建订单失败'); |
||||
|
} |
||||
|
$this->createOrderGoods($order->id, $goodsId, $skuId, $count);/*创建订单商品*/ |
||||
|
$this->saveGoodsInfo($order);/*保存订单商品信息*/ |
||||
|
$tra->commit(); |
||||
|
return true; |
||||
|
} catch (\Exception $e) { |
||||
|
$tra->rollBack(); |
||||
|
return $e; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param Order $order |
||||
|
* @throws ServerErrorHttpException |
||||
|
*/ |
||||
|
private function saveGoodsInfo($order) |
||||
|
{ |
||||
|
$orderGoods = $this->findOrderGoods($order->id); |
||||
|
foreach ($orderGoods as $goods) { |
||||
|
$order->goods_amount += $goods->price; |
||||
|
$order->goods_count += $goods->goods_count; |
||||
|
} |
||||
|
if (!$order->save()) { |
||||
|
throw new ServerErrorHttpException('服务器创建订单失败'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @param int $id |
||||
|
* @param int $goodsId |
||||
|
* @param int $skuId |
||||
|
* @param int $count |
||||
|
* @throws NotFoundHttpException |
||||
|
* @throws ServerErrorHttpException |
||||
|
* 创建订单商品 |
||||
|
*/ |
||||
|
private function createOrderGoods($id, $goodsId, $skuId, $count) |
||||
|
{ |
||||
|
$goods = Goods::findOne($goodsId); |
||||
|
if (!$goods) { |
||||
|
throw new NotFoundHttpException('商品未找到'); |
||||
|
} |
||||
|
$orderGoods = new OrderGoods(); |
||||
|
$orderGoods->order_id = $id; |
||||
|
$orderGoods->goods_id = $skuId; |
||||
|
$orderGoods->sku_value = Helper::skuValue($skuId); |
||||
|
$orderGoods->goods_count = $count; |
||||
|
$orderGoods->goods_img = $goods->image; |
||||
|
$orderGoods->goods_name = $goods->name; |
||||
|
$orderGoods->price = Helper::countPrice($goodsId, $skuId); |
||||
|
if (!$orderGoods->save()) { |
||||
|
throw new ServerErrorHttpException('服务器创建订单商品失败'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @return array|Order|null |
||||
|
* 获取订单 |
||||
|
*/ |
||||
|
private function findOrder() |
||||
|
{ |
||||
|
$id = Yii::$app->request->getQueryParam('id'); |
||||
|
return Order::find() |
||||
|
->where(['id' => $id]) |
||||
|
->andWhere(['user_id' => Yii::$app->user->getId()]) |
||||
|
->one(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $id |
||||
|
* @return array|ActiveRecord[]|OrderGoods[] |
||||
|
*/ |
||||
|
private function findOrderGoods($id) |
||||
|
{ |
||||
|
return OrderGoods::find() |
||||
|
->where(['order_id' => $id]) |
||||
|
->all(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue