Ver código fonte

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

chenxiqiang 7 anos atrás
pai
commit
829daacd7b

+ 77
- 43
manage-server/app/Http/Controllers/ShopcarController.php Ver arquivo

17
         $product_id = $request->input("product_id");
17
         $product_id = $request->input("product_id");
18
         $wechat_id = $request->header("openid");
18
         $wechat_id = $request->header("openid");
19
         $amount = $request->input("product_amount");
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
             $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
31
             $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
23
             if(!$shopcar) {
32
             if(!$shopcar) {
24
                 $shopcar = new Shopcar;
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
         } else {
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
     public function pstore(Request $request) {
57
     public function pstore(Request $request) {
53
 
58
 
54
         $wechat_id = $request->header("openid");
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
             $shopcar->product_amount = $amount;
77
             $shopcar->product_amount = $amount;
66
 
78
 
67
             $shopcar->save();
79
             $shopcar->save();
79
      * @return mixed
91
      * @return mixed
80
      */
92
      */
81
     public function amount(Request $request) {
93
     public function amount(Request $request) {
94
+        $id = $request->input("id");
82
         $product_id = $request->input("product_id");
95
         $product_id = $request->input("product_id");
83
         $wechat_id = $request->header("openid");
96
         $wechat_id = $request->header("openid");
84
         $amount = $request->input("product_amount");
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
                 return response()->json([
111
                 return response()->json([
93
-                    "status" => 0,
94
-                    "message" => "修改成功!"
112
+                    "status" => -1,
113
+                    "error" => "请输入产品数量"
95
                 ]);
114
                 ]);
96
             }
115
             }
116
+
97
         } else {
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
      * @return mixed
142
      * @return mixed
109
      */
143
      */
110
     public function delete(Request $request, $id) {
144
     public function delete(Request $request, $id) {
111
-        $product_id = $request->route("id");
145
+        $id = $request->route("id");
112
         $wechat_id = $request->header("openid");
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
             if($shopcar) {
150
             if($shopcar) {
117
                 $shopcar->delete();
151
                 $shopcar->delete();
118
                 return response()->json([
152
                 return response()->json([
123
         } else {
157
         } else {
124
             return response()->json([
158
             return response()->json([
125
                 "status" => -1,
159
                 "status" => -1,
126
-                "error" => "请输入产品ID"
160
+                "error" => "请输入ID"
127
             ]);
161
             ]);
128
         }
162
         }
129
     }
163
     }
139
 
173
 
140
         if($ids) {
174
         if($ids) {
141
             foreach ($ids as $id) {
175
             foreach ($ids as $id) {
142
-                $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $id)->first();
176
+                $shopcar = Shopcar::find($id);
143
                 if($shopcar) {
177
                 if($shopcar) {
144
                     $shopcar->delete();
178
                     $shopcar->delete();
145
                 }
179
                 }

Carregando…
Cancelar
Salvar