Goodsbrowse.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class Goodsbrowse extends BaseModel
  12. {
  13. public $page_info;
  14. /**
  15. * 浏览过的商品
  16. * @access public
  17. * @author csdeshang
  18. * @param int $member_id 会员ID(一般传递session('member_id'))
  19. * @param int $shownum 查询的条数,默认0则为返回全部
  20. * @return array
  21. */
  22. public function getViewedGoodsList($member_id = 0, $shownum = 0)
  23. {
  24. $shownum = ($t = intval($shownum)) > 0 ? $t : 0;
  25. $browselist = array();
  26. //如果会员ID存在,则读取数据库浏览历史,或者memcache
  27. if ($member_id > 0) {
  28. if (!config('ds_config.cache_open')) { //查询数据库
  29. $browselist_tmp = Db::name('goodsbrowse')->where(array('member_id' => $member_id))->order('goodsbrowse_time desc')->limit($shownum)->select()->toArray();
  30. if (!empty($browselist_tmp)) {
  31. foreach ($browselist_tmp as $k => $v) {
  32. $browselist[$v['goods_id']] = $v;
  33. }
  34. }
  35. } else {
  36. //生成缓存的键值
  37. $hash_key = $member_id;
  38. //先查找$hash_key缓存
  39. $browse_goodsid = rcache($hash_key, 'goodsbrowse');
  40. $goodsid_arr = $browse_goodsid['goodsid'] ? unserialize($browse_goodsid['goodsid']) : array();
  41. unset($browse_goodsid['goodsid']);
  42. if ($goodsid_arr) {
  43. //截取需要的记录数
  44. if ($shownum) {
  45. $goodsid_arr = array_slice($goodsid_arr, -$shownum, $shownum, true);
  46. }
  47. $goodsid_arr = array_reverse($goodsid_arr, true); //翻转数组,按照添加顺序倒序排列
  48. //获得缓存的浏览商品信息
  49. $browselist_tmp = rcache($hash_key, 'goodsbrowse');
  50. foreach ($goodsid_arr as $v) {
  51. $browselist[$v] = isset($browselist_tmp[$v]) ? unserialize($browselist_tmp[$v]) : array();
  52. }
  53. }
  54. }
  55. }
  56. //查询浏览过的商品记录cookie
  57. if (!$member_id) {
  58. $browselist = array();
  59. if (cookie('viewed_goods')) {
  60. $string_viewed_goods = ds_decrypt(cookie('viewed_goods'));
  61. if (get_magic_quotes_gpc())
  62. $string_viewed_goods = stripslashes($string_viewed_goods); //去除斜杠
  63. $cookie_arr_tmp = unserialize($string_viewed_goods);
  64. $cookie_arr = array();
  65. foreach ((array) $cookie_arr_tmp as $k => $v) {
  66. $info = explode("-", $v);
  67. if (is_numeric($info[0])) {
  68. $cookie_arr[$info[0]] = intval($info[1]);
  69. }
  70. }
  71. //截取需要的记录数
  72. if ($shownum) {
  73. $cookie_arr = array_slice($cookie_arr, 0, $shownum, true);
  74. }
  75. $cookie_arr = array_reverse($cookie_arr, true); //翻转数组,按照添加顺序倒序排列
  76. if ($cookie_arr) {
  77. foreach ($cookie_arr as $k => $v) {
  78. $browselist[$k] = array('goodsbrowse_time' => $v);
  79. }
  80. }
  81. }
  82. }
  83. //查询商品数据
  84. $browselist_new = array();
  85. if ($browselist) {
  86. $goods_list_tmp = model('goods')->getGoodsList(array(array('goods_id', 'in', array_keys($browselist))), 'goods_id, goods_name, goods_advword, goods_promotion_price, goods_promotion_type, goods_marketprice, goods_image, store_id, gc_id, gc_id_1, gc_id_2, gc_id_3');
  87. $goods_list = array();
  88. foreach ((array) $goods_list_tmp as $v) {
  89. $goods_list[$v['goods_id']] = $v;
  90. }
  91. foreach ((array) $browselist as $k => $v) {
  92. if (isset($goods_list[$k])) {
  93. $browselist_new[] = array(
  94. "goods_id" => $goods_list[$k]['goods_id'],
  95. "goods_advword" => $goods_list[$k]['goods_advword'],
  96. "goodsbrowse_time" => $v['goodsbrowse_time'],
  97. "goods_name" => $goods_list[$k]['goods_name'],
  98. "goods_image" => $goods_list[$k]['goods_image'],
  99. "goods_marketprice" => $goods_list[$k]['goods_marketprice'],
  100. "goods_promotion_price" => $goods_list[$k]['goods_promotion_price'],
  101. "goods_promotion_type" => $goods_list[$k]['goods_promotion_type'],
  102. "gc_id" => $goods_list[$k]['gc_id'],
  103. "gc_id_1" => $goods_list[$k]['gc_id_1'],
  104. "gc_id_2" => $goods_list[$k]['gc_id_2'],
  105. "gc_id_3" => $goods_list[$k]['gc_id_3'],
  106. "store_id" => $goods_list[$k]['store_id']
  107. );
  108. }
  109. }
  110. }
  111. return $browselist_new;
  112. }
  113. /**
  114. * 删除浏览记录
  115. * @access public
  116. * @author csdeshang
  117. * @param array $where 检索条件
  118. * @return array
  119. */
  120. public function delGoodsbrowse($where)
  121. {
  122. return Db::name('goodsbrowse')->where($where)->delete();
  123. }
  124. /**
  125. * 添加单条浏览记录
  126. * @access public
  127. * @author csdeshang
  128. * @param array $insert_arr 插入数组数据
  129. * @return array
  130. */
  131. public function addGoodsbrowse($insert_arr)
  132. {
  133. Db::name('goodsbrowse')->insert($insert_arr);
  134. }
  135. /**
  136. * 添加多条浏览记录
  137. * @access public
  138. * @author csdeshang
  139. * @param array $insert_arr 插入数组数据
  140. * @return array
  141. */
  142. public function addGoodsbrowseAll($insert_arr)
  143. {
  144. Db::name('goodsbrowse')->insertAll($insert_arr);
  145. }
  146. /**
  147. * 查询单条浏览记录
  148. * @access public
  149. * @author csdeshang
  150. * @param type $where 查询条件
  151. * @param type $field 字段
  152. * @param type $order 排序
  153. * @return array
  154. */
  155. public function getOneGoodsbrowse($where, $field = '*', $order = '')
  156. {
  157. $result = Db::name('goodsbrowse')->field($field)->where($where)->order($order)->find();
  158. return $result;
  159. }
  160. /**
  161. * 获取浏览记录列表
  162. * @access public
  163. * @author csdeshang
  164. * @param type $where 条件
  165. * @param type $field 字段
  166. * @param type $limit 限制
  167. * @param type $order 排序
  168. * @param type $group 分组
  169. * @return array
  170. */
  171. public function getGoodsbrowseList($where, $field = '*', $limit = 0, $order = '', $group = '')
  172. {
  173. return Db::name('goodsbrowse')->field($field)->where($where)->limit($limit)->order($order)->group($group)->select()->toArray();
  174. }
  175. /**
  176. * 获取猜你喜欢商品
  177. * @access public
  178. * @author csdeshang
  179. * @param array $member_id 会员id
  180. * @param array $shownum 显示数量
  181. * @return array
  182. */
  183. public function getGuessLikeGoods($member_id = 0, $shownum = 0)
  184. {
  185. $shownum = ($t = intval($shownum)) > 0 ? $t : 0;
  186. $browseclass_arr = array(); //浏览历史商品分类数组
  187. //如果会员ID存在,则读取数据库浏览历史,或者memcache
  188. if ($member_id > 0) {
  189. if (!config('ds_config.cache_open')) { //查询数据库
  190. $browseclass_list = $this->getGoodsbrowseList(array('member_id' => $member_id), 'gc_id', 20, '', 'gc_id'); //随机抽取20条信息
  191. foreach ((array) $browseclass_list as $k => $v) {
  192. $browseclass_arr[] = $v['gc_id'];
  193. }
  194. } else {
  195. //生成缓存的键值
  196. $hash_key = $member_id;
  197. //先查找$hash_key缓存
  198. $browse_goodsid = rcache($hash_key, 'goodsbrowse');
  199. $goodsid_arr_tmp = $browse_goodsid['goodsid'] ? unserialize($browse_goodsid['goodsid']) : array();
  200. unset($browse_goodsid['goodsid']);
  201. $browseclass_arr = array();
  202. if ($goodsid_arr_tmp) {
  203. $num = count($goodsid_arr_tmp) < 20 ? count($goodsid_arr_tmp) : 20;
  204. $goodsidkey_arr = $goodsid_arr_tmp ? @array_rand($goodsid_arr_tmp, $num) : array(); //随机抽取20条信息
  205. foreach ((array) $goodsidkey_arr as $v) {
  206. $goodsid_arr[] = $goodsid_arr_tmp[$v];
  207. }
  208. unset($num);
  209. //获得缓存的浏览商品信息
  210. $browselist_tmp = rcache($hash_key, 'goodsbrowse');
  211. foreach ((array) $browselist_tmp as $k => $v) {
  212. if (in_array($k, $goodsid_arr)) {
  213. $v = unserialize($v);
  214. if (!$browseclass_arr || !in_array($v['gc_id'], $browseclass_arr)) {
  215. $browseclass_arr[] = $v['gc_id'];
  216. }
  217. }
  218. }
  219. }
  220. }
  221. }
  222. //如果会员ID不存在,则查询浏览过的商品记录cookie
  223. if (!$member_id) {
  224. $browselist = array();
  225. if (cookie('viewed_goods')) {
  226. $string_viewed_goods = ds_decrypt(cookie('viewed_goods'));
  227. if (get_magic_quotes_gpc())
  228. $string_viewed_goods = stripslashes($string_viewed_goods); //去除斜杠
  229. $cookie_arr_tmp = unserialize($string_viewed_goods);
  230. if (count($cookie_arr_tmp) >= 20) {
  231. shuffle($cookie_arr_tmp); //对数组进行随机排序
  232. $cookie_arr_tmp = array_slice($cookie_arr_tmp, 0, 20); //随机抽取20条信息
  233. }
  234. $cookie_arr = array();
  235. foreach ((array) $cookie_arr_tmp as $k => $v) {
  236. $info = explode("-", $v); //每项cookie的值为商品ID-访问时间
  237. if (is_numeric($info[0])) {
  238. $cookie_arr[$info[0]] = intval($info[1]);
  239. }
  240. }
  241. $browselist = array();
  242. if ($cookie_arr) {
  243. $viewed_list = model('goods')->getGoodsList(array(array('goods_id', 'in', array_keys($cookie_arr))), 'gc_id');
  244. foreach ((array) $viewed_list as $v) {
  245. if (!in_array($v['gc_id'], $browseclass_arr)) {
  246. $browseclass_arr[] = $v['gc_id'];
  247. }
  248. }
  249. }
  250. }
  251. }
  252. //商品分类缓存
  253. $gc_list = model('goodsclass')->getGoodsclassForCacheModel();
  254. //处理商品分类
  255. $browseclass_arrtmp = array();
  256. foreach ((array) $browseclass_arr as $k => $v) {
  257. $browseclass_arrtmp[] = $v;
  258. $browseclass_arrtmp = array_merge($browseclass_arrtmp, ($t = @$gc_list[$v]['child']) ? explode(',', $t) : array());
  259. $browseclass_arrtmp = array_merge($browseclass_arrtmp, ($t = @$gc_list[$v]['childchild']) ? explode(',', $t) : array());
  260. }
  261. $where = array();
  262. $where[] = array('gc_id', 'in', $browseclass_arrtmp);
  263. $where[] = array('boothgoods_state', '=', 1);
  264. //随机显示符合以上分类的推荐展位商品
  265. $boothgoodslist = model('pbooth')->getBoothgoodsList($where, '', 0, $shownum, '');
  266. $goodsid_arr = array();
  267. foreach ((array) $boothgoodslist as $k => $v) {
  268. $goodsid_arr[] = $v['goods_id'];
  269. }
  270. $goodslist = model('goods')->getGoodsOnlineList(array(array('goods_id', 'in', $goodsid_arr)), '*', 0, 'goods_id desc', 0, 'goods_commonid');
  271. return $goodslist;
  272. }
  273. /**
  274. * 登录之后把cookie中的浏览记录存入数据库
  275. * @access public
  276. * @author csdeshang
  277. * @param type $member_id 会员ID
  278. * @param type $store_id 店铺ID
  279. * @return type
  280. */
  281. public function mergeGoodsbrowse($member_id, $store_id = 0)
  282. {
  283. if ($member_id <= 0) {
  284. return array('state' => false, 'msg' => '参数错误');
  285. }
  286. //获取浏览历史cookie商品列表
  287. $cookie_list = $this->getViewedGoodsList();
  288. $cookie_list = array_reverse($cookie_list, true); //翻转数组,按照添加顺序排列
  289. if (empty($cookie_list)) {
  290. return array('state' => true);
  291. }
  292. $cookie_goodsid = array();
  293. foreach ((array) $cookie_list as $k => $v) {
  294. $cookie_goodsid[] = $v['goods_id'];
  295. }
  296. $goods_info = array();
  297. $result = array();
  298. //cookie中浏览记录,去除店铺自己的商品,并加入数据库
  299. if (!empty($cookie_list)) {
  300. $insert_arr = array();
  301. foreach ($cookie_list as $v) {
  302. if ($v['store_id'] != $store_id) {
  303. $tmp = array();
  304. $tmp['goods_id'] = $v['goods_id'];
  305. $tmp['member_id'] = $member_id;
  306. $tmp['goodsbrowse_time'] = $v['goodsbrowse_time'];
  307. $tmp['gc_id'] = $v['gc_id'];
  308. $tmp['gc_id_1'] = $v['gc_id_1'];
  309. $tmp['gc_id_2'] = $v['gc_id_2'];
  310. $tmp['gc_id_3'] = $v['gc_id_3'];
  311. $goods_info[$v['goods_id']] = $tmp;
  312. }
  313. }
  314. if (!empty($goods_info)) {
  315. if (!config('ds_config.cache_open')) { //存入数据库
  316. $result = $this->addViewedGoodsToDatabase(array_keys($goods_info), $member_id, 0, $goods_info);
  317. } else { //存入缓存
  318. $result = $this->addViewedGoodsToCache(array_keys($goods_info), $member_id, 0, $goods_info);
  319. }
  320. }
  321. }
  322. //最后清空浏览记录cookie
  323. cookie('viewed_goods', '', -3600);
  324. return $result;
  325. }
  326. /**
  327. * 浏览过的商品
  328. * @access public
  329. * @author csdeshang
  330. * @param type $goods_id 商品ID
  331. * @param type $member_id 会员ID(一般传递session('member_id'))
  332. * @param type $store_id 店铺ID
  333. * @return array
  334. */
  335. public function addViewedGoods($goods_id, $member_id = 0, $store_id = 0)
  336. {
  337. //未登录生成浏览过产品cookie
  338. if ($member_id <= 0) {
  339. $result = $this->addViewedGoodsToCookie($goods_id);
  340. }
  341. //登录后记录浏览历史
  342. if ($member_id > 0) {
  343. if (!config('ds_config.cache_open')) { //存入数据库
  344. $result = $this->addViewedGoodsToDatabase($goods_id, $member_id, $store_id);
  345. } else { //存入缓存
  346. $result = $this->addViewedGoodsToCache($goods_id, $member_id, $store_id);
  347. }
  348. }
  349. return $result;
  350. }
  351. /**
  352. * 浏览过的商品加入浏览历史cache
  353. * @access public
  354. * @author csdeshang
  355. * @param mixed $goods_id 商品ID或者商品ID数组
  356. * @param int $member_id 会员ID(一般传递session('member_id'))
  357. * @param int $store_id 店铺ID(一般传递session('store_id'))
  358. * @param array $goods_info 如果已经获取了商品信息则可传递至函数,避免重复查询
  359. * @return array
  360. */
  361. public function addViewedGoodsToCache($goods_id, $member_id, $store_id = 0, $goods_info = array())
  362. {
  363. if (!$goods_id || $member_id <= 0) {
  364. return array('state' => false, 'msg' => '参数错误');
  365. }
  366. $goodsbrowse_time = TIMESTAMP;
  367. if (!$goods_info) {
  368. //查询商品详细信息
  369. $goods_model = model('goods');
  370. if (is_array($goods_id)) {
  371. $goods_infotmp = $goods_model->getGoodsList(array(array('goods_id', 'in', $goods_id)), 'goods_id,gc_id,gc_id_1,gc_id_2,gc_id_3,store_id');
  372. if (!$goods_infotmp) {
  373. return array('state' => true);
  374. }
  375. foreach ($goods_infotmp as $k => $v) {
  376. if ($store_id <> $goods_infotmp['store_id']) { //店铺浏览自己的商品不加入浏览历史
  377. $goods_infotmp[$v['goods_id']] = $v;
  378. }
  379. }
  380. if (!$goods_infotmp) {
  381. return array('state' => true);
  382. }
  383. //对数组按照浏览先后进行排序
  384. foreach ($goods_id as $v) {
  385. if ($goods_infotmp[$v]) {
  386. $goods_info[$v] = $goods_infotmp[$v];
  387. }
  388. }
  389. } else {
  390. $goods_infotmp = $goods_model->getGoodsInfoByID($goods_id);
  391. if (!$goods_infotmp || $store_id == $goods_infotmp['store_id']) { //店铺浏览自己的商品不加入浏览历史
  392. return array('state' => true);
  393. }
  394. $goods_info[$goods_id] = $goods_infotmp;
  395. }
  396. }
  397. //生成缓存的键值
  398. $hash_key = $member_id;
  399. //处理浏览历史cache中商品ID
  400. $_cache = rcache($hash_key, 'goodsbrowse');
  401. $goodsid_arr = $_cache['goodsid'] ? unserialize($_cache['goodsid']) : array();
  402. if ($goodsid_arr) {
  403. //如果商品ID已经存在则先删除该ID,然后在数组末尾新增该ID
  404. $goodsid_arr = array_diff($goodsid_arr, array_keys($goods_info));
  405. array_push($goodsid_arr, implode(',', array_keys($goods_info)));
  406. } else {
  407. $goodsid_arr = array_keys($goods_info);
  408. }
  409. $_cache['goodsid'] = serialize($goodsid_arr);
  410. //处理浏览历史cache中商品详细信息
  411. foreach ($goods_info as $k => $v) {
  412. $tmp_arr = array();
  413. $tmp_arr['goods_id'] = $v['goods_id'];
  414. $tmp_arr['member_id'] = $member_id;
  415. $tmp_arr['goodsbrowse_time'] = !empty($v['goodsbrowse_time']) ? $v['goodsbrowse_time'] : $goodsbrowse_time;
  416. $tmp_arr['gc_id'] = $v['gc_id'];
  417. $tmp_arr['gc_id_1'] = $v['gc_id_1'];
  418. $tmp_arr['gc_id_2'] = $v['gc_id_2'];
  419. $tmp_arr['gc_id_3'] = $v['gc_id_3'];
  420. $_cache[$k] = serialize($tmp_arr);
  421. }
  422. //缓存商品信息
  423. wcache($hash_key, $_cache, 'goodsbrowse');
  424. return array('state' => true);
  425. }
  426. /**
  427. * 浏览过的商品加入浏览历史数据库
  428. * @access public
  429. * @author csdeshang
  430. * @param mixed $goods_id 商品ID或者商品ID数组
  431. * @param int $member_id 会员ID(一般传递session('member_id'))
  432. * @param int $store_id 店铺ID(一般传递session('store_id'))
  433. * @param array $goods_info 如果已经获取了商品信息则可传递至函数,避免重复查询
  434. * @return array
  435. */
  436. public function addViewedGoodsToDatabase($goods_id, $member_id, $store_id = 0, $goods_info = array())
  437. {
  438. if (!$goods_id || $member_id <= 0) {
  439. return array('state' => false, 'msg' => '参数错误');
  440. }
  441. $goodsbrowse_time = TIMESTAMP;
  442. if (!$goods_info) {
  443. //查询商品详细信息
  444. $goods_model = model('goods');
  445. if (is_array($goods_id)) {
  446. $goods_infotmp = $goods_model->getGoodsList(array(array('goods_id', 'in', $goods_id)), 'goods_id,gc_id,gc_id_1,gc_id_2,gc_id_3,store_id');
  447. if (!$goods_infotmp) {
  448. return array('state' => true);
  449. }
  450. foreach ($goods_infotmp as $k => $v) {
  451. //店铺浏览自己的商品不加入浏览历史
  452. if ($store_id <> $goods_infotmp['store_id']) {
  453. $goods_infotmp[$v['goods_id']] = $v;
  454. }
  455. }
  456. if (!$goods_infotmp) {
  457. return array('state' => true);
  458. }
  459. //对数组按照浏览先后进行排序
  460. foreach ($goods_id as $v) {
  461. if ($goods_infotmp[$v]) {
  462. $goods_info[$v] = $goods_infotmp[$v];
  463. }
  464. }
  465. } else {
  466. $goods_infotmp = $goods_model->getGoodsInfoByID($goods_id);
  467. if (!$goods_infotmp || $store_id == $goods_infotmp['store_id']) { //店铺浏览自己的商品不加入浏览历史
  468. return array('state' => true);
  469. }
  470. $goods_info[$goods_id] = $goods_infotmp;
  471. }
  472. }
  473. //构造新增的数组
  474. $insert_arr = array();
  475. //当天时间年月日
  476. $today = date('Y-m-d', $goodsbrowse_time);
  477. //处理浏览历史cache中商品详细信息
  478. foreach ($goods_info as $k => $v) {
  479. $tmp_arr = array();
  480. $tmp_arr['goods_id'] = $v['goods_id'];
  481. $tmp_arr['member_id'] = $member_id;
  482. $tmp_arr['goodsbrowse_time'] = isset($v['goodsbrowse_time']) ? $v['goodsbrowse_time'] : $goodsbrowse_time;
  483. $tmp_arr['gc_id'] = $v['gc_id'];
  484. $tmp_arr['gc_id_1'] = $v['gc_id_1'];
  485. $tmp_arr['gc_id_2'] = $v['gc_id_2'];
  486. $tmp_arr['gc_id_3'] = $v['gc_id_3'];
  487. $insert_arr[] = $tmp_arr;
  488. $oncondition = array();
  489. $oncondition[] = array('member_id', '=', $member_id);
  490. $oncondition[] = array('goods_id', '=', $v['goods_id']);
  491. $goodsbrowseinfo = $this->getOneGoodsbrowse($oncondition, '*', 'goodsbrowse_time desc');
  492. if (empty($goodsbrowseinfo)) {
  493. $this->addGoodsbrowseAll($insert_arr);
  494. } else {
  495. $goodsbrowseinfotime = date('Y-m-d', $goodsbrowseinfo['goodsbrowse_time']);
  496. if ($goodsbrowseinfotime !== $today) {
  497. $this->addGoodsbrowseAll($insert_arr);
  498. }
  499. }
  500. }
  501. return array('state' => true);
  502. }
  503. /**
  504. * 浏览过的商品加入浏览历史数据库
  505. * @access public
  506. * @author csdeshang
  507. * @param mixed $goods_id 商品ID或者商品ID数组
  508. * @return array
  509. */
  510. public function addViewedGoodsToCookie($goods_id)
  511. {
  512. if (!$goods_id) {
  513. return array('state' => false, 'msg' => '参数错误');
  514. }
  515. //浏览时间
  516. $goodsbrowse_time = TIMESTAMP;
  517. //构造cookie的一项值,每项cookie的值为商品ID-访问时间
  518. if (is_array($goods_id)) {
  519. $goods_idarr = $goods_id;
  520. foreach ($goods_id as $v) {
  521. $cookievalue[] = $v . '-' . $goodsbrowse_time;
  522. }
  523. } else {
  524. $cookievalue[] = $goods_id . '-' . $goodsbrowse_time;
  525. $goods_idarr[] = $goods_id;
  526. }
  527. unset($goods_id);
  528. if (cookie('viewed_goods')) { //如果cookie已经存在
  529. $string_viewed_goods = ds_decrypt(cookie('viewed_goods'));
  530. if (get_magic_quotes_gpc()) {
  531. $string_viewed_goods = stripslashes($string_viewed_goods); // 去除斜杠
  532. }
  533. $vg_ca = @unserialize($string_viewed_goods);
  534. if (!empty($vg_ca) && is_array($vg_ca)) {
  535. foreach ($vg_ca as $vk => $vv) {
  536. $vv_arr = explode('-', $vv);
  537. if (in_array($vv_arr[0], $goods_idarr)) { //如果该商品的浏览记录已经存在,则删除它
  538. unset($vg_ca[$vk]);
  539. }
  540. }
  541. } else {
  542. $vg_ca = array();
  543. }
  544. //将新浏览历史加入cookie末尾
  545. array_push($vg_ca, implode(',', $cookievalue));
  546. //cookie中最多存储50条浏览信息
  547. if (count($vg_ca) > 50) {
  548. $vg_ca = array_slice($vg_ca, -50, 50);
  549. }
  550. } else {
  551. $vg_ca = $cookievalue;
  552. }
  553. $vg_ca = ds_encrypt(serialize($vg_ca));
  554. cookie('viewed_goods', $vg_ca);
  555. }
  556. }