Goodsbrowse.php 24 KB

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