back.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //微信回退事件的监听
  2. var listenerBackHandler = {
  3. param: {
  4. isRun: false,
  5. },
  6. listenerBack: function () {
  7. var state = {
  8. title: "title",
  9. url: "#"
  10. };
  11. window.history.pushState(state, "title", "#");
  12. window.addEventListener("popstate", function (e) {
  13. if (listenerBackHandler.param.isRun) {
  14. if (window.location.pathname === '/index/book/chapter') {
  15. $.fn.cookie('back', 'read', {path: '/'});
  16. $.fn.cookie('pre_chapter_idx', window.map.chapter_idx, {path: '/'});
  17. }
  18. listenerBackHandler.param.isRun = false;
  19. cpslog([703, {}, 'back'], function () {
  20. if (window.history.length <= 2) {
  21. WeixinJSBridge.call('closeWindow');
  22. }else{
  23. window.history.go(-2);
  24. }
  25. });
  26. }
  27. },false);
  28. },
  29. initBack: function () {
  30. window.addEventListener('pageshow', function () {
  31. if ($('.return_popup_box').length > 0) {
  32. if ($.fn.cookie('back') === 'read' && !$.fn.cookie('return_close_but') && window.history.length > 1) {
  33. $.ajax({
  34. url:'/index/book/getreturnchapterbook',
  35. xhrFields: {
  36. withCredentials: true
  37. },
  38. type:'post',
  39. success:function(result){
  40. if (result.data) {
  41. $('.return_box div a').attr('href', result.data.url).attr('data-book_id', result.data.id);
  42. $('.return_tit a').text(result.data.name);
  43. $('.return_p a').text(result.data.description);
  44. $('.return_img img').attr('src', result.data.image);
  45. $('.return_popup_box').removeClass('hide');
  46. cpslog([701, {map: {pre_chapter_idx: $.fn.cookie('pre_chapter_idx')}}])
  47. }
  48. },
  49. error:function(){
  50. console.log('error');
  51. }
  52. })
  53. }else{
  54. $('.return_popup_box').addClass('hide');
  55. }
  56. }
  57. $.fn.cookie('back', '', {path: '/'});
  58. listenerBackHandler.param.isRun = false;
  59. setTimeout(function () { listenerBackHandler.param.isRun = true; }, 600); //延迟1秒 防止微信返回立即执行popstate事件
  60. listenerBackHandler.listenerBack();
  61. });
  62. }
  63. }
  64. $(function () {
  65. listenerBackHandler.initBack();
  66. $('.return_close_but').click(function () {
  67. $.fn.cookie('return_close_but', 1, {expires: 0.33, path: '/'});
  68. $('.return_popup_box').addClass('hide');
  69. });
  70. $('.url_link').click(function () {
  71. $.fn.cookie('return_close_but', 1, {expires: 0.33, path: '/'});
  72. $('.return_popup_box').addClass('hide');
  73. cpslog([702, {
  74. map: {
  75. box_book_id: commont.getQueryString('book_id'),
  76. pre_chapter_idx: $.fn.cookie('pre_chapter_idx')
  77. }
  78. }, 'return_link']);
  79. });
  80. });