AddCartForm.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * @Author: your name
  9. * @Date: 2021-03-02 09:50:20
  10. * @LastEditTime: 2021-05-15 11:46:19
  11. * @LastEditors: Please set LastEditors
  12. * @Description: In User Settings Edit
  13. * @FilePath: \admin_php\modules\client\models\v1\AddCartForm.php
  14. */
  15. namespace app\modules\client\models\v1;
  16. use app\models\Cart;
  17. use app\models\Goods;
  18. use app\models\Level;
  19. use app\models\Order;
  20. use app\models\User;
  21. use yii\base\Model;
  22. class AddCartForm extends Model
  23. {
  24. public $user_id;
  25. public $store_id;
  26. public $goods_id;
  27. public $attr;
  28. public $num;
  29. public function rules()
  30. {
  31. return [
  32. [['goods_id', 'attr', 'num'], 'required'],
  33. [['goods_id', 'num'], 'integer'],
  34. [['num'], 'integer', 'min' => 1],
  35. ];
  36. }
  37. public function save($params = [])
  38. {
  39. try {
  40. $type = (int)$params['type'];
  41. if (!$this->validate()) {
  42. return [
  43. 'code' => 1,
  44. 'msg' => $this->getErrorSummary(false)[0]
  45. ];
  46. }
  47. $goods = Goods::findOne([
  48. 'id' => $this->goods_id,
  49. 'store_id' => $this->store_id,
  50. 'is_delete' => 0,
  51. 'status' => 1,
  52. ]);
  53. if (!$goods) {
  54. return [
  55. 'code' => 1,
  56. 'msg' => '商品不存在或已下架',
  57. ];
  58. }
  59. //判断会员是否可以购买
  60. // if (!empty($goods->buy_type_info)) {
  61. // $buy_type_info = json_decode($goods->buy_type_info, true);
  62. // if ((int)$buy_type_info['type'] === 1) { //会员购买
  63. // if ((int)$buy_type_info['member_level'] !== 0) {
  64. // $user = User::findOne($this->user_id);
  65. // if ((int)$user->level !== (int)$buy_type_info['member_level']) {
  66. // $level = Level::findOne(['level' => $buy_type_info['member_level']])->name ?? "普通会员";
  67. // return [
  68. // 'code' => 1,
  69. // 'msg' => '当前商品仅支持' . $level . '购买',
  70. // ];
  71. // }
  72. // }
  73. // }
  74. // if ((int)$buy_type_info['type'] === 2) { //新用户购买
  75. // $order = Order::findOne(['user_id' => $this->user_id]);
  76. // if (!empty($order)) {
  77. // return [
  78. // 'code' => 1,
  79. // 'msg' => "当前商品仅支持新用户购买",
  80. // ];
  81. // }
  82. // }
  83. // }
  84. $this->attr = json_decode($this->attr, true);
  85. $attr = [];
  86. foreach ($this->attr as $item) {
  87. if (!empty($item['attr_id'])) {
  88. $attr[] = intval($item['attr_id']);
  89. }
  90. }
  91. sort($attr);
  92. $attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
  93. $cart = Cart::findOne([
  94. 'type'=>$type,
  95. 'store_id' => $this->store_id,
  96. 'goods_id' => $this->goods_id,
  97. 'user_id' => $this->user_id,
  98. 'md_id' => get_md_id(),
  99. 'is_delete' => 0,
  100. 'attr' => $attr,
  101. ]);
  102. if (!$cart) {
  103. $cart = new Cart();
  104. $cart->type = $type;
  105. $cart->store_id = $this->store_id;
  106. $cart->goods_id = $this->goods_id;
  107. $cart->mch_id = $goods->mch_id;
  108. $cart->user_id = $this->user_id;
  109. $cart->num = 0;
  110. $cart->created_at = time();
  111. $cart->is_delete = 0;
  112. $cart->attr = $attr;
  113. if (get_md_id()) {
  114. $cart->md_id = get_md_id();
  115. }
  116. }
  117. $cart->num += $this->num;
  118. if ($cart->save()) {
  119. return [
  120. 'code' => 0,
  121. 'msg' => '添加成功',
  122. ];
  123. } else {
  124. return ['code' => 1, 'msg' => $cart->errors[0]];
  125. }
  126. } catch (\Exception $e) {
  127. return [
  128. 'code' => 0,
  129. 'msg' => $e->getMessage() . 'line=' . $e->getLine()
  130. ];
  131. }
  132. }
  133. }