Cart.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\api\controller;
  10. use app\common\exception\BaseException;
  11. use think\facade\Db;
  12. use think\facade\Request;
  13. use mall\basic\Users;
  14. use mall\basic\Shopping;
  15. use app\api\service\Cart as CartService;
  16. class Cart extends Base {
  17. /**
  18. * 购物车
  19. * @return \think\response\Json
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\DbException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. */
  24. public function index(){
  25. try{
  26. return $this->returnAjax("ok",1,CartService::getList(Request::param()));
  27. }catch (BaseException $ex){
  28. return $this->returnAjax($ex->getMessage(),$ex->getCode(),$ex->getRaw());
  29. }
  30. }
  31. /**
  32. * 添加商品
  33. * @return \think\response\Json
  34. */
  35. public function add(){
  36. $id = Request::param("id","","intval");
  37. $sku_id = Request::param("sku_id","","intval");
  38. $num = Request::param("num","0","intval");
  39. try {
  40. Shopping::add($id,$sku_id,$num);
  41. }catch (\Exception $ex){
  42. return $this->returnAjax($ex->getMessage(),0);
  43. }
  44. return $this->returnAjax("商品添加至购物车成功",1,[
  45. "count" => Db::name("cart")->where('user_id',Users::get("id"))->sum("goods_nums")
  46. ]);
  47. }
  48. /**
  49. * 修改商品数量
  50. * @return \think\response\Json
  51. */
  52. public function change(){
  53. $id = Request::param("id","","intval");
  54. $sku_id = Request::param("sku_id","","intval");
  55. $num = Request::param("num","","intval");
  56. try {
  57. Shopping::add($id,$sku_id,$num);
  58. }catch (\Exception $ex){
  59. return $this->returnAjax($ex->getMessage(),0);
  60. }
  61. return $this->returnAjax("ok",1,[
  62. "count" => Db::name("cart")->where('user_id',Users::get("id"))->sum("goods_nums")
  63. ]);
  64. }
  65. /**
  66. * 删除商品
  67. * @return \think\response\Json
  68. */
  69. public function delete(){
  70. try{
  71. return $this->returnAjax("ok",1,CartService::delete(Request::param("id","0","strip_tags")));
  72. }catch (BaseException $ex){
  73. return $this->returnAjax($ex->getMessage(),0);
  74. }
  75. }
  76. }