|
@@ -7,423 +7,421 @@ use Main\Lib\Core;
|
|
|
|
|
|
class Buy extends Core
|
|
|
{
|
|
|
- public function __construct()
|
|
|
- {
|
|
|
- parent::__construct();
|
|
|
- $this->checkLogin();
|
|
|
- $this->shop_id = Dever::input('shop_id');
|
|
|
-
|
|
|
- if (!$this->shop_id) {
|
|
|
- Dever::alert('请选择门店');
|
|
|
- }
|
|
|
-
|
|
|
- $this->shop = Dever::db('shop/info')->getOne($this->shop_id);
|
|
|
-
|
|
|
- if (!$this->shop) {
|
|
|
- Dever::alert('门店不存在');
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # 分享
|
|
|
- public function share()
|
|
|
- {
|
|
|
- $act_id = Dever::input('act_id', 1);
|
|
|
- $path = Dever::input('path');
|
|
|
- $this->data = Dever::load('shop/lib/share')->up($this->uid, $this->shop_id, $act_id, $path);
|
|
|
-
|
|
|
- return $this->data;
|
|
|
- }
|
|
|
-
|
|
|
- # 获取店铺的优惠券
|
|
|
- public function getCoupon()
|
|
|
- {
|
|
|
- $this->data['coupon'] = Dever::load('shop/lib/coupon')->getAll($this->shop_id);
|
|
|
-
|
|
|
- $this->data['shop'] = $this->shop;
|
|
|
-
|
|
|
- $act_id = Dever::input('act_id', 1);
|
|
|
-
|
|
|
- $this->data['act'] = Dever::db('act/info')->find($act_id);
|
|
|
-
|
|
|
- return $this->data;
|
|
|
- }
|
|
|
-
|
|
|
- # 领取优惠券 也得判断一下用户是否有权限领取
|
|
|
- public function takeCoupon()
|
|
|
- {
|
|
|
- # 验证这个券是否可以领取
|
|
|
- $shop_coupon_id = Dever::input('shop_coupon_id');
|
|
|
- $coupon = explode(',', $shop_coupon_id);
|
|
|
-
|
|
|
- $this->data['take'] = array();
|
|
|
- foreach ($coupon as $k => $v) {
|
|
|
- $this->data['take']['shop_coupon_id'] = $v;
|
|
|
- $this->data['take']['status'] = Dever::load('shop/lib/coupon')->getOne($this->uid, $this->shop, $v);
|
|
|
- }
|
|
|
-
|
|
|
- return $this->data;
|
|
|
- }
|
|
|
-
|
|
|
- # 获取商品详细信息
|
|
|
- public function getGoodsInfo()
|
|
|
- {
|
|
|
- $id = Dever::input('goods_id');
|
|
|
- if (!$id) {
|
|
|
- Dever::alert('错误的商品');
|
|
|
- }
|
|
|
-
|
|
|
- $this->data['goods'] = Dever::load('shop/lib/info')->getGoodsInfo($this->shop_id, $id);
|
|
|
-
|
|
|
- //$this->data['shop'] = $this->shop;
|
|
|
-
|
|
|
- return $this->data;
|
|
|
- }
|
|
|
-
|
|
|
- # 获取购物车的数据
|
|
|
- public function getCart()
|
|
|
- {
|
|
|
- $where['uid'] = $this->uid;
|
|
|
- $where['shop_id'] = $this->shop_id;
|
|
|
-
|
|
|
- $this->data['cart'] = Dever::db('shop/cart')->select($where);
|
|
|
-
|
|
|
- if ($this->data['cart']) {
|
|
|
- foreach ($this->data['cart'] as $k => $v) {
|
|
|
- $this->data['cart'][$k]['goods'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
|
|
|
-
|
|
|
- if (isset($this->data['cart'][$k]['goods']['sku_id'])) {
|
|
|
- $this->data['cart'][$k]['price_id'] = $this->data['cart'][$k]['goods']['sku_id'];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $this->data['cart'];
|
|
|
- }
|
|
|
-
|
|
|
- # 加入到购物车
|
|
|
- public function addCart()
|
|
|
- {
|
|
|
- $where['uid'] = $this->uid;
|
|
|
- $where['shop_id'] = $this->shop_id;
|
|
|
- $where['goods_id'] = Dever::input('goods_id');
|
|
|
- $where['sku_id'] = Dever::input('price_id');
|
|
|
- if (!$where['goods_id']) {
|
|
|
- Dever::alert('错误的商品');
|
|
|
- }
|
|
|
-
|
|
|
- $info = Dever::db('shop/cart')->find($where);
|
|
|
-
|
|
|
- $where['num'] = Dever::input('num');
|
|
|
- if (!$info) {
|
|
|
- # 验证库存
|
|
|
- Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
|
|
|
- $state = Dever::db('shop/cart')->insert($where);
|
|
|
- } else {
|
|
|
- $where['num'] += $info['num'];
|
|
|
- # 验证库存
|
|
|
- Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
|
|
|
- $where['where_id'] = $info['id'];
|
|
|
- $state = Dever::db('shop/cart')->update($where);
|
|
|
- }
|
|
|
-
|
|
|
- return $this->getCart();
|
|
|
- }
|
|
|
-
|
|
|
- # 清空购物车
|
|
|
- public function dropCart()
|
|
|
- {
|
|
|
- $where['uid'] = $this->uid;
|
|
|
- $where['shop_id'] = $this->shop_id;
|
|
|
-
|
|
|
- $state = Dever::db('shop/cart')->delete($where);
|
|
|
-
|
|
|
- return $this->alert($state);
|
|
|
- }
|
|
|
-
|
|
|
- # 更新购物车数量
|
|
|
- public function upCart()
|
|
|
- {
|
|
|
- $where['uid'] = $this->uid;
|
|
|
- $where['shop_id'] = $this->shop_id;
|
|
|
- $where['id'] = Dever::input('cart_id');
|
|
|
-
|
|
|
- $status = Dever::input('status', 1);
|
|
|
- $type = Dever::input('type', 1);
|
|
|
- $num = Dever::input('num');
|
|
|
-
|
|
|
- $info = Dever::db('shop/cart')->find($where);
|
|
|
-
|
|
|
- $state = false;
|
|
|
-
|
|
|
- if ($info) {
|
|
|
- if ($type == 1) {
|
|
|
- $num = $info['num'] + $num;
|
|
|
- } else {
|
|
|
- $num = $info['num'] - $num;
|
|
|
- }
|
|
|
- $where = array();
|
|
|
- $where['where_id'] = $info['id'];
|
|
|
- if ($num <= 0) {
|
|
|
- $state = Dever::db('shop/cart')->delete($info['id']);
|
|
|
- } else {
|
|
|
- $where['status'] = $status;
|
|
|
- $where['num'] = $num;
|
|
|
-
|
|
|
- # 验证库存
|
|
|
- $total = Dever::load('shop/lib/info')->checkTotal($num, $info['goods_id'], $this->shop_id, $info['sku_id']);
|
|
|
-
|
|
|
- if ($where['num'] > $total) {
|
|
|
- # 库存不足
|
|
|
- $where['num'] = $total;
|
|
|
- $state = Dever::db('shop/cart')->update($where);
|
|
|
- return array('cart' => 2, 'data' => $total);
|
|
|
- }
|
|
|
- $state = Dever::db('shop/cart')->update($where);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return array('cart' => 1, 'data' => $this->getCart());
|
|
|
- }
|
|
|
-
|
|
|
- # 确认订单页面
|
|
|
- public function confirm()
|
|
|
- {
|
|
|
- $this->data['user'] = $this->user;
|
|
|
- $this->goods();
|
|
|
- $this->coupon($this->data['price'], $this->data['method']);
|
|
|
-
|
|
|
- return $this->data;
|
|
|
- }
|
|
|
-
|
|
|
- # 优惠券
|
|
|
- public function coupon($price, $method, $type = 1)
|
|
|
- {
|
|
|
- $user_coupon_id = Dever::input('user_coupon_id');
|
|
|
-
|
|
|
- $this->data['user_coupon_id'] = 0;
|
|
|
- $this->data['coupon_id'] = 0;
|
|
|
- $this->data['coupon_cash'] = 0;
|
|
|
-
|
|
|
- if ($type == 1) {
|
|
|
- if ($price <= 0) {
|
|
|
- Dever::alert('付款价格错误');
|
|
|
- }
|
|
|
- # 查找符合要求的优惠券
|
|
|
- $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $this->uid, 'city' => $this->shop['city'], 'status' => 1, 'edate' => time()));
|
|
|
-
|
|
|
- if ($coupon) {
|
|
|
- foreach ($coupon as $k => $v) {
|
|
|
- if ($v['shop_id'] != $this->shop_id) {
|
|
|
- # 查找该券是否符合当前店铺
|
|
|
- if ($this->shop['coupon_city'] == 2) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
|
|
|
- if ($coupon_info && ($coupon_info['method'] == $method || $coupon_info['method'] == 3)) {
|
|
|
- $kou = false;
|
|
|
- if ($coupon_info['type'] == 1) {
|
|
|
- # 满减券
|
|
|
- if ($price >= $coupon_info['total_cash']) {
|
|
|
- $kou = true;
|
|
|
- }
|
|
|
- } else {
|
|
|
- $kou = true;
|
|
|
- }
|
|
|
-
|
|
|
- if ($kou) {
|
|
|
- $coupon_info['user_coupon_id'] = $v['id'];
|
|
|
- $coupon_info['uid'] = $v['uid'];
|
|
|
- $coupon_info['shop_id'] = $v['shop_id'];
|
|
|
- $coupon_info['edate'] = date('Y-m-d', $v['edate']);
|
|
|
- $this->data['coupon'][] = $coupon_info;
|
|
|
-
|
|
|
- if (!$user_coupon_id && $this->data['coupon_cash'] <= $coupon_info['cash']) {
|
|
|
- $this->data['user_coupon_id'] = $v['id'];
|
|
|
- $this->data['coupon_id'] = $coupon_info['id'];
|
|
|
- $this->data['coupon_cash'] = $coupon_info['cash'];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if ($user_coupon_id) {
|
|
|
- if ($price <= 0) {
|
|
|
- Dever::alert('付款价格错误');
|
|
|
- }
|
|
|
- $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $this->uid, 'id' => $user_coupon_id, 'status' => 1));
|
|
|
- if (!$coupon) {
|
|
|
- Dever::alert('优惠券不可用');
|
|
|
- }
|
|
|
- if (time() > $coupon['edate']) {
|
|
|
- Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
|
|
|
- Dever::alert('优惠券已过期');
|
|
|
- }
|
|
|
-
|
|
|
- if ($coupon['shop_id'] != $this->shop_id) {
|
|
|
- if ($this->shop['coupon_city'] == 2) {
|
|
|
- Dever::alert('优惠券不可用');
|
|
|
- } else {
|
|
|
- $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
|
|
|
- if (!$coupon_info) {
|
|
|
- Dever::alert('优惠券不可用');
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
|
|
|
- if ($goods_coupon['type'] == 2 && $this->data['price'] < $goods_coupon['total_cash']) {
|
|
|
- Dever::alert('优惠券不可用');
|
|
|
- }
|
|
|
- if ($goods_coupon['method'] != $method && $goods_coupon['method'] != 3) {
|
|
|
- Dever::alert('优惠券不可用');
|
|
|
- }
|
|
|
-
|
|
|
- $this->data['user_coupon_id'] = $user_coupon_id;
|
|
|
- $this->data['coupon_id'] = $goods_coupon['id'];
|
|
|
- $this->data['coupon_cash'] = $goods_coupon['cash'];
|
|
|
- }
|
|
|
-
|
|
|
- if (isset($this->data['coupon_cash']) && $this->data['coupon_cash'] > 0) {
|
|
|
- $this->data['price'] -= $this->data['coupon_cash'];
|
|
|
- if ($this->data['price'] < 0) {
|
|
|
- $this->data['price'] = 0;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # 得到商品和总价
|
|
|
- private function goods()
|
|
|
- {
|
|
|
- # 1自提,2配送
|
|
|
- $this->data['method'] = Dever::input('method', 1);
|
|
|
- $this->data['pay_method'] = Dever::input('pay_method');
|
|
|
- $card = Dever::input('card');
|
|
|
- $pwd = Dever::input('pwd');
|
|
|
-
|
|
|
- if ($this->data['pay_method'] == 3 && $card && $pwd) {
|
|
|
- $this->data['card'] = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd, 'status' => 1));
|
|
|
- if (!$this->data['card']) {
|
|
|
- Dever::alert('卡号/密码错误');
|
|
|
- }
|
|
|
- if (time() > $this->data['card']['edate']) {
|
|
|
- Dever::alert('礼品卡已失效');
|
|
|
- }
|
|
|
- $card_info = Dever::db('goods/card')->find($this->data['card']['card_id']);
|
|
|
- if (!$card_info) {
|
|
|
- Dever::alert('卡号/密码错误');
|
|
|
- }
|
|
|
- $goods = Dever::array_decode($card_info['goods']);
|
|
|
-
|
|
|
- $goods_id = array();
|
|
|
- $num = array();
|
|
|
- $sku_id = array();
|
|
|
- foreach ($goods as $k => $v) {
|
|
|
- $goods_id[] = $v['goods_id'];
|
|
|
- $num[] = $v['num'];
|
|
|
- $sku_id[] = -1;
|
|
|
- }
|
|
|
- } else {
|
|
|
- $goods_id = Dever::input('goods_id');
|
|
|
- if (!$goods_id) {
|
|
|
- Dever::alert('请传入商品');
|
|
|
- }
|
|
|
- $goods_id = explode(',', $goods_id);
|
|
|
-
|
|
|
- $sku_id = Dever::input('price_id');
|
|
|
- if ($sku_id) {
|
|
|
- $sku_id = explode(',', $sku_id);
|
|
|
- }
|
|
|
-
|
|
|
- $num = Dever::input('num');
|
|
|
- $num = Dever::input('num');
|
|
|
- if (!$num) {
|
|
|
- Dever::alert('请传入商品数量');
|
|
|
- }
|
|
|
- $num = explode(',', $num);
|
|
|
- }
|
|
|
-
|
|
|
- $this->data['price'] = 0;
|
|
|
- $this->data['num'] = 0;
|
|
|
- $this->data['name'] = '';
|
|
|
- $count = count($goods_id);
|
|
|
- # 计算总价格
|
|
|
- foreach ($goods_id as $k => $v) {
|
|
|
- $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
|
|
|
- $n = isset($num[$k]) ? $num[$k] : 1;
|
|
|
- $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $n);
|
|
|
- $this->data['list'][$k]['num'] = $n;
|
|
|
-
|
|
|
- # 2是库存不足
|
|
|
- $this->data['list'][$k]['ku_state'] = 1;
|
|
|
-
|
|
|
- # 验证是否有货
|
|
|
- $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $this->shop_id, $s);
|
|
|
-
|
|
|
- if ($total <= 0) {
|
|
|
- $this->data['list'][$k]['ku_state'] = 2;
|
|
|
- $this->data['list'][$k]['buy_num'] = 0;
|
|
|
- }
|
|
|
-
|
|
|
- if ($this->data['list'][$k]['ku_state'] == 1) {
|
|
|
- $this->data['list'][$k]['buy_num'] = $n;
|
|
|
- $this->data['num'] += $n;
|
|
|
- $this->data['price'] += $this->data['list'][$k]['price'] * $n;
|
|
|
- if (!$this->data['name']) {
|
|
|
- if ($count > 1) {
|
|
|
- $this->data['name'] = $this->data['list'][$k]['name'] . '等' . $count . '种商品';
|
|
|
- } else {
|
|
|
- $this->data['name'] = $this->data['list'][$k]['name'];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # 开始下单
|
|
|
- public function pay()
|
|
|
- {
|
|
|
- $refer = Dever::input('refer');
|
|
|
- $cart = Dever::input('cart', 1);
|
|
|
- $parent_uid = Dever::input('parent_uid');
|
|
|
- $address_id = Dever::input('address_id');
|
|
|
- $invoice_id = Dever::input('invoice_id');
|
|
|
-
|
|
|
- $info = Dever::input('info');
|
|
|
-
|
|
|
- $this->goods();
|
|
|
- $this->coupon($this->data['price'], $this->data['method'], 2);
|
|
|
-
|
|
|
- if ($this->data['pay_method'] > 1) {
|
|
|
- $cart = 2;
|
|
|
- }
|
|
|
-
|
|
|
- if (!$this->data['coupon_id'] && $this->data['price'] <= 0) {
|
|
|
- Dever::alert('已售空');
|
|
|
- }
|
|
|
-
|
|
|
- if ($this->data['method'] == 2 && !$address_id) {
|
|
|
- Dever::alert('收货地址不能为空');
|
|
|
- }
|
|
|
-
|
|
|
- if (!isset($this->data['card'])) {
|
|
|
- $this->data['card'] = false;
|
|
|
- }
|
|
|
-
|
|
|
- $pay = Dever::load('shop/lib/sell')->action($this->data['method'], $this->data['pay_method'], $this->user, $this->shop, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], $address_id, $invoice_id, $info, $this->data['card'], $this->data['user_coupon_id'], $cart, $refer);
|
|
|
- return $pay;
|
|
|
- }
|
|
|
-
|
|
|
- # 再次付款
|
|
|
- public function r_pay()
|
|
|
- {
|
|
|
- $refer = Dever::input('refer');
|
|
|
- $order_id = Dever::input('order_id');
|
|
|
-
|
|
|
- $pay = Dever::load('shop/lib/sell')->raction($order_id, $refer);
|
|
|
-
|
|
|
- return $pay;
|
|
|
- }
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ parent::__construct();
|
|
|
+ $this->checkLogin();
|
|
|
+ $this->shop_id = Dever::input('shop_id');
|
|
|
+
|
|
|
+ if (!$this->shop_id) {
|
|
|
+ Dever::alert('请选择门店');
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->shop = Dever::db('shop/info')->getOne($this->shop_id);
|
|
|
+
|
|
|
+ if (!$this->shop) {
|
|
|
+ Dever::alert('门店不存在');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ # 分享
|
|
|
+ public function share()
|
|
|
+ {
|
|
|
+ $act_id = Dever::input('act_id', 1);
|
|
|
+ $path = Dever::input('path');
|
|
|
+ $this->data = Dever::load('shop/lib/share')->up($this->uid, $this->shop_id, $act_id, $path);
|
|
|
+
|
|
|
+ return $this->data;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 获取店铺的优惠券
|
|
|
+ public function getCoupon()
|
|
|
+ {
|
|
|
+ $this->data['coupon'] = Dever::load('shop/lib/coupon')->getAll($this->shop_id);
|
|
|
+
|
|
|
+ $this->data['shop'] = $this->shop;
|
|
|
+
|
|
|
+ $act_id = Dever::input('act_id', 1);
|
|
|
+
|
|
|
+ $this->data['act'] = Dever::db('act/info')->find($act_id);
|
|
|
+
|
|
|
+ return $this->data;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 领取优惠券 也得判断一下用户是否有权限领取
|
|
|
+ public function takeCoupon()
|
|
|
+ {
|
|
|
+ # 验证这个券是否可以领取
|
|
|
+ $shop_coupon_id = Dever::input('shop_coupon_id');
|
|
|
+ $coupon = explode(',', $shop_coupon_id);
|
|
|
+
|
|
|
+ $this->data['take'] = array();
|
|
|
+ foreach ($coupon as $k => $v) {
|
|
|
+ $this->data['take']['shop_coupon_id'] = $v;
|
|
|
+ $this->data['take']['status'] = Dever::load('shop/lib/coupon')->getOne($this->uid, $this->shop, $v);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->data;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 获取商品详细信息
|
|
|
+ public function getGoodsInfo()
|
|
|
+ {
|
|
|
+ $id = Dever::input('goods_id');
|
|
|
+ if (!$id) {
|
|
|
+ Dever::alert('错误的商品');
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->data['goods'] = Dever::load('shop/lib/info')->getGoodsInfo($this->shop_id, $id);
|
|
|
+
|
|
|
+ //$this->data['shop'] = $this->shop;
|
|
|
+
|
|
|
+ return $this->data;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 获取购物车的数据
|
|
|
+ public function getCart()
|
|
|
+ {
|
|
|
+ $where['uid'] = $this->uid;
|
|
|
+ $where['shop_id'] = $this->shop_id;
|
|
|
+
|
|
|
+ $this->data['cart'] = Dever::db('shop/cart')->select($where);
|
|
|
+
|
|
|
+ if ($this->data['cart']) {
|
|
|
+ foreach ($this->data['cart'] as $k => $v) {
|
|
|
+ $this->data['cart'][$k]['goods'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
|
|
|
+
|
|
|
+ if (isset($this->data['cart'][$k]['goods']['sku_id'])) {
|
|
|
+ $this->data['cart'][$k]['price_id'] = $this->data['cart'][$k]['goods']['sku_id'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->data['cart'];
|
|
|
+ }
|
|
|
+
|
|
|
+ # 加入到购物车
|
|
|
+ public function addCart()
|
|
|
+ {
|
|
|
+ $where['uid'] = $this->uid;
|
|
|
+ $where['shop_id'] = $this->shop_id;
|
|
|
+ $where['goods_id'] = Dever::input('goods_id');
|
|
|
+ $where['sku_id'] = Dever::input('price_id');
|
|
|
+ if (!$where['goods_id']) {
|
|
|
+ Dever::alert('错误的商品');
|
|
|
+ }
|
|
|
+
|
|
|
+ $info = Dever::db('shop/cart')->find($where);
|
|
|
+
|
|
|
+ $where['num'] = Dever::input('num');
|
|
|
+ if (!$info) {
|
|
|
+ # 验证库存
|
|
|
+ Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
|
|
|
+ $state = Dever::db('shop/cart')->insert($where);
|
|
|
+ } else {
|
|
|
+ $where['num'] += $info['num'];
|
|
|
+ # 验证库存
|
|
|
+ Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
|
|
|
+ $where['where_id'] = $info['id'];
|
|
|
+ $state = Dever::db('shop/cart')->update($where);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->getCart();
|
|
|
+ }
|
|
|
+
|
|
|
+ # 清空购物车
|
|
|
+ public function dropCart()
|
|
|
+ {
|
|
|
+ $where['uid'] = $this->uid;
|
|
|
+ $where['shop_id'] = $this->shop_id;
|
|
|
+
|
|
|
+ $state = Dever::db('shop/cart')->delete($where);
|
|
|
+
|
|
|
+ return $this->alert($state);
|
|
|
+ }
|
|
|
+
|
|
|
+ # 更新购物车数量
|
|
|
+ public function upCart()
|
|
|
+ {
|
|
|
+ $where['uid'] = $this->uid;
|
|
|
+ $where['shop_id'] = $this->shop_id;
|
|
|
+ $where['id'] = Dever::input('cart_id');
|
|
|
+
|
|
|
+ $status = Dever::input('status', 1);
|
|
|
+ $type = Dever::input('type', 1);
|
|
|
+ $num = Dever::input('num');
|
|
|
+
|
|
|
+ $info = Dever::db('shop/cart')->find($where);
|
|
|
+
|
|
|
+ $state = false;
|
|
|
+
|
|
|
+ if ($info) {
|
|
|
+ if ($type == 1) {
|
|
|
+ $num = $info['num'] + $num;
|
|
|
+ } else {
|
|
|
+ $num = $info['num'] - $num;
|
|
|
+ }
|
|
|
+ $where = array();
|
|
|
+ $where['where_id'] = $info['id'];
|
|
|
+ if ($num <= 0) {
|
|
|
+ $state = Dever::db('shop/cart')->delete($info['id']);
|
|
|
+ } else {
|
|
|
+ $where['status'] = $status;
|
|
|
+ $where['num'] = $num;
|
|
|
+
|
|
|
+ # 验证库存
|
|
|
+ $total = Dever::load('shop/lib/info')->checkTotal($num, $info['goods_id'], $this->shop_id, $info['sku_id']);
|
|
|
+
|
|
|
+ if ($where['num'] > $total) {
|
|
|
+ # 库存不足
|
|
|
+ $where['num'] = $total;
|
|
|
+ $state = Dever::db('shop/cart')->update($where);
|
|
|
+ return array('cart' => 2, 'data' => $total);
|
|
|
+ }
|
|
|
+ $state = Dever::db('shop/cart')->update($where);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return array('cart' => 1, 'data' => $this->getCart());
|
|
|
+ }
|
|
|
+
|
|
|
+ # 确认订单页面
|
|
|
+ public function confirm()
|
|
|
+ {
|
|
|
+ $this->data['user'] = $this->user;
|
|
|
+ $this->goods();
|
|
|
+ $this->coupon($this->data['price'], $this->data['method']);
|
|
|
+
|
|
|
+ return $this->data;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 优惠券
|
|
|
+ public function coupon($price, $method, $type = 1)
|
|
|
+ {
|
|
|
+ $user_coupon_id = Dever::input('user_coupon_id');
|
|
|
+
|
|
|
+ $this->data['user_coupon_id'] = 0;
|
|
|
+ $this->data['coupon_id'] = 0;
|
|
|
+ $this->data['coupon_cash'] = 0;
|
|
|
+
|
|
|
+ if ($type == 1) {
|
|
|
+ if ($price <= 0) {
|
|
|
+ Dever::alert('付款价格错误');
|
|
|
+ }
|
|
|
+ # 查找符合要求的优惠券
|
|
|
+ $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $this->uid, 'city' => $this->shop['city'], 'status' => 1, 'edate' => time()));
|
|
|
+
|
|
|
+ if ($coupon) {
|
|
|
+ foreach ($coupon as $k => $v) {
|
|
|
+ if ($v['shop_id'] != $this->shop_id) {
|
|
|
+ # 查找该券是否符合当前店铺
|
|
|
+ if ($this->shop['coupon_city'] == 2) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
|
|
|
+ if ($coupon_info && ($coupon_info['method'] == $method || $coupon_info['method'] == 3)) {
|
|
|
+ $kou = false;
|
|
|
+ if ($coupon_info['type'] == 1) {
|
|
|
+ # 满减券
|
|
|
+ if ($price >= $coupon_info['total_cash']) {
|
|
|
+ $kou = true;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $kou = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($kou) {
|
|
|
+ $coupon_info['user_coupon_id'] = $v['id'];
|
|
|
+ $coupon_info['uid'] = $v['uid'];
|
|
|
+ $coupon_info['shop_id'] = $v['shop_id'];
|
|
|
+ $coupon_info['edate'] = date('Y-m-d', $v['edate']);
|
|
|
+ $this->data['coupon'][] = $coupon_info;
|
|
|
+
|
|
|
+ if (!$user_coupon_id && $this->data['coupon_cash'] <= $coupon_info['cash']) {
|
|
|
+ $this->data['user_coupon_id'] = $v['id'];
|
|
|
+ $this->data['coupon_id'] = $coupon_info['id'];
|
|
|
+ $this->data['coupon_cash'] = $coupon_info['cash'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($user_coupon_id) {
|
|
|
+ if ($price <= 0) {
|
|
|
+ Dever::alert('付款价格错误');
|
|
|
+ }
|
|
|
+ $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $this->uid, 'id' => $user_coupon_id, 'status' => 1));
|
|
|
+ if (!$coupon) {
|
|
|
+ Dever::alert('优惠券不可用');
|
|
|
+ }
|
|
|
+ if (time() > $coupon['edate']) {
|
|
|
+ Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
|
|
|
+ Dever::alert('优惠券已过期');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($coupon['shop_id'] != $this->shop_id) {
|
|
|
+ if ($this->shop['coupon_city'] == 2) {
|
|
|
+ Dever::alert('优惠券不可用');
|
|
|
+ } else {
|
|
|
+ $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
|
|
|
+ if (!$coupon_info) {
|
|
|
+ Dever::alert('优惠券不可用');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
|
|
|
+ if ($goods_coupon['type'] == 2 && $this->data['price'] < $goods_coupon['total_cash']) {
|
|
|
+ Dever::alert('优惠券不可用');
|
|
|
+ }
|
|
|
+ if ($goods_coupon['method'] != $method && $goods_coupon['method'] != 3) {
|
|
|
+ Dever::alert('优惠券不可用');
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->data['user_coupon_id'] = $user_coupon_id;
|
|
|
+ $this->data['coupon_id'] = $goods_coupon['id'];
|
|
|
+ $this->data['coupon_cash'] = $goods_coupon['cash'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($this->data['coupon_cash']) && $this->data['coupon_cash'] > 0) {
|
|
|
+ $this->data['price'] -= $this->data['coupon_cash'];
|
|
|
+ if ($this->data['price'] < 0) {
|
|
|
+ $this->data['price'] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ # 得到商品和总价
|
|
|
+ private function goods()
|
|
|
+ {
|
|
|
+ # 1自提,2配送
|
|
|
+ $this->data['method'] = Dever::input('method', 1);
|
|
|
+ $this->data['pay_method'] = Dever::input('pay_method');
|
|
|
+ $card = Dever::input('card');
|
|
|
+ $pwd = Dever::input('pwd');
|
|
|
+
|
|
|
+ if ($this->data['pay_method'] == 3 && $card && $pwd) {
|
|
|
+ $this->data['card'] = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd, 'status' => 1));
|
|
|
+ if (!$this->data['card']) {
|
|
|
+ Dever::alert('卡号/密码错误');
|
|
|
+ }
|
|
|
+ if (time() > $this->data['card']['edate']) {
|
|
|
+ Dever::alert('礼品卡已失效');
|
|
|
+ }
|
|
|
+ $card_info = Dever::db('goods/card')->find($this->data['card']['card_id']);
|
|
|
+ if (!$card_info) {
|
|
|
+ Dever::alert('卡号/密码错误');
|
|
|
+ }
|
|
|
+ $goods = Dever::array_decode($card_info['goods']);
|
|
|
+
|
|
|
+ $goods_id = array();
|
|
|
+ $num = array();
|
|
|
+ $sku_id = array();
|
|
|
+ foreach ($goods as $k => $v) {
|
|
|
+ $goods_id[] = $v['goods_id'];
|
|
|
+ $num[] = $v['num'];
|
|
|
+ $sku_id[] = -1;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $goods_id = Dever::input('goods_id');
|
|
|
+ if (!$goods_id) {
|
|
|
+ Dever::alert('请传入商品');
|
|
|
+ }
|
|
|
+ $goods_id = explode(',', $goods_id);
|
|
|
+
|
|
|
+ $sku_id = Dever::input('price_id');
|
|
|
+ if ($sku_id) {
|
|
|
+ $sku_id = explode(',', $sku_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ $num = Dever::input('num');
|
|
|
+ $num = Dever::input('num');
|
|
|
+ if (!$num) {
|
|
|
+ Dever::alert('请传入商品数量');
|
|
|
+ }
|
|
|
+ $num = explode(',', $num);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->data['price'] = 0;
|
|
|
+ $this->data['num'] = 0;
|
|
|
+ $this->data['name'] = array();
|
|
|
+ $count = count($goods_id);
|
|
|
+ # 计算总价格
|
|
|
+ foreach ($goods_id as $k => $v) {
|
|
|
+ $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
|
|
|
+ $n = isset($num[$k]) ? $num[$k] : 1;
|
|
|
+ $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $n);
|
|
|
+ $this->data['list'][$k]['num'] = $n;
|
|
|
+
|
|
|
+ # 2是库存不足
|
|
|
+ $this->data['list'][$k]['ku_state'] = 1;
|
|
|
+
|
|
|
+ # 验证是否有货
|
|
|
+ $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $this->shop_id, $s);
|
|
|
+
|
|
|
+ if ($total <= 0) {
|
|
|
+ $this->data['list'][$k]['ku_state'] = 2;
|
|
|
+ $this->data['list'][$k]['buy_num'] = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->data['list'][$k]['ku_state'] == 1) {
|
|
|
+ $this->data['list'][$k]['buy_num'] = $n;
|
|
|
+ $this->data['num'] += $n;
|
|
|
+ $this->data['price'] += $this->data['list'][$k]['price'] * $n;
|
|
|
+ if ($count > 1) {
|
|
|
+ $this->data['name'][] = $this->data['list'][$k]['name'];
|
|
|
+ } else {
|
|
|
+ $this->data['name'][] = $this->data['list'][$k]['name'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ # 开始下单
|
|
|
+ public function pay()
|
|
|
+ {
|
|
|
+ $refer = Dever::input('refer');
|
|
|
+ $cart = Dever::input('cart', 1);
|
|
|
+ $parent_uid = Dever::input('parent_uid');
|
|
|
+ $address_id = Dever::input('address_id');
|
|
|
+ $invoice_id = Dever::input('invoice_id');
|
|
|
+
|
|
|
+ $info = Dever::input('info');
|
|
|
+
|
|
|
+ $this->goods();
|
|
|
+ $this->coupon($this->data['price'], $this->data['method'], 2);
|
|
|
+
|
|
|
+ if ($this->data['pay_method'] > 1) {
|
|
|
+ $cart = 2;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$this->data['coupon_id'] && $this->data['price'] <= 0) {
|
|
|
+ Dever::alert('已售空');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->data['method'] == 2 && !$address_id) {
|
|
|
+ Dever::alert('收货地址不能为空');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isset($this->data['card'])) {
|
|
|
+ $this->data['card'] = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $pay = Dever::load('shop/lib/sell')->action($this->data['method'], $this->data['pay_method'], $this->user, $this->shop, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], $address_id, $invoice_id, $info, $this->data['card'], $this->data['user_coupon_id'], $cart, $refer);
|
|
|
+ return $pay;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 再次付款
|
|
|
+ public function r_pay()
|
|
|
+ {
|
|
|
+ $refer = Dever::input('refer');
|
|
|
+ $order_id = Dever::input('order_id');
|
|
|
+
|
|
|
+ $pay = Dever::load('shop/lib/sell')->raction($order_id, $refer);
|
|
|
+
|
|
|
+ return $pay;
|
|
|
+ }
|
|
|
}
|