cartLogic = new \app\home\logic\CartLogic();
if (session('?user')) {
$nuser=array(
'cart3', 'cart3_nosub','cart6_nosub','cart6'
);
if (!in_array(ACTION_NAME, $nuser)) {
$user = session('user');
$user = M('users')->where("user_id", $user['user_id'])->find();
session('user', $user); //覆盖session 中的 user
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
}else{
$user = session('user');
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
}
$nologin = array(
'getNum', 'addskill', 'cart2skill', 'del_address','cart2','ajaxAddCart',
'get_area', 'edit_address', 'add_address', 'cart3', 'reg_user','cart3_nosub','cart6_nosub',
'cart5','cart6'
);
if ($this->user_id == 0 && ACTION_NAME!='getNum') {
$stoid=getMobileStoId();
if($stoid==1){
$this->user=['user_id'=>982127];
$this->user_id=982127;
}
else
$this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId(), 'oldurl' => urlencode(curHostURL() . $_SERVER['']))));
}
if (!in_array(ACTION_NAME, $nologin)) {
if (($this->pm_erpid && (!$user || !$user['erpvipid'] || !$user['erpvipno']) || ($this->pm_erpid && (!$user || !$user['mobile'])))) {
$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
exit;
}
}
} else {
$uid = Cookie::get('user_id');
if ($uid) {
$user = M('users')->where("user_id", $uid)->where("store_id", getMobileStoId())->find();
session('user', $user); //覆盖session 中的 user
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
} else {
$stoid = getMobileStoId();
if ($stoid == 1) {
$this->user = ['user_id' => 982127];
$this->user_id = 982127;
}
}
}
/*----
$getylpres=getylapp_res(getMobileStoId(),'2');
if ($getylpres)
{
$this->assign('ylpres',$getylpres);
}-----*/
}
public function cart()
{
$t=time();
//获取热卖商品
$hot_goods = M('Goods')
->where("on_time<".$t." and (down_time>".$t." or down_time=0 or down_time='' or down_time is null)")
->where('is_hot=1 and is_on_sale=1 ' . getMobileStoWhere())->limit(20)->select();
$this->assign('hot_goods', $hot_goods);
return $this->fetch('cart', getMobileStoId());
}
/**
* 将商品加入购物车
*/
function addCart()
{
$goods_id = I("goods_id/d"); // 商品id
$goods_num = I("goods_num/d");// 商品数量
$goods_spec = I("goods_spec"); // 商品规格
$goods_spec = json_decode($goods_spec, true); //app 端 json 形式传输过来
$unique_id = I("unique_id"); // 唯一id 类似于 pc 端的session id
$user_id = I('user_id/d', 0); // 用户id
if (empty($this->user['erpvipid']))
return array('status' => -101, 'msg' => '购买商品必须先绑定线下会员', 'result' => '');
$result = $this->cartLogic->addCart($goods_id, $goods_num, $goods_spec, $unique_id, $user_id); // 将商品加入购物车
upload_ylp_log('商品加入购物车');
exit(json_encode($result));
}
/**
* ajax 将商品加入购物车
*/
function ajaxAddCart()
{
$goods_id = I("goods_id/d"); // 商品id
$goods_num = I("goods_num/d");// 商品数量
$goods_spec = I("goods_spec/a", array()); // 商品规格
$pick_up = I("pickup_id/d"); //获取门店id
$to_catr = I("to_catr/d");//1:立即购买
if($to_catr==3) $to_catr=1;
$goods_list = I('goods_list/s');//搭配有值,其他的值是underfined
$isinte=I('isinte/d',0);//积分商品普通购买
$ispd=I('ispd/d',0);//拼团商品普通购买
if ($this->user_id == 0) {
return array('status' => -101, 'msg' => '非法操作', 'result' => '');
}
$result = $this->cartLogic
->addCart($goods_id, $goods_num, $goods_spec, $this->session_id, $this->user, $pick_up, $to_catr, $goods_list,$isinte,$ispd); // 将商品加入购物车
upload_ylp_log('商品加入购物车');
exit(json_encode($result));
}
/**
* 秒杀ajax将商品加入购物车
*/
public function addskill()
{
$goods_id = I("goods_id/d"); // 商品id
$goods_num = I("goods_num/d");// 商品数量
$goods_spec = I("goods_spec/a", array()); // 商品规格
$pick_up = I("pickup_id/d"); //获取门店id
$to_catr = I("to_catr/d");//1:立即购买
if ($this->user_id == 0) {
return array('status' => -101, 'msg' => '非法操作', 'result' => '');
}
$result = $this->cartLogic
->addCart($goods_id, $goods_num, $goods_spec, $this->session_id, $this->user, $pick_up, $to_catr); // 将商品加入购物车
upload_ylp_log('商品加入购物车');
exit(json_encode($result));
}
/*
* 请求获取购物车列表
*/
public function cartList()
{
$cart_form_data = input('cart_form_data'); // goods_num 购物车商品数量
$cart_form_data = json_decode($cart_form_data, true); //app 端 json 形式传输过来
$unique_id = I("unique_id"); // 唯一id 类似于 pc 端的session id
$user_id = I("user_id/d"); // 用户id
$where['session_id'] = $unique_id; // 默认按照 $unique_id 查询
if ($user_id) {
$where['user_id'] = $user_id;
}
$cartList = M('Cart')->where($where)->getField("id,goods_num,selected,goods_id");
/*--要剔除下架商品,活动有过期的商品--*/
foreach ($cartList as $k => $v) {
$good = M('goods')
->where('goods_id', $v['goods_id'])
->where('is_on_sale=1')->find();
if ($good) {
if ($v['prom_type'] != 0) {
$prom = get_goods_promotion($v["goods_id"], $user_id);
if ($prom['is_end'] == 2 || $prom['is_end'] == 3 || $prom['is_end'] == 1) {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $user_id)->delete();
unset($cartList[$k]);
}
}
} else {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $user_id)->delete();
unset($cartList[$k]);
}
}
if ($cart_form_data) {
// 修改购物车数量 和勾选状态
foreach ($cart_form_data as $key => $val) {
$data['goods_num'] = $val['goodsNum'];
$data['selected'] = $val['selected'];
$cartID = $val['cartID'];
if (($cartList[$cartID]['goods_num'] != $data['goods_num']) || ($cartList[$cartID]['selected'] != $data['selected']))
M('Cart')->where("id", $cartID)->save($data);
}
//$this->assign('select_all', $_POST['select_all']); // 全选框
}
$result = $this->cartLogic->cartList($this->user, $unique_id, 0);
upload_ylp_log('购物车列表');
exit(json_encode($result));
}
/**
* 购物车第二步确定页面
*/
public function cart2()
{
$killarr = null;
if ($this->user_id == 0)
$this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
/*--不能重复的一张表--*/
//$userd=M("users")->where('user_id',$this->user_id)->find();
$userd = $this->user;
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
$this->assign('u_money', number_format($userd['user_money'] - $userd['frozen_money'] - $locking_money, 2));
//20180717 season(增加短信随机码校验)
$storeconfig = M('store_config')->where('store_id', getMobileStoId())->field('reg_type,reg_info,reg_default,sms_send_type,switch_list')->find();
$sms_send_type = json_decode($storeconfig['sms_send_type'], true);
$this->assign('s_type', $sms_send_type['type'] ? $sms_send_type['type'] : 0);
$this->assign('s_time_out', $sms_send_type['time_out'] ? $sms_send_type['time_out'] : 60);
$this->assign('is_verifycode', $sms_send_type['is_verifycode'] ? $sms_send_type['is_verifycode'] : 0);
$user_openid = $this->user['openid'];
//$user_openid="ou608v4s8TmwUsGLtJKDvdPKat5w"; //测试用
$this->assign("user_openid", $user_openid);
$user_openidkey = md5($user_openid . "&" . getErpKey());
$this->assign("user_openid", $user_openid);
$this->assign("user_openidkey", $user_openidkey);
//
/*---获取地区---*/
$region_list = get_region_list();
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
$address_id = I('address_id/d');
if ($address_id) {
foreach ($addlist as $kk => $vv) {
if ($vv['address_id'] == $address_id) {
$address = $vv;
}
}
} else {
foreach ($addlist as $kk => $vv) {
if ($vv['is_default'] == 1) {
$address = $vv;
}
}
}
$p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
$this->assign('province', $p);
if (empty($address)) {
//header("Location: ".U('Mobile/User/add_address',array('stoid'=>getMobileStoId(),'source'=>'cart2')));
$this->assign('setadd', 1);
} else {
$this->assign('address', $address);
$this->assign('setadd', 0);
}
/*--是否是购物车--*/
$tocart = I("tocart/d", 0);
$result = null;
if ($tocart == 0) {
if ($this->cartLogic->cart_count($this->user_id, 1) == 0)
$this->error('你的购物车没有选中商品', 'Index/index', array('stoid' => getMobileStoId()));
$result = $this->cartLogic->cartList_cart2($this->user, $this->session_id, 1, 1); //获取购物车商品
} else {
$count = M('Cart')->where(['user_id' => $this->user_id, 'state' => 1])->count();
if ($count == 0)
$this->redirect('Index/index', array('stoid' => getMobileStoId()));
$result = $this->cartLogic->gobuyList_cart2($this->user, $this->session_id); //获取购物车商品
}
/*--购物车商品分组--*/
$cart_goods = $result['cartList'];
$expty = 0;
foreach ($cart_goods as $kp => $vp) {
$exp2 = 0;
if ($vp['distr_type'] != $vp['bdistr_type']) {
$exp2 = $vp['distr_type'] == 0 ? $vp['bdistr_type'] : $vp['distr_type'];
} else {
$exp2 = $vp['distr_type'];
}
$cart_goods[$kp]['expty'] = $exp2;
if ($vp['is_gift'] == 0) {
//判断最后的物流方式
if ($expty != 0) {
if ($expty != $exp2) {
if ($exp2 == 2 || $expty == 2) {
$expty = 2;
} else {
$expty = 0;
}
}
} else {
$expty = $exp2;
}
}
}
$this->assign("expty", $expty);
$shippingList = M('store_shipping')->alias('a')->join("shipping b", "a.shipping_id=b.shipping_id")
->field('b.shipping_id,b.shipping_code,b.shipping_name,b.shipping_desc,b.shipping_logo')
->where("a.status=1 and a.store_id=" . getMobileStoId())->cache("shippingList_" . getMobileStoId(), TPSHOP_CACHE_TIME)
->select();//物流公司
/*--
if(I('cid') != ''){
$cid = I('cid');
$checkconpon = M('coupon')->field('id,name,money')->where("id = $cid")->find(); //要使用的优惠券
$checkconpon['lid'] = I('lid');
}--*/
/*----------购物车商品分组---------*/
$sql = null;
if ($tocart == 0) {
$sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id)";
} else if ($tocart == 1) {
$sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id)";
} else {
$sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=2 GROUP BY pick_id)";
}
//mlog('$tocart:'.$sql,'cart');
$plist_cart = Db::query($sql);
foreach ($plist_cart as $k => $v) {
$isallskill = 1;
$expty00 = 0;
foreach ($cart_goods as $kk => $vv) {
if ($vv['pick_id'] == $v['pickup_id']) {
$list[] = $vv;
$expty11 = $vv['expty'];
if ($expty00 == 0) {
$expty00 = $expty11;
}
}
if ($vv['prom_type'] != 1) {
if ($vv['prom_type'] == 6) {
$isallskill = 1;
}else if($vv['prom_type'] == 2 && $isallskill==1){
$t = time();
$gb = M('group_buy')->where('store_id', getMobileStoId())
->where('goods_id', $vv['goods_id'])
->where('start_time<' . $t)
->where('end_time>' . $t)->where('is_end', 0)
->field('is_quan')
->find();
if ($gb) {
if ($gb['is_quan'] ==1 ) $isallskill = 0;
}
}else{
$isallskill = 0;
}
} else {
$t = time();
$fl = M('flash_sale')->where('store_id', getMobileStoId())->where('goods_id', $vv['goods_id'])
->where('start_time<' . $t)->where('end_time>' . $t)->where('is_end', 0)
->find();
if ($fl) {
$killarr[] = ['goods_id' => $vv['goods_id'], 'fid' => $fl['id'], 'num' => $vv['goods_num']];
}
}
}
if ($killarr) {
$this->assign("killarr", json_encode($killarr));
}
$plist_cart[$k]["goods"] = $list;
$plist_cart[$k]["expty"] = $expty00;
// 获取订单商品总价,$tocart=0为购物车 1为立即购买
//$r = $this->cartLogic->my_calculate($list,$this->user_id,$tocart);
$r = $this->cartLogic->my_calculate_cart2($list, $this->user_id);
unset($list);
// 找出这个用户的优惠券没过期的 并且订单金额达到condition 优惠券指定标准的
$fzquan = M('frozen_quan')->where('user_id', $this->user_id)->field('CashRepNo')->select();
$snow = date("Y-m-d", time());
$is_pt = I("is_pt");
//--如果所有商品不全是秒杀且没有优惠商品--
if ($isallskill == 0 && !$is_pt) {
//如果是注册的会员
$qlistall = "";
if ($this->user['erpvipid']) {
$tk = tpCache('shop_info.api_token', getMobileStoId());
//$user = M("users")->where("user_id",$this->user_id)->field("erpvipid")->find();
$where = "VIPId='" . $this->user['erpvipid'] . "'";
$where .= " and isnull(useObjectID,'')='' and IsUse=0 and (ValidDate is null or ValidDate='' or ValidDate>='" . $snow . "')";
$where .= " and (BuySum<=" . $r." or BuySum=0 or BuySum is null)";
$where .= " and Sum<=" . $r;
$where .= " and (BeginDate='' or BeginDate is null or BeginDate<='" . $snow . "')";
$order['Sum'] = 'desc';
//$rs=getApiData("wxderp.cashreplace.list.get",$tk["api_token"],null,$where,1,1000,$order);
$rs = getApiData("wxderp.cashreplace.list.get", $tk, null, $where, 1, 1000, $order);
$qlistall = json_decode($rs, true);
if (!empty($qlistall)) {
if ($qlistall['data']) {
foreach ($qlistall['data'] as $k1 => $v1) {
$qlistall['data'][$k1]['Sum'] = number_format(empty($v1['Sum']) ? 0 : $v1['Sum'], 2);
$qlistall['data'][$k1]['BuySum'] = number_format(empty($v1['BuySum']) ? 0 : $v1['BuySum'], 2);
$qlistall['data'][$k1]['BillDate'] = date('Y-m-d', strtotime($v1['BeginDate']));
if (empty($v1['ValidDate'])) {
$qlistall['data'][$k1]['ValidDate'] = "不限";
} else {
$qlistall['data'][$k1]['ValidDate'] = date('Y-m-d', strtotime($v1['ValidDate']));
}
//冻结的券不能使用
foreach ($fzquan as $ku => $vu) {
if ($qlistall['data'][$k1]['CashRepNo'] == $vu['CashRepNo']) {
unset($qlistall['data'][$k1]);
break;
}
}
}
}
}
//$couponList = DB::query($sql);
$plist_cart[$k]["couponList"] = $qlistall['data'];
unset($qlistall);
}
//通过缓存拿tk
$ERPId = $this->pm_erpid;
//如果是erp用户
if (empty($ERPId)) {
//非erp用户
$ti = time();
$qlistall = M('coupon_list')->where('store_id', getMobileStoId())
->where('uid', $this->user_id)->where('is_user', 0)
->where('begintime<' . $ti . ' and validtime>' . $ti)
->where('buysum<=' . $r . ' and sum<=' . $r)
->select();
if (!empty($qlistall)) {
foreach ($qlistall as $k1 => $v1) {
$qlistall[$k1]['CashRepNo']=$qlistall[$k1]['code'];
$qlistall[$k1]['Sum'] = number_format(empty($v1['sum']) ? 0 : $v1['sum'], 2);
$qlistall[$k1]['BuySum'] = number_format(empty($v1['buysum']) ? 0 : $v1['buysum'], 2);
$qlistall[$k1]['BillDate'] = date('Y-m-d', $v1['begintime']);
if (empty($v1['validtime'])) {
$qlistall[$k1]['ValidDate'] = "不限";
} else {
$qlistall[$k1]['ValidDate'] = date('Y-m-d', $v1['validtime']);
}
//冻结的券不能使用
foreach ($fzquan as $ku => $vu) {
if ($qlistall[$k1]['code'] == $vu['CashRepNo']) {
unset($qlistall[$k1]);
break;
}
}
}
}
$plist_cart[$k]["couponList"] = $qlistall;
unset($qlistall);
}
}
}
$this->assign('slist', $plist_cart);
$this->assign('shippingList', $shippingList); // 物流公司
//$this->assign('cartList', $result['cartList']); // 购物车的商品
$this->assign('total_price', $result['total_price']); // 总计
upload_ylp_log('购物车已选商品显示');
return $this->fetch('', getMobileStoId());
}
/*---------------------------------秒杀购物详情-------------------------------------------*/
public function cart2skill()
{
$this->assign("skill", 1);
if ($this->user_id == 0)
$this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
/*--不能重复的一张表--*/
//$userd=M("users")->where('user_id',$this->user_id)->find();
$userd = $this->user;
/*--清理未支付订单--*/
clear_flash_Order(getMobileStoId());
$storeconfig = M('store_config')->where('store_id', getMobileStoId())->field('reg_type,reg_info,reg_default,sms_send_type,switch_list')->find();
$sms_send_type = json_decode($storeconfig['sms_send_type'], true);
$this->assign('s_type', $sms_send_type['type'] ? $sms_send_type['type'] : 0);
$this->assign('s_time_out', $sms_send_type['time_out'] ? $sms_send_type['time_out'] : 60);
$this->assign('is_verifycode', $sms_send_type['is_verifycode'] ? $sms_send_type['is_verifycode'] : 0);
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
$this->assign('u_money', number_format($userd['user_money'] - $userd['frozen_money'] - $locking_money, 2));
$user_openid = $this->user['openid'];
//$user_openid="ou608v4s8TmwUsGLtJKDvdPKat5w"; //测试用
$this->assign("user_openid", $user_openid);
$user_openidkey = md5($user_openid . "&" . getErpKey());
$this->assign("user_openid", $user_openid);
$this->assign("user_openidkey", $user_openidkey);
mlog($user_openid."|".$user_openidkey,"cart2skill/".getMobileStoId());
/*---获取地区---*/
$region_list = get_region_list();
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
$address_id = I('address_id/d');
if ($address_id) {
foreach ($addlist as $kk => $vv) {
if ($vv['address_id'] == $address_id) {
$address = $vv;
}
}
} else {
foreach ($addlist as $kk => $vv) {
if ($vv['is_default'] == 1) {
$address = $vv;
}
}
}
$p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
$this->assign('province', $p);
if (empty($address)) {
//header("Location: ".U('Mobile/User/add_address',array('stoid'=>getMobileStoId(),'source'=>'cart2')));
$this->assign('setadd', 1);
} else {
$this->assign('address', $address);
$this->assign('setadd', 0);
}
/*--是否是购物车--*/
$tocart = I("tocart/d", 0);
$result = null;
$count = M('Cart')->where(['user_id' => $this->user_id, 'state' => 1])->count();
if ($count == 0)
$this->redirect('Index/index', array('stoid' => getMobileStoId()));
$result = $this->cartLogic->gobuyList_cart2($this->user, $this->session_id); //获取购物车商品
/*--购物车商品分组--*/
$cart_goods = $result['cartList'];
$expty = 0;
$killarr=null;
foreach ($cart_goods as $kp => $vp) {
$exp2=0;
if($vp['distr_type']!=$vp['bdistr_type']){
$exp2=$vp['distr_type']==0?$vp['bdistr_type']:$vp['distr_type'];
}else{
$exp2=$vp['distr_type'];
}
$cart_goods[$kp]['expty']=$exp2;
if ($vp['is_gift'] == 0) {
//判断最后的物流方式
if ($expty != 0) {
if ($expty != $exp2) {
if($exp2==2 || $expty==2 ){
$expty=2;
}else{
$expty=0;
}
}
} else {
$expty =$exp2;
}
}
$t=time();
$fl=M('flash_sale')->where('store_id',getMobileStoId())->where('goods_id',$vp['goods_id'])
->where('start_time<'.$t)->where('end_time>'.$t)->where('is_end',0)
->find();
if($fl) {
$killarr[] = ['goods_id' => $vp['goods_id'], 'fid' =>$fl['id'],'num'=>$vp['goods_num']];
}
}
$this->assign("expty", $expty);
if($killarr)
$this->assign("killarr", json_encode($killarr));
$shippingList = M('store_shipping')->alias('a')->join("shipping b", "a.shipping_id=b.shipping_id")
->field('b.shipping_id,b.shipping_code,b.shipping_name,b.shipping_desc,b.shipping_logo')
->where("a.status=1 and a.store_id=" . getMobileStoId())->cache("shippingList_" . getMobileStoId(), TPSHOP_CACHE_TIME)
->select();//物流公司
/*----------购物车商品分组---------*/
$sql = null;
$sql = "select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id)";
//mlog('$tocart:'.$sql,'cart');
$plist_cart = Db::query($sql);
foreach ($plist_cart as $k => $v) {
$expty00 = 0;
foreach ($cart_goods as $kk => $vv) {
if ($vv['pick_id'] == $v['pickup_id']) {
$list[] = $vv;
$expty11=$vv['expty'];
if($expty00==0){
$expty00=$expty11;
}
}
}
$plist_cart[$k]["goods"] = $list;
$plist_cart[$k]["expty"]=$expty00;
unset($list);
}
$this->assign('slist', $plist_cart);
$this->assign('shippingList', $shippingList); // 物流公司
$this->assign('total_price', $result['total_price']); // 总计
upload_ylp_log('购物车已选商品显示');
return $this->fetch('cart2', getMobileStoId());
}
/**
* ajax 获取订单商品价格或者提交订单
*/
public function cart3()
{
if(empty($this->redis)){
$this->redis=new \Redis();
$this->redis->connect(redisip, 6379);
}
$popvalue=null;
$popvalue2=null;
$stoid = I("stoid");
//拼单有关
$ispt=I('is_pt');
$pdid=I('pdid');
$team_qh=I('team_qh');
$pdgoodsnum=I('pdgoodsnum/d',0);
$no="";
$notime=microtime_float();
//记录秒杀数据
$sd=null;
if ($_REQUEST['act'] == 'submit_order') {
$sd=I('da');
if(!empty($sd)){
$no='r'.microtime_float().rand(10000, 99990);
$skillarray=json_decode($sd,true);
foreach ($skillarray as $lk=>$lv){
//判断秒杀数量是否超限
$name='ms'.$lv['fid'].'-'.$stoid;
$num0=$lv['num'];
if($this->redis->lLen($name)<$num0){
return json(array('status' => -4, 'msg' => "秒杀商品已经抢光", 'result' => ''));
}
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$value = $this->redis->lPop($name);
$popvalue[]=$value;
}
//redis要加入set中,编号,类型,活动id,数量,时间,stoid
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$this->redis->sAdd('redis_set_'.$stoid,$key);
}
}
//如果是拼单
if($ispt){
$no='r'.microtime_float().rand(10000, 99990);
$name='pind'.$pdid.'-'.$stoid;
if($this->redis->lLen($name)<$pdgoodsnum){
return json(array('status' => -4, 'msg' => "您晚了一步,商品已被抢光!", 'result' => ''));
}
//拼单队列控制
for($i=0;$i<$pdgoodsnum;$i++) {
$value = $this->redis->lPop($name);
$popvalue[]=$value;
}
//如果,$ispt是会员团的时候,要判断团的缓存
if($team_qh && $ispt=='2'){
$name2='pind'.$pdid.'-'.$team_qh.$stoid;
$value = $this->redis->lPop($name2);
if (empty($value)) {
foreach ($popvalue as $k=>$v)
$this->redis->lPush($name,$v);
return array('status' => -4, 'msg' => "手慢了,您参与的团已满团!", 'result' => '');
}
$popvalue2[]=$value;
}
//redis要加入set中,编号,类型,活动id,数量,时间,stoid
$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$this->redis->sAdd('redis_set_'.$stoid,$key);
}
}
//return;
/*--物流方式--*/
$exptype = I("exptype/d");
$state = I("state/d");
if ($this->user_id == 0) {
$this->backredis($no,$notime);
exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态
}
$address_id = I("address_id/d"); // 收货地址id
$setadd = I("setadd/d");
$is_skill = I("skill"); //是否是秒杀
/*--是否是编辑地址--*/
if ($setadd == 1 && $exptype == 2 && empty($address_id)) {
$consignee = I("consignee");
$mobile = I("mobile");
$province = I("province");
$city = I("city");
$district = I("district");
$twon = I("twon");
$more_address = I("more_address");
$address = I("address");
$data = ['consignee' => $consignee,
'mobile' => $mobile,
'province' => $province,
'city' => $city,
'district' => $district,
'twon' => $twon,
'more_address' => $more_address,
'address' => $address,
];
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id, 0, $data);
$address_id = $data['result'];
}
$shipping_code = I("shipping_code/a"); // 物流编号
$invoice_title = I('invoice_title/a'); // 发票
$exparr= I('exparr/a');//获取物流配送数组
$couponTypeSelect = I("couponTypeSelect"); // 优惠券类型 1 下拉框选择优惠券 2 输入框输入优惠券代码
$coupon_id = I("coupon_id/a"); // 优惠券id
$couponCode = I("couponCode"); // 优惠券代码
$user_note = I('user_note/a'); //买家留言
$pay_points = I("pay_points", 0); // 使用积分
$user_money = I("user_money", 0); // 使用余额
$user_money = str_replace(',', '', $user_money);
$user_money = doubleval($user_money);
$exptype=0;
foreach ($exparr as $ko=>$vo){
if($vo==1){ $exptype=1; break;}
}
if($user_money<0) {
$this->backredis($no,$notime);
exit(json_encode(array('status' => -2, 'msg' => '你的余额不足', 'result' => null))); //返回结果状态
}
//if(!empty($user_money))
//$user_money=number_format(empty($user_money)?0:$user_money,2); //使用俩位
/*--购物车商品提取--*/
/*--
$cart_goods = M('Cart')->alias('a')
->join("pick_up b","a.pick_id=b.pickup_id",'left')
->where(['a.user_id'=>$this->user_id,'a.selected'=>1])
->where($where)->getField("a.*,b.pickup_name,b.pickup_id");--*/
/*---购物车和立即购买的区分---*/
$result = null;
if ($state == 0) {
$result = $this->cartLogic->cartList($this->user, $this->session_id, 1, 1); //获取购物车商品
} else {
$result = $this->cartLogic->gobuyList($this->user, $this->session_id); //获取购物车商品
}
$cart_goods = $result['cartList'];
if (empty($cart_goods)) {
$this->backredis($no,$notime);
exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
}
$address = M('UserAddress')->where("address_id", $address_id)->find();
$allusrnum = $user_money;
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
$userd = $this->user;
$salerule = tpCache('basic.sales_rules', $stoid);
$erpid = tpCache('shop_info.ERPId', $stoid);
$userd['sales_rules'] = $salerule;
$userd['ERPId'] = $erpid;
// M("users")->alias('a')
// ->join('store_config b',' a.store_id=b.store_id','left')
// ->join('store c',' a.store_id=c.store_id','left')
// ->field('a.user_money,a.frozen_money,b.sales_rules,c.ERPId')
// ->where('a.user_id',$this->user_id)->find();
//订单余额判断
if (!empty($allusrnum)) {
$ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
if ($ye . '' < $allusrnum) {
$this->backredis($no,$notime);
return array('status' => -4,
'msg' => "您的可用余额不足", 'result' => '');
}
}
// $srule=M('store_config')->where('store_id',$stoid)->find();
// $uerp=M('store')->where('store_id',$stoid)->find();
$count_arr = null;
/*----如果是线下库存,获取线下库存----*/
if ($userd['sales_rules'] == 2) {
if ($is_skill != 1) {
$count_arr = getcountarr($cart_goods, $stoid);
}
}
$err_txt = "";
$prom_goods = array();
//建立优惠促销数组
foreach ($cart_goods as $k => $value) {
if ($value['prom_type'] == 3 && $value['is_gift'] == 0 && $value['is_collocation'] == 0) {
$prom_goods[$value['pick_id']][$value['prom_id']]['num'] += $value['goods_num'];
$prom_goods[$value['pick_id']][$value['prom_id']]['price'] += $value['goods_num'] * $value['goods_price'];
}
if ($value['is_gift'] == 1) {
$prom_goods[$value['pick_id']][$value['prom_id']]['key'] = $k;
}
}
if (!empty($prom_goods)) {
$prom_info = M('prom_goods')->where(['start_time' => ['lt', time()], 'end_time' => ['gt', time()], 'is_end' => 0])
->where('store_id',$stoid)
->getfield('id,name');
foreach ($prom_goods as $kk => $vv) {
foreach ($vv as $key => $val) {
if (!empty($prom_info[$key])) {
$prom = discount($val['price'], $key, $val['num'], $this->user_id);
if (empty($prom['goods_id']) && !empty($val['key'])) {
unset($cart_goods[$val['key']]);
}
$prom_goods[$kk][$key]['int'] = $prom['int'];
$prom_goods[$kk][$key]['coupon'] = $prom['coupon_id'];
$prom_goods[$kk][$key]['is_past'] = $prom['is_past'];
$prom_goods[$kk][$key]['gift_id'] = $prom['gift_id'];
$prom_goods[$kk][$key]['price'] = $prom['price'];
$prom_goods[$kk][$key]['all_price'] = $val['price'];
} else {
//return array('status' => -4, 'msg' => "优惠活动已经结束", 'result' => '');
unset($prom_goods[$kk][$key]);
}
}
}
}
/*--预出库库存--*/
$gidarr=get_arr_column($cart_goods,'goods_id');
$fgid0=implode(',',$gidarr);
$strq = 'select pickup_id,goods_id,sum(out_qty) as sum0 from wxd_erp_yqty where goods_id in(' . $fgid0 . ') and store_id='.$stoid.' GROUP BY pickup_id,goods_id';
$dd = Db::query($strq);
$ddd = [];
//---组装数组---
if ($dd){
foreach ($dd as $ku => $vu) {
$ypkid=$vu['pickup_id'];
$ypgid=$vu['goods_id'];
$ddd[$ypkid.".".$ypgid]=$vu['sum0'];
}
}
/*--库存判断和活动判断,商品的库存和活动不能过期--*/
foreach ($cart_goods as $k => $value) {
$ginfo = $value;
/*--线下库存的序列key--*/
$bkey = $value['pick_id'] . $value['goods_id'];
//商品参与活动查看
if ($value['prom_type'] > 0 && $value['prom_type'] != 3 && $value['is_collocation'] == 0 && $value['is_integral_normal']!=1 && $value['is_pd_normal']!=1 ) {
/*--修改获取活动的函数--*/
$prom = get_goods_promotion2($ginfo, null, $this->user_id);
if ($prom['is_end'] == 1) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品活动已经结束", 'result' => '');
}
if ($prom['is_end'] == 2) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品已经售完", 'result' => '');
}
if ($prom['is_end'] == 3) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量", 'result' => '');
}
if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4) {
if ($value['goods_num'] > $prom['buy_limit']) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量", 'result' => '');
}
}
if ($value['member_goods_price'] != $prom['price']) {
$err_txt .= "{$ginfo['goods_name']}商品价格已经变化,是否继续购买!
";
}
if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4 || $value['prom_type'] == 6) {
if ($value['goods_num'] > $ginfo['store_count']) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else {
if ($userd['sales_rules'] == 1) {
if ($value['goods_num'] > $ginfo['store_count']) {
$this->backredis($no,$notime);
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else if ($userd['sales_rules'] == 2) {
if (empty($count_arr)) {
return array('status' => -4, 'msg' => "库存获取失败", 'result' => '');
}
/*--预出库判断--*/
$ynum=$ddd[$bkey];
if($ynum) {
if ($value['goods_num']+$ynum > $count_arr[$bkey]) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}else{
if ($value['goods_num'] > $count_arr[$bkey]) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}
}
}
}else{
if ($value['is_gift'] == 0) {
if ($userd['sales_rules'] == 1) {
if ($value['goods_num'] > $ginfo['store_count']) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else if ($userd['sales_rules'] == 2) {
if (empty($count_arr)) {
return array('status' => -4, 'msg' => "库存获取失败", 'result' => '');
}
if ($value['goods_num'] > $count_arr[$bkey]) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}
/*--判断价格是否发生变化 并且不是搭配商品--*/
if ($value['member_goods_price'] != $ginfo['shop_price'] && $value['is_collocation'] == 0 && $value['is_integral_normal']!=1 && $value['is_pd_normal']!=1) {
$err_txt .= "{$ginfo['goods_name']} 商品价格已经变化
";
}
}
}
}
if ($err_txt != "") {
$this->backredis($no,$notime);
return array('status' => -1004, 'msg' => $err_txt, 'result' => '');
}
/*--购物车商品分组,生成不同订单--*/
$plist = null;
if ($state == 0) {
$sql = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id";
$plist = Db::query($sql);
foreach ($plist as $k => $v) {
$count = array();
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
if (!empty($prom_goods[$v['pick_id']]) && $vv['prom_type'] == 3 && $count[$vv['prom_id']] == 0) {
$plist[$k]['order']['int'] += $prom_goods[$v['pick_id']][$vv['prom_id']]['int'];
$plist[$k]['order']['coupon'][] = $prom_goods[$v['pick_id']][$vv['prom_id']]['coupon'];
$plist[$k]['order'][$vv['prom_id']]['gift_id'] = $prom_goods[$vv['pick_id']][$vv['prom_id']]['gift_id'];
$plist[$k]['discount'] = $prom_goods[$v['pick_id']];
$count[$vv['prom_id']] += 1;
}
}
$plist[$k]["goods"] = $list;
unset($list);
}
} else {
$sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id";
$plist = Db::query($sql1);
foreach ($plist as $k => $v) {
$count = array();
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
if (!empty($prom_goods[$v['pick_id']]) && $vv['prom_type'] == 3 && $count[$vv['prom_id']] == 0) {
$plist[$k]['order']['int'] += $prom_goods[$v['pick_id']][$vv['prom_id']]['int'];
$plist[$k]['order']['coupon'][] = $prom_goods[$v['pick_id']][$vv['prom_id']]['coupon'];
$plist[$k]['order'][$vv['prom_id']]['gift_id'] = $prom_goods[$vv['pick_id']][$vv['prom_id']]['gift_id'];
$plist[$k]['discount'] = $prom_goods[$v['pick_id']];
$count[$vv['prom_id']] += 1;
}
if($plist[$k]['order']) mlog(json_encode($plist[$k]['order']).$k,"cart3_yhui/".$stoid);
}
$plist[$k]["goods"] = $list;
unset($list);
}
}
/*--开始生成多单--*/
//$uerp=M('store')->where('store_id',$stoid)->find();
$pno = "";
if (count($plist) > 1) $pno = $userd['ERPId'] . date('ymdHis') . get_total_millisecond() . rand(1000, 9999);
$postFee = 0;$couponFee = 0;$balance = 0;$pointsFee = 0;$payables = 0;$payables1 = 0;$goodsFee = 0;$order_prom_id = 0;$order_prom_amount = 0;
//启动事务
Db::startTrans();
try {
foreach ($plist as $k => $v) {
/*--计算订单相关--*/
$result = calculate_price2($this->user, $v["goods"], $shipping_code[$v['pick_id']], 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, $coupon_id[$v['pick_id']], $couponCode, $stoid, $exparr[$v['pick_id']], $v['discount']);
if ($result['status'] < 0) {
$this->backredis($no,$notime);
exit(json_encode($result));
}
/*---余额使用---*/
$user_money = round($user_money - $result['result']['user_money'], 2);
/*----
$order_prom = get_order_promotion($result['result']['order_amount'],$stoid);
$result['result']['order_amount'] = $order_prom['order_amount'];
$result['result']['order_prom_id'] = $order_prom['order_prom_id'];
$result['result']['order_prom_amount'] = $order_prom['order_prom_amount'];
---*/
$postFee += $result['result']['shipping_price']; // 物流费
$couponFee += $result['result']['coupon_price'];// 优惠券
$balance += $result['result']['user_money']; // 使用用户余额
$pointsFee += $result['result']['integral_money']; // 积分支付
$payables += $result['result']['order_amount'];// 应付金额
$payables1 += $result['result']['order_amount1'];// 应付金额,不包含余额的那部分
$goodsFee += $result['result']['goods_price'];// 商品价格
$order_prom_id .= $result['result']['order_prom_id'] . ",";// 订单优惠活动id
$order_prom_amount += $result['result']['order_prom_amount'] + $result['result']['discount'];// 订单优惠活动优惠了多少钱
// 提交订单
if ($_REQUEST['act'] == 'submit_order') {
$address_id = $_REQUEST['address_id'];
mlog("日志" . $address_id . "是否编辑地址" . $setadd, "cart");
if (!$address_id && $exptype == 0) {
$this->backredis($no,$notime);
exit(json_encode(array('status' => -3, 'msg' => '请先填写收货人信息', 'result' => null)));
} // 返回结果状态
if (!$shipping_code && $exptype == 0){
$this->backredis($no,$notime);
exit(json_encode(array('status' => -4, 'msg' => '请选择物流信息', 'result' => null)));
} // 返回结果状态
if (empty($coupon_id) && !empty($couponCode)) {
$coupon_id = M('CouponList')->where("code", $couponCode)->getField('id');
}
//$iii=$result['result']['user_money'];
$car_price0 = array(
'postFee' => $result['result']['shipping_price'], // 物流费
'couponFee' => $result['result']['coupon_price'], // 优惠券
'balance' => $result['result']['user_money'], // 使用用户余额
'pointsFee' => $result['result']['integral_money'], // 积分支付
'payables' => $result['result']['order_amount'], // 应付金额
'payables1' => $result['result']['order_amount1'], // 应付金额
'goodsFee' => $result['result']['goods_price'],// 商品价格
'order_prom_id' => $result['result']['order_prom_id'], // 订单优惠活动id
'order_prom_amount' => $result['result']['order_prom_amount'], // 订单优惠活动优惠了多少钱
'discount_amount' => $result['result']['discount'],//优惠、搭配促销优惠的金额
'good_prom'=>$result['result']['good_prom'],//优惠活动
);
/*--调用生成订单的函数--*/
$result = $this->cartLogic->addOrder($this->user_id, $address_id,
$shipping_code[$v['pick_id']], $invoice_title[$v['pick_id']], $car_price0,
$v["goods"], $coupon_id[$v['pick_id']], $user_note[$v['pick_id']], $stoid, $exparr[$v['pick_id']], $pno, $v['pick_id'], $v['order'], $this->user,$no,$ispt,$pdid,$team_qh); // 添加订单
if($result["status"] != 1) {
//如果失败就补回队列
if(!empty($sd)){
$skillarray=json_decode($sd,true);
foreach ($skillarray as $lk=>$lv){
$name='ms'.$lv['fid'].'-'.$stoid;
$num0=$lv['num'];
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$this->redis->lPush($name,time().'_'.$i);
}
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$this->redis->sRem('redis_set_'.$stoid,$key);
}
}
//如果是拼单
if($ispt){
$name='pind'.$pdid.'-'.$stoid;
//拼单队列控制
for($i=0;$i<$pdgoodsnum;$i++) {
$this->redis->lPush($name,time().'_'.$i);
}
if($team_qh && $ispt=='2') {
$name2 = 'pind' . $pdid . '-' . $team_qh . $stoid;
$this->redis->lPush($name2,time().'_0');
}
$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$this->redis->sRem('redis_set_'.$stoid,$key);
}
//如果订单失败要还原数量
exit(json_encode($result));
}
if (empty($pno) && count($plist) == 1) {
$pno = $result["result"];
}
}
}
//提交订单
if ($_REQUEST['act'] == 'submit_order') {
upload_ylp_log('提交订单');
if ($state == 0) {
// 删除购物车中已提交订单商品
M('Cart')->where(['user_id' => $this->user_id, 'selected' => 1])->delete();
//删除购物车的cookie
Cookie::delete("cn");
Db::commit();
if(!empty($sd)) {
$skillarray = json_decode($sd, true);
foreach ($skillarray as $lk => $lv) {
//判断秒杀数量是否超限
$num0 = $lv['num'];
$name='ms'.$lv['fid'].'-'.$stoid;
$jsda['len']=$this->redis->lLen($name);
$jsda['num']=-$num0;
$jsda['order_sn']=$pno;
$jsda['type']=0; //0是买单 1是5分钟自动补回 2是手动补回
mlog(json_encode($jsda),'flash_log/' . $stoid . '/' . $lv['fid']."_1");
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$this->redis->sRem('redis_set_'.$stoid,$key);
}
}
exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
} else {
// 删除cart表中立即购买的商品
M('Cart')->where(['user_id' => $this->user_id, 'state' => 1])->delete();
Db::commit();
//正确提交订单记录redis数量
if(!empty($sd)) {
$skillarray = json_decode($sd, true);
foreach ($skillarray as $lk => $lv) {
//判断秒杀数量是否超限
$num0 = $lv['num'];
$name='ms'.$lv['fid'].'-'.$stoid;
$jsda['len']=$this->redis->lLen($name);
$jsda['num']=-$num0;
$jsda['order_sn']=$pno;
$jsda['type']=0; //0是买单 1是5分钟自动补回 2是手动补回
mlog(json_encode($jsda),'flash_log/' . $stoid . '/' . $lv['fid']."_1");
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$this->redis->sRem('redis_set_'.$stoid,$key);
}
}
if($ispt){
$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$this->redis->sRem('redis_set_'.$stoid,$key);
}
exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
}
}
}catch (\Exception $e) {
// 回滚事务
Db::rollback();
if ($_REQUEST['act'] == 'submit_order') {
//如果失败就补回队列
if(!empty($sd)){
$skillarray=json_decode($sd,true);
foreach ($skillarray as $lk=>$lv){
$name='ms'.$lv['fid'].'-'.$stoid;
$num0=$lv['num'];
//秒杀队列控制
for($i=0;$i<$num0;$i++) {
$this->redis->lPush($name,time().'_'.$i);
}
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$this->redis->sRem('redis_set_'.$stoid,$key);
}
}
//如果是拼单
if($ispt){
$name='pind'.$pdid.'-'.$stoid;
//拼单队列控制
for($i=0;$i<$pdgoodsnum;$i++) {
$this->redis->lPush($name,time().'_'.$i);
}
if($team_qh && $ispt=='2') {
$name2 = 'pind' . $pdid . '-' . $team_qh . $stoid;
$this->redis->lPush($name2,time().'_0');
}
$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$this->redis->sRem('redis_set_'.$stoid,$key);
}
}
exit(json_encode(array('status' => -1, 'msg' => '提交订单失败')));
}
$car_price = array(
'postFee' => round($postFee, 2), // 物流费
'couponFee' => round($couponFee, 2), // 优惠券
'balance' => round($allusrnum - $user_money, 2), // 使用用户余额
'pointsFee' => round($pointsFee, 2), // 积分支付
'payables' => round($payables, 2), // 应付金额
'payables1' => round($payables1, 2), // 应付金额,不包含使用余额的那一部分
'goodsFee' => round($goodsFee, 2),// 商品价格
'order_prom_id' => $order_prom_id, // 订单优惠活动id
'order_prom_amount' => round($order_prom_amount, 2), // 订单优惠活动优惠了多少钱
);
$return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
exit(json_encode($return_arr));
}
/**
* ajax 获取订单商品价格
*/
public function cart3_nosub()
{
/*--物流方式--*/
$exptype = I("exptype/d");
$stoid = I("stoid");
$state = I("state/d");
if ($this->user_id == 0)
exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态
$address_id = I("address_id/d"); //收货地址id
$setadd = I("setadd/d");
$is_skill = I("skill"); //是否是秒杀
$exparr= I('exparr/a');//获取物流配送数组
/*--是否是编辑地址--*/
if ($setadd == 1 && $exptype == 0 && empty($address_id)) {
$consignee = I("consignee");
$mobile = I("mobile");
$province = I("province");
$city = I("city");
$district = I("district");
$twon = I("twon");
$address = I("address");
$data = ['consignee' => $consignee,
'mobile' => $mobile,
'province' => $province,
'city' => $city,
'district' => $district,
'twon' => $twon,
'address' => $address,
];
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id, 0, $data);
$address_id = $data['result'];
}
$shipping_code = I("shipping_code/a"); // 物流编号
$invoice_title = I('invoice_title/a'); // 发票
$couponTypeSelect = I("couponTypeSelect"); // 优惠券类型 1 下拉框选择优惠券 2 输入框输入优惠券代码
$coupon_id = I("coupon_id/a"); // 优惠券id
$couponCode = I("couponCode"); // 优惠券代码
$user_note = I('user_note/a'); //买家留言
$pay_points = I("pay_points", 0); // 使用积分
$user_money = I("user_money", 0); // 使用余额
$user_money = str_replace(',', '', $user_money);
$user_money = doubleval($user_money);
//if(!empty($user_money))
//$user_money=number_format($user_money,2); //使用俩位
$result = null;
if ($state == 0) {
$result = $this->cartLogic->cartList_cart2($this->user, $this->session_id, 1, 1); //获取购物车商品
} else {
$result = $this->cartLogic->gobuyList_cart2($this->user, $this->session_id); //获取购物车商品
}
$cart_goods = $result['cartList'];
if (empty($cart_goods))
exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
$address = M('UserAddress')->where("address_id", $address_id)->find();
$allusrnum = $user_money;
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
$userd = $this->user;
$salerule = tpCache('basic.sales_rules', $stoid);
$erpid = tpCache('shop_info.ERPId', $stoid);
$userd['sales_rules'] = $salerule;
$userd['ERPId'] = $erpid;
//订单余额判断
if (!empty($allusrnum)) {
$ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
if ($ye . '' < $allusrnum) {
return array('status' => -4,
'msg' => "您的可用余额不足", 'result' => '');
}
}
/*----如果是线下库存,获取线下库存----*/
$count_arr = null;
if ($userd['sales_rules'] == 2) {
if ($is_skill != 1)
$count_arr = getcountarr($cart_goods, $stoid);
}
/*--购物车商品分组,生成不同订单--*/
$plist = null;
if ($state == 0) {
$sql = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id";
$plist = Db::query($sql);
foreach ($plist as $k => $v) {
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
}
$plist[$k]["goods"] = $list;
unset($list);
}
} else {
$sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=1 GROUP BY pick_id";
$plist = Db::query($sql1);
foreach ($plist as $k => $v) {
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
}
$plist[$k]["goods"] = $list;
unset($list);
}
}
/*--开始生成多单--*/
//$uerp=M('store')->where('store_id',$stoid)->find();
$pno = "";
if (count($plist) > 1) $pno = $userd['ERPId'] . date('ymdHis') . get_total_millisecond() . rand(1000, 9999);
$postFee = 0;$couponFee = 0;$balance = 0;$pointsFee = 0;$payables = 0;$payables1 = 0;$goodsFee = 0;$order_prom_id = 0;$order_prom_amount = 0;$goods_prom_amount=0;
foreach ($plist as $k => $v) {
/*--计算订单相关--*/
$result = calculate_price2($this->user, $v["goods"], $shipping_code[$v['pick_id']], 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, $coupon_id[$v['pick_id']], $couponCode, $stoid, $exparr[$v['pick_id']],$v['discount']);
if ($result['status'] < 0)
exit(json_encode($result));
/*---余额使用---*/
$user_money = round($user_money - $result['result']['user_money'], 2);
$postFee += $result['result']['shipping_price']; // 物流费
$couponFee += $result['result']['coupon_price'];// 优惠券
$balance += $result['result']['user_money']; // 使用用户余额
$pointsFee += $result['result']['integral_money']; // 积分支付
$payables += $result['result']['order_amount'];// 应付金额
$payables1 += $result['result']['order_amount1'];// 应付金额,不包含余额的那部分
$goodsFee += $result['result']['goods_price'];// 商品价格
$order_prom_id .= $result['result']['order_prom_id'] . ",";// 订单优惠活动id
$order_prom_amount += $result['result']['order_prom_amount'];//订单优惠
$goods_prom_amount+= $result['result']['discount'];//优惠促销活动
}
$car_price = array(
'postFee' => round($postFee, 2), // 物流费
'couponFee' => round($couponFee, 2), // 优惠券
'balance' => round($allusrnum - $user_money, 2), // 使用用户余额
'pointsFee' => round($pointsFee, 2), // 积分支付
'payables' => round($payables, 2), // 应付金额
'payables1' => round($payables1, 2), // 应付金额,不包含使用余额的那一部分
'goodsFee' => round($goodsFee, 2),// 商品价格
'order_prom_id' => $order_prom_id, // 订单优惠活动id
'order_prom_amount' => round($order_prom_amount, 2), // 订单优惠优惠了多少钱
'goods_prom_amount' => round($goods_prom_amount, 2), // 优惠活动优惠了多少钱
);
$return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
exit(json_encode($return_arr));
}
/*
* 订单支付页面
*/
public function cart4()
{
$orderno = I('order_id');
$order_amount = M('Order')->where("parent_sn", $orderno)->sum('order_amount');
$order = M('Order')->where("parent_sn", $orderno)->select();
// 如果已经支付过的订单直接到订单详情页面. 不再进入支付页面
if ($order[0]['pay_status'] == 1) {
$order_detail_url = U("Mobile/User/order_detail", array('stoid' => getMobileStoId(), 'id' => $order["order_id"]));
header("Location: $order_detail_url");
exit;
}
$paymentList = M('Plugin')->where("`type`='payment' and status = 1 and scene in(0,1)")->select();
//微信浏览器
if (strstr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
//$paymentList = M('Plugin')->where("`type`='payment' and status = 1 and code in('weixin','cod')")->select();
$paymentList = M('Plugin')->where("`type`='payment' and status = 1 and code in('weixin')")->select();
}
$paymentList = convert_arr_key($paymentList, 'code');
foreach ($paymentList as $key => $val) {
$val['config_value'] = unserialize($val['config_value']);
if ($val['config_value']['is_bank'] == 2) {
$bankCodeList[$val['code']] = unserialize($val['bank_code']);
}
//判断当前浏览器显示支付方式
if (($key == 'weixin' && !is_weixin()) || ($key == 'alipayMobile' && is_weixin())) {
unset($paymentList[$key]);
}
}
$bank_img = include APP_PATH . 'home/bank.php'; // 银行对应图片
$payment = M('Plugin')->where("`type`='payment' and status = 1")->select();
$this->assign('paymentList', $paymentList);
$this->assign('bank_img', $bank_img);
$this->assign('order_amount', $order_amount);
$this->assign('bankCodeList', $bankCodeList);
$this->assign('pay_date', date('Y-m-d', strtotime("+1 day")));
upload_ylp_log('订单支付');
return $this->fetch('', getMobileStoId());
}
public function cart5()
{
if ($this->user_id == 0)
$this->error('请先登录', U('Mobile/User/login', array('stoid' => getMobileStoId())));
$userd=$this->user;
//获取积分
$integ=get_userpoints($userd,getMobileStoId(),$this->pm_erpid);
//20180717 season(增加短信随机码校验)
$storeconfig = M('store_config')->where('store_id', getMobileStoId())->field('reg_type,reg_info,reg_default,sms_send_type,switch_list')->find();
$sms_send_type = json_decode($storeconfig['sms_send_type'], true);
$this->assign('s_type', $sms_send_type['type'] ? $sms_send_type['type'] : 0);
$this->assign('s_time_out', $sms_send_type['time_out'] ? $sms_send_type['time_out'] : 60);
$this->assign('is_verifycode', $sms_send_type['is_verifycode'] ? $sms_send_type['is_verifycode'] : 0);
$user_openid = $this->user['openid'];
//$user_openid="ou608v4s8TmwUsGLtJKDvdPKat5w"; //测试用
$this->assign("user_openid", $user_openid);
$user_openidkey = md5($user_openid . "&" . getErpKey());
$this->assign("user_openid", $user_openid);
$this->assign("user_openidkey", $user_openidkey);
//
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
$this->assign('u_money', number_format($userd['user_money'] - $userd['frozen_money'] - $locking_money, 2));
$this->assign('u_integral', $integ);
/*---获取地区---*/
$region_list = get_region_list();
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
$address_id = I('address_id/d');
if ($address_id) {
foreach ($addlist as $kk => $vv) {
if ($vv['address_id'] == $address_id) {
$address = $vv;
}
}
} else {
foreach ($addlist as $kk => $vv) {
if ($vv['is_default'] == 1) {
$address = $vv;
}
}
}
$p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
$this->assign('province', $p);
if (empty($address)) {
//header("Location: ".U('Mobile/User/add_address',array('stoid'=>getMobileStoId(),'source'=>'cart2')));
$this->assign('setadd', 1);
} else {
$this->assign('address', $address);
$this->assign('setadd', 0);
}
/*--是否是购物车--*/
$tocart = I("tocart/d", 0);
$result = null;
$count = M('Cart')->where(['user_id' => $this->user_id, 'state' => 2])->count();
if ($count == 0)
$this->redirect('Index/index', array('stoid' => getMobileStoId()));
$result = $this->cartLogic->gointeList($this->user, $this->session_id); //获取购物车商品
$cartinfo = M('cart')->where(array('state' => 2, 'user_id' => $this->user_id))->select(); //购物车积分购
$proinfo = M('goods')->where(array('goods_id' => $cartinfo[0]['goods_id']))->select();
$inteinfo = M('integral_buy')->where(array('goods_id' => $proinfo[0]['goods_id'], 'is_end' => 0))->select();
mlog(json_encode($result), "cart");
$shippingList = M('store_shipping')->alias('a')->join("shipping b", "a.shipping_id=b.shipping_id")
->field('b.shipping_id,b.shipping_code,b.shipping_name,b.shipping_desc,b.shipping_logo')
->where("a.status=1 and a.store_id=" . getMobileStoId())
->select();//物流公司
/*--购物车商品分组--*/
$cart_goods = $result['cartList'];
$expty = 0;
foreach ($cart_goods as $kp => $vp) {
$exp2=0;
if($vp['distr_type']!=$vp['bdistr_type']){
$exp2=$vp['distr_type']==0?$vp['bdistr_type']:$vp['distr_type'];
}else{
$exp2=$vp['distr_type'];
}
$cart_goods[$kp]['expty']=$exp2;
if ($vp['is_gift'] == 0) {
//判断最后的物流方式
if ($expty != 0) {
if ($expty != $exp2) {
if($exp2==2 || $expty==2 ){
$expty=2;
}else{
$expty=0;
}
}
} else {
$expty =$exp2;
}
}
}
$this->assign("expty", $expty);
/*----
foreach ($cart_goods as $kp => $vp) {
if ($vp['distr_type'] != 0) {
if ($expty != 0) {
if ($expty != $vp['distr_type']) {
$this->error('您不能同时选择自提和物流商品', 'cart/cart', array('stoid' => getMobileStoId()));
}
} else {
$expty = $vp['distr_type'];
}
}
}
$this->assign("expty", $expty);---*/
$sql = null;
//mlog('$tocart:'.$sql,'cart');
$plist_cart = Db::query("select pickup_id,pickup_name from __PREFIX__pick_up where pickup_id in(select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=2 GROUP BY pick_id)");
foreach ($plist_cart as $k => $v) {
$expty00 = 0;
foreach ($cart_goods as $kk => $vv) {
if ($vv['pick_id'] == $v['pickup_id']) {
$list[] = $vv;
$expty11=$vv['expty'];
if($expty00==0){
$expty00=$expty11;
}
}
}
$plist_cart[$k]["goods"] = $list;
$plist_cart[$k]["expty"]=$expty00;
// 获取订单商品总价,$tocart=0为购物车 1为立即购买
//$r = $this->cartLogic->my_calculate($list,$this->user_id,$tocart);
// 找出这个用户的优惠券没过期的 并且订单金额达到condition 优惠券指定标准的
// $fzquan=M('frozen_quan')->where('user_id',$this->user_id)->field('CashRepNo')->select();
// $snow=date("Y-m-d H:i:s",time());
// $tk = M("store")->where("store_id", getMobileStoId())->field("api_token")->find();
// $user = M("users")->where("user_id",$this->user_id)->field("erpvipid")->find();
// $where = "VIPId='" . $user['erpvipid'] . "'";
// $where .= " and IsUse=0 and ValidDate>='" . $snow . "'";
//// $where .= " and BuySum<=".$r;
//// $where .= " and Sum<=".$r;
// $order['Sum']='desc';
//
// $rs=getApiData("wxderp.cashreplace.list.get",$tk["api_token"],null,$where,1,1000,$order);
// $qlistall = json_decode($rs, true);
// if(!empty($qlistall)){
// if($qlistall['data']) {
// foreach ($qlistall['data'] as $k1 => $v1) {
// foreach ($fzquan as $ku=>$vu){
// $qlistall['data'][$k1]['CashRepNo']=$vu['CashRepNo'];
// unset($qlistall['data'][$k1]);
// break;
// }
// $qlistall['data'][$k1]['Sum'] = number_format(empty($v1['Sum']) ? 0 : $v1['Sum'], 2);
// $qlistall['data'][$k1]['BuySum'] = number_format(empty($v1['BuySum']) ? 0 : $v1['BuySum'], 2);
// $qlistall['data'][$k1]['ValidDate']=date('Y-m-d H:i:s', strtotime($v1['ValidDate']));
// }
//
// }
// }
// //$couponList = DB::query($sql);
// $plist_cart[$k]["couponList"]=$qlistall['data'];
unset($list);
//unset($qlistall);
}
$this->assign('slist', $plist_cart);
$this->assign('shippingList', $shippingList); // 物流公司
$this->assign('total_price', $result['total_price']); // 总计
upload_ylp_log('购买积分购商品订单页面');
return $this->fetch('', getMobileStoId());
}
/**
* ajax 获取积分购订单商品价格或者积分购提交订单
*/
public function cart6()
{
/*--物流方式--*/
$exptype = I("exptype/d");
$stoid = I("stoid");
$state = I("state/d");
if ($this->user_id == 0)
exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态
$address_id = I("address_id/d"); // 收货地址id
$setadd = I("setadd/d");
/*--积分购传到前台的值--*/
$arystr = I("goodsstr");
$ary0 = explode("-", $arystr);
/*--是否是编辑地址--*/
if ($setadd == 1 && $exptype == 0) {
$consignee = I("consignee");
$mobile = I("mobile");
$province = I("province");
$city = I("city");
$district = I("district");
$twon = I("twon");
$address = I("address");
$data = ['consignee' => $consignee,
'mobile' => $mobile,
'province' => $province,
'city' => $city,
'district' => $district,
'twon' => $twon,
'address' => $address,
];
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id, 0, $data);
$address_id = $data['result'];
}
$shipping_code = I("shipping_code/a"); // 物流编号
$invoice_title = I('invoice_title/a'); // 发票
$exparr= I('exparr/a');//获取物流配送数组
$couponTypeSelect = I("couponTypeSelect"); // 优惠券类型 1 下拉框选择优惠券 2 输入框输入优惠券代码
$coupon_id = I("coupon_id/a"); // 优惠券id
$couponCode = I("couponCode"); // 优惠券代码
$user_note = I('user_note/a'); //买家留言
$pay_points = I("pay_points", 0); // 使用积分
$user_money = I("user_money", 0); // 使用余额
$user_money = str_replace(',', '', $user_money);
$user_money = doubleval($user_money);
$exptype=0;
foreach ($exparr as $ko=>$vo){
if($vo==1){ $exptype=1; break;}
}
/*--购物车商品提取--*/
/*--
$cart_goods = M('Cart')->alias('a')
->join("pick_up b","a.pick_id=b.pickup_id",'left')
->where(['a.user_id'=>$this->user_id,'a.selected'=>1])
->where($where)->getField("a.*,b.pickup_name,b.pickup_id");--*/
/*---购物车和立即购买的区分---*/
$result = null;
$result = $this->cartLogic->gointeList($this->user, $this->session_id); //获取购物车商品
$cart_goods = $result['cartList'];
if (empty($cart_goods))
exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
$address = M('UserAddress')->where("address_id", $address_id)->find();
$allusrnum = $user_money;
$userd = $this->user;
if ($state == 2) {
$pay_points = $cart_goods[0]['prom']['integral'] * $cart_goods[0]["goods_num"];
}
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
//订单余额判断
if (!empty($allusrnum)) {
$ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
if ($ye . '' < $allusrnum) {
return array('status' => -4,
'msg' => "您的可用余额不足", 'result' => '');
}
}
$err_txt = "";
/*--库存判断和活动判断,商品的库存和活动不能过期--*/
foreach ($cart_goods as $k => $value) {
$ginfo = $value;
/*--线下库存的序列key--*/
$bkey = $value['pick_id'] . $value['goods_id'];
//商品参与活动查看
if ($value['prom_type'] > 0) {
$prom = get_goods_promotion1($value, null, $this->user_id);
if ($prom['is_end'] == 1) {
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品活动已经结束", 'result' => '');
}
if ($prom['is_end'] == 2) {
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此商品已经售完", 'result' => '');
}
if ($prom['is_end'] == 3) {
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量", 'result' => '');
}
if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4) {
if ($value['goods_num'] > $prom['buy_limit']) {
return array('status' => -4, 'msg' => "{$ginfo['goods_name']} 此活动商品超出限购数量",
'result' => '');
}
}
if ($ary0[0] != $prom['integral'] && $ary0[0] != "" && $ary0[1] != "") {
$err_txt .= "商品购买积分已经变化";
}
if ($ary0[1] != $prom['addmoney'] && $ary0[0] != "" && $ary0[1] != "") {
$err_txt .= "商品价格已经变化
";
}
if ($value['prom_type'] == 1 || $value['prom_type'] == 2 || $value['prom_type'] == 4) {
if ($value['goods_num'] > $ginfo['store_count']) {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
} else {
return array('status' => -4, 'msg' => "库存不足", 'result' => '');
}
}
if ($err_txt != "") {
return array('status' => -1004, 'msg' => $err_txt, 'result' => '');
}
/*--购物车商品分组,生成不同订单--*/
$plist = null;
if ($state == 0) {
$sql = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and selected=1 and state=0 GROUP BY pick_id";
$plist = Db::query($sql);
foreach ($plist as $k => $v) {
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
}
$plist[$k]["goods"] = $list;
unset($list);
}
} else {
$sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=" . $state . " GROUP BY pick_id";
$plist = Db::query($sql1);
foreach ($plist as $k => $v) {
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
}
$plist[$k]["goods"] = $list;
unset($list);
}
}
/*--开始生成多单--*/
//$uerp = M('store')->where('store_id', $stoid)->find();
$uerp =tpCache('shop_info',$stoid);
$pno = "";
if (count($plist) > 1) $pno = $uerp['ERPId'] . date('YmdHis') . get_total_millisecond() . rand(1000, 9999);
$postFee = 0;
$couponFee = 0;
$balance = 0;$pointsFee = 0;$payables = 0;$payables1 = 0;$goodsFee = 0;$order_prom_id = 0;$order_prom_id = 0;$order_prom_amount = 0;
// 启动事务
Db::startTrans();
try {
foreach ($plist as $k => $v) {
/*--计算订单相关--*/
$scode = $shipping_code[$v['pick_id']];
// if (!empty($shipping_code)){
// if ($scode == 'mdzz'){
// $exptype = 1;
// }else{
// $exptype = 0;
// }
// }
$result = calculate_price3($this->user, $v["goods"], $scode, 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, 0, "", $stoid, $exparr[$v['pick_id']]);
if ($result['status'] < 0)
exit(json_encode($result));
/*---余额使用---*/
$user_money = $user_money - $result['result']['user_money'];
$postFee += $result['result']['shipping_price']; // 物流费
$couponFee += $result['result']['coupon_price'];// 优惠券
$balance += $result['result']['user_money']; // 使用用户余额
$pointsFee += $result['result']['integral_money']; // 积分支付
$payables += $result['result']['order_amount'];// 应付金额
$payables1 += $result['result']['order_amount1'];// 应付金额,不包含余额的那部分
$goodsFee += $result['result']['goods_price'];// 商品价格
// 提交订单
if ($_REQUEST['act'] == 'submit_order') {
$address_id = $_REQUEST['address_id'];
mlog("日志" . $address_id . "是否编辑地址" . $setadd, "cart");
if (!$address_id && $exptype == 0) exit(json_encode(array('status' => -3, 'msg' => '请先填写收货人信息', 'result' => null))); // 返回结果状态
if (!$shipping_code && $exptype == 0) exit(json_encode(array('status' => -4, 'msg' => '请选择物流信息', 'result' => null))); // 返回结果状态
if (empty($coupon_id) && !empty($couponCode)) {
$coupon_id = M('CouponList')->where("code", $couponCode)->getField('id');
}
//$iii=$result['result']['user_money'];
$car_price0 = array(
'postFee' => $result['result']['shipping_price'], // 物流费
'couponFee' => $result['result']['coupon_price'], // 优惠券
'balance' => $result['result']['user_money'], // 使用用户余额
'pointsFee' => $result['result']['integral_money'], // 积分支付
'payables' => $result['result']['order_amount'], // 应付金额
'payables1' => $result['result']['order_amount1'], // 应付金额,不包含余额那一部分
'goodsFee' => $result['result']['goods_price'],// 商品价格
'order_prom_id' => 0, // 订单优惠活动id
'order_prom_amount' => 0, // 订单优惠活动优惠了多少钱
);
/*--调用生成订单的函数--*/
$result = $this->cartLogic->addOrder($this->user_id, $address_id,
$shipping_code[$v['pick_id']], $invoice_title[$v['pick_id']], $car_price0,
$v["goods"], $coupon_id[$v['pick_id']], $user_note[$v['pick_id']], $stoid, $exparr[$v['pick_id']], $pno, $v['pick_id'], null, $this->user); // 添加订单
if ($result["status"] != 1)
exit(json_encode($result));
if (empty($pno) && count($plist) == 1) {
$pno = $result["result"];
}
}
}
// 提交订单
if ($_REQUEST['act'] == 'submit_order') {
upload_ylp_log('积分购商品提交订单');
if ($state == 0) {
// 删除购物车中已提交订单商品
M('Cart')->where(['user_id' => $this->user_id, 'selected' => 1])->delete();
//删除购物车的cookie
Cookie::delete("cn");
Db::commit();
$odr_arr=M('order')->where('parent_sn',$pno)->where('store_id',$stoid)
->field('order_amount,order_sn,order_id,user_id')->select();
foreach ($odr_arr as $k=>$v) {
//不是拼单的订单,才进行分成
if($data['pt_prom_id']>0){
}else{
$order=$v;
//分销开关全局
$distribut_switch = tpCache('distribut.switch', getMobileStoId());
mlog("分成开关-" . $order['order_id'] . $order['order_sn'] . ":" . $distribut_switch, 'rebate_log/' . getAdmStoId());
if ($distribut_switch == 1 && file_exists(APP_PATH . 'common/logic/DistributLogic.php')) {
$distributLogic = new \app\common\logic\DistributLogic();
$distributLogic->rebate_log($order, getMobileStoId()); // 生成分成记录
}
}
if ($v['order_amount'] == 0) {
update_pay_status2($v['order_sn'], 1);
}
}
exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
} else {
// 删除cart表中立即购买的商品
M('Cart')->where(['user_id' => $this->user_id, 'state' => 1])->delete();
Db::commit();
exit(json_encode(array('status' => 1, 'msg' => '提交订单成功', 'result' => $pno, 'payables' => $payables)));
}
}
}
catch (\Exception $e) {
// 回滚事务
Db::rollback();
exit(json_encode(array('status' => -1, 'msg' => '提交订单失败')));
}
$car_price = array(
'postFee' => round($postFee, 2), // 物流费
'couponFee' => round($couponFee, 2), // 优惠券
'balance' => round($allusrnum - $user_money, 2), // 使用用户余额
'pointsFee' => round($pointsFee, 2), // 积分支付
'payables' => round($payables, 2), // 应付金额
'payables1' => round($payables1, 2), // 应付金额,不包含使用余额的那一部分
'goodsFee' => round($goodsFee, 2)// 商品价格
);
$return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
exit(json_encode($return_arr));
}
/**
* ajax 获取积分购订单商品价格
*/
public function cart6_nosub()
{
/*--物流方式--*/
$exptype = I("exptype/d");
$stoid = I("stoid");
$state = I("state/d",0);
if ($this->user_id == 0)
exit(json_encode(array('status' => -100, 'msg' => "登录超时请重新登录!", 'result' => null))); // 返回结果状态
$address_id = I("address_id/d"); // 收货地址id
$setadd = I("setadd/d");
/*--是否是编辑地址--*/
if ($setadd == 1 && $exptype == 0) {
$consignee = I("consignee");
$mobile = I("mobile");
$province = I("province");
$city = I("city");
$district = I("district");
$twon = I("twon");
$address = I("address");
$data = ['consignee' => $consignee,
'mobile' => $mobile,
'province' => $province,
'city' => $city,
'district' => $district,
'twon' => $twon,
'address' => $address,
];
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id, 0, $data);
$address_id = $data['result'];
}
$shipping_code = I("shipping_code/a"); // 物流编号
$invoice_title = I('invoice_title/a'); // 发票
$exparr= I('exparr/a');//获取物流配送数组
$couponTypeSelect = I("couponTypeSelect"); // 优惠券类型 1 下拉框选择优惠券 2 输入框输入优惠券代码
$coupon_id = I("coupon_id/a"); // 优惠券id
$couponCode = I("couponCode"); // 优惠券代码
$user_note = I('user_note/a'); //买家留言
$pay_points = I("pay_points", 0); // 使用积分
$user_money = I("user_money", 0); // 使用余额
$user_money = str_replace(',', '', $user_money);
$user_money = doubleval($user_money);
/*--购物车商品提取--*/
/*--
$cart_goods = M('Cart')->alias('a')
->join("pick_up b","a.pick_id=b.pickup_id",'left')
->where(['a.user_id'=>$this->user_id,'a.selected'=>1])
->where($where)->getField("a.*,b.pickup_name,b.pickup_id");--*/
/*---购物车和立即购买的区分---*/
$result = null;
$result = $this->cartLogic->gointeList($this->user, $this->session_id); //获取购物车商品
$cart_goods = $result['cartList'];
if (empty($cart_goods))
exit(json_encode(array('status' => -2, 'msg' => '你的购物车没有选中商品', 'result' => null))); //返回结果状态
$address = M('UserAddress')->where("address_id", $address_id)->find();
$allusrnum = $user_money;
$userd = $this->user;
$pay_points = $cart_goods[0]['prom']['integral'] * $cart_goods[0]["goods_num"];
$locking_money = M('withdrawals')->where(array('user_id' => $this->user_id, 'status' => 0))->sum('money');
//订单余额判断
if (!empty($allusrnum)) {
$ye = $userd['user_money'] - $userd['frozen_money'] - $locking_money;
if ($ye . '' < $allusrnum) {
return array('status' => -4,
'msg' => "您的可用余额不足", 'result' => '');
}
}
/*--购物车商品分组,生成不同订单--*/
$plist = null;
$sql1 = "select pick_id from __PREFIX__cart where user_id=" . $this->user_id . " and state=" . $state . " GROUP BY pick_id";
$plist = Db::query($sql1);
foreach ($plist as $k => $v) {
foreach ($cart_goods as $kk => $vv) {
if ($v['pick_id'] == $vv['pickup_id']) {
$list[] = $vv;
}
}
$plist[$k]["goods"] = $list;
unset($list);
}
/*--开始生成多单--*/
//$uerp = M('store')->where('store_id', $stoid)->find();
$uerp = tpCache('shop_info',$stoid);
$pno = "";
if (count($plist) > 1) $pno = $uerp['ERPId'] . date('YmdHis') . get_total_millisecond() . rand(1000, 9999);
$postFee = 0;
$couponFee = 0;
$balance = 0;
$pointsFee = 0;
$payables = 0;
$payables1 = 0;
$goodsFee = 0;
$order_prom_id = 0;
$order_prom_id = 0;
$order_prom_amount = 0;
foreach ($plist as $k => $v) {
/*--计算订单相关--*/
$scode = $shipping_code[$v['pick_id']];
$result = calculate_price3($this->user, $v["goods"], $scode, 0, $address['province'], $address['city'], $address['district'], $pay_points, $user_money, 0, "", $stoid, $exparr[$v['pick_id']]);
if ($result['status'] < 0)
exit(json_encode($result));
/*---余额使用---*/
$user_money = $user_money - $result['result']['user_money'];
$postFee += $result['result']['shipping_price']; // 物流费
$couponFee += $result['result']['coupon_price'];// 优惠券
$balance += $result['result']['user_money']; // 使用用户余额
$pointsFee += $result['result']['integral_money']; // 积分支付
$payables += $result['result']['order_amount'];// 应付金额
$payables1 += $result['result']['order_amount1'];// 应付金额,不包含余额的那部分
$goodsFee += $result['result']['goods_price'];// 商品价格
}
$car_price = array(
'postFee' => round($postFee, 2), // 物流费
'couponFee' => round($couponFee, 2), // 优惠券
'balance' => round($allusrnum - $user_money, 2), // 使用用户余额
'pointsFee' => round($pointsFee, 2), // 积分支付
'payables' => round($payables, 2), // 应付金额
'payables1' => round($payables1, 2), // 应付金额,不包含使用余额的那一部分
'goodsFee' => round($goodsFee, 2)// 商品价格
);
$return_arr = array('status' => 1, 'msg' => '计算成功', 'result' => $car_price); // 返回结果状态
exit(json_encode($return_arr));
}
/*
* ajax 请求获取购物车列表
*/
public function ajaxCartList()
{
$post_goods_num = I("goods_num/a"); // goods_num 购物车商品数量
$post_cart_select = I("cart_select/a"); // 购物车选中状态
$where['session_id'] = $this->session_id; // 默认按照 session_id 查询
// 如果这个用户已经等了则按照用户id查询
$user_id = $this->user_id;
$stoid = getMobileStoId();
if ($user_id) {
unset($where);
$where['user_id'] = $user_id;
}
$where['state'] = 0;
$where['store_id'] = $stoid;
$where['is_gift'] = 0;
M('Cart')->where($where)->save(['selected' => 0]);
$cartList = M('Cart')->alias('a')->join('goods b', 'a.goods_id=b.goods_id')->where(['a.user_id' => $user_id, 'a.store_id' => $stoid, 'a.state' => 0])
->getfield('a.id,a.prom_type,a.selected,a.pick_id,a.goods_id,a.goods_num,a.prom_id,b.shop_price,b.store_count,b.is_on_sale,a.is_gift,b.prom_type as prom_type1,b.viplimited');
$now0 = time();
/*---如果是线下销售规则,获取所有购物车商品和门店的组合库存数组---*/
$sale_r = tpCache("basic.sales_rules", $stoid);
if ($sale_r == 2) {
$dlinearr = getcountarr($cartList, $stoid);
}
/*-----------------页面一开始加载的时候更新数量,剔除下架和活动结束的商品-------------*/
if (empty($post_goods_num) && !empty($cartList)) {
/*--要剔除下架商品,活动有过期的商品,或者商品在参加活动之前被添加到购物车--*/
foreach ($cartList as $k => $v) {
if ($v['is_on_sale'] == 1 && $v['on_time']< $now0 && ($v['down_time']> $now0 || empty($v['down_time']))) {
if ($v['prom_type'] != 0 && $v['is_gift'] == 0) {
$prom = get_goods_promotion1($v, null, $user_id);
if ($prom['is_end'] == 2 || $prom['is_end'] == 3 || $prom['is_end'] == 1) {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
}
} else {
$where = [
'end_time' => ['>=', $now0],
'start_time' => ['<=', $now0],
'goods_id' => $v['goods_id'],
'is_end' => 0
];
if ($v['prom_type1'] == 1) {//秒杀
$jj = M('flash_sale')->where($where)->find();
if ($jj) {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
}
}
if ($v['prom_type1'] == 2) {//团购
$jj = M('group_buy')->where($where)->find();
if ($jj) {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
}
}
if ($v['prom_type1'] == 4) {//积分购
$jj = M('flash_sale')->where($where)->find();
if ($jj) {
M('Cart')->where('goods_id', $v['goods_id'])->where('user_id', $this->user_id)->delete();
unset($cartList[$k]);
}
}
if ($v['is_gift'] == 1) {
M('cart')->where(['id' => $v['id'], 'user_id' => $user_id])->delete();
unset($cartList[$k]);
}
}
} else {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
unset($cartList[$k]);
}
}
}
//统计赠品
// $count=array();
// foreach ($cartList as $val){
// if ($val['is_gift']==1){
// $count[$val['prom_id']]+=1;
// $count[$val['pick_id']][$val['prom_id']]+=1;
// }
// }
/*--------------改变数量和选择商品--------------*/
if (!empty($post_goods_num)) {
// 修改购物车数量,和勾选状态
foreach ($post_goods_num as $key => $val) {
/*--线下库存的序列key--*/
$bkey = $cartList[$key]['pick_id'] . $cartList[$key]['goods_id'];
$data['goods_num'] = $val < 1 ? 1 : $val;
if ($cartList[$key]['prom_type'] > 0 && $cartList[$key]['prom_type'] != 3 && $cartList[$key]['prom_type'] != 5) {
$prom = get_goods_promotion1($cartList[$key], null, $user_id);
/*---如果大于线上库存--*/
$data['goods_num'] = $data['goods_num'] > $prom['store_count'] ? $prom['store_count'] : $data['goods_num'];
if ($cartList[$key]['prom_type'] == 2) {
$data['goods_num'] = $data['goods_num'] > $prom['onlybuy'] ? $prom['onlybuy'] : $data['goods_num'];
}
/*---如果是秒杀和积分购---*/
if ($cartList[$key]['prom_type'] == 1 || $cartList[$key]['prom_type'] == 4) {
$data['goods_num'] = $data['goods_num'] > $prom['buy_limit'] ? $prom['buy_limit'] : $data['goods_num'];
}
} else {
/*--如果是线下规则--*/
if ($sale_r == 2) {
$dlcount = $dlinearr[$bkey];
if (!empty($dlcount) && $dlcount > 0) {
$data['goods_num'] = $data['goods_num'] > $dlcount ? $dlcount : $data['goods_num'];
}
} else if ($sale_r == 1) {
$data['goods_num'] = $data['goods_num'] > $cartList[$key]['store_count'] ? $cartList[$key]['store_count'] : $data['goods_num'];
}
}
$data['selected'] = $post_cart_select[$key] ? 1 : 0;
if ($cartList[$key]['goods_num'] != $data['goods_num'] || $cartList[$key]['selected'] != $data['selected']) {
M('Cart')->where("id", $key)->save($data);
}
}
$this->assign('select_all', input('post.select_all')); // 全选框
}
$prom_goods = array();
if (!empty($post_cart_select)) {
foreach ($post_cart_select as $kk => $vv) {
if ($cartList[$kk]['prom_type'] == 3) {
$prom_goods[$cartList[$kk]['pick_id']][$cartList[$kk]['prom_id']]['num'] += $post_goods_num[$kk];
$prom_goods[$cartList[$kk]['pick_id']][$cartList[$kk]['prom_id']]['price'] += $cartList[$kk]['shop_price'] * $post_goods_num[$kk];
$prom_goods['pick'][] = $cartList[$kk]['pick_id'];//收集选中的门店
}
}
}
if (!empty($prom_goods)) {
M('cart')->where(['is_gift' => 1, 'user_id' => $user_id, 'pick_id' => ['not in', $prom_goods['pick']]])->delete();
unset($prom_goods['pick']);
$prominfo = M('prom_goods')->where(['start_time' => ['lt', time()], 'end_time' => ['gt', time()], 'is_end' => 0])->getfield('id,good_object,name');
foreach ($prom_goods as $k => $v) {
foreach ($v as $kk => $vv) {
$prom_id_list[] = $kk;//收集选中门店选中优惠活动id
if (!empty($prominfo[$kk])) {
$prom = discount($vv['price'], $kk, $vv['num'], $user_id);
if (!empty($prom['goods_id'])) {
$count = M('cart')->where(['user_id' => $user_id, 'prom_id' => $kk, 'is_gift' => 1])->count();
$pick = M('cart')->where(['user_id' => $user_id, 'prom_id' => $kk, 'is_gift' => 1, 'pick_id' => $k])->count();
if ($count < $prom['limit_num'] && $pick < 1) {
M('cart')->add(['user_id' => $user_id, 'session_id' => $this->session_id, 'goods_id' => $prom['goods_id'], 'goods_sn' => '',
'goods_name' => $prom['goods_name'], 'market_price' => 0, 'goods_price' => 0, 'member_goods_price' => 0, 'goods_num' => 1, 'sku' => '',
'add_time' => time(), 'prom_type' => 3, 'prom_id' => $kk, 'pick_id' => $k, 'store_id' => $stoid, 'state' => 0, 'main_cart_id' => 1,
'is_gift' => 1, 'selected' => 1]);
}
} else {
M('cart')->where(['user_id' => $user_id, 'is_gift' => 1, 'prom_id' => $kk, 'pick_id' => $k])->delete();
}
if ($prom['type'] == 0) {
if ($vv['price'] < $prom['condition']) {
$prom_goods[$k][$kk]['condition'] = ($prom['condition'] - $vv['price']) . '元';
}
} else {
if ($vv['num'] < $prom['condition']) {
$prom_goods[$k][$kk]['condition'] = ($prom['condition'] - $vv['num']) . '件';
}
}
if (!empty($prom['rule'])){
$prom_goods[$k][$kk]['next_mon'] = $prom['rule']['money'];
$prom_goods[$k][$kk]['next_sal'] = $prom['rule']['sale'];
$prom_goods[$k][$kk]['next_pas'] = $prom['rule']['is_past'];
$prom_goods[$k][$kk]['next_int'] = $prom['rule']['int'];
$prom_goods[$k][$kk]['next_cou'] = $prom['rule']['coupon'];
$prom_goods[$k][$kk]['next_gif'] = $prom['rule']['gift'];
}
$prom_goods[$k][$kk]['all'] = $prominfo[$kk]['good_object'];
}
}
M('cart')->where(['is_gift' => 1, 'user_id' => $user_id, 'pick_id' => $k, 'prom_id' => ['not in', $prom_id_list]])->delete();
}
// $pick = substr($pick, 0, strlen($pick) - 1);//去除最后一个字符
} else {
M('cart')->where(['user_id' => $user_id, 'is_gift' => 1])->delete();
}
/*------购物车中商品库存要判断线下库存------*/
$result = $this->cartLogic->cartList($this->user, $this->session_id, 0, 1);
$cart_goods = $result['cartList'];
foreach ($cart_goods as $k=>$v){
if (empty($v['viplimited'])){
$cart_goods[$k]['viplimited'] = 10000;
}else{
$limit=M('order')->alias('a')->join('order_goods b','a.order_id=b.order_id')->where(['b.goods_id'=>$v['goods_id'],'a.user_id'=>$user_id,'a.order_status'=>[['=',4],['<',3],'or']])->sum('b.goods_num');
$cart_goods[$k]['viplimited']-= $limit;
}
}
$allprice = 0;
if ($sale_r == 2) {
/*---循环变更购物车商品的库存---*/
foreach ($cart_goods as $km => $vm) {
/*--线下库存的序列key--*/
$bkey = $vm['pick_id'] . $vm['goods_id'];
if ($vm['prom_type'] == 0 || $vm['prom_type'] == 3 || $vm['prom_type'] == 5) {
$dlcount = $dlinearr[$bkey];
if ($dlcount == null) $dlcount = 0;
$cart_goods[$km]['store_count'] = $dlcount;
if ($cart_goods[$km]['goods_num'] > $dlcount) {
$cart_goods[$km]['selected'] = 0;
$cart_goods[$km]['goods_num'] = $dlcount;
$rss = M('Cart')->where("id", $vm['id'])->save(['selected' => 0, 'goods_num' => $dlcount]);
} else {
if ($vm['selected'])
$allprice += $vm['goods_num'] * $vm['member_goods_price'];
}
} else {
if ($cart_goods[$km]['goods_num'] > $vm['store_count']) {
$cart_goods[$km]['selected'] = 0;
M('Cart')->where("id", $vm['id'])->save(['selected' => 0, 'goods_num' => $vm['store_count']]);
} else {
if ($vm['selected'])
$allprice += $vm['goods_num'] * $vm['member_goods_price'];
}
}
}
} else {
/*---循环变更购物车商品的库存---*/
foreach ($cart_goods as $km => $vm) {
if ($cart_goods[$km]['goods_num'] > $vm['store_count']) {
$cart_goods[$km]['selected'] = 0;
M('Cart')->where("id", $vm['id'])->save(['selected' => 0, 'goods_num' => $vm['store_count']]);
} else {
if ($vm['selected'])
$allprice += $vm['goods_num'] * $vm['member_goods_price'];
}
}
}
$this->assign("allprice", $allprice);
$countC = M('Cart')->where('user_id', $this->user_id)->where('store_id', getMobileStoId())->where('state', '0')->where('is_gift', 0)->sum('goods_num');
$this->assign("cart_num", $countC);
/*---计算价格---*/
// if (empty($result['total_price']))
// $result['total_price'] = Array('total_fee' => 0, 'cut_fee' => 0, 'num' => 0, 'atotal_fee' => 0, 'acut_fee' => 0, 'anum' => 0);
$cartsql = "select pickup_id,pickup_name from wxd_pick_up where pickup_id in(select pick_id from wxd_cart where user_id=" . $this->user_id . " and state=0 and store_id=" . getMobileStoId() . " GROUP BY pick_id)";
$plist_cart = Db::query($cartsql);
foreach ($plist_cart as $k => $v) {
$isallsele = 1;
foreach ($cart_goods as $kk => $vv) {
if ($vv['pick_id'] == $v['pickup_id']) {
$plist_cart[$k]['goods'][] = $vv;
if ($vv["selected"] != 1) {
$isallsele = 0;
}
// else {
// $good_jg = 0;
// if ($vv['prom_type'] != 0 && $vv['prom_type'] != 3) {
// $prom = get_goods_promotion1($vv,null, $user_id);
// $good_jg = $prom['price'];
// } else {
// $good_jg = $vv['member_goods_price'];
// }
// }
}
}
$plist_cart[$k]['selected'] = $isallsele;
if (!empty($prom_goods)) {
foreach ($prom_goods as $key => $val) {
if ($key == $v['pickup_id']) {
$plist_cart[$k]['condition'] = $val;
}
}
}
// $freight_free = tpCache('shopping.freight_free', getMobileStoId()); // 全场满多少免运费
// if ($selenum >= $freight_free || $selenum == 0) {
// $plist_cart[$k]["moremoney"] = 0;
// } else if ($selenum > 0) {
// $plist_cart[$k]["moremoney"] = $freight_free - $selenum;
// }
}
//$this->assign('cartList', $result['cartList']); // 购物车的商品
$this->assign('cartList', $plist_cart); // 购物车的商品
// $this->assign('total_price', $result['total_price']); // 总计
upload_ylp_log('购物车列表');
return $this->fetch('ajax_cart_list', getMobileStoId());
}
/*
* ajax 获取用户收货地址 用于购物车确认订单页面
*/
public function ajaxAddress()
{
$regionList = M('Region')->getField('id,name');
$address_list = M('UserAddress')->where("user_id", $this->user_id)->select();
$c = M('UserAddress')->where("user_id = {$this->user_id} and is_default = 1")->count(); // 看看有没默认收货地址
if ((count($address_list) > 0) && ($c == 0)) // 如果没有设置默认收货地址, 则第一条设置为默认收货地址
$address_list[0]['is_default'] = 1;
$this->assign('regionList', $regionList);
$this->assign('address_list', $address_list);
return $this->fetch('ajax_address', getMobileStoId());
}
/**
* ajax 删除购物车的商品
*/
public function ajaxDelCart()
{
$ids = I("ids"); // 购物车ids
M("Cart")->where("id", "in", $ids)->delete(); // 删除id为5的用户数据
$return_arr = array('status' => 1, 'msg' => '删除成功', 'result' => ''); // 返回结果状态
exit(json_encode($return_arr));
}
public function getNum()
{
$user_id = $this->user_id;
$now0 = time();
if (session('?user')) {
/*--要剔除下架商品,活动有过期的商品,或者商品在参加活动之前被添加到购物车--*/
$cartList = M('Cart')->alias('a')->join('goods b', 'a.goods_id=b.goods_id')->where(['a.user_id' => $user_id, 'a.store_id' => getMobileStoId(), 'a.state' => 0])
->field('a.id,a.prom_type,a.goods_id,a.prom_id,b.shop_price,b.store_count,b.is_on_sale,a.is_gift,b.prom_type as prom_type1')->select();
if (!empty($cartList)) {
foreach ($cartList as $k => $v) {
if ($v['is_on_sale'] == 1 && $v['on_time']< $now0 && ($v['down_time']> $now0 || empty($v['down_time'])) ) {
if ($v['prom_type'] != 0 && $v['is_gift'] == 0) {
$prom = get_goods_promotion1($v, null, $user_id);
if ($prom['is_end'] == 2 || $prom['is_end'] == 3 || $prom['is_end'] == 1) {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
}
} else {
$where = [
'end_time' => ['>=', $now0],
'start_time' => ['<=', $now0],
'goods_id' => $v['goods_id'],
'is_end' => 0
];
if ($v['prom_type1'] == 1) {//秒杀
$jj = M('flash_sale')->where($where)->find();
if ($jj) {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
}
}
if ($v['prom_type1'] == 2) {//团购
$jj = M('group_buy')->where($where)->find();
if ($jj) {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
}
}
if ($v['prom_type1'] == 4) {//积分购
$jj = M('flash_sale')->where($where)->find();
if ($jj) {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
}
}
}
} else {
M('Cart')->where('id', $v['id'])->where('user_id', $user_id)->delete();
}
}
}
$num = M("Cart")->where('user_id', $user_id)->where('store_id', getMobileStoId())->where('state', 0)->where('is_gift', 0)->sum('goods_num');
if (empty($num)) {
return 0;
} else {
return $num;
}
} else {
return 0;
}
}
/*----------------------------------地址管理------------------------------------*/
/*
* 添加地址
*/
public function add_address()
{
upload_ylp_log('添加地址');
$logic = new UsersLogic();
$id = I('address_id', 0);
$seleid = I("seleid");
$this->assign("seleid", $seleid);
$data = $logic->add_address($this->user_id, $id, I('get.'));
if ($data['status'] != 1)
return json(['code' => -1, 'fir' => $data['fir'],]);
else {
/*---获取地区---*/
$region_list = get_region_list();
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
return json(['code' => 1, 'fir' => $data['fir'], 'id' => $data['result'],
'data' => $this->fetch('ajax_addrlist', getMobileStoId())]);
}
}
/*
* 地址编辑
*/
public function edit_address()
{
upload_ylp_log('编辑地址');
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id, 0, I('get.'));
if ($data['status'] != 1)
return json(['code' => -1]);
else {
return json(['code' => 1]);
}
}
/*
* 获取省市区
*/
public function get_area()
{
$id = I('aid');
$address = M("user_address")->where('address_id', $id)->find();
//获取省份
$p = M('region')->where(array('parent_id' => 0, 'level' => 1))->select();
$c = M('region')->where(array('parent_id' => $address['province'], 'level' => 2))->select();
$d = M('region')->where(array('parent_id' => $address['city'], 'level' => 3))->select();
$data['province'] = $p;
$data['city'] = $c;
$data['district'] = $d;
$region_list = get_region_list();
$addrstr = $region_list[$address['province']]['name'] . "-" . $region_list[$address['city']]['name'];
if (!empty($address['district']))
$addrstr .= "-" . $region_list[$address['district']]['name'];
if (!empty($address['twon']))
$addrstr = $addrstr . "-" . $region_list[$address['twon']]['name'];
$address['addname'] = $addrstr;
if (empty($data['district'])) {
$e = M('region')->where(array('parent_id' => $address['city'], 'level' => 4))->select();
$data['twon'] = $e;
} else {
$e = M('region')->where(array('parent_id' => $address['district'], 'level' => 4))->select();
$data['twon'] = $e;
}
$jsond = ['code' => 1, 'data' => $data, 'data2' => $address];
return json($jsond);
}
/*
* 地址删除
*/
public function del_address()
{
upload_ylp_log('删除地址');
$backaddr = null;
$id = I('get.id');
$seleid = I("seleid");
$region_list = get_region_list();
$address = M('user_address')->where("address_id", $id)->find();
$row = M('user_address')->where(array('user_id' => $this->user_id, 'address_id' => $id))->delete();
// 如果删除的是默认收货地址 则要把第一个地址设置为默认收货地址
if ($address['is_default'] == 1) {
$address2 = M('user_address')->where("user_id", $this->user_id)->find();
$address2 && M('user_address')->where("address_id", $address2['address_id'])->save(array('is_default' => 1));
if ($address2) {
$provinename = M('region')->where("id", $address2['province'])->field('name')->find();
$cityname = M('region')->where("id", $address2['city'])->field('name')->find();
$distname = M('region')->where("id", $address2['district'])->field('name')->find();
$twonname = M('region')->where("id", $address2['twon'])->field('name')->find();
$addrstr = $provinename['name'] . "-" . $cityname['name'] . "-" . $distname['name'];
if ($twonname) {
$addrstr = $addrstr . "-" . $twonname['name'];
}
$address2['addrstr'] = $addrstr . " " . $address2['address'];
$backaddr = $address2;
$seleid = $address2['address_id'];
}
} else {
$provinename = $region_list[$address['province']]['name'];
$cityname = $region_list[$address['city']]['name'];
$distname = $region_list[$address['district']]['name'];
$twonname = $region_list[$address['twon']]['name'];
$addrstr = $provinename . "-" . $cityname;
if ($distname) {
$addrstr = $addrstr . "-" . $distname;
}
if ($twonname) {
$addrstr = $addrstr . "-" . $twonname;
}
$address['addrstr'] = $addrstr . " " . $address['address'];
$backaddr = $address;
}
$this->assign("seleid", $seleid);
if (!$row)
return json(['code' => -1]);
else {
/*---获取地区---*/
$addlist = M('user_address')->where(['user_id' => $this->user_id])->select();
foreach ($addlist as $kk => $vv) {
$vv['provicename'] = $region_list[$vv['province']]['name'];
$vv['cityname'] = $region_list[$vv['city']]['name'];
$vv['districtname'] = $region_list[$vv['district']]['name'];
$vv['twonname'] = $region_list[$vv['twon']]['name'];
//$address = M('user_address')->where(['user_id'=>$this->user_id,'is_default'=>1])->find();
$addrstr = $vv['provicename'] . "-" . $vv['cityname'];
if (!empty($vv['district']))
$addrstr .= "-" . $vv['districtname'];
if (!empty($vv['twon']))
$addrstr = $addrstr . "-" . $vv['twonname'];
$vv['addrstr'] = $addrstr;
$addlist[$kk] = $vv;
}
$this->assign("addlist", $addlist);
return json(['code' => 1, 'address2' => $backaddr,
'data' => $this->fetch('ajax_addrlist', getMobileStoId())]);
}
}
/*--绑定手机号--*/
public function reg_user()
{
$code = I('co');
$mobile = I('mob');
$session_id = session_id();
$logic = new UsersLogic();
$stoid = getMobileStoId();
mlog("0", 'reg_user/' . $stoid);
if (getMobileStoId() != "1") {
$check_code = $logic->check_validate_code($code, $mobile, $session_id);
if ($check_code['status'] != 1) {
return array('status' => -1, 'msg' => $check_code['msg']);
exit;
}
}
mlog("1", 'reg_user/' . $stoid);
$map = null;
// 成为分销商条件
$distribut_condition = tpCache('distribut.condition', $stoid);
$distribut_switch = tpCache('distribut.switch', $stoid);
if ($distribut_switch === 1 && $distribut_condition === 0 && $this->user['is_distribut'] == 0) // 本来要购买成为分销,后来商家改为直接注册就可以成为分销商进行更改
{
//判断分销可以注册的人数
$distr_num = M('store_distribut')->where('store_id', $stoid)->field('distribut_num')->find();
$count1 = M("users")->where('store_id', getMobileStoId())->where('is_distribut', 1)->count();
if ($distr_num['distribut_num'] > $count1) {
$map['is_distribut'] = 1;
$map['be_distribut_time'] = time();
$map['be_dis_condition'] = "直接购买成为分销商";
} else {
$is_out = M("store_module_endtime")
->where('store_id', getMobileStoId())
->where('type', 2)->find();
if (empty($is_out)) {
$yy = M('distri_price')->where('type', 0)->where('money', 0)->find();
$allnum = $yy['user_num'];
if ($allnum > $count1) {
$map['is_distribut'] = 1;
$map['be_distribut_time'] = time();
$map['be_dis_condition'] = "直接购买成为分销商";
}
}
}
}
$map = ['mobile' => $mobile];
$is_distri_rate = tpCache('distribut.is_distri_rate', $stoid); //分销商奖励
$is_invitation_rate = tpCache('distribut.is_invitation_rate', $stoid); //是否参与邀请送积分或代金券
//启动事务
Db::startTrans();
try {
$first_leader = Cookie::get('first_leader');//上级会员ID
if ($first_leader != "") {
$first_info = M('users')->where(array('user_id' => $first_leader, 'store_id' => $stoid))->find();
if ($first_info && $first_info['is_distribut'] == "1" ) {
$map['first_leader'] = $first_leader;
} else {
//往上找会员是否的分销商,
//如果是分销商则注册会员的 first_leader 就是此分销商
$first_info0 = M('users')->where(array('user_id' => $first_info['first_leader'], 'store_id' => $stoid))->find();
if ($first_info0 && $first_info0['is_distribut'] == "1" ) {
$map['first_leader'] = $first_info0['user_id'];
}
}
if ($map['first_leader']) {
$first_leader = M('users')->where("user_id", $map['first_leader'])->field("first_leader,second_leader")->find();
$map['second_leader'] = $first_leader['first_leader']; // 第一级推荐人
$map['third_leader'] = $first_leader['second_leader']; // 第二级推荐人
//他上线分销的下线人数要加1
M('users')->where(array('user_id' => $map['first_leader']))->setInc('underling_number');
M('users')->where(array('user_id' => $map['second_leader']))->setInc('underling_number');
M('users')->where(array('user_id' => $map['third_leader']))->setInc('underling_number');
} else {
$map['first_leader'] = 0;
}
}
mlog("6", 'reg_user/' . $stoid);
$rs = M("users")->where("user_id", $this->user_id)->save($map);
$user_info['api_token'] = tpCache('shop_info.api_token', $stoid);
$map['user_id']=$this->user_id;
$news_info = $map;
$is_distri_rate = tpCache('distribut.is_distri_rate', $stoid); //分销商奖励
$is_invitation_rate = tpCache('distribut.is_invitation_rate', $stoid); //是否参与邀请送积分或代金券
mlog("7", 'reg_user/' . $stoid);
//邀请赠送积分
if (!empty($first_info) &&
!empty($first_info['mobile']) &&
!empty($first_info['erpvipid'])
) { //如果上级会员是绑定,说明是通过链接进来,给发链接的用户赠送积分、优惠券
/*---当上级不是分销商 或者 开启分销商奖励,参与邀请送积分或代金券---*/
if ($first_info['is_distribut'] == 0 || $is_distri_rate == 0 || ($is_invitation_rate == 1 && $is_distri_rate == 1)) {
mlog("701", 'reg_user/' . $stoid);
$inv_type = tpCache('basic.inv_type', $stoid);
$inv_info = tpCache('basic.invitation_rate', $stoid);
mlog("703" . $inv_info, 'reg_user/' . $stoid);
$invitation_rate = json_decode($inv_info, true);
/*--当上级的会员是绑定了的--*/
if ($inv_type == 0) { //邀请送积分
if ($invitation_rate['inv_jf'] != "") {
/*--
$data = array(
'Id' => $first_info["erpvipid"],//会员id
'Integral' => $invitation_rate['inv_jf'],//金额
'Remark' => '邀请送积分',
);
$add_rs = getApiData("wxd.vip.addreduce", $user_info['api_token'], array($data));
mlog("711邀请送积分:" . json_encode($data) . "返回:" . $add_rs, 'smslog/' . $stoid);
$add_rs = json_decode($add_rs, true);
if ($add_rs['code'] != 1) {
// return json(array('status' => -1, 'msg' => '服务器繁忙, 请联系管理员!'));
}--*/
$accdb=tpCache("shop_info.ERPId",$stoid);
$res=com_update_integral($accdb,$first_info,$invitation_rate['inv_jf'],"购买时注册邀请送积分",$module="cart");
}
} else {
if ($invitation_rate['inv_coupon'] != "") {
mlog("72", 'reg_user/' . $stoid);
$coupon = M("coupon")->where("id", $invitation_rate['inv_coupon'])->find();
if ($coupon["send_end_time"] > time()) {
$CashRepNo = "1005" . date("ymdHis") . get_total_millisecond();
$data = array(
'CashRepNo' => $CashRepNo,//券号
'VIPId' => $first_info["erpvipid"],//会员id
'Sum' => $coupon["money"],//金额
'SendMan' => '邀请发放',//发放类型
'Operator' => '微信商城',//来源
'BuySum' => $coupon["condition"],//满多少才能使用
'Remark' => '邀请送微券【消费满' . $coupon["condition"] . '元可用】',
);
if ($coupon["use_start_time"] != "") {
$data['BeginDate'] = date('Y-m-d', $coupon["use_start_time"]);//使用开始日期
}
if ($coupon["use_end_time"] != "") {
$data['ValidDate'] = date('Y-m-d', $coupon["use_end_time"]);//截止期限
}
$add_rs = getApiData("wxderp.cashreplace.add", $user_info['api_token'], array($data));
mlog("721邀请送微券:" . json_encode($data) . "返回:" . $add_rs, 'reg_user/' . $stoid);
$add_rs = json_decode($add_rs, true);
if ($add_rs['code'] != 1) {
// return json(array('status' => -1, 'msg' => '服务器繁忙, 请联系管理员!'));
}
$colorlist = urlencode("#FF0000|#173177|#173177|#173177|#173177|#173177|#FF0000|#ca003a");
$pd['typeid'] = "1006";
$pd['backurl'] = curHostURL() . '/index.php/Mobile/User/coupon/stoid/' . $stoid;
$pd['colorlist'] = $colorlist;
$pd['title'] = "您好,恭喜您成功领取" . $coupon["money"] . "元优惠券";
$pd['key1'] = $first_info['vipname'];
$pd['key2'] = $coupon["money"] . "元优惠券";
$pd['key3'] = date("Y-m-d");
$pd['remark'] = "点击详情查看优惠券详情信息";
$Event = \think\Loader::controller('home/Api');
$Event->web_poswxcode($pd, $stoid, $first_info);
}
}
}
}
}
$wx_user = M('wx_user')->where('store_id', $stoid)->find();
$jssdk = new \app\mobile\logic\Jssdk($wx_user['appid'], $wx_user['appsecret']);
mlog("7.1", 'reg_user/' . $stoid);
/*---当是分销商 且 开启分销商奖励---*/
if ($is_distri_rate == 1) {
mlog("7.1.1", 'reg_user/' . $stoid);
$ydd_str = tpCache('distribut.invitation_ratelist', $stoid);
$ydd = json_decode($ydd_str, true);
// 一级 分销商赚 的钱
if ($ydd['first_money'] > 0 && $first_info['is_distribut'] == 1) {
// 微信推送消息
$tmp_user = $first_info;
$rrs1 = $this->add_user_money($ydd['first_money'], $first_info['user_id']);
if ($rrs1) {
$odata['user_id'] = $first_info['user_id'];
$odata['create_time'] = time();
$odata['money'] = $ydd['first_money'];
$odata['remark'] = "线下一级注册";
$odata['store_id'] = $stoid;
$odata['order_sn'] = "zc" . date("YmdHis") . rand(1000, 9999);
$odata['status'] = 1;
$odata['type'] = 4;
M('withdrawals')->save($odata);
if ($tmp_user['oauth'] == 'weixin') {
$wx_content = "你的一级下线刚刚绑定,你获得 ¥" . $ydd['first_money'] . "奖励 !";
$first_res = $jssdk->push_msg($tmp_user['openid'], $wx_content);
mlog($news_info['mobile'] . "的一级[" . $first_res['mobile'] . "]推送返回:" . json_encode($first_res), 'reg_user/' . $stoid);
}
}
}
mlog("7.1.2", 'reg_user/' . $stoid);
// 二级 分销商赚 的钱
if ($news_info['second_leader'] > 0 && $ydd['second_money'] > 0) {
$fuid2 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $news_info['second_leader'])->find();
if ($fuid2 && $fuid2['is_distribut'] == 1) {
$rrs2 = $this->add_user_money($ydd['second_money'], $fuid2['user_id']);
if ($rrs2) {
$odata['user_id'] = $fuid2['user_id'];
$odata['create_time'] = time();
$odata['money'] = $ydd['second_money'];
$odata['remark'] = "线下二级注册";
$odata['store_id'] = $stoid;
$odata['order_sn'] = "zc" . date("YmdHis") . rand(1000, 9999);
$odata['status'] = 1;
$odata['type'] = 4;
M('withdrawals')->save($odata);
// 微信推送消息
$tmp_user = $fuid2;
if ($tmp_user['oauth'] == 'weixin') {
$wx_content = "你的二级下线刚刚绑定,你获得 ¥" . $ydd['second_money'] . "奖励 !";
$second_res = $jssdk->push_msg($tmp_user['openid'], $wx_content);
mlog($news_info['mobile'] . "的二级[" . $tmp_user['mobile'] . "]推送返回:" . json_encode($second_res), 'reg_user/' . $stoid);
}
}
}
}
mlog("7.1.3", 'reg_user/' . $stoid);
// 三级 分销商赚 的钱
if ($news_info['third_leader'] > 0 && $ydd['third_money'] > 0) {
$fuid3 = M('users')->where('store_id=' . $stoid . ' and user_id=' . $news_info['third_leader'])->find();
if ($fuid3 && $fuid3['is_distribut'] == 1) {
$rrs3 = $this->add_user_money($ydd['third_money'], $fuid3['user_id']);
if ($rrs3) {
$odata['user_id'] = $fuid3['user_id'];
$odata['create_time'] = time();
$odata['money'] = $ydd['third_money'];
$odata['remark'] = "线下三级注册";
$odata['store_id'] = $stoid;
$odata['order_sn'] = "zc" . date("YmdHis") . rand(1000, 9999);
$odata['status'] = 1;
$odata['type'] = 4;
M('withdrawals')->save($odata);
// 微信推送消息
$tmp_user = $fuid3;
if ($tmp_user['oauth'] == 'weixin') {
$wx_content = "你的三级下线刚刚绑定,你获得 ¥" . $ydd['third_money'] . "奖励 !";
$third_res = $jssdk->push_msg($tmp_user['openid'], $wx_content);
mlog($news_info['mobile'] . "的三级[" . $tmp_user['mobile'] . "]推送返回:" . json_encode($third_res), 'reg_user/' . $stoid);
}
}
}
}
mlog("7.1.4", 'reg_user/' . $stoid);
}
mlog("7.3", 'reg_user/' . $stoid);
Db::commit();
return array('status' => 1, 'msg' => "绑定成功");
}catch (\Exception $e){
Db::rollback();
mlog("err-".json_encode($e), 'reg_user/' . $stoid);
return array('status' => -1, 'msg' => "绑定失败");
}
}
/*---循环更新订单的状态---*/
public function updatecart2()
{
$tocart = I("state");
if ($tocart == 0) {
$result = $this->cartLogic->cartList($this->user, $this->session_id, 1, 1); //获取购物车商品
} else if ($tocart == 1) {
$result = $this->cartLogic->gobuyList($this->user, $this->session_id); //获取立即购买的商品
} else if ($tocart == 2) {
$result = $this->cartLogic->gointeList($this->user, $this->session_id); //获取积分购的商品
}
return "1";
}
/*----增加会员的余额----*/
public function add_user_money($addmoney,$user_id){
$rs=M('users')->where('user_id',$user_id)->setInc('user_money',$addmoney);
return $rs;
}
/*----相应的返回redis----*/
public function backredis($no,$notime)
{
if ($_REQUEST['act'] == 'submit_order') {
$popvalue = null;
$popvalue2 = null;
$stoid = I("stoid");
//拼单有关
$ispt = I('is_pt');
$pdid = I('pdid');
$team_qh = I('team_qh');
$pdgoodsnum = I('pdgoodsnum/d', 0);
$sd = I('da');
if (!empty($sd)) {
$skillarray = json_decode($sd, true);
foreach ($skillarray as $lk => $lv) {
$name = 'ms' . $lv['fid'] . '-' . $stoid;
$num0 = $lv['num'];
//秒杀队列控制
for ($i = 0; $i < $num0; $i++) {
$this->redis->lPush($name, time() . '_' . $i);
}
$key=$no.'-1-'.$lv['fid'].'-'.$num0.'-'.$notime;
$this->redis->sRem('redis_set_'.$stoid,$key);
}
}
//如果是拼单
if ($ispt) {
$name = 'pind' . $pdid . '-' . $stoid;
//拼单队列控制
for ($i = 0; $i < $pdgoodsnum; $i++) {
$this->redis->lPush($name, time() . '_' . $i);
}
if ($team_qh && $ispt == '2') {
$name2 = 'pind' . $pdid . '-' . $team_qh . $stoid;
$this->redis->lPush($name2, time() . '_0' );
}
$key=$no.'-6-'.$pdid.'-'.$pdgoodsnum.'-'.$notime.'-'.$team_qh;
$this->redis->sRem('redis_set_'.$stoid,$key);
}
}
}
}