Goodsbrowse.php 25 KB

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