家政小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShopcarController.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Shopcar;
  5. use Illuminate\Support\Facades\DB;
  6. class ShopcarController extends Controller
  7. {
  8. /**
  9. * 添加购物车
  10. * @param Request $request
  11. * @return mixed
  12. */
  13. public function add(Request $request) {
  14. $product_id = $request->input("product_id");
  15. $wechat_id = $request->header("openid");
  16. $amount = $request->input("product_amount");
  17. if($product_id) {
  18. $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
  19. if(!$shopcar) {
  20. $shopcar = new Shopcar;
  21. $shopcar->wechat_id = $wechat_id;
  22. $shopcar->product_id = $product_id;
  23. }
  24. if($amount) {
  25. $shopcar->product_amount = $amount;
  26. } else {
  27. $shopcar->product_amount = $shopcar->product_amount + 1;
  28. }
  29. $shopcar->save();
  30. return response()->json([
  31. "status" => 0,
  32. "message" => "已加入购物车!"
  33. ]);
  34. } else {
  35. return response()->json([
  36. "status" => -1,
  37. "error" => "请输入产品ID"
  38. ]);
  39. }
  40. }
  41. /**
  42. * 批量添加/修改购物车数量
  43. * @param Request $request
  44. * @return \Illuminate\Http\JsonResponse
  45. */
  46. public function pstore(Request $request) {
  47. $wechat_id = $request->header("openid");
  48. $jsonStr = $request->getContent();
  49. foreach(json_decode($jsonStr, true) as $data){
  50. $id = $data["id"];
  51. $amount = $data["amount"];
  52. $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $id)->first();
  53. if(!$shopcar) {
  54. $shopcar = new Shopcar;
  55. $shopcar->wechat_id = $wechat_id;
  56. $shopcar->product_id = $id;
  57. }
  58. $shopcar->product_amount = $amount;
  59. $shopcar->save();
  60. }
  61. return response()->json([
  62. "status" => 0,
  63. "message" => "修改成功!"
  64. ]);
  65. }
  66. /**
  67. * 修改购物内产品数量
  68. * @param Request $request
  69. * @return mixed
  70. */
  71. public function amount(Request $request) {
  72. $product_id = $request->input("product_id");
  73. $wechat_id = $request->header("openid");
  74. $amount = $request->input("product_amount");
  75. if($product_id && $amount) {
  76. $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
  77. if($shopcar) {
  78. $shopcar->product_amount = $amount;
  79. $shopcar->save();
  80. return response()->json([
  81. "status" => 0,
  82. "message" => "修改成功!"
  83. ]);
  84. }
  85. } else {
  86. return response()->json([
  87. "status" => -1,
  88. "error" => "请输入产品ID和数量"
  89. ]);
  90. }
  91. }
  92. /**
  93. * 移除购物车
  94. * @param Request $request
  95. * @return mixed
  96. */
  97. public function delete(Request $request, $id) {
  98. $product_id = $request->route("id");
  99. $wechat_id = $request->header("openid");
  100. if($product_id) {
  101. $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
  102. if($shopcar) {
  103. $shopcar->delete();
  104. return response()->json([
  105. "status" => 0,
  106. "message" => "删除成功!"
  107. ]);
  108. }
  109. } else {
  110. return response()->json([
  111. "status" => -1,
  112. "error" => "请输入产品ID"
  113. ]);
  114. }
  115. }
  116. /**
  117. * 批量删除ids
  118. * @param Request $request
  119. * @return \Illuminate\Http\JsonResponse
  120. */
  121. public function pdelete(Request $request) {
  122. $ids = $request->input("ids");
  123. $wechat_id = $request->header("openid");
  124. if($ids) {
  125. foreach ($ids as $id) {
  126. $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $id)->first();
  127. if($shopcar) {
  128. $shopcar->delete();
  129. }
  130. }
  131. return response()->json([
  132. "status" => 0,
  133. "message" => "批量删除成功!"
  134. ]);
  135. }
  136. }
  137. /**
  138. * 列出购物车列表
  139. * @param Request $request
  140. * @return mixed
  141. */
  142. public function list(Request $request) {
  143. $wechat_id = $request->header("openid");
  144. $sql = 'select t1.*, t2.name, t2.price, t2.classify, t2.color, t2.status, t2.photo '
  145. . 'from shopcars t1 '
  146. . 'inner join '
  147. . 'products t2 '
  148. . 'on t1.product_id = t2.id '
  149. . 'where t1.wechat_id = ?';
  150. $shopcars = DB::select($sql, [$wechat_id]);
  151. return response()->json([
  152. "status" => 0,
  153. "data" => $shopcars
  154. ]);
  155. }
  156. }