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);
 }
 public function create()
 {
     $Request = $this->container->get('Core\\Http\\Request\\Request');
     if ($Request->getMethod() == 'POST') {
         $form = $Request->getPost()->get('form');
         if ($form->get('title') && $form->get('content')) {
             $em = $this->container->get('Core\\Orm\\Orm')->getEntityManager();
             $Product = new Product();
             $Product->setName($form->get('title'));
             $Product->setContent($form->get('content'));
             $em->persist($Product);
             $em->flush();
             $flash = "Added";
         } else {
             $flash = "Empty";
         }
     }
     return new Response('Default/create.html.twig', ['flash' => !empty($flash) ? $flash : '']);
 }
 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 productAdd(Request $request)
 {
     $name = $request->input('name', '');
     $summary = $request->input('summary', '');
     $price = $request->input('price', '');
     $category_id = $request->input('category_id', '');
     $preview = $request->input('preview', '');
     $content = $request->input('content', '');
     $preview1 = $request->input('preview1', '');
     $preview2 = $request->input('preview2', '');
     $preview3 = $request->input('preview3', '');
     $preview4 = $request->input('preview4', '');
     $preview5 = $request->input('preview5', '');
     $product = new Product();
     $product->summary = $summary;
     $product->price = $price;
     $product->category_id = $category_id;
     $product->preview = $preview;
     $product->name = $name;
     $product->save();
     $pdt_content = new PdtContent();
     $pdt_content->product_id = $product->id;
     $pdt_content->content = $content;
     $pdt_content->save();
     if ($preview1 != '') {
         $pdt_images = new PdtImages();
         $pdt_images->image_path = $preview1;
         $pdt_images->image_no = 1;
         $pdt_images->product_id = $product->id;
         $pdt_images->save();
     }
     if ($preview2 != '') {
         $pdt_images = new PdtImages();
         $pdt_images->image_path = $preview2;
         $pdt_images->image_no = 2;
         $pdt_images->product_id = $product->id;
         $pdt_images->save();
     }
     if ($preview3 != '') {
         $pdt_images = new PdtImages();
         $pdt_images->image_path = $preview3;
         $pdt_images->image_no = 3;
         $pdt_images->product_id = $product->id;
         $pdt_images->save();
     }
     if ($preview4 != '') {
         $pdt_images = new PdtImages();
         $pdt_images->image_path = $preview4;
         $pdt_images->image_no = 4;
         $pdt_images->product_id = $product->id;
         $pdt_images->save();
     }
     if ($preview5 != '') {
         $pdt_images = new PdtImages();
         $pdt_images->image_path = $preview5;
         $pdt_images->image_no = 5;
         $pdt_images->product_id = $product->id;
         $pdt_images->save();
     }
     $m3_result = new M3Result();
     $m3_result->status = 0;
     $m3_result->message = '添加成功';
     return $m3_result->toJson();
 }