| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- /*
- * @Author: your name
- * @Date: 2021-03-02 09:50:20
- * @LastEditTime: 2021-05-15 11:46:19
- * @LastEditors: Please set LastEditors
- * @Description: In User Settings Edit
- * @FilePath: \admin_php\modules\client\models\v1\AddCartForm.php
- */
- namespace app\modules\client\models\v1;
- use app\models\Cart;
- use app\models\Goods;
- use app\models\Level;
- use app\models\Order;
- use app\models\User;
- use yii\base\Model;
- class AddCartForm extends Model
- {
- public $user_id;
- public $store_id;
- public $goods_id;
- public $attr;
- public $num;
- public function rules()
- {
- return [
- [['goods_id', 'attr', 'num'], 'required'],
- [['goods_id', 'num'], 'integer'],
- [['num'], 'integer', 'min' => 1],
- ];
- }
- public function save($params = [])
- {
- try {
- $type = (int)$params['type'];
- 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' => '商品不存在或已下架',
- ];
- }
- //判断会员是否可以购买
- // if (!empty($goods->buy_type_info)) {
- // $buy_type_info = json_decode($goods->buy_type_info, true);
- // if ((int)$buy_type_info['type'] === 1) { //会员购买
- // if ((int)$buy_type_info['member_level'] !== 0) {
- // $user = User::findOne($this->user_id);
- // if ((int)$user->level !== (int)$buy_type_info['member_level']) {
- // $level = Level::findOne(['level' => $buy_type_info['member_level']])->name ?? "普通会员";
- // return [
- // 'code' => 1,
- // 'msg' => '当前商品仅支持' . $level . '购买',
- // ];
- // }
- // }
- // }
- // if ((int)$buy_type_info['type'] === 2) { //新用户购买
- // $order = Order::findOne(['user_id' => $this->user_id]);
- // if (!empty($order)) {
- // 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 = Cart::findOne([
- 'type'=>$type,
- 'store_id' => $this->store_id,
- 'goods_id' => $this->goods_id,
- 'user_id' => $this->user_id,
- 'md_id' => get_md_id(),
- 'is_delete' => 0,
- 'attr' => $attr,
- ]);
- if (!$cart) {
- $cart = new Cart();
- $cart->type = $type;
- $cart->store_id = $this->store_id;
- $cart->goods_id = $this->goods_id;
- $cart->mch_id = $goods->mch_id;
- $cart->user_id = $this->user_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()
- ];
- }
- }
- }
|