cart.model.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. /**
  3. * 购物车模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class cartModel extends Model
  11. {
  12. /**
  13. * 购物车商品总金额
  14. */
  15. private $cart_all_price = 0;
  16. /**
  17. * 购物车商品总数
  18. */
  19. private $cart_goods_num = 0;
  20. const db_type = 'db';
  21. const cookie_type = 'cookie';
  22. const session_type = 'Session';
  23. public function __construct()
  24. {
  25. parent::__construct('cart');
  26. }
  27. /**
  28. * 取属性值魔术方法
  29. *
  30. * @param string $name
  31. */
  32. public function __get($name)
  33. {
  34. return $this->$name;
  35. }
  36. /**
  37. * 检查购物车内商品是否存在
  38. *
  39. * @param
  40. */
  41. public function checkCart($condition = array(),$save_type = 'db')
  42. {
  43. if($save_type == self::db_type) {
  44. return $this->table('cart')->where($condition)->find();
  45. }
  46. elseif ($save_type == self::session_type)
  47. {
  48. $carts = $_SESSION['carts'];
  49. if(empty($carts) ||count($carts) == 0) {
  50. return false;
  51. }
  52. if(array_key_exists($condition['cart_id'],$carts)) {
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. }
  58. else
  59. {
  60. return false;
  61. }
  62. }
  63. /**
  64. * 取得 单条购物车信息
  65. * @param unknown $condition
  66. * @param string $field
  67. */
  68. public function getCartInfo($condition = array(), $field = '*')
  69. {
  70. if(!is_mobile() || $_SESSION['is_login'] == 1) {
  71. return $this->table('cart')->field($field)->where($condition)->find();
  72. }
  73. else
  74. {
  75. $carts = $_SESSION['carts'];
  76. if(!is_array($carts)) {
  77. return array();
  78. }
  79. if(empty($condition)) {
  80. return $carts;
  81. } else {
  82. $cart_id = $condition['cart_id'];
  83. return $carts[$cart_id]; // session 中的购物车没有buyer_id
  84. }
  85. }
  86. }
  87. /**
  88. * 将商品添加到购物车中
  89. *
  90. * @param array $data 商品数据信息
  91. * @param string $save_type 保存类型,可选值 db,cookie
  92. * @param int $quantity 购物数量
  93. */
  94. public function addCart($data = array(), $save_type = '', $quantity = null)
  95. {
  96. $method = '_addCart' . ucfirst($save_type);
  97. $insert = $this->$method($data, $quantity);
  98. //更改购物车总商品数和总金额,传递数组参数只是给DB使用
  99. $this->getCartNum($save_type, array('buyer_id' => $data['buyer_id']));
  100. return $insert;
  101. }
  102. /**
  103. * 添加数据库购物车
  104. *
  105. * @param unknown_type $goods_info
  106. * @param unknown_type $quantity
  107. * @return unknown
  108. */
  109. private function _addCartDb($goods_info = array(), $quantity)
  110. {
  111. $condition = array();
  112. $condition['goods_id'] = $goods_info['goods_id'];
  113. $condition['buyer_id'] = $goods_info['buyer_id'];
  114. if (isset($goods_info['bl_id'])) {
  115. $condition['bl_id'] = $goods_info['bl_id'];
  116. } else {
  117. $condition['bl_id'] = 0;
  118. }
  119. $check_cart = $this->checkCart($condition);
  120. if (!empty($check_cart))
  121. {
  122. $goods_model = Model("goods");
  123. $goods_storage = $goods_model->getGoodsStorageById($goods_info['goods_id']);
  124. if (intval($goods_storage) < intval($check_cart['goods_num']) + $quantity) {
  125. return false;
  126. } else {
  127. $data['goods_num'] = intval($check_cart['goods_num']) + $quantity;
  128. $ret = $this->table('cart')->where($condition)->update($data);
  129. if($ret && $this->affected_rows() > 0) {
  130. $item = $this->table('cart')->field('cart_id')->where($condition)->find();
  131. return intval($item['cart_id']);
  132. } else {
  133. return false;
  134. }
  135. }
  136. }
  137. $array = array();
  138. $array['buyer_id'] = $goods_info['buyer_id'];
  139. $array['store_id'] = $goods_info['store_id'];
  140. $array['goods_id'] = $goods_info['goods_id'];
  141. $array['goods_name'] = $goods_info['goods_name'];
  142. $array['goods_price'] = $goods_info['goods_price'];
  143. $array['goods_num'] = $quantity;
  144. $array['goods_image'] = $goods_info['goods_image'];
  145. $array['store_name'] = $goods_info['store_name'];
  146. $array['bl_id'] = isset($goods_info['bl_id']) ? $goods_info['bl_id'] : 0;
  147. return $this->insert($array);
  148. }
  149. /**
  150. * 添加到cookie购物车,最多保存5个商品
  151. *
  152. * @param unknown_type $goods_info
  153. * @param unknown_type $quantity
  154. * @return unknown
  155. */
  156. private function _addCartCookie($goods_info = array(), $quantity = null)
  157. {
  158. //去除斜杠
  159. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  160. $cart_str = base64_decode(decrypt($cart_str));
  161. $cart_array = @unserialize($cart_str);
  162. $cart_array = !is_array($cart_array) ? array() : $cart_array;
  163. if (count($cart_array) >= 5) return false;
  164. if (in_array($goods_info['goods_id'], array_keys($cart_array))) return true;
  165. $cart_array[$goods_info['goods_id']] = array(
  166. 'store_id' => $goods_info['store_id'],
  167. 'goods_id' => $goods_info['goods_id'],
  168. 'goods_name' => $goods_info['goods_name'],
  169. 'goods_price' => $goods_info['goods_price'],
  170. 'goods_image' => $goods_info['goods_image'],
  171. 'goods_num' => $quantity
  172. );
  173. setNcCookie('cart', encrypt(base64_encode(serialize($cart_array))), 24 * 3600);
  174. return true;
  175. }
  176. private function _addCartSession($goods_info = array(), $quantity = null)
  177. {
  178. $condition = array();
  179. $condition['cart_id'] = $goods_info['goods_id'];
  180. $isExist = $this->checkCart($condition,self::session_type);
  181. if ($isExist)
  182. {
  183. $goods_model = Model("goods");
  184. $goods_storage = $goods_model->getGoodsStorageById($goods_info['goods_id']);
  185. $cart_id = intval($goods_info['goods_id']);
  186. $cart = &$_SESSION['carts'][$cart_id];
  187. if (intval($goods_storage) < intval($cart['goods_num']) + $quantity) {
  188. return false;
  189. } else {
  190. $cart['goods_num'] = intval($cart['goods_num']) + $quantity;
  191. }
  192. $cart['store_id'] = $goods_info['store_id'];
  193. $cart['goods_id'] = $goods_info['goods_id'];
  194. $cart['goods_name'] = $goods_info['goods_name'];
  195. $cart['goods_price'] = $goods_info['goods_price'];
  196. $cart['goods_image'] = $goods_info['goods_image'];
  197. $cart['store_name'] = $goods_info['store_name'];
  198. $cart['bl_id'] = isset($goods_info['bl_id']) ? $goods_info['bl_id'] : 0;
  199. }
  200. else
  201. {
  202. $cart_id = intval($goods_info['goods_id']);
  203. $cart = array();
  204. $cart['store_id'] = $goods_info['store_id'];
  205. $cart['goods_id'] = $goods_info['goods_id'];
  206. $cart['goods_name'] = $goods_info['goods_name'];
  207. $cart['goods_price'] = $goods_info['goods_price'];
  208. $cart['goods_num'] = $quantity;
  209. $cart['goods_image'] = $goods_info['goods_image'];
  210. $cart['store_name'] = $goods_info['store_name'];
  211. $cart['bl_id'] = isset($goods_info['bl_id']) ? $goods_info['bl_id'] : 0;
  212. $cart['cart_id'] = $cart_id;
  213. if(!is_array($_SESSION['carts'])) {
  214. $_SESSION['carts'] = array();
  215. }
  216. $_SESSION['carts'][$cart_id] = $cart;
  217. }
  218. return $cart_id;
  219. }
  220. /**
  221. * 更新购物车
  222. *
  223. * @param array $param 商品信息
  224. */
  225. public function editCart($data, $condition)
  226. {
  227. if(!is_mobile() || $_SESSION['is_login'] == 1)
  228. {
  229. $result = $this->table('cart')->where($condition)->update($data);
  230. if ($result) {
  231. $this->getCartNum(self::db_type, array('buyer_id' => $condition['buyer_id']));
  232. }
  233. return $result;
  234. }
  235. else
  236. {
  237. if(is_array($condition) && array_key_exists('cart_id',$condition)) {
  238. $cart_id = $condition['cart_id'];
  239. }
  240. if(is_array($_SESSION['carts']) && $cart_id > 0)
  241. {
  242. $carts = &$_SESSION['carts'];
  243. $cart = &$carts[$cart_id];
  244. if(is_array($cart))
  245. {
  246. foreach($data as $key => $val) {
  247. $cart[$key] = $val;
  248. }
  249. $this->getCartNum(self::session_type);
  250. return true;
  251. }
  252. }
  253. return false;
  254. }
  255. }
  256. /**
  257. * 获取数量
  258. *
  259. * @param $condition
  260. * @param string $field
  261. * @return mixed
  262. */
  263. public function getCartListCount($condition, $field = '*')
  264. {
  265. return $this->table('cart')->where($condition)->group('')->count1($field);
  266. }
  267. /**
  268. * 获取购物车列表
  269. *
  270. */
  271. public function getCartList($condition, $page = 0)
  272. {
  273. $count = $this->getCartListCount($condition);
  274. $cart_list = array();
  275. if ($count != 0) {
  276. $cart_list = $this->table('cart')->where($condition)->page($page, $count)->order('cart_id desc')->select();
  277. }
  278. return $cart_list;
  279. }
  280. /**
  281. * 购物车列表
  282. *
  283. * @param string $type 存储类型 db,cookie
  284. * @param unknown_type $condition
  285. * @param int $limit
  286. */
  287. public function listCart($type, $condition = array(), $limit = '')
  288. {
  289. if ($type == self::db_type)
  290. {
  291. if(is_mobile()) {
  292. $cart_list = $this->table('cart')->where($condition)->limit($limit)->order('cart_id desc')->select();
  293. } else {
  294. $cart_list = $this->table('cart')->where($condition)->limit($limit)->select();
  295. }
  296. } elseif ($type == self::cookie_type) {
  297. //去除斜杠
  298. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  299. $cart_str = base64_decode(decrypt($cart_str));
  300. $cart_list = @unserialize($cart_str);
  301. } elseif ($type == self::session_type) {
  302. $cart_list = $_SESSION['carts'];
  303. }
  304. $cart_list = is_array($cart_list) ? $cart_list : array();
  305. //顺便设置购物车商品数和总金额
  306. $this->cart_goods_num = count($cart_list);
  307. $cart_all_price = 0;
  308. if (is_array($cart_list)) {
  309. foreach ($cart_list as $val) {
  310. $cart_all_price += $val['goods_price'] * $val['goods_num'];
  311. }
  312. }
  313. $this->cart_all_price = ncPriceFormat($cart_all_price);
  314. return !is_array($cart_list) ? array() : $cart_list;
  315. }
  316. /**
  317. * 删除购物车商品
  318. *
  319. * @param string $type 存储类型 db,cookie
  320. * @param unknown_type $condition
  321. */
  322. public function delCart($type, $condition = array())
  323. {
  324. if ($type == self::db_type) {
  325. $result = $this->table('cart')->where($condition)->delete();
  326. } elseif ($type == self::cookie_type) {
  327. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  328. $cart_str = base64_decode(decrypt($cart_str));
  329. $cart_array = @unserialize($cart_str);
  330. if (key_exists($condition['goods_id'], (array)$cart_array)) {
  331. unset($cart_array[$condition['goods_id']]);
  332. }
  333. setNcCookie('cart', encrypt(base64_encode(serialize($cart_array))), 24 * 3600);
  334. $result = true;
  335. } elseif ($type == self::session_type) {
  336. $cart_id = $condition['cart_id'];
  337. $carts = &$_SESSION['carts'];
  338. if(is_array($carts) && array_key_exists($cart_id,$carts)) {
  339. unset($carts[$cart_id]);
  340. $result = true;
  341. } else {
  342. $result = false;
  343. }
  344. }
  345. //重新计算购物车商品数和总金额
  346. if ($result) {
  347. $this->getCartNum($type, array('buyer_id' => $condition['buyer_id']));
  348. }
  349. return $result;
  350. }
  351. /**
  352. * 清空购物车
  353. * @param string $type 存储类型 db,cookie
  354. * @param unknown_type $condition
  355. */
  356. public function clearCart($type, $condition = array())
  357. {
  358. if ($type == self::cookie_type) {
  359. setNcCookie('cart', '', -3600);
  360. }
  361. else if ($type == self::db_type) {
  362. //数据库暂无浅清空操作
  363. }
  364. else if ($type == self::session_type)
  365. {
  366. if(is_array($_SESSION['carts'])) {
  367. unset($_SESSION['carts']);
  368. $_SESSION['carts'] = array();
  369. }
  370. }
  371. }
  372. /**
  373. * 计算购物车总商品数和总金额
  374. * @param string $type 购物车信息保存类型 db,cookie
  375. * @param array $condition 只有登录后操作购物车表时才会用到该参数
  376. */
  377. public function getCartNum($type, $condition = array())
  378. {
  379. if ($type == self::db_type)
  380. {
  381. $cart_all_price = 0;
  382. $cart_goods = $this->listCart(self::db_type, $condition);
  383. $this->cart_goods_num = count($cart_goods);
  384. if (!empty($cart_goods) && is_array($cart_goods)) {
  385. foreach ($cart_goods as $val) {
  386. $cart_all_price += $val['goods_price'] * $val['goods_num'];
  387. }
  388. }
  389. $this->cart_all_price = ncPriceFormat($cart_all_price);
  390. }
  391. elseif ($type == self::cookie_type)
  392. {
  393. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  394. $cart_str = base64_decode(decrypt($cart_str));
  395. $cart_array = @unserialize($cart_str);
  396. $cart_array = !is_array($cart_array) ? array() : $cart_array;
  397. $this->cart_goods_num = count($cart_array);
  398. $cart_all_price = 0;
  399. foreach ($cart_array as $v) {
  400. $cart_all_price += floatval($v['goods_price']) * intval($v['goods_num']);
  401. }
  402. $this->cart_all_price = $cart_all_price;
  403. @setNcCookie('cart_goods_num', $this->cart_goods_num, 2 * 3600);
  404. }
  405. else if ($type == self::session_type)
  406. {
  407. $cart_array = $_SESSION['carts'];
  408. $cart_all_price = 0;
  409. $this->cart_goods_num = count($cart_array);
  410. foreach ($cart_array as $v) {
  411. $cart_all_price += floatval($v['goods_price']) * intval($v['goods_num']);
  412. }
  413. $this->cart_all_price = $cart_all_price;
  414. }
  415. return $this->cart_goods_num;
  416. }
  417. public function getCartItemNum($type,$cart_id)
  418. {
  419. if ($type == self::db_type)
  420. {
  421. $cart_goods = $this->listCart(self::db_type, array("cart_id" => $cart_id));
  422. if(empty($cart_goods)) {
  423. return 0;
  424. } else {
  425. return intval($cart_goods[0]['goods_num']);
  426. }
  427. }
  428. elseif ($type == self::cookie_type)
  429. {
  430. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  431. $cart_str = base64_decode(decrypt($cart_str));
  432. $cart_array = @unserialize($cart_str);
  433. $cart_array = !is_array($cart_array) ? array() : $cart_array;
  434. $cart = $cart_array[$cart_id];
  435. if(empty($cart)) {
  436. return 0;
  437. } else {
  438. return intval($cart['goods_num']);
  439. }
  440. }
  441. else if ($type == self::session_type)
  442. {
  443. $cart_array = $_SESSION['carts'];
  444. $cart = $cart_array[$cart_id];
  445. if(empty($cart)) {
  446. return 0;
  447. } else {
  448. return intval($cart['goods_num']);
  449. }
  450. }
  451. }
  452. /**
  453. * 登录之后,把登录前购物车内的商品加到购物车表
  454. *
  455. */
  456. public function mergecart($member_info = array(), $store_id = null)
  457. {
  458. if (!$member_info['member_id']) return;
  459. if(is_mobile()) {
  460. $save_type = self::session_type;
  461. } else {
  462. $save_type = self::cookie_type;
  463. }
  464. $cart_new_list = $this->listCart($save_type);
  465. if (empty($cart_new_list)) return;
  466. //取出当前DB购物车已有信息
  467. $cart_cur_list = $this->listCart(self::db_type, array('buyer_id' => $member_info['member_id']));
  468. //数据库购物车已经有的商品,不再添加
  469. if (!empty($cart_cur_list) && is_array($cart_cur_list) && is_array($cart_new_list)) {
  470. foreach ($cart_new_list as $k => $v) {
  471. if (!is_numeric($k) || in_array($k, array_keys($cart_cur_list))) {
  472. unset($cart_new_list[$k]);
  473. }
  474. }
  475. }
  476. //查询在购物车中,不是店铺自己的商品,未禁售,上架,有库存的商品,并加入DB购物车
  477. $model_goods = Model('goods');
  478. $condition = array();
  479. if (!empty($_SESSION['store_id'])) {
  480. $condition['store_id'] = array('neq', $store_id);
  481. }
  482. $condition['goods_id'] = array('in', array_keys($cart_new_list));
  483. $goods_list = $model_goods->getGoodsOnlineList($condition);
  484. if (!empty($goods_list)) {
  485. foreach ($goods_list as $goods_info) {
  486. $goods_info['buyer_id'] = $member_info['member_id'];
  487. $this->addCart($goods_info, self::db_type, $cart_new_list[$goods_info['goods_id']]['goods_num']);
  488. }
  489. }
  490. //最后清空登录前购物车内容
  491. $this->clearCart($save_type);
  492. }
  493. }