瀏覽代碼

添加批量保存、删除接口

chenxiqiang 6 年之前
父節點
當前提交
19635386d7
共有 3 個檔案被更改,包括 123 行新增6 行删除
  1. 61
    6
      manage-server/app/Http/Controllers/ShopcarController.php
  2. 60
    0
      manage-server/readme.md
  3. 2
    0
      manage-server/routes/api.php

+ 61
- 6
manage-server/app/Http/Controllers/ShopcarController.php 查看文件

16
     public function add(Request $request) {
16
     public function add(Request $request) {
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
 
20
 
20
         if($product_id) {
21
         if($product_id) {
21
             $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
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
                 $shopcar = new Shopcar;
24
                 $shopcar = new Shopcar;
27
                 $shopcar->wechat_id = $wechat_id;
25
                 $shopcar->wechat_id = $wechat_id;
28
                 $shopcar->product_id = $product_id;
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
             return response()->json([
35
             return response()->json([
33
                 "status" => 0,
36
                 "status" => 0,
34
                 "message" => "已加入购物车!"
37
                 "message" => "已加入购物车!"
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
      * @param Request $request
78
      * @param Request $request
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
      * @param Request $request
156
      * @param Request $request

+ 60
- 0
manage-server/readme.md 查看文件

1787
 参数            |  类型    |   可选               |    备注
1787
 参数            |  类型    |   可选               |    备注
1788
 -----------    |  ------  |  ------------------ |  --------
1788
 -----------    |  ------  |  ------------------ |  --------
1789
 product_id     |  int    | 	必须               |  产品id
1789
 product_id     |  int    | 	必须               |  产品id
1790
+product_amount |  int    | 	可选,默认为1       |  产品数量
1790
 
1791
 
1791
 - 响应:
1792
 - 响应:
1792
 
1793
 
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
 - `DELETE /shopcar/{id}`
1870
 - `DELETE /shopcar/{id}`
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
 - `GET /shopcars`
1913
 - `GET /shopcars`
1854
 - 参数: 无
1914
 - 参数: 无

+ 2
- 0
manage-server/routes/api.php 查看文件

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

Loading…
取消
儲存