CartController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1;
  8. use app\models\Cart;
  9. use app\models\Goods;
  10. use app\models\Option;
  11. use app\modules\client\controllers\BaseController;
  12. use app\modules\client\models\v1\AddCartForm;
  13. use app\modules\client\models\v1\CartDeleteForm;
  14. use app\modules\client\models\v1\CartListForm;
  15. use yii\base\BaseObject;
  16. use yii\helpers\Json;
  17. class CartController extends BaseController
  18. {
  19. /**
  20. * 购物车列表
  21. */
  22. public function actionList()
  23. {
  24. $form = new CartListForm();
  25. $form->attributes = \Yii::$app->request->get();
  26. $form->store_id = get_store_id();
  27. $form->user_id = get_user_id();
  28. $form->cart_id_list = json_decode(\Yii::$app->request->post('cart_id_list'),true);
  29. return $this->asJson($form->search(all_params()));
  30. }
  31. /**
  32. * 添加购物车
  33. */
  34. public function actionAddCart()
  35. {
  36. if (\Yii::$app->request->isPost) {
  37. $post = post_params();
  38. $form = new AddCartForm();
  39. $form->store_id = get_store_id();
  40. $form->user_id = get_user_id();
  41. if(isset($post['attr_list']) && !empty($post['attr_list'])){
  42. $attr_list = Json::decode($post['attr_list']);
  43. $form->goods_id = $post['goods_id'];
  44. $error = 0;
  45. $goods_info = Goods::findOne($post['goods_id']);
  46. //
  47. // //加价保护start
  48. $platform_profit_strategy = Option::get('platform_profit_strategy', 0, 'store')['value'];
  49. $platform_profit_strategy = json_decode($platform_profit_strategy, true);
  50. if (!empty($platform_profit_strategy) && $goods_info->cloud_goods_id > 0) {
  51. if (intval($platform_profit_strategy['is_risk_control'])) {
  52. //开启风控
  53. $id = \Yii::$app->cache->get('risk_control_model_id_' . $post['goods_id']);
  54. if ($id && !\Yii::$app->queue->isDone($id)) {//\Yii::$app->queue->status($id)
  55. \Yii::$app->queue->remove($id);
  56. }
  57. $id =\queue_push(new \app\jobs\RiskControlModelJob(['goods_id' => $post['goods_id']]));
  58. \Yii::$app->cache->set('risk_control_model_id_' . $post['goods_id'], $id);
  59. }
  60. }
  61. foreach ($attr_list as $value){
  62. $form->attr = Json::encode($value['attr_list']);
  63. $form->num = $value['number'];
  64. $status = $form->save();
  65. if($status['code'] != 0){
  66. $error++;
  67. }
  68. }
  69. if($error > 0){
  70. return $this->asJson([
  71. 'code' => 0,
  72. 'msg' => '添加成功,' . $error . "个失败",
  73. ]);
  74. }else{
  75. return $this->asJson([
  76. 'code' => 0,
  77. 'msg' => '添加成功'
  78. ]);
  79. }
  80. }else{
  81. $form->attributes = post_params();
  82. return $this->asJson($form->save(all_params()));
  83. }
  84. }
  85. }
  86. public function actionUpdateCart()
  87. {
  88. if (\Yii::$app->request->isPost) {
  89. $post = post_params();
  90. if (!isset($post['attr']) || empty($post['attr'])) {
  91. return $this->asJson([
  92. 'code' => 1,
  93. 'msg' => '规格数据不能为空'
  94. ]);
  95. }
  96. if (!isset($post['cart_id'])) {
  97. return $this->asJson([
  98. 'code' => 1,
  99. 'msg' => '缺少必要参数'
  100. ]);
  101. }
  102. if (!isset($post['num']) || $post['num'] <= 0) {
  103. return $this->asJson([
  104. 'code' => 1,
  105. 'msg' => '缺少必要参数'
  106. ]);
  107. }
  108. $id = $post['cart_id'];
  109. $cart = Cart::findOne($id);
  110. if (!$cart) {
  111. return $this->asJson([
  112. 'code' => 1,
  113. 'msg' => '数据不存在'
  114. ]);
  115. }
  116. $params_attr = json_decode($post['attr'], true);
  117. $attr = [];
  118. foreach ($params_attr as $item) {
  119. if (!empty($item['attr_id'])) {
  120. $attr[] = $item['attr_id'];
  121. }
  122. }
  123. sort($attr);
  124. $attr = json_encode($attr, JSON_UNESCAPED_UNICODE);
  125. $cart->attr = $attr;
  126. $cart->num = (int)$post['num'];
  127. if ($cart->save()) {
  128. return $this->asJson([
  129. 'code' => 0,
  130. 'msg' => '规格修改成功'
  131. ]);
  132. }
  133. return $this->asJson([
  134. 'code' => 1,
  135. 'msg' => '规格修改失败'
  136. ]);
  137. }
  138. }
  139. /**
  140. * 删除
  141. */
  142. public function actionDelete()
  143. {
  144. $form = new CartDeleteForm();
  145. $form->attributes = get_params();
  146. $form->store_id = get_store_id();
  147. $form->user_id = get_user_id();
  148. return $this->asJson($form->save(all_params()));
  149. }
  150. /**
  151. * 购物车详情
  152. */
  153. public function actionCartEdit()
  154. {
  155. if (\Yii::$app->request->isPost) {
  156. $form = new CartListForm();
  157. $form->store_id = get_store_id();
  158. $form->user_id = get_user_id();
  159. $form->list = post_params('list');
  160. $form->mch_list = post_params('mch_list');
  161. return $this->asJson($form->save());
  162. }
  163. }
  164. /**
  165. * 购物车详情
  166. */
  167. public function actionUpdateNum()
  168. {
  169. $cart = Cart::findOne(['id' => get_params('cart_id', 0), 'store_id' => get_store_id(), 'is_delete' => 0]);
  170. if (empty($cart)) {
  171. return $this->asJson([
  172. 'code' => 1,
  173. 'msg' => '参数错误'
  174. ]);
  175. }
  176. // 兼容首页减到0 的时候直接删除
  177. $num = get_params('num');
  178. if($num == 0){
  179. $cart->is_delete = 1;
  180. }else{
  181. $cart->num = get_params('num', 1);
  182. }
  183. if ($cart->save()) {
  184. return $this->asJson([
  185. 'code' => 0,
  186. 'msg' => '保存成功'
  187. ]);
  188. } else {
  189. return $this->asJson([
  190. 'code' => 1,
  191. 'msg' => '保存失败'
  192. ]);
  193. }
  194. }
  195. /**
  196. * 商品列表减少购物车
  197. */
  198. public function actionCartReduce()
  199. {
  200. $type = input_params('type', 0);
  201. $goods_id = get_params('goods_id');
  202. $attr_info = Json::decode(get_params('arrt_info'));
  203. if($attr_info){
  204. $attr_str = '[';
  205. foreach ($attr_info as $k=>$v){
  206. $attr_str .= $k==0 ? $v['attr_id'] : ','.$v['attr_id'];
  207. }
  208. $attr_str .= ']';
  209. }
  210. $user_id = get_user_id();
  211. if(empty($goods_id)||empty($user_id)){
  212. return $this->asJson(['code'=>1,'msg'=>'出错,请刷新重新操作']);
  213. }
  214. $cart_info = $attr_str ? Cart::findOne(['type'=>$type,'goods_id'=>$goods_id,'user_id'=>$user_id,'is_delete'=>0,'attr'=>$attr_str]) : Cart::findOne(['type'=>$type,'goods_id'=>$goods_id,'user_id'=>$user_id,'is_delete'=>0]);
  215. if(empty($cart_info->id)){
  216. return $this->asJson(['code'=>1,'msg'=>'出错,请刷新重新操作']);
  217. }
  218. if($cart_info->num ==1 || \Yii::$app->request->get('is_delete')){
  219. $cart_info->is_delete = 1;
  220. }
  221. $cart_info->num = $cart_info->num-1;
  222. if($cart_info->save()){
  223. $this->asJson(['code'=>0,'msg'=>'成功']);
  224. }else{
  225. $this->asJson(['code'=>1,'msg'=>'出错,请刷新重新操作']);
  226. }
  227. }
  228. /**
  229. * 批量添加购物车
  230. */
  231. public function actionBatchAddCart()
  232. {
  233. if (\Yii::$app->request->isPost) {
  234. $post = post_params();
  235. if (empty($post['goods_list'])) {
  236. return $this->asJson([
  237. 'code' => 1,
  238. 'msg' => '数据为空'
  239. ]);
  240. }
  241. $goods_list = Json::decode($post['goods_list']);
  242. $form = new AddCartForm();
  243. $form->store_id = get_store_id();
  244. $form->user_id = get_user_id();
  245. $error = 0;
  246. foreach ($goods_list as $goods) {
  247. $form->goods_id = $goods['goods_id'];
  248. $form->attr = Json::encode($goods['attr_list']);
  249. $form->num = $goods['num'];
  250. $status = $form->save();
  251. if ($status['code'] != 0) {
  252. $error++;
  253. }
  254. }
  255. if ($error > 0) {
  256. return $this->asJson([
  257. 'code' => 0,
  258. 'msg' => '添加成功,' . $error . "个失败",
  259. ]);
  260. } else {
  261. return $this->asJson([
  262. 'code' => 0,
  263. 'msg' => '添加成功'
  264. ]);
  265. }
  266. }
  267. }
  268. /**
  269. * 扫码加购物车
  270. */
  271. public function actionScanAddCart() {
  272. try {
  273. $type = input_params('type', 0);
  274. $scan_code = post_params('scan_code');
  275. $store_id = get_store_id();
  276. $user_id = get_user_id();
  277. $goods = Goods::find()->where([
  278. 'is_delete' => 0,
  279. 'product_type' => 0,
  280. 'store_id' => $store_id
  281. ])->andWhere(['LIKE', "JSON_UNQUOTE(JSON_EXTRACT(attr, '$[*].no'))", $scan_code])->select('id, attr')
  282. ->asArray()->one();
  283. if (!$goods) {
  284. throw new \Exception('商品不存在');
  285. }
  286. $current_attr = [];
  287. $attr = json_decode($goods['attr'], true);
  288. foreach ($attr as $attr_item) {
  289. if ($attr_item['no'] === $scan_code) {
  290. $current_attr = $attr_item;
  291. }
  292. }
  293. $attr_id = array_column($current_attr['attr_list'], 'attr_id');
  294. sort($attr_id);
  295. $attr = json_encode($attr_id, JSON_UNESCAPED_UNICODE);
  296. $cart = Cart::findOne([
  297. 'type'=>$type,
  298. 'store_id' => $store_id,
  299. 'goods_id' => $goods['id'],
  300. 'user_id' => $user_id,
  301. 'md_id' => get_md_id(),
  302. 'is_delete' => 0,
  303. 'attr' => $attr,
  304. ]);
  305. if (!$cart) {
  306. $cart = new Cart();
  307. $cart->store_id = $store_id;
  308. $cart->goods_id = $goods['id'];
  309. $cart->mch_id = 0;
  310. $cart->user_id = $user_id;
  311. $cart->num = 0;
  312. $cart->created_at = time();
  313. $cart->is_delete = 0;
  314. $cart->attr = $attr;
  315. if (get_md_id()) {
  316. $cart->md_id = get_md_id();
  317. }
  318. }
  319. $cart->num += 1;
  320. if (!$cart->save()) {
  321. throw new \Exception($cart->errors[0]);
  322. }
  323. return $this->asJson([
  324. 'code' => 0,
  325. 'msg' => '添加购物车成功',
  326. ]);
  327. } catch (\Exception $e) {
  328. return $this->asJson([
  329. 'code' => 1,
  330. 'msg' => $e->getMessage()
  331. ]);
  332. }
  333. }
  334. }