cart.model.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. public function __construct()
  21. {
  22. parent::__construct('cart');
  23. }
  24. /**
  25. * 取属性值魔术方法
  26. *
  27. * @param string $name
  28. */
  29. public function __get($name)
  30. {
  31. return $this->$name;
  32. }
  33. /**
  34. * 检查购物车内商品是否存在
  35. *
  36. * @param
  37. */
  38. public function checkCart($condition = array())
  39. {
  40. return $this->where($condition)->find();
  41. }
  42. /**
  43. * 取得 单条购物车信息
  44. * @param unknown $condition
  45. * @param string $field
  46. */
  47. public function getCartInfo($condition = array(), $field = '*')
  48. {
  49. return $this->field($field)->where($condition)->find();
  50. }
  51. /**
  52. * 将商品添加到购物车中
  53. *
  54. * @param array $data 商品数据信息
  55. * @param string $save_type 保存类型,可选值 db,cookie
  56. * @param int $quantity 购物数量
  57. */
  58. public function addCart($data = array(), $save_type = '', $quantity = null)
  59. {
  60. $method = '_addCart' . ucfirst($save_type);
  61. $insert = $this->$method($data, $quantity);
  62. //更改购物车总商品数和总金额,传递数组参数只是给DB使用
  63. $this->getCartNum($save_type, array('buyer_id' => $data['buyer_id']));
  64. return $insert;
  65. }
  66. /**
  67. * 添加数据库购物车
  68. *
  69. * @param unknown_type $goods_info
  70. * @param unknown_type $quantity
  71. * @return unknown
  72. */
  73. private function _addCartDb($goods_info = array(), $quantity)
  74. {
  75. //验证购物车商品是否已经存在
  76. $condition = array();
  77. $condition['goods_id'] = $goods_info['goods_id'];
  78. $condition['buyer_id'] = $goods_info['buyer_id'];
  79. if (isset($goods_info['bl_id'])) {
  80. $condition['bl_id'] = $goods_info['bl_id'];
  81. } else {
  82. $condition['bl_id'] = 0;
  83. }
  84. $check_cart = $this->checkCart($condition);
  85. if (!empty($check_cart)) {
  86. // add by liax 20160118
  87. $goods_model = Model("goods");
  88. $goods_storage = $goods_model->getGoodsStorageById($goods_info['goods_id']);
  89. if (intval($goods_storage) < intval($check_cart['goods_num']) + $quantity) {
  90. return false;
  91. } else {
  92. // 更新数量
  93. $data['goods_num'] = intval($check_cart['goods_num']) + $quantity;
  94. $ret = $this->where($condition)->update($data);
  95. return true;
  96. }
  97. }
  98. $array = array();
  99. $array['buyer_id'] = $goods_info['buyer_id'];
  100. $array['store_id'] = $goods_info['store_id'];
  101. $array['goods_id'] = $goods_info['goods_id'];
  102. $array['goods_name'] = $goods_info['goods_name'];
  103. $array['goods_price'] = $goods_info['goods_price'];
  104. $array['goods_num'] = $quantity;
  105. $array['goods_image'] = $goods_info['goods_image'];
  106. $array['store_name'] = $goods_info['store_name'];
  107. $array['bl_id'] = isset($goods_info['bl_id']) ? $goods_info['bl_id'] : 0;
  108. return $this->insert($array);
  109. }
  110. /**
  111. * 添加到cookie购物车,最多保存5个商品
  112. *
  113. * @param unknown_type $goods_info
  114. * @param unknown_type $quantity
  115. * @return unknown
  116. */
  117. private function _addCartCookie($goods_info = array(), $quantity = null)
  118. {
  119. //去除斜杠
  120. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  121. $cart_str = base64_decode(decrypt($cart_str));
  122. $cart_array = @unserialize($cart_str);
  123. $cart_array = !is_array($cart_array) ? array() : $cart_array;
  124. if (count($cart_array) >= 5) return false;
  125. if (in_array($goods_info['goods_id'], array_keys($cart_array))) return true;
  126. $cart_array[$goods_info['goods_id']] = array(
  127. 'store_id' => $goods_info['store_id'],
  128. 'goods_id' => $goods_info['goods_id'],
  129. 'goods_name' => $goods_info['goods_name'],
  130. 'goods_price' => $goods_info['goods_price'],
  131. 'goods_image' => $goods_info['goods_image'],
  132. 'goods_num' => $quantity
  133. );
  134. setNcCookie('cart', encrypt(base64_encode(serialize($cart_array))), 24 * 3600);
  135. return true;
  136. }
  137. /**
  138. * 更新购物车
  139. *
  140. * @param array $param 商品信息
  141. */
  142. public function editCart($data, $condition)
  143. {
  144. $result = $this->where($condition)->update($data);
  145. if ($result) {
  146. $this->getCartNum('db', array('buyer_id' => $condition['buyer_id']));
  147. }
  148. return $result;
  149. }
  150. /**
  151. * 获取数量
  152. *
  153. * @param $condition
  154. * @param string $field
  155. * @return mixed
  156. */
  157. public function getCartListCount($condition, $field = '*')
  158. {
  159. return $this->table('cart')->where($condition)->group('')->count1($field);
  160. }
  161. /**
  162. * 获取购物车列表
  163. *
  164. */
  165. public function getCartList($condition, $page = 0)
  166. {
  167. $count = $this->getCartListCount($condition);
  168. $cart_list = array();
  169. if ($count != 0) {
  170. $cart_list = $this->table('cart')->where($condition)->page($page, $count)->order('cart_id desc')->select();
  171. }
  172. return $cart_list;
  173. }
  174. /**
  175. * 购物车列表
  176. *
  177. * @param string $type 存储类型 db,cookie
  178. * @param unknown_type $condition
  179. * @param int $limit
  180. */
  181. public function listCart($type, $condition = array(), $limit = '')
  182. {
  183. if ($type == 'db') {
  184. $cart_list = $this->where($condition)->limit($limit)->select();
  185. } elseif ($type == 'cookie') {
  186. //去除斜杠
  187. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  188. $cart_str = base64_decode(decrypt($cart_str));
  189. $cart_list = @unserialize($cart_str);
  190. }
  191. $cart_list = is_array($cart_list) ? $cart_list : array();
  192. //顺便设置购物车商品数和总金额
  193. $this->cart_goods_num = count($cart_list);
  194. $cart_all_price = 0;
  195. if (is_array($cart_list)) {
  196. foreach ($cart_list as $val) {
  197. $cart_all_price += $val['goods_price'] * $val['goods_num'];
  198. }
  199. }
  200. $this->cart_all_price = ncPriceFormat($cart_all_price);
  201. return !is_array($cart_list) ? array() : $cart_list;
  202. }
  203. /**
  204. * 删除购物车商品
  205. *
  206. * @param string $type 存储类型 db,cookie
  207. * @param unknown_type $condition
  208. */
  209. public function delCart($type, $condition = array())
  210. {
  211. if ($type == 'db') {
  212. $result = $this->where($condition)->delete();
  213. } elseif ($type == 'cookie') {
  214. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  215. $cart_str = base64_decode(decrypt($cart_str));
  216. $cart_array = @unserialize($cart_str);
  217. if (key_exists($condition['goods_id'], (array)$cart_array)) {
  218. unset($cart_array[$condition['goods_id']]);
  219. }
  220. setNcCookie('cart', encrypt(base64_encode(serialize($cart_array))), 24 * 3600);
  221. $result = true;
  222. }
  223. //重新计算购物车商品数和总金额
  224. if ($result) {
  225. $this->getCartNum($type, array('buyer_id' => $condition['buyer_id']));
  226. }
  227. return $result;
  228. }
  229. /**
  230. * 清空购物车
  231. *
  232. * @param string $type 存储类型 db,cookie
  233. * @param unknown_type $condition
  234. */
  235. public function clearCart($type, $condition = array())
  236. {
  237. if ($type == 'cookie') {
  238. setNcCookie('cart', '', -3600);
  239. } else if ($type == 'db') {
  240. //数据库暂无浅清空操作
  241. }
  242. }
  243. /**
  244. * 计算购物车总商品数和总金额
  245. * @param string $type 购物车信息保存类型 db,cookie
  246. * @param array $condition 只有登录后操作购物车表时才会用到该参数
  247. */
  248. public function getCartNum($type, $condition = array())
  249. {
  250. if ($type == 'db') {
  251. $cart_all_price = 0;
  252. $cart_goods = $this->listCart('db', $condition);
  253. $this->cart_goods_num = count($cart_goods);
  254. if (!empty($cart_goods) && is_array($cart_goods)) {
  255. foreach ($cart_goods as $val) {
  256. $cart_all_price += $val['goods_price'] * $val['goods_num'];
  257. }
  258. }
  259. $this->cart_all_price = ncPriceFormat($cart_all_price);
  260. } elseif ($type == 'cookie') {
  261. $cart_str = get_magic_quotes_gpc() ? stripslashes(cookie('cart')) : cookie('cart');
  262. $cart_str = base64_decode(decrypt($cart_str));
  263. $cart_array = @unserialize($cart_str);
  264. $cart_array = !is_array($cart_array) ? array() : $cart_array;
  265. $this->cart_goods_num = count($cart_array);
  266. $cart_all_price = 0;
  267. foreach ($cart_array as $v) {
  268. $cart_all_price += floatval($v['goods_price']) * intval($v['goods_num']);
  269. }
  270. $this->cart_all_price = $cart_all_price;
  271. }
  272. @setNcCookie('cart_goods_num', $this->cart_goods_num, 2 * 3600);
  273. return $this->cart_goods_num;
  274. }
  275. /**
  276. * 登录之后,把登录前购物车内的商品加到购物车表
  277. *
  278. */
  279. public function mergecart($member_info = array(), $store_id = null)
  280. {
  281. if (!$member_info['member_id']) return;
  282. // $save_type = C('cache.type') != 'file' ? 'cache' : 'cookie';
  283. $save_type = 'cookie';
  284. $cart_new_list = $this->listCart($save_type);
  285. if (empty($cart_new_list)) return;
  286. //取出当前DB购物车已有信息
  287. $cart_cur_list = $this->listCart('db', array('buyer_id' => $member_info['member_id']));
  288. //数据库购物车已经有的商品,不再添加
  289. if (!empty($cart_cur_list) && is_array($cart_cur_list) && is_array($cart_new_list)) {
  290. foreach ($cart_new_list as $k => $v) {
  291. if (!is_numeric($k) || in_array($k, array_keys($cart_cur_list))) {
  292. unset($cart_new_list[$k]);
  293. }
  294. }
  295. }
  296. //查询在购物车中,不是店铺自己的商品,未禁售,上架,有库存的商品,并加入DB购物车
  297. $model_goods = Model('goods');
  298. $condition = array();
  299. if (!empty($_SESSION['store_id'])) {
  300. $condition['store_id'] = array('neq', $store_id);
  301. }
  302. $condition['goods_id'] = array('in', array_keys($cart_new_list));
  303. $goods_list = $model_goods->getGoodsOnlineList($condition);
  304. if (!empty($goods_list)) {
  305. foreach ($goods_list as $goods_info) {
  306. $goods_info['buyer_id'] = $member_info['member_id'];
  307. $this->addCart($goods_info, 'db', $cart_new_list[$goods_info['goods_id']]['goods_num']);
  308. }
  309. }
  310. //最后清空登录前购物车内容
  311. $this->clearCart($save_type);
  312. }
  313. }