123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- <?php
- namespace Shop\Src;
- use Dever;
- 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($where);
- } 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)
- {
- if ($price <= 0) {
- Dever::alert('付款价格错误');
- }
- $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) {
- # 查找符合要求的优惠券
- $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) {
- $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::load('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd, 'status' => 1));
- if (!$this->data['card']) {
- Dever::alert('卡号/密码错误');
- }
- $goods = Dever::array_decode($this->data['card']['goods']);
- $goods_id = array();
- $num = array();
- $sku_id = array();
- foreach ($goods as $k => $v) {
- $goods_id[] = $v['goods_id'];
- $num[] = $v['num'];
- }
- } 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 = 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] : 0;
- $n = isset($num[$k]) ? $num[$k] : 1;
- $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $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;
- }
- 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['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['coupon_id'], $this->data['user_coupon_id'], $parent_uid, $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;
- }
- }
|