public function toPdtContent(Request $request, $product_id)
 {
     $product = Product::find($product_id);
     $pdt_content = PdtContent::where('product_id', $product_id)->first();
     $pdt_images = PdtImages::where('product_id', $product_id)->get();
     $count = 0;
     $member = $request->session()->get('member', '');
     if ($member != '') {
         $cart_items = CartItem::where('member_id', $member->id)->get();
         foreach ($cart_items as $cart_item) {
             if ($cart_item->product_id == $product_id) {
                 $count = $cart_item->count;
                 break;
             }
         }
     } else {
         $bk_cart = $request->cookie('bk_cart');
         $bk_cart_arr = $bk_cart != null ? explode(',', $bk_cart) : array();
         foreach ($bk_cart_arr as $value) {
             // 一定要传引用
             $index = strpos($value, ':');
             if (substr($value, 0, $index) == $product_id) {
                 $count = (int) substr($value, $index + 1);
                 break;
             }
         }
     }
     return view('pdt_content')->with('product', $product)->with('pdt_content', $pdt_content)->with('pdt_images', $pdt_images)->with('count', $count);
 }
 public function toOrderCommit(Request $request)
 {
     // 获取微信重定向返回的code
     $code = $request->input('code', '');
     if ($code != '') {
         //获取code码,以获取openid
         $openid = WXTool::getOpenid($code);
         // 将openid保存到session
         $request->session()->put('openid', $openid);
     }
     $product_ids = $request->input('product_ids', '');
     $product_ids_arr = $product_ids != '' ? explode(',', $product_ids) : array();
     $member = $request->session()->get('member', '');
     $cart_items = CartItem::where('member_id', $member->id)->whereIn('product_id', $product_ids_arr)->get();
     $order = new Order();
     $order->member_id = $member->id;
     $order->save();
     $cart_items_arr = array();
     $cart_items_ids_arr = array();
     $total_price = 0;
     $name = '';
     foreach ($cart_items as $cart_item) {
         $cart_item->product = Product::find($cart_item->product_id);
         if ($cart_item->product != null) {
             $total_price += $cart_item->product->price * $cart_item->count;
             $name .= '《' . $cart_item->product->name . '》';
             array_push($cart_items_arr, $cart_item);
             array_push($cart_items_ids_arr, $cart_item->id);
             $order_item = new OrderItem();
             $order_item->order_id = $order->id;
             $order_item->product_id = $cart_item->product_id;
             $order_item->count = $cart_item->count;
             $order_item->pdt_snapshot = json_encode($cart_item->product);
             $order_item->save();
         }
     }
     CartItem::whereIn('id', $cart_items_ids_arr)->delete();
     $order->name = $name;
     $order->total_price = $total_price;
     $order->order_no = 'E' . time() . '' . $order->id;
     $order->save();
     // JSSDK 相关
     $access_token = WXTool::getAccessToken();
     $jsapi_ticket = WXTool::getJsApiTicket($access_token);
     $noncestr = WXTool::createNonceStr();
     $timestamp = time();
     $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
     // 签名
     $signature = WXTool::signature($jsapi_ticket, $noncestr, $timestamp, $url);
     // 返回微信参数
     $bk_wx_js_config = new BKWXJsConfig();
     $bk_wx_js_config->appId = config('wx_config.APPID');
     $bk_wx_js_config->timestamp = $timestamp;
     $bk_wx_js_config->nonceStr = $noncestr;
     $bk_wx_js_config->signature = $signature;
     return view('order_commit')->with('cart_items', $cart_items_arr)->with('total_price', $total_price)->with('name', $name)->with('order_no', $order->order_no)->with('bk_wx_js_config', $bk_wx_js_config);
 }
 private function syncCart($member_id, $bk_cart_arr)
 {
     $cart_items = CartItem::where('member_id', $member_id)->get();
     $cart_items_arr = array();
     foreach ($bk_cart_arr as $value) {
         $index = strpos($value, ':');
         $product_id = substr($value, 0, $index);
         $count = (int) substr($value, $index + 1);
         // 判断离线购物车中product_id 是否存在 数据库中
         $exist = false;
         foreach ($cart_items as $temp) {
             if ($temp->product_id == $product_id) {
                 if ($temp->count < $count) {
                     $temp->count = $count;
                     $temp->save();
                 }
                 $exist = true;
                 break;
             }
         }
         // 不存在则存储进来
         if ($exist == false) {
             $cart_item = new CartItem();
             $cart_item->member_id = $member_id;
             $cart_item->product_id = $product_id;
             $cart_item->count = $count;
             $cart_item->save();
             $cart_item->product = Product::find($cart_item->product_id);
             array_push($cart_items_arr, $cart_item);
         }
     }
     // 为每个对象附加产品对象便于显示
     foreach ($cart_items as $cart_item) {
         $cart_item->product = Product::find($cart_item->product_id);
         array_push($cart_items_arr, $cart_item);
     }
     return $cart_items_arr;
 }
 public function deleteCart(Request $request)
 {
     $m3_result = new M3Result();
     $m3_result->status = 0;
     $m3_result->message = '删除成功';
     $product_ids = $request->input('product_ids', '');
     if ($product_ids == '') {
         $m3_result->status = 1;
         $m3_result->message = '书籍ID为空';
         return $m3_result->toJson();
     }
     $product_ids_arr = explode(',', $product_ids);
     $member = $request->session()->get('member', '');
     if ($member != '') {
         // 已登录
         CartItem::whereIn('product_id', $product_ids_arr)->delete();
         return $m3_result->toJson();
     }
     $product_ids = $request->input('product_ids', '');
     if ($product_ids == '') {
         $m3_result->status = 1;
         $m3_result->message = '书籍ID为空';
         return $m3_result->toJson();
     }
     // 未登录
     $bk_cart = $request->cookie('bk_cart');
     $bk_cart_arr = $bk_cart != null ? explode(',', $bk_cart) : array();
     foreach ($bk_cart_arr as $key => $value) {
         $index = strpos($value, ':');
         $product_id = substr($value, 0, $index);
         // 存在, 删除
         if (in_array($product_id, $product_ids_arr)) {
             array_splice($bk_cart_arr, $key, 1);
             continue;
         }
     }
     return response($m3_result->toJson())->withCookie('bk_cart', implode(',', $bk_cart_arr));
 }