public function indexAction()
 {
     //        // 判断当前URL是否传入了code和state值
     $checkBrower = C('WECHAT_BROWER');
     if ($checkBrower == true) {
         $code = I('get.code', 'defaultCode');
         $state = I('get.state', '');
         //无授权code,则跳转获取
         if ($code == 'defaultCode' || $state == '' || $code == session('code')) {
             goto_auth();
             return;
         }
         //通过微信返回的CODE值,获取用户信息
         $userInfo = get_user_info($code);
         $userToken = $userInfo['access_token'];
         $this->openid = $userInfo['openid'];
         session('openid', $this->openid);
         session('openidTime', time());
         //            //判断用户权限,未注册跳转到提示页面
         //            $customerInfo = get_customer_info($this->openid);
         //            $customerType = $customerInfo['subscribe_state'];
         //            if($customerType == 0)
         //            {
         ////                转到注册界面
         ////                $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
         ////                $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
         ////                $url = U('Register/Index/index');
         //                //转至提示如果成为分销商的界面
         //                $id = 1;
         //                $reply = new ReplyModel();
         //                $reply->setId($id);
         //                $replyInfo = $reply->getInfoById();
         //                redirect($replyInfo['url']);
         //            }
         session('code', $code);
         //获取调用收货地址配置信息
         //获取调用微信JSSDK签名信息
         $jssdk = new JSSDKController();
         $jssdk->setCode($code);
         $jssdk->setState($state);
         $jssdk->setUserToken($userToken);
         //        echo 'pay中当前code值' . $code . "<br />";
         //        echo 'pay中当前state值' . $state . "<br />";
         //        echo 'pay中当前userToken值' . $userToken . '<br />';
         $addressPackage = $jssdk->getAddressPackage();
         $signPackage = $jssdk->getSignPackage();
     }
     $openid = get_openid();
     $this->assign('openid', $openid);
     $title = '确认订单';
     $this->assign('title', $title);
     //获取get信息,去除html过滤.数据类型为JSON格式的字符串
     $ids = I('get.ids', '');
     //字符串转为数组
     $this->assign('ids', $ids);
     $ids = explode("-", $ids);
     $shoppingCart = new ShoppingCartModel();
     $counts = $shoppingCart->getCounts($ids, $openid);
     $this->assign('counts', implode('-', $counts));
     //取用户收货地址信息
     $address = new CustomerAddressModel();
     $address->setOpenid($openid);
     $customerAddress = $address->getLastUseAdderss();
     $this->assign('address', $customerAddress);
     //设置GET收货地址管理的URL
     $customerAddress['chooseAddressUrl'] = U('Address/chooseAddress');
     $this->assign('address', $customerAddress);
     //取收货地址信息HTML
     $this->assign('indexAddress', $this->fetch('indexAddress'));
     /* 进行数据处理,添加商品详情,按不同来源、物流方式进行分组,并将分组后数组的信息返回 */
     $data = $this->dataAccess($ids, $counts);
     $product = $data[0];
     $totalInfo = $data[1];
     //传用户id,优惠券表里取对应的优惠券
     $couponM = D('Coupon');
     $coupon = $couponM->getCoupon($openid);
     $freightM = D('System/Config');
     $freight = $freightM->freightCount();
     //取运费的五个字段
     $this->assign('freight', $freight);
     //给出收货地址URL信息
     $data['addUrl'] = U('OrderForm/Address/add?openid=' . $openid);
     $data['editUrl'] = U('OrderForm/Address/edit?openid=' . $openid);
     $data['saveAddressUrl'] = U('OrderForm/Address/save');
     $data['uploadImageUrl'] = U('OrderForm/Address/uploadImage');
     $data['updateAddressUrl'] = U('OrderForm/Address/updateAddress');
     $data['deleteUrl'] = U('OrderForm/Address/delete');
     $this->assign('data', $data);
     //给出可以定制的header信息
     $this->assign('header', $this->fetch('indexHeader'));
     $this->assign('coupon', $coupon);
     $this->assign('total', $totalInfo);
     //地址下面的条纹图片路径
     $guoduUrl = add_root_path('/theme/wemall/images/guodu.png');
     $this->assign('guoduUrl', $guoduUrl);
     $this->assign('css', $this->fetch('indexCss'));
     $this->assign('signPackage', $signPackage);
     $this->assign('addressPackage', $addressPackage);
     $updateUrl = U('Index/payNow');
     $this->assign('url', $updateUrl);
     $wxJs = $this->fetch('wxJs');
     $this->assign('wxJs', $wxJs);
     $js = $this->fetch('js');
     $this->assign('js', $js);
     $this->assign('product', $product);
     //送入首页URL
     $indexUrl = U('Home/Index/index');
     $this->assign('indexUrl', $indexUrl);
     //定制底部菜单
     $this->assign('footer', $this->fetch('footer'));
     $this->assign('YZBody', $this->fetch('index'));
     $this->display(YZ_TEMPLATE);
 }
 public function payNowAction()
 {
     $openid = get_openid();
     $ids = I('get.ids', '', false);
     $counts = I('get.counts', '', false);
     $coupon = I("get.coupons", '', false);
     //字符串转为数组
     if ($ids == '') {
         $ids = array();
     } else {
         $ids = explode("-", $ids);
     }
     if ($counts == '') {
         $counts = array();
     } else {
         $counts = explode("-", $counts);
     }
     if ($coupon == '') {
         $coupon = array();
     } else {
         $coupon = explode("-", $coupon);
     }
     //取出商品信息,总计信息
     $group = new SubmitController();
     $product = $group->dataAccess($ids, $counts);
     if ($product == false) {
         return;
     }
     $goods = $product[0];
     $totalInfo = $product[1];
     $countTotal = $totalInfo['totalCount'];
     //看优惠券数量是否大于商品数量
     if (count($coupon) > $countTotal) {
         //跳转至报错界面
         return;
     }
     //送入优惠券ID组成的数组,按ID值取出优惠券信息
     $couponModel = new CouponModel();
     $couponInfo = $couponModel->getInfo($coupon, $openid);
     //利用foreach,计算优惠券总金额(过期优惠券\已使用优惠券直接抛弃)
     $couponTotal = 0;
     foreach ($couponInfo as $key => $value) {
         /* 优惠券使用为0,未使用为1*/
         if ($value['end_time'] > time() && (int) $value['state'] & 1 == 1) {
             $couponTotal += $value['cover'];
         }
     }
     $totalInfo['totalCoupon'] = $couponTotal;
     $totalInfo['totalPayable'] -= $couponTotal;
     $totalInfo['totalPayable'] = $totalInfo['totalPayable'] > 0 ? $totalInfo['totalPayable'] : 1;
     //接收收货地址信息,通过收货地址,取详细信息
     $addressId = I('get.addressId', '');
     $address = new CustomerAddressModel();
     $address->setId($addressId);
     $addressInfo = $address->getAddress();
     $totalInfo['addressInfo'] = $addressInfo;
     $totalInfo['buyOpenid'] = $openid;
     //接收支付类型,类型为0为微信支付,类型为1为其它支付方式
     //        $payType = I('get.payType','0');
     //传入总金额,生成订单支付数据
     $orderRelation = new OrderRelationModel();
     $orderRelation->setTotalInfo($totalInfo);
     $payId = $orderRelation->addOrder();
     //传入订单信息,生成订单数据,返回订单ID
     /*
      * $payid, 支付id
      * $openid, 
      * $logisticsMode, 物流方式
      * $source, 来源
      * $payable, 总金额
      * $weightTotal, 总重量
      * $countTotal,商品总数量
      * $address,收货地址
      * $telephone, 收货电话
      * $name,则货人姓名
      * $frontId, 身份证正面ID
      * $backId, 身份证反面ID
      * $idNo, 身份证号
      * $couponTotal, 优惠总数
      * $directTotal, 直销返点总数
      * $lineTotal,线销返点总数
      * $logisticFee,物流费用
      * $maxAmonut,最大优惠金额
      * $coupon,使用优惠券信息
      */
     $orderForm = new OrderFormModel();
     $orderForm->setPayid($payId);
     $orderForm->setOpenid($openid);
     foreach ($goods as $key => $value) {
         $orderForm->setLogisticsMode($value['logisticType']['name']);
         $orderForm->setSource($value['source']['name']);
         $orderForm->setPayable($value['payable']);
         $orderForm->setWeight($value['weightTotal']);
         $orderForm->setCountTotal($value['countTotal']);
         $orderForm->setDirectFee($value['directTotal']);
         $orderForm->setLineFee($value['lineTotal']);
         $orderForm->setLogisticFee($value['logisticFee']);
         $orderForm->setMaxAmonut($value['maxAmonut']);
         $orderId = $orderForm->addOrder();
         /*生成订单-商品数据**************/
         $orderGoods = new OrderGoodsModel();
         $orderGoods->addOrderGoods($orderId, $value['goods']);
         /*添加商品的销量********************/
         $goodsM = new GoodsModel();
         $goodsM->updateSaled($value['goods']);
     }
     /*删除购物车数据******************/
     $shoppingCart = new ShoppingCartModel();
     $shoppingCart->deleteGoods($openid, $goods);
     /*将当前使用优惠券做使用处理*/
     $couponModel->updateUsed($coupon);
     /* 判断支付类型,拼接好URL后跳转 1为其它支付方式 0为微信支付 
      * 注意,在不同模式下,U方法将生成不同的路径,必须按U方法生成的目录设置微信的授权目录
      * 否则将导致出现  get_brand_wcpay_request:fail_invalid appid错误(      */
     //        if($payType == 1)
     //        {
     //            $url = U('WxPay/OtherPay/payNow?payid=' . $payId);
     //        }
     //        else
     //        {
     //            $url = U('WxPay/Pay/payNow?payid=' . $payId);
     //            //$url1 = U('WxPay/Pay/payNow') . '?payid=' . $payId;
     //        }
     $url = U('Pay/Index/index') . '?payid=' . $payId;
     redirect_url($url);
 }