| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace addons\Purchase\api\modules\v1\controllers;
- use addons\Purchase\common\models\PurchaseCart;
- use addons\Purchase\common\models\PurchaseManager;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- /**
- * Class CartController
- * @package api\modules\goods\controllers
- */
- class CartController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['add'];
- /**
- * 添加购物车
- * @return mixed|void
- */
- public function actionAdd()
- {
- try {
- $params = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($params, [
- [['goods_id', 'num'], 'required'],
- [['attr_id'], 'safe'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $goods = Goods::findOne(['id' => $params['goods_id'], 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]);
- if (empty($goods)) return $this->error("商品不存在");
- // 检查用户申请是否通过
- if (PurchaseManager::check($this->user_id) == false) return $this->error("请申请资格");
- // 没有规格id则查找默认规格【寻找价格最低的规格】
- $where = ['goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED];
- !empty($params['attr_id']) && $where['id'] = $params['attr_id'];
- $goods_attr = GoodsAttr::find()->where($where)->orderBy('price asc')->one();
- if ($goods_attr->goods_id != $params['goods_id']) return $this->error("goods_id与attr_id不配对");
- if (empty($goods->is_attr)) {
- if (empty($goods->stock)) return $this->error('库存不足');
- } else {
- //多规格
- if (empty($goods_attr)) return $this->error("所选的规格不存在");
- if (empty($goods_attr->stock)) return $this->error("所选的规格库存不足");
- }
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- PurchaseCart::addCart($params);
- return $this->success('加入购物车成功', []);
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- }
- /**
- * 删除购物车
- * @return mixed|void
- */
- public function actionDel()
- {
- $params = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($params, [
- [['list'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- PurchaseCart::delCart($params);
- return $this->success('操作成功', []);
- }
- /**
- * 购物车列表
- * @return mixed|void
- */
- public function actionIndex()
- {
- $list = PurchaseCart::getCartList($this->user_id);
- $data = [];
- if (!empty($list)) {
- $attr_id_arr = [];
- $num_arr = [];
- foreach ($list as $v) {
- $row = json_decode($v, true);
- if ($row) {
- $attr_id_arr[] = $row['attr_id'];
- $num_arr[$row['attr_id']] = $row['num'];
- }
- }
- $params['where'] = [['id' => $attr_id_arr]];
- $params['with'] = ['goods' => function ($query) {
- $query->select('id,goods_name,is_on_sale,cover_pic,is_attr')->with(['extra']);
- }];
- $params['select'] = 'id,sign_id,goods_id,pic_url,price,name';
- $list = GoodsAttr::lists($params);
- $rows = [];
- if (!empty($list)) {
- foreach ($list as $v) {
- $rows[] = [
- 'goods_id' => $v['goods_id'],
- 'num' => $num_arr[$v['id']],
- 'attr_id' => $v['id'],
- 'price' => $v['price'],
- 'cover_pic' => $v['goods']['cover_pic']??'',
- 'attr' => $v['name'],
- 'goods_name' => $v['goods']['goods_name'],
- 'is_on_sale' => $v['goods']['is_on_sale'],
- ];
- }
- }
- $mall_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.name');
- $mall_logo = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.logo');
- $mch_arr = [
- ['id' => 0, 'mch_pic' => $mall_logo, 'mch_name' => $mall_name, 'list' => $rows]
- ];
- }
- return $this->success('操作成功', $mch_arr);
- }
- /**
- * @notes:修改购物车
- * @return mixed|null
- * @author: lun
- * @Time: 2022/10/24 14:56
- */
- public function actionUpdateCart()
- {
- try {
- $params = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($params, [
- [['goods_id', 'num'], 'required'],
- [['attr_id'], 'safe'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $goods = Goods::findOne(['id' => $params['goods_id'], 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]);
- if (empty($goods)) return $this->error("商品不存在");
- // 没有规格id则查找默认规格【寻找价格最低的规格】
- $where = ['goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED];
- !empty($params['attr_id']) && $where['id'] = $params['attr_id'];
- $goods_attr = GoodsAttr::find()->where($where)->orderBy('price asc')->one();
- if ($goods_attr->goods_id != $params['goods_id']) return $this->error("goods_id与attr_id不配对");
- if (empty($goods->is_attr)) {
- if (empty($goods->stock)) return $this->error('库存不足');
- } else {
- //多规格
- if (empty($goods_attr)) return $this->error("所选的规格不存在");
- if (empty($goods_attr->stock)) return $this->error("所选的规格库存不足");
- }
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- PurchaseCart::updateCart($params);
- return $this->success('更新购物车成功', []);
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- }
- }
|