Goodsbrowse.php 24 KB

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