| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\models\BusinessCart;
- use app\models\Goods;
- use app\models\Store;
- use yii\base\Model;
- class CartForm extends Model
- {
- public $saas_id;
- public $store_id;
- public $goods_id;
- public $attr;
- public $num;
- public $cart_id_list;
- public function rules()
- {
- return [
- //[['goods_id', 'attr', 'num','cart_id_list'], 'required'],
- [['goods_id', 'num','saas_id'], 'integer'],
- [['num'], 'integer', 'min' => 1],
- [['cart_id_list','attr'],'string']
- ];
- }
- public function save()
- {
- try {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- $goods = Goods::findOne([
- 'id' => $this->goods_id,
- //'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'status' => 1,
- ]);
- if (!$goods) {
- return [
- 'code' => 1,
- 'msg' => '商品不存在或已下架',
- ];
- }
- $store = \app\models\Store::findOne(['id' => $goods->store_id, 'is_delete' => 0, 'business_model' => 2]);
- if (!$store) {
- return [
- 'code' => 1,
- 'msg' => '店铺不存在或非平台运营',
- ];
- }
- $this->attr = json_decode($this->attr, true);
- $attr = [];
- foreach ($this->attr as $item) {
- if (!empty($item['attr_id'])) {
- $attr[] = intval($item['attr_id']);
- }
- }
- sort($attr);
- $attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
- $cart = BusinessCart::findOne([
- 'store_id' => $goods->store_id,
- 'goods_id' => $this->goods_id,
- 'saas_id' => $this->saas_id,
- //'md_id' => get_md_id(),
- 'is_delete' => 0,
- 'attr' => $attr,
- ]);
- if (!$cart) {
- $cart = new BusinessCart();
- $cart->store_id = $goods->store_id;
- $cart->goods_id = $this->goods_id;
- $cart->saas_id = $this->saas_id;
- $cart->num = 0;
- $cart->created_at = time();
- $cart->is_delete = 0;
- $cart->attr = $attr;
- // if (get_md_id()) {
- // $cart->md_id = get_md_id();
- // }
- }
- $cart->num += $this->num;
- if ($cart->save()) {
- return [
- 'code' => 0,
- 'msg' => '添加购物车成功',
- ];
- } else {
- return ['code' => 1, 'msg' => $cart->errors[0]];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 0,
- 'msg' => $e->getMessage() . 'line=' . $e->getLine()
- ];
- }
- }
- public function del()
- {
- if (!$this->validate()) {
- return $this->errorResponse;
- }
- try {
- $this->cart_id_list = json_decode($this->cart_id_list, true);
- foreach ($this->cart_id_list as $cart_id) {
- $cart = BusinessCart::findOne([
- //'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'saas_id' => $this->saas_id,
- 'id' => $cart_id,
- ]);
- if (!$cart) {
- continue;
- }
- $cart->is_delete = 1;
- $cart->save();
- }
- return [
- 'code' => 0,
- 'msg' => '删除完成',
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getLine(),
- ];
- }
- }
- public function clear()
- {
- try {
- $store_id = $this->store_id;
- $saas_id = $this->saas_id;
- $cart = BusinessCart::findOne([
- 'is_delete' => 0,
- 'saas_id' => $saas_id,
- 'store_id' => $store_id,
- ]);
- if (!$cart) {
- throw new \Exception('购物车列表为空');
- }
- BusinessCart::updateAll(['is_delete' => 1], [
- 'is_delete' => 0,
- 'saas_id' => $saas_id,
- 'store_id' => $store_id,
- ]);
- return [
- 'code' => 0,
- 'msg' => '清空完成',
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getLine(),
- ];
- }
- }
- }
|