家政小程序
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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. $id = $request->input("id");
  18. if(!$product_id) {
  19. return response()->json([
  20. "status" => -1,
  21. "error" => "请输入产品ID"
  22. ]);
  23. }
  24. if($id) {
  25. $shopcar = Shopcar::find($id);
  26. } else {
  27. $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
  28. if(!$shopcar) {
  29. $shopcar = new Shopcar;
  30. }
  31. }
  32. $shopcar->wechat_id = $wechat_id;
  33. $shopcar->product_id = $product_id;
  34. if($amount) {
  35. $shopcar->product_amount = $amount;
  36. } else {
  37. $shopcar->product_amount = $shopcar->product_amount + 1;
  38. }
  39. $shopcar->save();
  40. return response()->json([
  41. "status" => 0,
  42. "message" => "已加入购物车!"
  43. ]);
  44. }
  45. /**
  46. * 批量添加/修改购物车数量
  47. * @param Request $request
  48. * @return \Illuminate\Http\JsonResponse
  49. */
  50. public function pstore(Request $request) {
  51. $wechat_id = $request->header("openid");
  52. $products_prices = $request->input("data");
  53. foreach($products_prices as $data){
  54. if(isset($data["id"])) {
  55. $id = $data["id"];
  56. }
  57. $product_id = $data["product_id"];
  58. $amount = $data["product_amount"];
  59. if(isset($id)) {
  60. $shopcar = Shopcar::find($id);
  61. } else {
  62. $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
  63. if(!$shopcar) {
  64. $shopcar = new Shopcar;
  65. }
  66. }
  67. $shopcar->wechat_id = $wechat_id;
  68. $shopcar->product_id = $product_id;
  69. $shopcar->product_amount = $amount;
  70. $shopcar->save();
  71. }
  72. return response()->json([
  73. "status" => 0,
  74. "message" => "修改成功!"
  75. ]);
  76. }
  77. /**
  78. * 修改购物内产品数量
  79. * @param Request $request
  80. * @return mixed
  81. */
  82. public function amount(Request $request) {
  83. $id = $request->input("id");
  84. $product_id = $request->input("product_id");
  85. $wechat_id = $request->header("openid");
  86. $amount = $request->input("product_amount");
  87. if($id) {
  88. if($amount) {
  89. $shopcar = Shopcar::find($id);
  90. if($shopcar) {
  91. $shopcar->product_amount = $amount;
  92. $shopcar->save();
  93. return response()->json([
  94. "status" => 0,
  95. "message" => "修改成功!"
  96. ]);
  97. }
  98. } else {
  99. return response()->json([
  100. "status" => -1,
  101. "error" => "请输入产品数量"
  102. ]);
  103. }
  104. } else {
  105. if($product_id && $amount) {
  106. $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
  107. if($shopcar) {
  108. $shopcar->product_amount = $amount;
  109. $shopcar->save();
  110. return response()->json([
  111. "status" => 0,
  112. "message" => "修改成功!"
  113. ]);
  114. }
  115. } else {
  116. return response()->json([
  117. "status" => -1,
  118. "error" => "请输入产品ID和数量"
  119. ]);
  120. }
  121. }
  122. }
  123. /**
  124. * 移除购物车
  125. * @param Request $request
  126. * @return mixed
  127. */
  128. public function delete(Request $request, $id) {
  129. $id = $request->route("id");
  130. $wechat_id = $request->header("openid");
  131. if($id) {
  132. $shopcar = Shopcar::find($id);
  133. if($shopcar) {
  134. $shopcar->delete();
  135. return response()->json([
  136. "status" => 0,
  137. "message" => "删除成功!"
  138. ]);
  139. }
  140. } else {
  141. return response()->json([
  142. "status" => -1,
  143. "error" => "请输入ID"
  144. ]);
  145. }
  146. }
  147. /**
  148. * 批量删除ids
  149. * @param Request $request
  150. * @return \Illuminate\Http\JsonResponse
  151. */
  152. public function pdelete(Request $request) {
  153. $ids = $request->input("ids");
  154. $wechat_id = $request->header("openid");
  155. if($ids) {
  156. foreach ($ids as $id) {
  157. $shopcar = Shopcar::find($id);
  158. if($shopcar) {
  159. $shopcar->delete();
  160. }
  161. }
  162. return response()->json([
  163. "status" => 0,
  164. "message" => "批量删除成功!"
  165. ]);
  166. }
  167. }
  168. /**
  169. * 列出购物车列表
  170. * @param Request $request
  171. * @return mixed
  172. */
  173. public function list(Request $request) {
  174. $wechat_id = $request->header("openid");
  175. $sql = 'select t1.*, t2.name, t2.price, t2.classify, t2.color, t2.status, t2.photo '
  176. . 'from shopcars t1 '
  177. . 'inner join '
  178. . 'products t2 '
  179. . 'on t1.product_id = t2.id '
  180. . 'where t1.wechat_id = ?';
  181. $shopcars = DB::select($sql, [$wechat_id]);
  182. return response()->json([
  183. "status" => 0,
  184. "data" => $shopcars
  185. ]);
  186. }
  187. }