123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
-
- namespace App\Http\Controllers;
-
- use Illuminate\Http\Request;
- use App\Models\Shopcar;
- use Illuminate\Support\Facades\DB;
-
- class ShopcarController extends Controller
- {
- /**
- * 添加购物车
- * @param Request $request
- * @return mixed
- */
- public function add(Request $request) {
- $product_id = $request->input("product_id");
- $wechat_id = $request->header("openid");
- $amount = $request->input("product_amount");
-
- if($product_id) {
- $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
- if(!$shopcar) {
- $shopcar = new Shopcar;
- $shopcar->wechat_id = $wechat_id;
- $shopcar->product_id = $product_id;
- }
- if($amount) {
- $shopcar->product_amount = $amount;
- } else {
- $shopcar->product_amount = $shopcar->product_amount + 1;
- }
-
- $shopcar->save();
- return response()->json([
- "status" => 0,
- "message" => "已加入购物车!"
- ]);
- } else {
- return response()->json([
- "status" => -1,
- "error" => "请输入产品ID"
- ]);
- }
- }
-
- /**
- * 批量添加/修改购物车数量
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function pstore(Request $request) {
-
- $wechat_id = $request->header("openid");
- $jsonStr = $request->getContent();
- foreach(json_decode($jsonStr, true) as $data){
- $id = $data["id"];
- $amount = $data["amount"];
- $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $id)->first();
- if(!$shopcar) {
- $shopcar = new Shopcar;
- $shopcar->wechat_id = $wechat_id;
- $shopcar->product_id = $id;
- }
- $shopcar->product_amount = $amount;
-
- $shopcar->save();
- }
- return response()->json([
- "status" => 0,
- "message" => "修改成功!"
- ]);
-
- }
-
- /**
- * 修改购物内产品数量
- * @param Request $request
- * @return mixed
- */
- public function amount(Request $request) {
- $product_id = $request->input("product_id");
- $wechat_id = $request->header("openid");
- $amount = $request->input("product_amount");
-
- if($product_id && $amount) {
- $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
- if($shopcar) {
- $shopcar->product_amount = $amount;
- $shopcar->save();
-
- return response()->json([
- "status" => 0,
- "message" => "修改成功!"
- ]);
- }
- } else {
- return response()->json([
- "status" => -1,
- "error" => "请输入产品ID和数量"
- ]);
- }
- }
-
- /**
- * 移除购物车
- * @param Request $request
- * @return mixed
- */
- public function delete(Request $request, $id) {
- $product_id = $request->route("id");
- $wechat_id = $request->header("openid");
-
- if($product_id) {
- $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $product_id)->first();
- if($shopcar) {
- $shopcar->delete();
- return response()->json([
- "status" => 0,
- "message" => "删除成功!"
- ]);
- }
- } else {
- return response()->json([
- "status" => -1,
- "error" => "请输入产品ID"
- ]);
- }
- }
-
- /**
- * 批量删除ids
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function pdelete(Request $request) {
- $ids = $request->input("ids");
- $wechat_id = $request->header("openid");
-
- if($ids) {
- foreach ($ids as $id) {
- $shopcar = Shopcar::where("wechat_id", $wechat_id)->where("product_id", $id)->first();
- if($shopcar) {
- $shopcar->delete();
- }
- }
- return response()->json([
- "status" => 0,
- "message" => "批量删除成功!"
- ]);
- }
- }
-
- /**
- * 列出购物车列表
- * @param Request $request
- * @return mixed
- */
- public function list(Request $request) {
-
- $wechat_id = $request->header("openid");
-
- $sql = 'select t1.*, t2.name, t2.price, t2.classify, t2.color, t2.status, t2.photo '
- . 'from shopcars t1 '
- . 'inner join '
- . 'products t2 '
- . 'on t1.product_id = t2.id '
- . 'where t1.wechat_id = ?';
- $shopcars = DB::select($sql, [$wechat_id]);
-
- return response()->json([
- "status" => 0,
- "data" => $shopcars
- ]);
- }
-
- }
|