qc.ifish7.com/Application/Shop/Controller/ShopCartController.class.php

210 lines
7.0 KiB
PHP

<?php
namespace Shop\Controller;
use Member\Controller\BaseController;
class ShopCartController extends BaseController{
public function __construct() {
parent::__construct();
$this->ShopCart = D("ShopCart");
}
public function confirmation_order(){
$ids = session("confirmation_order_ids");
$member_id = session("member.member_id");
if(!$ids){
$this->error("请选择购买商品");
}
$where["id"] = array("in",$ids);
$where["member_id"] = $member_id;
$data = $this->ShopCart->relation(true)->where($where)->select();
if(!$data){
$this->error('数据错误');
}
if(IS_POST){
//收货地址
$sh_address=M("member_address")->where(array(
"member_id"=>$member_id,
"id"=>I("post.order_sh_address_id")
))->find();
//发票信息
$order_invoice=M("member_invoice")->where(array(
"member_id"=>$member_id,
"id"=>I("post.order_invoice_id")
))->find();
//收票地址
$sp_address=M("member_address")->where(array(
"member_id"=>$member_id,
"id"=>I("post.order_sp_address_id")
))->find();
//
if(!$sh_address){
$this->error('请选择收货地址');
}
if(!I("post.order_invoice_id")){
$this->error('请设置发票信息');
}
//
if(!$sp_address){
$this->error('请选择收票地址');
}
$order_id = D("ShopOrder")->add_order($data,$sh_address,$order_invoice,$sp_address);
$url=U("pay",array("order_id"=>$order_id));
$this->success($url);
}else{
//默认收货地址
$this->sh_address = M("member_address")->where(array(
"member_id"=>$member_id,
"sh_status"=>1,
))->find();
//默认发票
$this->invoice = M("member_invoice")->where(array(
"member_id"=>$member_id,
"status"=>1,
))->find();
//默认收票地址
$this->sp_address = M("member_address")->where(array(
"member_id"=>$member_id,
"sp_status"=>1,
))->find();
$this->pagetitle="确认订单信息";
$this->data = $data;
$this->display();
}
}
public function pay($order_id){
$this->order=M("shop_order")->find($order_id);
if(!$this->order){
$this->error("订单不存在");
}
$this->display();
}
public function index(){
if(IS_POST){
$ids = I("post.ids");
if(count($ids) <= 0){
$this->error("请选择购物车商品提交");
}
session("confirmation_order_ids",$ids);
$this->success("提交成功");
}else{
$this->pagetitle="我的购物车";
$member_id = session("member.member_id");
$where["member_id"] = $member_id;
$data = $this->ShopCart->relation(true)->where($where)->select();
$this->data = $data;
$this->shopcart_act = "on";
$this->display();
}
}
public function change_shop_cart(){
$id = I("get.id");
$quantity = I("get.num");
// 修改条件
$where["id"] = $id;
$where["member_id"] = session("member.member_id");
// 保存数据
$data["quantity"] = $quantity;
$res = $this->ShopCart->where($where)->save($data);
$this->success($res);
}
public function add_shop_cart(){
$product_id = I("post.id");
$member_id = session("member.member_id");
$quantity = I("post.num");
$where["product_id"] = $product_id;
$where["member_id"] = $member_id;
$res = $this->ShopCart->where($where)->find();
if($res){
$res["quantity"] += $quantity;
$this->ShopCart->save($res);
}else{
$data["quantity"] = $quantity;
$data["catid"] = M("cms_product")->where(array("id"=>$product_id))->getField("catid");
$data["product_id"] = $product_id;
$data["member_id"] = $member_id;
$data["addtime"] = time();
$this->ShopCart->add($data);
}
$this->success("加入购物车成功!");
}
public function move_collect(){
$member_id = session("member.member_id");
$ids = I("post.ids");
if(is_array($ids)){
$where["id"] = array("in",$ids);
}else if($ids){
$where["id"] = $ids;
}else{
$this->error("请选择购物车商品");
}
$where["member_id"] = $member_id;
// 查询所有需要修改的购物车信息
$res_data = $this->ShopCart->where($where)->select();
// 删除购物车信息
// $this->ShopCart->where($where)->delete();
$collect = M("collect");
foreach($res_data as $k => $v){
$where = array();
$where["product_id"] = $v["product_id"];
$where["member_id"] = $member_id;
$rs = $collect->where($where)->find();
if(!$rs){
$data[$k]["product_id"] = $v["product_id"];
$data[$k]["member_id"] = $member_id;
$data[$k]["addtime"] = time();
$data[$k]["checked"] = 1;
}
if($rs["checked"] == 0){
$rs["checked"] = 1;
$collect->save($rs);
}
}
if($data){
$res = $collect->addAll($data);
}
$this->success("添加成功");
}
public function delete(){
$id = I("get.delete_id");
$member_id = session("member.member_id");
$where["id"] = $id;
$where["member_id"] = $member_id;
$this->ShopCart->where($where)->delete();
$this->success();
}
//当前用户收件地址列表
public function addressList(){
$member_id = session("member.member_id");
$where["member_id"] = $member_id;
$this->data= M("member_address")->where($where)->select();
$this->display();
}
//新增收件地址
public function addAddress(){
$this->display();
}
//编辑收货地址
public function editorAddress(){
$where["id"] = I("get.id");
$where["member_id"] = session("member.member_id");
$this->r= M("member_address")->where($where)->find();
$this->display();
}
//发票抬头列表
public function fpList(){
$member_id = session("member.member_id");
$where["member_id"] = $member_id;
$data= M("member_invoice")->where($where)->order("id desc")->select();
$this->success($data);
}
}