Buy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. namespace Shop\Src;
  3. use Dever;
  4. use Main\Lib\Core;
  5. class Buy extends Core
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. $this->checkLogin();
  11. $this->shop_id = Dever::input('shop_id');
  12. if (!$this->shop_id) {
  13. Dever::alert('请选择门店');
  14. }
  15. $this->shop = Dever::db('shop/info')->getOne($this->shop_id);
  16. if (!$this->shop) {
  17. Dever::alert('门店不存在');
  18. }
  19. }
  20. # 分享
  21. public function share()
  22. {
  23. $act_id = Dever::input('act_id', 1);
  24. $path = Dever::input('path');
  25. $this->data = Dever::load('shop/lib/share')->up($this->uid, $this->shop_id, $act_id, $path);
  26. return $this->data;
  27. }
  28. # 获取店铺的优惠券
  29. public function getCoupon()
  30. {
  31. $this->data['coupon'] = Dever::load('shop/lib/coupon')->getAll($this->shop_id);
  32. $this->data['shop'] = $this->shop;
  33. $act_id = Dever::input('act_id', 1);
  34. $this->data['act'] = Dever::db('act/info')->find($act_id);
  35. return $this->data;
  36. }
  37. # 领取优惠券 也得判断一下用户是否有权限领取
  38. public function takeCoupon()
  39. {
  40. # 验证这个券是否可以领取
  41. $shop_coupon_id = Dever::input('shop_coupon_id');
  42. $coupon = explode(',', $shop_coupon_id);
  43. $this->data['take'] = array();
  44. foreach ($coupon as $k => $v) {
  45. $this->data['take']['shop_coupon_id'] = $v;
  46. $this->data['take']['status'] = Dever::load('shop/lib/coupon')->getOne($this->uid, $this->shop, $v);
  47. }
  48. return $this->data;
  49. }
  50. # 获取商品详细信息
  51. public function getGoodsInfo()
  52. {
  53. $id = Dever::input('goods_id');
  54. if (!$id) {
  55. Dever::alert('错误的商品');
  56. }
  57. $this->data['goods'] = Dever::load('shop/lib/info')->getGoodsInfo($this->shop_id, $id);
  58. //$this->data['shop'] = $this->shop;
  59. return $this->data;
  60. }
  61. # 获取购物车的数据
  62. public function getCart()
  63. {
  64. $where['uid'] = $this->uid;
  65. $where['shop_id'] = $this->shop_id;
  66. $this->data['cart'] = Dever::db('shop/cart')->select($where);
  67. if ($this->data['cart']) {
  68. foreach ($this->data['cart'] as $k => $v) {
  69. $this->data['cart'][$k]['goods'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  70. if (isset($this->data['cart'][$k]['goods']['sku_id'])) {
  71. $this->data['cart'][$k]['price_id'] = $this->data['cart'][$k]['goods']['sku_id'];
  72. }
  73. }
  74. }
  75. return $this->data['cart'];
  76. }
  77. # 加入到购物车
  78. public function addCart()
  79. {
  80. $where['uid'] = $this->uid;
  81. $where['shop_id'] = $this->shop_id;
  82. $where['goods_id'] = Dever::input('goods_id');
  83. $where['sku_id'] = Dever::input('price_id');
  84. if (!$where['goods_id']) {
  85. Dever::alert('错误的商品');
  86. }
  87. $info = Dever::db('shop/cart')->find($where);
  88. $where['num'] = Dever::input('num');
  89. if (!$info) {
  90. # 验证库存
  91. Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
  92. $state = Dever::db('shop/cart')->insert($where);
  93. } else {
  94. $where['num'] += $info['num'];
  95. # 验证库存
  96. Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
  97. $where['where_id'] = $info['id'];
  98. $state = Dever::db('shop/cart')->update($where);
  99. }
  100. return $this->getCart();
  101. }
  102. # 清空购物车
  103. public function dropCart()
  104. {
  105. $where['uid'] = $this->uid;
  106. $where['shop_id'] = $this->shop_id;
  107. $state = Dever::db('shop/cart')->delete($where);
  108. return $this->alert($state);
  109. }
  110. # 更新购物车数量
  111. public function upCart()
  112. {
  113. $where['uid'] = $this->uid;
  114. $where['shop_id'] = $this->shop_id;
  115. $where['id'] = Dever::input('cart_id');
  116. $status = Dever::input('status', 1);
  117. $type = Dever::input('type', 1);
  118. $num = Dever::input('num');
  119. $info = Dever::db('shop/cart')->find($where);
  120. $state = false;
  121. if ($info) {
  122. if ($type == 1) {
  123. $num = $info['num'] + $num;
  124. } else {
  125. $num = $info['num'] - $num;
  126. }
  127. $where = array();
  128. $where['where_id'] = $info['id'];
  129. if ($num <= 0) {
  130. $state = Dever::db('shop/cart')->delete($where);
  131. } else {
  132. $where['status'] = $status;
  133. $where['num'] = $num;
  134. # 验证库存
  135. $total = Dever::load('shop/lib/info')->checkTotal($num, $info['goods_id'], $this->shop_id, $info['sku_id']);
  136. if ($where['num'] > $total) {
  137. # 库存不足
  138. $where['num'] = $total;
  139. $state = Dever::db('shop/cart')->update($where);
  140. return array('cart' => 2, 'data' => $total);
  141. }
  142. $state = Dever::db('shop/cart')->update($where);
  143. }
  144. }
  145. return array('cart' => 1, 'data' => $this->getCart());
  146. }
  147. # 确认订单页面
  148. public function confirm()
  149. {
  150. $this->data['user'] = $this->user;
  151. $this->goods();
  152. $this->coupon($this->data['price'], $this->data['method']);
  153. return $this->data;
  154. }
  155. # 优惠券
  156. public function coupon($price, $method, $type = 1)
  157. {
  158. if ($price <= 0) {
  159. Dever::alert('付款价格错误');
  160. }
  161. $user_coupon_id = Dever::input('user_coupon_id');
  162. $this->data['user_coupon_id'] = 0;
  163. $this->data['coupon_id'] = 0;
  164. $this->data['coupon_cash'] = 0;
  165. if ($type == 1) {
  166. # 查找符合要求的优惠券
  167. $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $this->uid, 'city' => $this->shop['city'], 'status' => 1, 'edate' => time()));
  168. if ($coupon) {
  169. foreach ($coupon as $k => $v) {
  170. if ($v['shop_id'] != $this->shop_id) {
  171. # 查找该券是否符合当前店铺
  172. if ($this->shop['coupon_city'] == 2) {
  173. continue;
  174. }
  175. }
  176. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  177. if ($coupon_info && ($coupon_info['method'] == $method || $coupon_info['method'] == 3)) {
  178. $kou = false;
  179. if ($coupon_info['type'] == 1) {
  180. # 满减券
  181. if ($price >= $coupon_info['total_cash']) {
  182. $kou = true;
  183. }
  184. } else {
  185. $kou = true;
  186. }
  187. if ($kou) {
  188. $coupon_info['user_coupon_id'] = $v['id'];
  189. $coupon_info['uid'] = $v['uid'];
  190. $coupon_info['shop_id'] = $v['shop_id'];
  191. $coupon_info['edate'] = date('Y-m-d', $v['edate']);
  192. $this->data['coupon'][] = $coupon_info;
  193. if (!$user_coupon_id && $this->data['coupon_cash'] <= $coupon_info['cash']) {
  194. $this->data['user_coupon_id'] = $v['id'];
  195. $this->data['coupon_id'] = $coupon_info['id'];
  196. $this->data['coupon_cash'] = $coupon_info['cash'];
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. if ($user_coupon_id) {
  204. $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $this->uid, 'id' => $user_coupon_id, 'status' => 1));
  205. if (!$coupon) {
  206. Dever::alert('优惠券不可用');
  207. }
  208. if (time() > $coupon['edate']) {
  209. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
  210. Dever::alert('优惠券已过期');
  211. }
  212. if ($coupon['shop_id'] != $this->shop_id) {
  213. if ($this->shop['coupon_city'] == 2) {
  214. Dever::alert('优惠券不可用');
  215. } else {
  216. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
  217. if (!$coupon_info) {
  218. Dever::alert('优惠券不可用');
  219. }
  220. }
  221. }
  222. $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  223. if ($goods_coupon['type'] == 2 && $this->data['price'] < $goods_coupon['total_cash']) {
  224. Dever::alert('优惠券不可用');
  225. }
  226. if ($goods_coupon['method'] != $method && $goods_coupon['method'] != 3) {
  227. Dever::alert('优惠券不可用');
  228. }
  229. $this->data['user_coupon_id'] = $user_coupon_id;
  230. $this->data['coupon_id'] = $goods_coupon['id'];
  231. $this->data['coupon_cash'] = $goods_coupon['cash'];
  232. }
  233. if (isset($this->data['coupon_cash']) && $this->data['coupon_cash'] > 0) {
  234. $this->data['price'] -= $this->data['coupon_cash'];
  235. if ($this->data['price'] < 0) {
  236. $this->data['price'] = 0;
  237. }
  238. }
  239. }
  240. # 得到商品和总价
  241. private function goods()
  242. {
  243. # 1自提,2配送
  244. $this->data['method'] = Dever::input('method', 1);
  245. $this->data['pay_method'] = Dever::input('pay_method');
  246. $card = Dever::input('card');
  247. $pwd = Dever::input('pwd');
  248. if ($this->data['pay_method'] == 3 && $card && $pwd) {
  249. $this->data['card'] = Dever::load('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd, 'status' => 1));
  250. if (!$this->data['card']) {
  251. Dever::alert('卡号/密码错误');
  252. }
  253. $goods = Dever::array_decode($this->data['card']['goods']);
  254. $goods_id = array();
  255. $num = array();
  256. $sku_id = array();
  257. foreach ($goods as $k => $v) {
  258. $goods_id[] = $v['goods_id'];
  259. $num[] = $v['num'];
  260. }
  261. } else {
  262. $goods_id = Dever::input('goods_id');
  263. if (!$goods_id) {
  264. Dever::alert('请传入商品');
  265. }
  266. $goods_id = explode(',', $goods_id);
  267. $sku_id = Dever::input('price_id');
  268. if ($sku_id) {
  269. $sku_id = explode(',', $sku_id);
  270. }
  271. $num = Dever::input('num');
  272. $num = explode(',', $num);
  273. }
  274. $this->data['price'] = 0;
  275. $this->data['num'] = 0;
  276. $this->data['name'] = '';
  277. $count = count($goods_id);
  278. # 计算总价格
  279. foreach ($goods_id as $k => $v) {
  280. $s = isset($sku_id[$k]) ? $sku_id[$k] : 0;
  281. $n = isset($num[$k]) ? $num[$k] : 1;
  282. $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s, $n);
  283. # 2是库存不足
  284. $this->data['list'][$k]['ku_state'] = 1;
  285. # 验证是否有货
  286. $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $this->shop_id, $s);
  287. if ($total <= 0) {
  288. $this->data['list'][$k]['ku_state'] = 2;
  289. }
  290. if ($this->data['list'][$k]['ku_state'] == 1) {
  291. $this->data['list'][$k]['buy_num'] = $n;
  292. $this->data['num'] += $n;
  293. $this->data['price'] += $this->data['list'][$k]['price'] * $n;
  294. if (!$this->data['name']) {
  295. if ($count > 1) {
  296. $this->data['name'] = $this->data['list'][$k]['name'] . '等' . $count . '种商品';
  297. } else {
  298. $this->data['name'] = $this->data['list'][$k]['name'];
  299. }
  300. }
  301. }
  302. }
  303. }
  304. # 开始下单
  305. public function pay()
  306. {
  307. $refer = Dever::input('refer');
  308. $cart = Dever::input('cart', 1);
  309. $parent_uid = Dever::input('parent_uid');
  310. $address_id = Dever::input('address_id');
  311. $invoice_id = Dever::input('invoice_id');
  312. $info = Dever::input('info');
  313. $this->goods();
  314. $this->coupon($this->data['price'], $this->data['method'], 2);
  315. if (!$this->data['coupon_id'] && $this->data['price'] <= 0) {
  316. Dever::alert('已售空');
  317. }
  318. if ($this->data['method'] == 2 && !$address_id) {
  319. Dever::alert('收货地址不能为空');
  320. }
  321. if (!isset($this->data['card'])) {
  322. $this->data['card'] = false;
  323. }
  324. $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);
  325. return $pay;
  326. }
  327. # 再次付款
  328. public function r_pay()
  329. {
  330. $refer = Dever::input('refer');
  331. $order_id = Dever::input('order_id');
  332. $pay = Dever::load('shop/lib/sell')->raction($order_id, $refer);
  333. return $pay;
  334. }
  335. }