/**
  * 商品详细页
  */
 public function index()
 {
     $id = I('get.id', 0, 'intval');
     !$id && $this->_productNotExist();
     $itemMod = M('item');
     $item = $itemMod->where(array('id' => $id, 'status' => 1))->find();
     !$item && $this->_productNotExist();
     // 促销判断
     if ($item['is_sell'] == 1) {
         if ($item['sell_end_time'] <= time()) {
             $update = array('is_sell' => 0, 'sell_end_time' => 0, 'sell_start_time' => 0, 'sell_sold' => 0, 'sell_price' => 0.0);
             $itemMod->where(array('id' => $id))->save($update);
             $item = array_merge($item, $update);
         }
     }
     /**
      * ***品牌
      */
     $brand = M('brandlist')->field('name')->find($item['brand']);
     $item['brand'] = $brand['name'];
     //会员价格
     if ($this->visitor->info) {
         $cate_id = $this->visitor->info['id'];
         $cate = M('item_userprice')->field('user_price')->where(array('cate_id' => $cate_id, 'item_id' => $item['id']))->find();
         if ($cate) {
             $item['price'] = $cate['user_price'];
         }
     }
     //商品相册
     $img_list = M('item_img')->field('url')->where(array('item_id' => $id))->order('ordid')->select();
     //标签
     $item['tag_list'] = unserialize($item['tag_cache']);
     //可能还喜欢
     /*   $item_tag_mod = M('item_tag');
          $db_pre = C('DB_PREFIX');
          $item_tag_table = $db_pre . 'item_tag';
          $maylike_list = array_slice($item['tag_list'], 0, 3, true);
          foreach ($maylike_list as $key => $val) {
          $maylike_list[$key] = array('name' => $val);
          $maylike_list[$key]['list'] = $item_tag_mod->field('i.id,i.img,i.intro,' . $item_tag_table . '.tag_id')->where(array($item_tag_table . '.tag_id' => $key, 'i.id' => array('neq', $id)))->join($db_pre . 'item i ON i.id = ' . $item_tag_table . '.item_id')->order('i.id DESC')->limit(9)->select();
          }
           */
     //第一页评论不使用AJAX利于SEO
     $item_comment_mod = M('order_comment');
     $pagesize = 8;
     $map = array('item_id' => $id);
     $count = $item_comment_mod->where($map)->count('id');
     $pager = $this->_pager($count, $pagesize);
     $pager->path = 'comment_list';
     $pager_bar = $pager->fshow();
     $cmt_list = $item_comment_mod->field('__ORDER_COMMENT__.*,__USER__.mobile')->join('__USER__ ON __USER__.id = __ORDER_COMMENT__.user_id')->where($map)->order('id DESC')->limit($pager->firstRow . ',' . $pager->listRows)->select();
     //$itemMod->where(array('id' => $id))->setInc('hits'); //点击量
     $this->assign('item', $item);
     $this->assign('img_list', $img_list);
     $this->assign('cmt_list', $cmt_list);
     $this->assign('page_bar', $pager_bar);
     $this->_config_seo(C('pin_seo_config.item'), array('item_title' => $item['title'], 'item_intro' => $item['intro'], 'item_tag' => implode(' ', (array) $item['tag_list']), 'seo_title' => $item['seo_title'], 'seo_keywords' => $item['seo_keys'], 'seo_description' => $item['seo_desc']));
     //属性
     $attr_list = M('item_attr')->where(array('item_id' => $id))->select();
     foreach ((array) $attr_list as $k => $v) {
         $attr_list[$k]['name_v'] = explode("|", $v['attr_value']);
     }
     $this->assign('attr_list', $attr_list);
     $this->assign('attr_list_count', count($attr_list));
     // user view log
     if (!empty($this->visitor->info['id'])) {
         $log = array('goods_id' => $id, 'user_id' => $this->visitor->info['id'], 'view_time' => time());
         D('item_view_log')->add($log);
     }
     import('Think.ORG.Cart');
     // 导入分页类
     $cart = new \Common\ORG\Cart();
     $num = $cart->getCnt();
     $this->assign('cartnum', $num ? $num : 0);
     $this->display();
 }
 public function pay()
 {
     if (IS_POST && count($_SESSION['cart']) > 0) {
         $cart = new \Common\ORG\Cart();
         $user_address = M('user_address');
         $item_order = M('item_order');
         $order_detail = M('order_detail');
         $item_goods = M('item');
         $this->visitor->info['id'];
         //用户ID
         $this->visitor->info['username'];
         //用户账号
         $this->visitor->info['wechatid'];
         //用户账号
         //生成订单号
         $dingdanhao = date("Y-m-dH-i-s");
         $dingdanhao = str_replace("-", "", $dingdanhao);
         $dingdanhao .= rand(1000, 2000);
         $time = time();
         //订单添加时间
         $address_options = input('post.address_options', 'intval');
         //地址  0:刚填的地址 大于0历史的地址
         $shipping_id = input('post.shipping_id', 'intval');
         //配送方式
         $postscript = input('post.postscript', 'trim');
         //卖家留言
         $data['note'] = $postscript;
         $data['freetype'] = 0;
         $data['order_sumPrice'] = $cart->getPrice();
         if ($data['order_sumPrice'] < C('FREE_DELIVERY_AMOUNT')) {
             $data['freeprice'] = C('DELIVERY_FEE');
             $data['order_sumPrice'] += $data['freeprice'];
         }
         $data['orderId'] = $dingdanhao;
         //订单号
         $data['add_time'] = $time;
         //添加时间
         $data['goods_sumPrice'] = $cart->getPrice();
         //商品总额
         $data['userId'] = $this->visitor->info['id'];
         //用户ID
         if ($this->visitor->info['username']) {
             $data['userName'] = $this->visitor->info['username'];
             //用户名
         } else {
             $data['userName'] = $this->visitor->info['wechatid'];
             //用微信id做用户名
         }
         if ($address_options == 0) {
             $consignee = input('post.consignee', 'trim');
             //真实姓名
             $sheng = input('post.sheng', 'trim');
             //省
             $shi = input('post.shi', 'trim');
             //市
             $qu = input('post.qu', 'trim');
             //区
             $address = input('post.address', 'trim');
             //详细地址
             $phone_mob = input('post.phone_mob', 'trim');
             //电话号码
             $save_address = input('post.save_address', 'trim');
             //是否保存地址
             $data['address_name'] = $consignee;
             //收货人姓名
             $data['mobile'] = $phone_mob;
             //电话号码
             $data['address'] = $sheng . $shi . $qu . $address;
             //地址
             if ($save_address) {
                 $add_address['uid'] = $this->visitor->info['id'];
                 $add_address['consignee'] = $consignee;
                 $add_address['address'] = $address;
                 $add_address['mobile'] = $phone_mob;
                 $add_address['sheng'] = $sheng;
                 $add_address['shi'] = $shi;
                 $add_address['qu'] = $qu;
                 $user_address->data($add_address)->add();
             }
         } else {
             $userId = $this->visitor->info['id'];
             $address = $user_address->where("uid='{$userId}'")->find($address_options);
             //取到地址
             $data['address_name'] = $address['consignee'];
             //收货人姓名
             $data['mobile'] = $address['mobile'];
             //电话号码
             $data['address'] = $address['sheng'] . $address['shi'] . $address['qu'] . $address['address'];
             //地址
         }
         if ($orderid = $item_order->data($data)->add()) {
             $orders['orderId'] = $dingdanhao;
             foreach ($_SESSION['cart'] as $item) {
                 $orders['itemId'] = $item['id'];
                 //商品ID
                 $orders['title'] = $item['name'];
                 //商品名称
                 $orders['img'] = $item['img'];
                 //商品图片
                 $orders['price'] = $item['price'];
                 //商品价格
                 $orders['quantity'] = $item['num'];
                 //购买数量
                 $orders['item_attr'] = $item['attr'];
                 //商品属性
                 $order_detail->data($orders)->add();
             }
             $cart->clear();
             //清空购物车
             $this->assign('orderid', $orderid);
             //订单ID
             $this->assign('dingdanhao', $dingdanhao);
             //订单号
             $this->assign('order_sumPrice', $data['order_sumPrice']);
             vendor('Sms.Sms', COMMON_PATH . 'Vendor/');
             $api = \Common\Vendor\Sms\Sms::getInstance(C('SMS_API'));
             $data['cid'] = \Common\Vendor\Sms\Api\SmsWeimi::ORDER_CREATE;
             $data['0'] = $this->visitor->info['mobile'];
             $data['1'] = $dingdanhao;
             $data['2'] = C('ORDER_KEEP_TIME');
             $api->send($mobile, $data);
         } else {
             $this->error('生成订单失败!');
         }
     } else {
         if (isset($_GET['orderId'])) {
             $item_order = M('item_order');
             $orderId = $_GET['orderId'];
             //订单号
             $userId = $this->visitor->info['id'];
             $orders = $item_order->where("userId='{$userId}' and orderId='{$orderId}'")->find();
             if (!is_array($orders)) {
                 $this->_404();
             }
             if (empty($orders['supportmetho']) || $orders['supportmetho'] == 4) {
                 $this->assign('orderid', $orders['id']);
                 //订单ID
                 $this->assign('dingdanhao', $orders['orderId']);
                 //订单号
                 $this->assign('order_sumPrice', $orders['order_sumPrice']);
             } elseif ($orders['supportmetho'] == 1) {
                 $pay = M('pay')->where(array('pay_type' => 'alipay'))->find();
                 $alipay = unserialize($pay['config']);
                 //$this->assign('alipayview',$pay['status']);
                 echo "<script>location.href='api/wapalipay/alipayapi.php?WIDseller_email=" . $alipay['alipayname'] . "&WIDout_trade_no=" . $orderId . "&WIDsubject=" . $orderId . "&WIDtotal_fee=" . $orders['order_sumPrice'] . "'</script>";
             } elseif ($orders['supportmetho'] == 3) {
                 //$pay=M('pay')->where(array('pay_type'=>'wxpay'))->find();
                 //dump($orders);exit;
                 //$wxpay=unserialize($pay['config']);
                 $wxconfig = $this->wxconfig();
                 $ip = get_client_ip();
                 //获取ip
                 //echo $ip;exit;
                 echo "<script>location.href='api/wxpay/jsapicall.php?ip=" . $ip . "&partner=" . $wxconfig['partnerid'] . "&out_trade_no=" . $orderId . "&body=" . $orderId . "&total_fee=" . $orders['order_sumPrice'] . "&notify_url=" . $wxconfig['notify_url'] . "&showwxpaytitle=1'</script>";
             } elseif ($orders['supportmetho'] == 4) {
                 //支付宝个人收款主页收款
                 $modpayset = M('setting');
                 $alipayhome = $modpayset->where("name='alipayhome'")->getField('data');
                 echo "<script>location.href='{$alipayhome}'</script>";
                 exit;
             }
         } else {
             $this->redirect('User/index');
         }
     }
     $alipay = M('pay')->where(array('pay_type' => 'alipay'))->find();
     $this->assign('alipaystatus', $alipay['status']);
     $wxpay = M('pay')->where(array('pay_type' => 'wxpay'))->find();
     $this->assign('wxpaystatus', $wxpay['status']);
     $this->display();
 }
 public function change_quantity()
 {
     import('Think.ORG.Cart');
     // 导入购物车类
     $cart = new \Common\ORG\Cart();
     $itemId = input('post.itemId', 'intval');
     //商品ID
     $quantity = input('post.quantity', 'intval');
     //购买数量
     // $seid = input('post.seid', 'intval');//sessionID
     //dump($seid);exit;
     $item = M('item')->field('goods_stock')->find($itemId);
     if ($item['goods_stock'] < $quantity) {
         $data = array('status' => 0, 'msg' => '该商品的库存不足');
     } else {
         $cart->modNum($itemId, $quantity);
         $data = array('status' => 1, 'item' => $cart->getItem($itemId), 'sumPrice' => $cart->getPrice());
     }
     echo json_encode($data);
 }