From f97120a968d3933e9d2919b33173a364c12b2d52 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 5 Dec 2019 15:26:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=9C=B0=E5=9D=80api?= =?UTF-8?q?=E5=B9=B6=E5=A2=9E=E5=8A=A0=E6=9F=A5=E7=9C=8B=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E5=92=8C=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C=E7=9A=84=E6=9D=83?= =?UTF-8?q?=E9=99=90=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/config/main.php | 2 + api/controllers/AddressController.php | 43 +++++++++ api/controllers/AdminController.php | 6 -- api/controllers/CartController.php | 24 +++-- api/controllers/CommonController.php | 38 ++++---- api/controllers/GoodsController.php | 16 ++-- api/controllers/OrderController.php | 16 +++- api/logic/AddressLogic.php | 68 +++++++++++++ api/logic/CartLogic.php | 29 +----- api/logic/Helper.php | 2 + api/logic/OrderLogic.php | 18 ++-- backend/models/ars/Order.php | 3 +- common/config/bootstrap.php | 1 + composer.lock | 131 ++++++++++++++++++++++++-- 14 files changed, 315 insertions(+), 82 deletions(-) create mode 100644 api/controllers/AddressController.php create mode 100644 api/logic/AddressLogic.php diff --git a/api/config/main.php b/api/config/main.php index df282b9..5f49a31 100644 --- a/api/config/main.php +++ b/api/config/main.php @@ -14,6 +14,7 @@ return [ 'cartLogic' => ['class' => 'api\logic\CartLogic'], 'orderLogic' => ['class' => 'api\logic\OrderLogic'], 'userLogic' => ['class' => 'api\logic\UserLogic'], + 'address' => ['class' => 'api\logic\AddressLogic'], 'request' => [ 'parsers' => [ 'application/json' => 'yii\web\JsonParser', @@ -47,6 +48,7 @@ return [ 'showScriptName' => false, 'rules' => [ ['class' => 'yii\rest\UrlRule', 'controller' => 'goods', 'pluralize' => false], + ['class' => 'yii\rest\UrlRule', 'controller' => 'address', 'pluralize' => false], ['class' => 'yii\rest\UrlRule', 'controller' => 'cart'], ['class' => 'yii\rest\UrlRule', 'controller' => 'order'], ['class' => 'yii\rest\UrlRule', diff --git a/api/controllers/AddressController.php b/api/controllers/AddressController.php new file mode 100644 index 0000000..88c2016 --- /dev/null +++ b/api/controllers/AddressController.php @@ -0,0 +1,43 @@ +where(['user_id' => Yii::$app->user->getId()]) + ->all(); + } + + public function actionUpdate() + { + + } + + public function actionCreate() + { + return Yii::$app->address->create(); + } + +} diff --git a/api/controllers/AdminController.php b/api/controllers/AdminController.php index 3403749..dc2adaa 100644 --- a/api/controllers/AdminController.php +++ b/api/controllers/AdminController.php @@ -2,12 +2,6 @@ namespace api\controllers; -use common\models\ars\Config; -use backend\models\User; -use yii\base\NotSupportedException; -use yii\filters\auth\HttpBearerAuth; -use yii\helpers\ArrayHelper; -use yii\helpers\Url; use yii\rest\ActiveController; use Yii; diff --git a/api/controllers/CartController.php b/api/controllers/CartController.php index b7e3416..8c7c54d 100644 --- a/api/controllers/CartController.php +++ b/api/controllers/CartController.php @@ -2,6 +2,7 @@ namespace api\controllers; +use backend\models\ars\Cart; use yii\db\ActiveRecord; use yii\rest\ActiveController; use Yii; @@ -14,6 +15,19 @@ class CartController extends CommonController { public $modelClass = 'common\models\ars\Cart'; + public function actions() + { + $action = parent::actions(); + unset($action['index']); + return $action; + } + + public function actionIndex() + { + return Cart::find() + ->where(['user_id' => Yii::$app->user->getId()]) + ->all(); + } /** * @return bool @@ -34,14 +48,8 @@ class CartController extends CommonController $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); + return Yii::$app->cartLogic->create($goodsId, $count, $skuId); } - /** - * 删除商品 - */ - public function actionDelete() - { - Yii::$app->cartLogic->delete(); - } + } diff --git a/api/controllers/CommonController.php b/api/controllers/CommonController.php index cde7cf6..bcf1ddc 100644 --- a/api/controllers/CommonController.php +++ b/api/controllers/CommonController.php @@ -5,7 +5,8 @@ namespace api\controllers; use yii\filters\auth\HttpBearerAuth; use yii\helpers\ArrayHelper; use yii\rest\ActiveController; - +use yii\web\NotFoundHttpException; +use Yii; /** * @author iron * @email weiriron@gmail.com @@ -13,6 +14,8 @@ use yii\rest\ActiveController; class CommonController extends ActiveController { + public $searchModel; + public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ @@ -26,28 +29,31 @@ class CommonController extends ActiveController 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() - ]; + unset($action['index']); 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; + return []; } - + public function checkAccess($action, $model = null, $params = []) + { + if ($model && isset($model->user_id) && $model->user_id !== Yii::$app->user->getId()) { + switch ($action) { + case 'view': + $message = '您无权访问该数据'; + break; + case 'delete': + $message = '您无权删除该数据'; + break; + default: + $message = '无相关权限'; + } + throw new NotFoundHttpException($message); + } + } } diff --git a/api/controllers/GoodsController.php b/api/controllers/GoodsController.php index 38aab47..d3fcd3c 100644 --- a/api/controllers/GoodsController.php +++ b/api/controllers/GoodsController.php @@ -12,13 +12,12 @@ use yii\rest\ActiveController; */ class GoodsController extends CommonController { - public $modelClass = 'antgoods\goods\models\ars\Goods'; + public $modelClass = '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, @@ -31,18 +30,17 @@ class GoodsController extends CommonController { $keyword = \Yii::$app->request->getBodyParam('keyword'); $category = \Yii::$app->request->getBodyParam('category'); - if (empty($keyword) && empty($category)) { - return null; - } - $array = []; + $data = \Yii::$app->request->getBodyParams(); + $data['user'] = true; + \Yii::$app->request->setBodyParams($data); if ($keyword) { - $array['name'] = ['like' => $keyword]; + $data['name'] = ['like' => $keyword]; } if ($category) { - $array['cat_id'] = $category; + $data['cat_id'] = $category; } $filter = ['class' => 'yii\data\ActiveDataFilter', - 'filter' => $array, + 'filter' => $data, 'searchModel' => ['class' => 'antgoods\goods\models\searchs\GoodsSearch']]; return $filter; } diff --git a/api/controllers/OrderController.php b/api/controllers/OrderController.php index 97f2e6b..715b1d7 100644 --- a/api/controllers/OrderController.php +++ b/api/controllers/OrderController.php @@ -13,6 +13,17 @@ class OrderController extends CommonController { public $modelClass = 'common\models\ars\Order'; + public function actions() + { + $action = parent::actions(); + unset($action['delete']); + $action['index'] = [ + 'class' => 'yii\rest\IndexAction', + 'modelClass' => $this->modelClass, + 'dataFilter' => $this->getFilter() + ]; + return $action; + } protected function getFilter() { @@ -35,7 +46,7 @@ class OrderController extends CommonController $originId = Yii::$app->request->getBodyParam('originId'); $count = Yii::$app->request->getBodyParam('count'); $skuId = Yii::$app->request->getBodyParam('skuId'); - return Yii::$app->orderLogic->addGoods($originId, $count, $skuId); + return Yii::$app->orderLogic->create($originId, $count, $skuId); } /** @@ -49,7 +60,4 @@ class OrderController extends CommonController } - - - } diff --git a/api/logic/AddressLogic.php b/api/logic/AddressLogic.php new file mode 100644 index 0000000..680e518 --- /dev/null +++ b/api/logic/AddressLogic.php @@ -0,0 +1,68 @@ +request->getBodyParam('consignee'); + $data['phone'] = Yii::$app->request->getBodyParam('phone'); + $data['province'] = Yii::$app->request->getBodyParam('province'); + $data['city'] = Yii::$app->request->getBodyParam('city'); + $data['district'] = Yii::$app->request->getBodyParam('district'); + $data['address'] = Yii::$app->request->getBodyParam('address'); + if (empty($data['consignee']) || empty($data['phone']) || empty($data['province']) || + empty($data['city']) || empty($data['district']) || empty($data['address'])) { + throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); + } + $address = new Address(); + $address->user_id = Yii::$app->user->getId(); + $address->load($data, ''); + if (!$address->save()) { + throw new ServerErrorHttpException('地址添加失败'); + } + Helper::createdResponse($address, $this->viewAction); + return $address; + } + + private function setDefaultAddress($address) + { + + } + + private function findAddress() + { + $id = Yii::$app->request->getQueryParam('id'); + $address = Address::find() + ->where(['id' => $id]) + ->andWhere(['user_id' => Yii::$app->user->getId()]) + ->one(); + if (!$address) { + throw new NotFoundHttpException('地址未找到'); + } + } + + +} \ No newline at end of file diff --git a/api/logic/CartLogic.php b/api/logic/CartLogic.php index 823c0c2..f1547d1 100644 --- a/api/logic/CartLogic.php +++ b/api/logic/CartLogic.php @@ -35,10 +35,10 @@ class CartLogic extends Component * @throws ServerErrorHttpException * 添加商品到购物车 */ - public function addGoods($goodsId, $count, $skuId) + public function create($goodsId, $count, $skuId) { if (empty($goodsId) || empty($count)) { - throw new BadRequestHttpException('无效参数'); + throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } //TODO 判断限购 //判断库存 @@ -51,7 +51,7 @@ class CartLogic extends Component throw new ServerErrorHttpException('服务器添加购物车商品失败'); } } else { - $cart = $this->create($goodsId, $skuId, $count); + $cart = $this->addGoods($goodsId, $skuId, $count); } Helper::createdResponse($cart, $this->viewAction); return $cart; @@ -67,7 +67,7 @@ class CartLogic extends Component public function addOrSubGoods($type) { if (empty($type) || ($type != self::TYPE_SUB && $type != self::TYPE_ADD)) { - throw new BadRequestHttpException('无效参数'); + throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } $cart = $this->findCart(); if (!$cart) { @@ -79,25 +79,6 @@ class CartLogic extends Component 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 @@ -108,7 +89,7 @@ class CartLogic extends Component * @throws ServerErrorHttpException * @throws NotFoundHttpException */ - private function create($goodsId, $skuId, $count) + private function addGoods($goodsId, $skuId, $count) { $goods = Goods::findOne($goodsId); if (!$goods) { diff --git a/api/logic/Helper.php b/api/logic/Helper.php index 61a34a7..f4db3fa 100644 --- a/api/logic/Helper.php +++ b/api/logic/Helper.php @@ -19,6 +19,8 @@ use Yii; */ class Helper { + const REQUEST_BAD_PARAMS = '参数缺失或包含无效参数'; + /** * @param $array * @return string diff --git a/api/logic/OrderLogic.php b/api/logic/OrderLogic.php index 7e15e98..ba3ff20 100644 --- a/api/logic/OrderLogic.php +++ b/api/logic/OrderLogic.php @@ -24,6 +24,7 @@ use yii\web\ServerErrorHttpException; */ class OrderLogic extends Component { + public $viewAction = 'view'; /*创建途径类型*/ const TYPE_ADD_GOODS_PURCHASE = 1; const TYPE_ADD_GOODS_CART = 2; @@ -89,7 +90,7 @@ class OrderLogic extends Component private function changeAddress($order, $data) { if (!isset($data['address_id'])) { - throw new BadRequestHttpException('参数缺少或无效'); + throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } if ($order->shipping_type !== Order::SHIPPING_TYPE_EXPRESS) { throw new BadRequestHttpException('配送方式异常'); @@ -117,7 +118,7 @@ class OrderLogic extends Component private function changeTakingSite($order, $data) { if (!isset($data['taking_site_id']) || empty($data['taking_site_id'])) { - throw new BadRequestHttpException('参数缺少或无效'); + throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } if ($order->shipping_type !== Order::SHIPPING_TYPE_PICKED_UP) { throw new BadRequestHttpException('配送方式异常'); @@ -138,7 +139,7 @@ class OrderLogic extends Component private function updateShippingType($order, $data) { if (!isset($data['shipping_type'])) { - throw new BadRequestHttpException('参数缺少或无效'); + throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } if ($order->status !== Order::STATUS_UNCONFIRMED) { throw new BadRequestHttpException('订单状态异常'); @@ -241,12 +242,12 @@ class OrderLogic extends Component * @throws NotFoundHttpException * 创建订单并添加商品 */ - public function addGoods($originId, $count, $skuId) + public function create($originId, $count, $skuId) { $type = Yii::$app->request->getQueryParam('type'); if (empty($type) || ($type == self::TYPE_ADD_GOODS_CART && empty($originId)) || ($type == self::TYPE_ADD_GOODS_PURCHASE && (empty($count) || empty($originId)))) { - throw new BadRequestHttpException('参数缺少或无效'); + throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS); } if ($type == self::TYPE_ADD_GOODS_PURCHASE) { $goodsId = $originId; @@ -270,10 +271,11 @@ class OrderLogic extends Component if (!$order->save()) { throw new ServerErrorHttpException('服务器创建订单失败'); } - $this->createOrderGoods($order->id, $goodsId, $skuId, $count);/*创建订单商品*/ + $this->addGoods($order->id, $goodsId, $skuId, $count);/*创建订单商品*/ $this->saveGoodsInfo($order);/*保存订单商品信息*/ $tra->commit(); - return true; + Helper::createdResponse($order, $this->viewAction); + return $order; } catch (\Exception $e) { $tra->rollBack(); throw $e; @@ -306,7 +308,7 @@ class OrderLogic extends Component * @throws ServerErrorHttpException * 创建订单商品 */ - private function createOrderGoods($id, $goodsId, $skuId, $count) + private function addGoods($id, $goodsId, $skuId, $count) { $goods = Goods::findOne($goodsId); if (!$goods) { diff --git a/backend/models/ars/Order.php b/backend/models/ars/Order.php index 2c5c285..772db5e 100644 --- a/backend/models/ars/Order.php +++ b/backend/models/ars/Order.php @@ -137,10 +137,11 @@ class Order extends \yii\db\ActiveRecord private function countShippingAmount() { //TODO 根据运费模板计算运费 + $amount = 0; if ($this->shipping_type !== Order::SHIPPING_TYPE_EXPRESS) { return 0; } - return 0; + return $amount; } /** diff --git a/common/config/bootstrap.php b/common/config/bootstrap.php index 4d8c7ee..504af63 100755 --- a/common/config/bootstrap.php +++ b/common/config/bootstrap.php @@ -3,3 +3,4 @@ Yii::setAlias('@common', dirname(__DIR__)); Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console'); Yii::setAlias('@backend', dirname(dirname(__DIR__)) . '/backend'); Yii::setAlias('@goods', dirname(dirname(__DIR__)) . '/goods'); +Yii::setAlias('@api', dirname(dirname(__DIR__)) . '/api'); diff --git a/composer.lock b/composer.lock index bdfd6be..35c31c8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,14 +4,76 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "546fcefd39f92f71d53ef5b56e5177f6", + "content-hash": "687669f68148fd8fc648686c605f5952", "packages": [ + { + "name": "antkaz/yii2-vue", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/antkaz/yii2-vue.git", + "reference": "150deabf6a08961d8876f0eaf55ca53d97badef7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/antkaz/yii2-vue/zipball/150deabf6a08961d8876f0eaf55ca53d97badef7", + "reference": "150deabf6a08961d8876f0eaf55ca53d97badef7", + "shasum": "" + }, + "require": { + "npm-asset/vue": "~2.6.0", + "yiisoft/yii2": "~2.0.14" + }, + "type": "yii2-extension", + "autoload": { + "psr-4": { + "antkaz\\vue\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Anton Kazarinov", + "email": "askazarinov@gmail.com" + } + ], + "description": "The Vue.js extension for the Yii framework", + "homepage": "https://github.com/antkaz/yii2-vue", + "keywords": [ + "Vue.js", + "vue", + "yii2" + ], + "time": "2019-04-08T06:19:27+00:00" + }, + { + "name": "bower-asset/babel", + "version": "dev-master", + "source": { + "type": "git", + "url": "git@github.com:Micua/babel.git", + "reference": "a08c19bcf39c6d6e7bfb73598e2b3148376ab6ff" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Micua/babel/zipball/a08c19bcf39c6d6e7bfb73598e2b3148376ab6ff", + "reference": "a08c19bcf39c6d6e7bfb73598e2b3148376ab6ff" + }, + "type": "bower-asset", + "license": [ + "MIT" + ], + "time": "2016-07-22T14:54:14+00:00" + }, { "name": "bower-asset/inputmask", "version": "3.3.11", "source": { "type": "git", - "url": "git@github.com:RobinHerbots/Inputmask.git", + "url": "https://github.com/RobinHerbots/Inputmask.git", "reference": "5e670ad62f50c738388d4dcec78d2888505ad77b" }, "dist": { @@ -955,6 +1017,25 @@ "MIT" ] }, + { + "name": "npm-asset/vue", + "version": "2.6.x-dev", + "source": { + "type": "git", + "url": "https://github.com/vuejs/vue.git", + "reference": "edf7df0c837557dd3ea8d7b42ad8d4b21858ade0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/vuejs/vue/zipball/edf7df0c837557dd3ea8d7b42ad8d4b21858ade0", + "reference": "edf7df0c837557dd3ea8d7b42ad8d4b21858ade0" + }, + "type": "npm-asset", + "license": [ + "MIT" + ], + "time": "2019-01-11T22:51:45+00:00" + }, { "name": "phpoffice/phpspreadsheet", "version": "dev-master", @@ -1096,18 +1177,54 @@ ], "time": "2017-10-23T01:57:42+00:00" }, + { + "name": "xj/yii2-babel", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/xjflyttp/yii2-babel.git", + "reference": "b63034971497d34347db6f050eb7bb70ea2aa870" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/xjflyttp/yii2-babel/zipball/b63034971497d34347db6f050eb7bb70ea2aa870", + "reference": "b63034971497d34347db6f050eb7bb70ea2aa870", + "shasum": "" + }, + "require": { + "bower-asset/babel": "*" + }, + "type": "library", + "autoload": { + "psr-4": { + "xj\\babel\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "xjflyttp", + "email": "xjflyttp@gmail.com" + } + ], + "description": "yii2-babel", + "time": "2016-05-16T04:46:32+00:00" + }, { "name": "yiisoft/yii2", "version": "dev-master", "source": { "type": "git", "url": "https://github.com/yiisoft/yii2-framework.git", - "reference": "0c1efae085dbf4f92db3d82bb530ad14cbc5fe83" + "reference": "811448abe28edfbda4c77c5ba07398b6f427db11" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/yiisoft/yii2-framework/zipball/0c1efae085dbf4f92db3d82bb530ad14cbc5fe83", - "reference": "0c1efae085dbf4f92db3d82bb530ad14cbc5fe83", + "url": "https://api.github.com/repos/yiisoft/yii2-framework/zipball/811448abe28edfbda4c77c5ba07398b6f427db11", + "reference": "811448abe28edfbda4c77c5ba07398b6f427db11", "shasum": "" }, "require": { @@ -1194,7 +1311,7 @@ "framework", "yii2" ], - "time": "2019-12-03T18:36:48+00:00" + "time": "2019-12-04T23:08:56+00:00" }, { "name": "yiisoft/yii2-bootstrap4", @@ -1762,6 +1879,8 @@ "kartik-v/yii2-tabs-x": 20, "kartik-v/yii2-editable": 20, "kartik-v/yii2-widget-depdrop": 20, + "antkaz/yii2-vue": 20, + "xj/yii2-babel": 20, "yiisoft/yii2-debug": 20, "kint-php/kint": 20, "yiisoft/yii2-gii": 20