Procházet zdrojové kódy

添加批量保存、删除接口

chenxiqiang před 6 roky
rodič
revize
19635386d7

+ 61
- 6
manage-server/app/Http/Controllers/ShopcarController.php Zobrazit soubor

@@ -16,19 +16,22 @@ class ShopcarController extends Controller
16 16
     public function add(Request $request) {
17 17
         $product_id = $request->input("product_id");
18 18
         $wechat_id = $request->header("openid");
19
+        $amount = $request->input("product_amount");
19 20
 
20 21
         if($product_id) {
21 22
             $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
22
-            if($shopcar) {
23
-                $shopcar->product_amount = $shopcar->product_amount + 1;
24
-                $shopcar->save();
25
-            } else {
23
+            if(!$shopcar) {
26 24
                 $shopcar = new Shopcar;
27 25
                 $shopcar->wechat_id = $wechat_id;
28 26
                 $shopcar->product_id = $product_id;
29
-                $shopcar->product_amount = 1;
30
-                $shopcar->save();
31 27
             }
28
+            if($amount) {
29
+                $shopcar->product_amount = $amount;
30
+            } else {
31
+                $shopcar->product_amount = $shopcar->product_amount + 1;
32
+            }
33
+
34
+            $shopcar->save();
32 35
             return response()->json([
33 36
                 "status" => 0,
34 37
                 "message" => "已加入购物车!"
@@ -41,6 +44,35 @@ class ShopcarController extends Controller
41 44
         }
42 45
     }
43 46
 
47
+    /**
48
+     * 批量添加/修改购物车数量
49
+     * @param Request $request
50
+     * @return \Illuminate\Http\JsonResponse
51
+     */
52
+    public function pstore(Request $request) {
53
+
54
+        $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;
64
+            }
65
+            $shopcar->product_amount = $amount;
66
+
67
+            $shopcar->save();
68
+        }
69
+        return response()->json([
70
+            "status" => 0,
71
+            "message" => "修改成功!"
72
+        ]);
73
+
74
+    }
75
+
44 76
     /**
45 77
      * 修改购物内产品数量
46 78
      * @param Request $request
@@ -96,6 +128,29 @@ class ShopcarController extends Controller
96 128
         }
97 129
     }
98 130
 
131
+    /**
132
+     * 批量删除ids
133
+     * @param Request $request
134
+     * @return \Illuminate\Http\JsonResponse
135
+     */
136
+    public function pdelete(Request $request) {
137
+        $ids = $request->input("ids");
138
+        $wechat_id = $request->header("openid");
139
+
140
+        if($ids) {
141
+            foreach ($ids as $id) {
142
+                $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $id)->first();
143
+                if($shopcar) {
144
+                    $shopcar->delete();
145
+                }
146
+            }
147
+            return response()->json([
148
+                "status" => 0,
149
+                "message" => "批量删除成功!"
150
+            ]);
151
+        }
152
+    }
153
+
99 154
     /**
100 155
      * 列出购物车列表
101 156
      * @param Request $request

+ 60
- 0
manage-server/readme.md Zobrazit soubor

@@ -1787,6 +1787,7 @@ content        |  text   | 	必须               |  底部富文本介绍
1787 1787
 参数            |  类型    |   可选               |    备注
1788 1788
 -----------    |  ------  |  ------------------ |  --------
1789 1789
 product_id     |  int    | 	必须               |  产品id
1790
+product_amount |  int    | 	可选,默认为1       |  产品数量
1790 1791
 
1791 1792
 - 响应:
1792 1793
 
@@ -1829,6 +1830,41 @@ product_amount |  int    | 	必须               |  产品数量
1829 1830
 ```
1830 1831
 
1831 1832
 
1833
+### 批量添加/修改购物车里产品数量
1834
+
1835
+- `POST /shopcar/pstore`
1836
+- 参数:
1837
+
1838
+```
1839
+[
1840
+  {
1841
+    "id": 5,
1842
+    "amount": 3
1843
+  },
1844
+  {
1845
+    "id": 6,
1846
+    "amount": 4
1847
+  },
1848
+  {
1849
+    "id": 35,
1850
+    "amount": 5
1851
+  },
1852
+  {
1853
+    "id": 37,
1854
+    "amount": 4
1855
+  }
1856
+]
1857
+```
1858
+
1859
+- 响应:
1860
+
1861
+```
1862
+{
1863
+    "status": 0,
1864
+    "message": "批量修改成功!"
1865
+}
1866
+```
1867
+
1832 1868
 ### 删除购物车里产品
1833 1869
 
1834 1870
 - `DELETE /shopcar/{id}`
@@ -1849,6 +1885,30 @@ product_amount |  int    | 	必须               |  产品数量
1849 1885
 }
1850 1886
 ```
1851 1887
 
1888
+### 批量删除购物车里产品
1889
+
1890
+- `DELETE /shopcar/pdelete`
1891
+- 参数: 
1892
+
1893
+参数            |  类型    |   可选               |    备注
1894
+-----------    |  ------  |  ------------------ |  --------
1895
+ids            |  array   | 	必须               |  产品ids,比如[1,2,3,4]
1896
+
1897
+- 响应:
1898
+
1899
+```
1900
+{
1901
+    "status": 0,
1902
+    "message": "批量删除成功!"
1903
+}
1904
+```
1905
+```
1906
+{
1907
+    "status": -1,
1908
+    "error": "请输入产品ID"
1909
+}
1910
+```
1911
+
1852 1912
 ### 购物车列表
1853 1913
 - `GET /shopcars`
1854 1914
 - 参数: 无

+ 2
- 0
manage-server/routes/api.php Zobrazit soubor

@@ -65,7 +65,9 @@ Route::middleware(['token'])->group(function () {
65 65
     Route::get('/my/orders', 'CommonController@orders');
66 66
     Route::get('/shopcars', 'ShopcarController@list');
67 67
     Route::post('/shopcar/add', 'ShopcarController@add');
68
+    Route::post('/shopcar/pstore', 'ShopcarController@pstore');
68 69
     Route::delete('/shopcar/{id}', 'ShopcarController@delete')->where('id', '[0-9]+');
70
+    Route::delete('/shopcar/pdelete', 'ShopcarController@pdelete');
69 71
     Route::post('/shopcar/amount', 'ShopcarController@amount');
70 72
     Route::post('/{order_type}/{order_id}/comment', 'CommonController@addComment');
71 73
     Route::post('/house_clean_order/store', 'HouseCleanOrderController@store');

Loading…
Zrušit
Uložit