main.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import $ from 'jquery';
  2. import Vue from 'vue';
  3. import MintUI from 'mint-ui'
  4. import 'mint-ui/lib/style.css'
  5. // import Vuex from 'vuex';
  6. import App from './components/app.vue';
  7. import 'rem';
  8. import lib from 'lib';
  9. // import User from './store/user';
  10. // Vue.use(Vuex);
  11. // const store = new Vuex.Store({
  12. // mutations: {},
  13. // actions: {},
  14. // getters: {},
  15. // modules: {
  16. // }
  17. // })
  18. import './sass/global.scss';
  19. Vue.use(MintUI)
  20. Vue.component('loading', resolve => require(['./components/loading.vue'], resolve));
  21. Vue.component('test-links', resolve => require(['./page/test/links.vue'], resolve)); //测试App Link
  22. Vue.component('taptapstar', resolve => require(['./page/taptapstar/index.vue'], resolve)); //点点大明星
  23. Vue.component('yahtzee', resolve => require(['./page/yahtzee/index.vue'], resolve)); //快艇骰子
  24. var pages = {};
  25. var oldPage;
  26. var BDY = {};
  27. if ('createTouch' in document) {
  28. BDY.click = 'touchclick';
  29. BDY.touchstart = "touchstart";
  30. BDY.touchmove = "touchmove";
  31. BDY.touchend = "touchend";
  32. BDY.longTap = 'longTap';
  33. } else {
  34. BDY.click = 'click';
  35. BDY.touchstart = "mousedown";
  36. BDY.touchmove = "mousemove";
  37. BDY.touchend = "mouseup";
  38. BDY.longTap = 'hover';
  39. }
  40. var VM = new Vue({
  41. // store,
  42. el: "#app",
  43. data: {
  44. currentView: "",
  45. isForward: true,
  46. isInit: true,
  47. transitionName: "fade"
  48. },
  49. computed: {
  50. transitionMode () {
  51. return this.isForward ? "out-int" : "in-out";
  52. }
  53. },
  54. mounted() {
  55. var _self = this;
  56. var page = lib.getParam("page") || "taptapstar";
  57. lib.setParam("page", page);
  58. // _self._getWxSign();
  59. _self._redirectPage(page);
  60. // 点击事件
  61. $('body').off(BDY.click + ".data-href").on(BDY.click + ".data-href", '[data-href]', function () {
  62. var $this = $(this);
  63. var href = $this.attr('data-href') || $this.attr('href');
  64. var clientParam = $this.attr('data-client');
  65. if (clientParam != null) {
  66. var title = $this.attr('data-title');
  67. _self.redirectClient(href, clientParam, title);
  68. } else {
  69. _self.redirect(href);
  70. }
  71. return false;
  72. });
  73. $('body').off(BDY.click + ".data-url").on(BDY.click + ".data-url", '[data-url]', function () {
  74. var $this = $(this);
  75. var href = $this.attr('data-url');
  76. if (window.Wjy) {
  77. if (href.indexOf('http') !== 0) {
  78. href = location.protocol + '//' + location.host + href;
  79. }
  80. var title = $this.attr('data-title');
  81. } else {
  82. location.href = href;
  83. }
  84. return false;
  85. });
  86. window.onpopstate = function (event) {
  87. // 支持onpopstate
  88. var count = 0;
  89. for (var page in pages) {
  90. if (count++ > 1) {
  91. break;
  92. };
  93. }
  94. // 防止首次进入就触发onpopstate导致再次刷新
  95. if (count > 1) {
  96. var newPage = lib.getParam('page') || 'taptapstar';
  97. _self._redirectPage(newPage);
  98. }
  99. }
  100. initActiveEvent();
  101. },
  102. methods: {
  103. afterEnter: function () {
  104. if (VM.isForward) {
  105. scrollTo(0, 0);
  106. } else {
  107. var scrollTop = pages[lib.getParam("page")]['scrollTop'] || 0;
  108. scrollTo(0, scrollTop);
  109. }
  110. VM.isForward = false;
  111. // NProgress.done();
  112. },
  113. _getWxSign: function(){
  114. if (!lib.checkWXAgent()) return;
  115. $.ajax({
  116. url : "//h5game.duowan.com/weixin/getJsSign",
  117. type : "GET",
  118. dataType: "jsonp",
  119. data : {
  120. url : location.href,
  121. appid : 22
  122. },
  123. success: function(ret){
  124. ret.data.debug = false;
  125. ret.data.jsApiList = [
  126. "onMenuShareTimeline",
  127. "onMenuShareAppMessage"
  128. ];
  129. wx.config(ret.data);
  130. }
  131. });
  132. },
  133. _redirectPage(_page) {
  134. var _self = this;
  135. /** 当前页面不重复请求 */
  136. if (_self.currentView == _page) {
  137. return;
  138. }
  139. pages[_page] = pages[_page] || {};
  140. // NProgress.start();
  141. if (oldPage) {
  142. pages[oldPage] = pages[oldPage] || {};
  143. //保存滚动条位置$pageswarp
  144. // pages[oldPage]['scrollTop'] = document.body.scrollTop;
  145. //console.log("oldPage:" + oldPage + ", scrollTop:" + pages[oldPage]['scrollTop']);
  146. // pages[oldPage]['height'] = $pageswarp.height();
  147. lib.clearRes();
  148. }
  149. _self.currentView = _page;
  150. $(document).trigger("pageChange", _page);
  151. oldPage = _page;
  152. },
  153. redirect(param) {
  154. this._redirect(param, true);
  155. },
  156. _redirect(param, causeHistory) {
  157. oldPage = this.currentView;
  158. pages[oldPage] = pages[oldPage] || {};
  159. pages[oldPage]['scrollTop'] = _getScrollTop();
  160. var values = lib.parseHash(param);
  161. this.isForward = true;
  162. lib.setParam(values, causeHistory);
  163. // 这里需要自己渲染
  164. this._redirectPage(values['page']);
  165. },
  166. redirect2(param) {
  167. var oldUrl = document.location.href;
  168. this._redirect(param, false);
  169. if (oldUrl != document.location.href) {
  170. var newPage = lib.getParam('page') || 'taptapstar';
  171. this._redirectPage(newPage);
  172. }
  173. },
  174. redirectClient(param, clientParam, title) {
  175. var _self = this;
  176. oldPage = this.currentView;
  177. var values = lib.parseHash(param);
  178. if (window.Wjy) {
  179. var search = document.location.search;
  180. search = lib.setParam2(values, search);
  181. if (clientParam) {
  182. values = lib.parseHash(clientParam);
  183. search = lib.setParam2(values, search);
  184. }
  185. } else {
  186. _self.redirect(param);
  187. }
  188. },
  189. historyBack() {
  190. var curPage = this.currentView;
  191. pages[curPage] = pages[curPage] || {};
  192. pages[curPage]['scrollTop'] = 0;
  193. history.back();
  194. }
  195. }
  196. })
  197. function initActiveEvent() {
  198. var selector = "input[type='submit'], [data-active]";
  199. var $elem, $activeElem;
  200. var timeHandler = 0;
  201. var LONG_TAP_TIME = 500;
  202. $(document).off(BDY.touchstart + ".body").on(BDY.touchstart + ".body", function (e) {
  203. $elem = $(e.target);
  204. $activeElem = $elem;
  205. if (!$activeElem.filter(selector).length) {
  206. $activeElem = $activeElem.parents(selector);
  207. }
  208. var activeStyle = $activeElem.attr("data-active") || "active";
  209. $activeElem.addClass(activeStyle);
  210. timeHandler = lib.setTimeout(function () {
  211. $elem && $elem.trigger('longTap');
  212. _removeActive();
  213. }, LONG_TAP_TIME);
  214. });
  215. $(document).off("touchmove.body, scroll.body").on("touchmove.body, scroll.body", function (event) {
  216. clearTimeout(timeHandler);
  217. timeHandler = 0;
  218. _removeActive();
  219. $elem = null;
  220. });
  221. $(document).off("mouseout.body").on("mouseout.body", selector, function (event) {
  222. _removeActive();
  223. $elem = null;
  224. });
  225. window.onResume = document.createEvent('Event');
  226. window.onResume.initEvent('onResume', true, true);
  227. $(document).on('onResume', function () {
  228. console.log('onResume');
  229. });
  230. var lastTime = 0;
  231. $(document).off(BDY.touchend + ".body").on(BDY.touchend + ".body", function (e) {
  232. clearTimeout(timeHandler);
  233. timeHandler = 0;
  234. if ($elem) {
  235. var currentTime = (new Date()).getTime();
  236. var duration = currentTime - lastTime;
  237. var MIN_TRIGGER_INTERVAL = 300;
  238. if (duration > MIN_TRIGGER_INTERVAL) {
  239. $elem.trigger('touchclick');
  240. // 记录当前时间
  241. lastTime = currentTime;
  242. }
  243. }
  244. _removeActive();
  245. });
  246. function _removeActive() {
  247. if ($activeElem != null) {
  248. var activeStyle = $activeElem.attr("data-active") || "active";
  249. $activeElem.blur();
  250. $activeElem.removeClass(activeStyle);
  251. $activeElem = null;
  252. }
  253. $elem = null;
  254. }
  255. ['longTap', 'touchclick'].forEach(function (m) {
  256. $.fn[m] = function (callback) {
  257. return this.on(m, callback);
  258. };
  259. });
  260. }
  261. function _getScrollTop() {
  262. return document.documentElement.scrollTop || document.body && document.body.scrollTop;
  263. }