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

ProductCollectionController.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\ProductCollection;
  5. use App\Models\Product;
  6. class ProductCollectionController extends Controller
  7. {
  8. /**
  9. * 添加收藏
  10. * @param Request $request
  11. * @return \Illuminate\Http\JsonResponse
  12. */
  13. public function store(Request $request)
  14. {
  15. $id = $request->input('id');
  16. //$user = session('wechat.oauth_user.default');
  17. //$wechat_id = $user->getId();
  18. $wechat_id = $request->input("openid");
  19. if ($id && $wechat_id) {
  20. $product = Product::find($id);
  21. if ($product->collect_num == null) {
  22. $product->collect_num = 1;
  23. } else {
  24. $product->collect_num = $product->collect_num + 1;
  25. }
  26. $product . save();
  27. // 获取微信id
  28. $wechat_id = "";
  29. $productCollection = new ProductCollection;
  30. $productCollection->wechat_id = $wechat_id;
  31. $productCollection->product_id = $id;
  32. $productCollection->save();
  33. return Response()->json([
  34. "status" => 0,
  35. "message" => "保存成功!"
  36. ]);
  37. } else {
  38. return Response()->json([
  39. "status" => -1,
  40. "message" => "id不能为空!"
  41. ]);
  42. }
  43. }
  44. /**
  45. * 删除收藏夹
  46. * @param Request $request
  47. * @return \Illuminate\Http\JsonResponse
  48. */
  49. public function delete(Request $request)
  50. {
  51. $product_id = $request->input('product_id');
  52. //$user = session('wechat.oauth_user.default');
  53. //$wechat_id = $user->getId();
  54. $wechat_id = $request->header("openid");
  55. if(!$wechat_id) {
  56. $wechat_id = $request->input("openid");
  57. }
  58. if ($product_id) {
  59. $product = Product::find($product_id);
  60. $product->collect_num = $product->collect_num - 1;
  61. $product->save();
  62. $productCollection = ProductCollection::where("product_id", $product_id)
  63. ->where("wechat_id", $wechat_id)->first();
  64. $productCollection->delete();
  65. return Response()->json([
  66. "status" => 0,
  67. "message" => "删除成功!"
  68. ]);
  69. } else {
  70. return Response()->json([
  71. "status" => -1,
  72. "message" => "id不能为空!"
  73. ]);
  74. }
  75. }
  76. }