Browse Source

feat: 增加地址api并增加查看详情和删除操作的权限判断

wechat_public_accounts
root 5 years ago
parent
commit
f97120a968
  1. 2
      api/config/main.php
  2. 43
      api/controllers/AddressController.php
  3. 6
      api/controllers/AdminController.php
  4. 24
      api/controllers/CartController.php
  5. 38
      api/controllers/CommonController.php
  6. 16
      api/controllers/GoodsController.php
  7. 16
      api/controllers/OrderController.php
  8. 68
      api/logic/AddressLogic.php
  9. 29
      api/logic/CartLogic.php
  10. 2
      api/logic/Helper.php
  11. 18
      api/logic/OrderLogic.php
  12. 3
      backend/models/ars/Order.php
  13. 1
      common/config/bootstrap.php
  14. 131
      composer.lock

2
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',

43
api/controllers/AddressController.php

@ -0,0 +1,43 @@
<?php
namespace api\controllers;
use backend\models\ars\Address;
use Yii;
use yii\web\NotFoundHttpException;
/**
* @author iron
* @email weiriron@gmail.com
*/
class AddressController extends CommonController
{
public $modelClass = 'backend\models\ars\Address';
public function actions()
{
$action = parent::actions();
return $action;
}
public function actionIndex()
{
return Address::find()
->where(['user_id' => Yii::$app->user->getId()])
->all();
}
public function actionUpdate()
{
}
public function actionCreate()
{
return Yii::$app->address->create();
}
}

6
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;

24
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();
}
}

38
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);
}
}
}

16
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;
}

16
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
}
}

68
api/logic/AddressLogic.php

@ -0,0 +1,68 @@
<?php
namespace api\logic;
use backend\models\ars\Address;
use Yii;
use yii\base\Component;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
/**
* @author iron
* @email weiriron@gmail.com
* Class CartLogic
* @package api\logic
*/
class AddressLogic extends Component
{
public $viewAction = 'view';
/**
* @return Address
* @throws BadRequestHttpException
* @throws ServerErrorHttpException
*/
public function create()
{
$data['consignee'] = Yii::$app->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('地址未找到');
}
}
}

29
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) {

2
api/logic/Helper.php

@ -19,6 +19,8 @@ use Yii;
*/
class Helper
{
const REQUEST_BAD_PARAMS = '参数缺失或包含无效参数';
/**
* @param $array
* @return string

18
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) {

3
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;
}
/**

1
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');

131
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

Loading…
Cancel
Save