Sfoglia il codice sorgente

修改根据id增删改查购物车

chenxiqiang 6 anni fa
parent
commit
829daacd7b
1 ha cambiato i file con 77 aggiunte e 43 eliminazioni
  1. 77
    43
      manage-server/app/Http/Controllers/ShopcarController.php

+ 77
- 43
manage-server/app/Http/Controllers/ShopcarController.php Vedi File

@@ -17,31 +17,36 @@ class ShopcarController extends Controller
17 17
         $product_id = $request->input("product_id");
18 18
         $wechat_id = $request->header("openid");
19 19
         $amount = $request->input("product_amount");
20
+        $id = $request->input("id");
20 21
 
21
-        if($product_id) {
22
+        if(!$product_id) {
23
+            return response()->json([
24
+                "status" => -1,
25
+                "error" => "请输入产品ID"
26
+            ]);
27
+        }
28
+        if($id) {
29
+            $shopcar = Shopcar::find($id);
30
+        } else {
22 31
             $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
23 32
             if(!$shopcar) {
24 33
                 $shopcar = new Shopcar;
25
-                $shopcar->wechat_id = $wechat_id;
26
-                $shopcar->product_id = $product_id;
27
-            }
28
-            if($amount) {
29
-                $shopcar->product_amount = $amount;
30
-            } else {
31
-                $shopcar->product_amount = $shopcar->product_amount + 1;
32 34
             }
35
+        }
33 36
 
34
-            $shopcar->save();
35
-            return response()->json([
36
-                "status" => 0,
37
-                "message" => "已加入购物车!"
38
-            ]);
37
+        $shopcar->wechat_id = $wechat_id;
38
+        $shopcar->product_id = $product_id;
39
+        if($amount) {
40
+            $shopcar->product_amount = $amount;
39 41
         } else {
40
-            return response()->json([
41
-                "status" => -1,
42
-                "error" => "请输入产品ID"
43
-            ]);
42
+            $shopcar->product_amount = $shopcar->product_amount + 1;
44 43
         }
44
+        $shopcar->save();
45
+        return response()->json([
46
+            "status" => 0,
47
+            "message" => "已加入购物车!"
48
+        ]);
49
+
45 50
     }
46 51
 
47 52
     /**
@@ -52,16 +57,23 @@ class ShopcarController extends Controller
52 57
     public function pstore(Request $request) {
53 58
 
54 59
         $wechat_id = $request->header("openid");
55
-        $jsonStr = $request->getContent();
56
-        foreach(json_decode($jsonStr, true) as $data){
57
-            $id = $data["id"];
58
-            $amount = $data["amount"];
59
-            $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $id)->first();
60
-            if(!$shopcar) {
61
-                $shopcar = new Shopcar;
62
-                $shopcar->wechat_id = $wechat_id;
63
-                $shopcar->product_id = $id;
60
+        $products_prices = $request->input("data");
61
+        foreach($products_prices as $data){
62
+            if(isset($data["id"])) {
63
+                $id = $data["id"];
64 64
             }
65
+            $product_id = $data["product_id"];
66
+            $amount = $data["product_amount"];
67
+            if(isset($id)) {
68
+                $shopcar = Shopcar::find($id);
69
+            } else {
70
+                $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
71
+                if(!$shopcar) {
72
+                    $shopcar = new Shopcar;
73
+                }
74
+            }
75
+            $shopcar->wechat_id = $wechat_id;
76
+            $shopcar->product_id = $product_id;
65 77
             $shopcar->product_amount = $amount;
66 78
 
67 79
             $shopcar->save();
@@ -79,27 +91,49 @@ class ShopcarController extends Controller
79 91
      * @return mixed
80 92
      */
81 93
     public function amount(Request $request) {
94
+        $id = $request->input("id");
82 95
         $product_id = $request->input("product_id");
83 96
         $wechat_id = $request->header("openid");
84 97
         $amount = $request->input("product_amount");
98
+        if($id) {
99
+            if($amount) {
100
+                $shopcar = Shopcar::find($id);
101
+                if($shopcar) {
102
+                    $shopcar->product_amount = $amount;
103
+                    $shopcar->save();
85 104
 
86
-        if($product_id && $amount) {
87
-            $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
88
-            if($shopcar) {
89
-                $shopcar->product_amount = $amount;
90
-                $shopcar->save();
91
-
105
+                    return response()->json([
106
+                        "status" => 0,
107
+                        "message" => "修改成功!"
108
+                    ]);
109
+                }
110
+            } else {
92 111
                 return response()->json([
93
-                    "status" => 0,
94
-                    "message" => "修改成功!"
112
+                    "status" => -1,
113
+                    "error" => "请输入产品数量"
95 114
                 ]);
96 115
             }
116
+
97 117
         } else {
98
-            return response()->json([
99
-                "status" => -1,
100
-                "error" => "请输入产品ID和数量"
101
-            ]);
118
+            if($product_id && $amount) {
119
+                $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
120
+                if($shopcar) {
121
+                    $shopcar->product_amount = $amount;
122
+                    $shopcar->save();
123
+
124
+                    return response()->json([
125
+                        "status" => 0,
126
+                        "message" => "修改成功!"
127
+                    ]);
128
+                }
129
+            } else {
130
+                return response()->json([
131
+                    "status" => -1,
132
+                    "error" => "请输入产品ID和数量"
133
+                ]);
134
+            }
102 135
         }
136
+
103 137
     }
104 138
 
105 139
     /**
@@ -108,11 +142,11 @@ class ShopcarController extends Controller
108 142
      * @return mixed
109 143
      */
110 144
     public function delete(Request $request, $id) {
111
-        $product_id = $request->route("id");
145
+        $id = $request->route("id");
112 146
         $wechat_id = $request->header("openid");
113 147
 
114
-        if($product_id) {
115
-            $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
148
+        if($id) {
149
+            $shopcar = Shopcar::find($id);
116 150
             if($shopcar) {
117 151
                 $shopcar->delete();
118 152
                 return response()->json([
@@ -123,7 +157,7 @@ class ShopcarController extends Controller
123 157
         } else {
124 158
             return response()->json([
125 159
                 "status" => -1,
126
-                "error" => "请输入产品ID"
160
+                "error" => "请输入ID"
127 161
             ]);
128 162
         }
129 163
     }
@@ -139,7 +173,7 @@ class ShopcarController extends Controller
139 173
 
140 174
         if($ids) {
141 175
             foreach ($ids as $id) {
142
-                $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $id)->first();
176
+                $shopcar = Shopcar::find($id);
143 177
                 if($shopcar) {
144 178
                     $shopcar->delete();
145 179
                 }

Loading…
Annulla
Salva