common.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. // 全局链接的配置
  2. var btGame;
  3. ;~function(bt){
  4. // 因考虑到,以后可能拿链接的时候,可能要区分游戏
  5. // 所以,统一通过函数返回链接吧~,能更加灵活的更改逻辑了~
  6. bt.URL = {
  7. root: "http://game.ikongzhong.cn"
  8. ,getMoreGame: function(){
  9. // 点击更多
  10. bt.dc("more");
  11. return "http://game.ikongzhong.cn";
  12. }
  13. ,getConcern: function(){
  14. return "http://mp.weixin.qq.com/s?__biz=MzI4MjA2MjE0MQ==&mid=246005295&idx=1&sn=490f8141976d607ba079d48f52a3fcd7#rd";
  15. }
  16. ,appId: "" // gh_f1ed7b95f79e
  17. };
  18. // 先用着一个很搓,很有效的方式~
  19. bt.getGameId = function(){
  20. var href = location.href;
  21. href = href.slice(href.indexOf("://") + 3);
  22. var id = href.split("/")[2];
  23. return id;
  24. }
  25. // 先用着一个很搓,很有效的方式~
  26. bt.getGamePath = function(){
  27. var href = location.href;
  28. href = href.slice(0, href.lastIndexOf("/") + 1);
  29. return href;
  30. }
  31. // 调用dc的统计
  32. bt.dc = function(button){
  33. window.Dc_SetButtonClickData && Dc_SetButtonClickData(bt.getGameId(), button);
  34. }
  35. }(btGame || (btGame = {}));
  36. // 基础弹出窗口的定义
  37. var btGame;
  38. ;~function(bt){
  39. function popupBox(id, hideClass){
  40. this.elemId = id;
  41. this.hideClass = hideClass || "bt-hide";
  42. };
  43. popupBox.prototype = {
  44. beforeShow: function(){
  45. // 给别人继承的,什么都不做
  46. }
  47. ,show: function(){
  48. this.beforeShow();
  49. // 为了让动画生效,加了一个timer
  50. var that = this;
  51. setTimeout(function(){
  52. $("#" + that.elemId).removeClass(that.hideClass);
  53. }, 1);
  54. }
  55. ,hide: function(){
  56. $("#" + this.elemId).addClass(this.hideClass);
  57. }
  58. };
  59. bt.popupBox = popupBox;
  60. }(btGame || (btGame = {}));
  61. // 代理函数
  62. var btGame;
  63. ;~function(bt){
  64. bt.proxy = function(func, define){
  65. return function(){
  66. func.apply(define, arguments);
  67. }
  68. }
  69. }(btGame || (btGame = {}));
  70. // 发布者
  71. var btGame;
  72. ;~function(bt){
  73. var publisher = function(obj){
  74. this.__publisher__ = obj;
  75. };
  76. publisher.prototype = {
  77. on: function(ev, func){
  78. this.__publisher__.on(ev, bt.proxy(func, this));
  79. },
  80. fire: function(ev){
  81. this.__publisher__.trigger(ev, [].slice.call(arguments, 1));
  82. },
  83. off: function(ev, func){
  84. if(func){
  85. this.__publisher__.off(ev, bt.proxy(func, this));
  86. }else{
  87. this.__publisher__.off(ev);
  88. }
  89. }
  90. };
  91. bt.makePublisher = function(obj){
  92. var type = typeof obj;
  93. var p = new publisher($("<div></div>"));
  94. if(type == "function"){
  95. obj.prototype.__publisher__ = p.__publisher__;
  96. $.extend(obj.prototype, publisher.prototype);
  97. }else if(type == "object"){
  98. obj.__publisher__ = p.__publisher__;
  99. $.extend(obj, publisher.prototype);
  100. }
  101. }
  102. }(btGame || (btGame = {}));
  103. // 获取 body 元素
  104. var btGame;
  105. ;~function(bt){
  106. // 因为插入,大部分都是基于 body 元素的
  107. // 提供一个获取 body 对象的方法
  108. var body;
  109. function getB(){
  110. if(!body){
  111. body = document.body || document.getElementsByTagName("body")[0];
  112. }
  113. return body;
  114. }
  115. bt.getDomBody = getB;
  116. function craeteDiv(){
  117. return document.createElement("div");
  118. }
  119. bt.getNewDiv = craeteDiv;
  120. }(btGame || (btGame = {}));
  121. // 锁屏
  122. var btGame;
  123. ;~function(bt){
  124. // 指定lock的id,如果没有,则调用默认的锁屏
  125. // 当调用show的时候,再检测锁屏是否存在,不存在锁屏,则创建
  126. // 通过添加、移除 class:bt-hide,进行显示和隐藏锁屏
  127. // 锁屏的动画,是通过 class:bt-animation 实现的
  128. var defaultLockId = "bt-lock-screen";
  129. var createLock = function(id){
  130. var div = bt.getNewDiv();
  131. div.id = id;
  132. var body = bt.getDomBody();
  133. body.appendChild(div);
  134. return $(div);
  135. };
  136. var lock = function(lockId){
  137. bt.popupBox.call(this, lockId || defaultLockId);
  138. };
  139. lock.__super__ = bt.popupBox;
  140. lock.prototype = $.extend({}, bt.popupBox.prototype, {
  141. beforeShow: function(){
  142. var elem = this.getElem();
  143. if(elem.size() <= 0){
  144. elem = createLock(this.elemId);
  145. elem.addClass("bt-lock-screen bt-animation bt-hide");
  146. }
  147. }
  148. ,remove: function(){
  149. var elem = this.getElem();
  150. if(elem.size() > 0){
  151. elem.addClass("bt-hide");
  152. // 为了看到动画,延迟一下吧
  153. setTimeout(function(){
  154. elem.remove();
  155. }, 200);
  156. }
  157. }
  158. ,getElem: function(){
  159. return $("#" + this.elemId);
  160. }
  161. });
  162. bt.lockScreen = function(id){
  163. return new lock(id);
  164. }
  165. }(btGame || (btGame = {}));
  166. // 闪屏
  167. var btGame;
  168. ;~function(bt){
  169. // 百田广告
  170. // 参数:
  171. // id: 广告div的id和className
  172. // html: 广告的内容
  173. // time: 广告多少秒后消失,小于0,则不会自动移除元素
  174. var defaultOptions = {id: "bt-advertisement", html: "广告", time: 1500};
  175. var flash = function(options){
  176. var newOptions = $.extend({}, defaultOptions, options || {});
  177. var $elem = $("#" + newOptions.id);
  178. var lock = new bt.lockScreen(newOptions.lockId);
  179. if($elem.size() <= 0){
  180. var $div = $(bt.getNewDiv()).attr({id: newOptions.id}).addClass(newOptions.id);
  181. var html = newOptions.html;
  182. $div.html(html);
  183. bt.getDomBody().appendChild($div[0]);
  184. $elem = $div;
  185. }
  186. this.event = newOptions.id + "_timeup";
  187. var that = this;
  188. if(newOptions.time > 0){
  189. // 时间到了之后,会发布事件哦~
  190. // 相同事件,不会重复发布
  191. var event = this.event;
  192. this.off(event);
  193. $elem.data("timer", setTimeout(
  194. function(){
  195. $elem.remove(), lock.hide();
  196. that.fire(event);
  197. newOptions = null;
  198. this.elem = this.lock = that.show = that.hide = null;
  199. }, newOptions.time <= 0 ? 1500 : newOptions.time)
  200. );
  201. }
  202. // 外部方法
  203. this.elem = $elem;
  204. this.lock = lock;
  205. this.show = function(html){
  206. html && this.elem.html(html);
  207. this.elem.removeClass("bt-hide");
  208. this.lock.show();
  209. }
  210. this.hide = function(){
  211. this.elem.addClass("bt-hide");
  212. this.lock.hide();
  213. }
  214. this.remove = function(){
  215. this.lock.remove();
  216. this.elem.remove();
  217. }
  218. }
  219. bt.makePublisher(flash);
  220. bt.advertisement = function(options){
  221. return new flash(options);
  222. };
  223. }(btGame || (btGame = {}));
  224. // 加载中的loading
  225. var btGame;
  226. ;~function(bt){
  227. var loadingDiv = null;
  228. var loadingText = null;
  229. var loading = function(rate, error){
  230. if(rate > 0 && !loadingDiv){
  231. loadingDiv = $(btGame.getNewDiv());
  232. loadingDiv.addClass("bt-game-loading");
  233. loadingDiv.html('<table><tr><td><img class="bt-img" src="img/preloadImage.png" /><div class="bt-text"></div></td></tr></table>');
  234. bt.getDomBody().appendChild(loadingDiv[0]);
  235. loadingText = loadingDiv.find(".bt-text");
  236. }
  237. if(loadingDiv){
  238. if(error){
  239. loadingText.html(error);
  240. }else{
  241. var r = Math.round(rate * 100);
  242. loadingText.html("加载进度:" + r + "%");
  243. }
  244. }
  245. if(rate >= 1){
  246. loadingDiv && loadingDiv.remove();
  247. loadingDiv = null;
  248. }
  249. }
  250. bt.gameLoading = loading;
  251. }(btGame || (btGame = {}));
  252. // 游戏区域大小设定
  253. // btGame.resizePlayArea($elem, width, height, top, left)
  254. // $elem: jQuery元素
  255. // width: 画布期望宽度
  256. // height: 画布期望高度
  257. // top: "top", "center", "bottom" 或 px
  258. // left: "left", "center", "right" 或 px
  259. var btGame;
  260. ;~function(bt){
  261. // @width: canvas期望的宽度
  262. // @height: canvas期望的高度
  263. function rate(width, height){
  264. var wWidth = window.innerWidth,
  265. wHeight = window.innerHeight;
  266. var mid;
  267. if(width <= wWidth && height <= wHeight){
  268. // 如果在屏幕内,就不用改了
  269. }else if(width > wWidth && height > wHeight){
  270. // 如果都大于屏幕
  271. var rateW = wWidth / width, rateH = wHeight / height;
  272. // 看谁更小,就以谁作为标准
  273. if(rateW <= rateH){
  274. mid = width;
  275. width = wWidth;
  276. height = height * width / mid;
  277. }else{
  278. mid = height;
  279. height = wHeight;
  280. width = width * height / mid;
  281. }
  282. }else if(width > wWidth){
  283. // 只有宽度大于屏幕
  284. mid = width;
  285. width = wWidth;
  286. height = height * wWidth / mid;
  287. }else if(height > wHeight){
  288. // 只有高度大于屏幕
  289. mid = height;
  290. height = wHeight;
  291. width = width * wHeight / mid;
  292. }else{
  293. // 没救了
  294. }
  295. var top = (wHeight - height) / 2, left = (wWidth - width) / 2;
  296. return {
  297. width: width
  298. ,height: height
  299. ,top: top
  300. ,left: left
  301. };
  302. }
  303. function resize($elem, width, height, top, left){
  304. var result = rate(width, height);
  305. $elem.css({
  306. width: result.width
  307. ,height: result.height
  308. ,top: top == "center" ? result.top : top == "left" ? 0 : top
  309. ,left: left == "center" ? result.left : left == "left" ? 0 : left
  310. });
  311. switch(top){
  312. case "top":
  313. $elem.css({top: 0});
  314. break;
  315. case "center":
  316. $elem.css({top: result.top});
  317. break;
  318. case "bottom":
  319. $elem.css({bottom: 0});
  320. break;
  321. default:
  322. $elem.css({top: top});
  323. }
  324. switch(left){
  325. case "left":
  326. $elem.css({left: 0});
  327. break;
  328. case "center":
  329. $elem.css({left: result.left});
  330. break;
  331. case "right":
  332. $elem.css({right: 0});
  333. break;
  334. default:
  335. $elem.css({left: left});
  336. }
  337. $elem.trigger("resizePlayArea", [result]);
  338. }
  339. function bindResize($elem, width, height, top, left){
  340. bt.checkHScreen(function(){
  341. setTimeout(function(){
  342. resize($elem, width, height, top, left);
  343. }, 500);
  344. });
  345. }
  346. bt.resizePlayArea = bindResize;
  347. }(btGame || (btGame = {}));
  348. // 告诉用户收藏
  349. var btGame;
  350. ;~function(bt){
  351. function ask(cb){
  352. if(confirm('关注"全球游戏排行榜"微信,就可以收藏这个游戏哦!')){
  353. cb ? cb() : top.location.href = bt.URL.getConcern();
  354. }
  355. }
  356. bt.attentOurGame = ask;
  357. }(btGame || (btGame = {}));
  358. // 横竖屏检测
  359. var btGame;
  360. ;~function(bt){
  361. // 检测,如果是横屏:true,如果是竖屏:false
  362. // 主要看window的宽高大小,如果width > height,就是横屏,反之~
  363. // btGame.checkHScreen(callback, once);
  364. // @param callback 回调函数 callback(true) -> 横屏
  365. // @param once 是否只检测一次,默认false,一直检测
  366. var screenResize = function(cb) {
  367. // 横屏true,竖屏false
  368. cb && cb(window.innerWidth > window.innerHeight);
  369. };
  370. function check(callback, once){
  371. if(!once){
  372. window.addEventListener("orientationchange", function() {
  373. screenResize(callback);
  374. });
  375. window.addEventListener("resize", function() {
  376. screenResize(callback);
  377. });
  378. }
  379. screenResize(callback);
  380. }
  381. bt.checkHScreen = check;
  382. }(btGame || (btGame = {}));
  383. // 横、竖屏的提醒
  384. var btGame;
  385. ;~function(bt){
  386. var onlyH = function(once, callback){
  387. this.myCallback = callback;
  388. this.tipsCount = 0;
  389. bt.checkHScreen(bt.proxy(this.callback, this), false);
  390. if(once){
  391. this.once = once;
  392. }
  393. };
  394. onlyH.prototype = {
  395. hscreen: function(){
  396. // 是横屏
  397. // 设置了提醒一次,就真的只提醒一次了
  398. this.buildScreen();
  399. if(this.once && this.tipsCount <= 0){
  400. this.screen && this.screen.show();
  401. }else if(!this.once){
  402. this.screen && this.screen.show();
  403. }
  404. this.tipsCount++;
  405. }
  406. ,vscreen: function(){
  407. // 不是横屏,把提醒去掉
  408. this.screen && this.screen.hide();
  409. this.myCallback && this.myCallback(this.tipsCount);
  410. }
  411. ,getScreenOption: function(){
  412. return {
  413. id: "bt-h-scrren"
  414. ,html: "<table><tr><td><img class='bt-h-screen-img' src='img/bt-play-h-screen.png' /></td></tr></table>"
  415. ,time: 0
  416. ,lockId: 'bt-hide-lock'
  417. };
  418. }
  419. // 下面的两个,逻辑是不用重新更改的
  420. ,buildScreen: function(){
  421. !this.screen && (
  422. this.screen = btGame.advertisement(this.getScreenOption())
  423. );
  424. }
  425. ,callback: function(isHScreen){
  426. isHScreen ? this.vscreen() : this.hscreen();
  427. }
  428. };
  429. // 竖屏的检测,继承于横屏
  430. var onlyV = function(once, callback){
  431. onlyH.call(this, once, callback);
  432. }
  433. onlyV.__super__ = onlyH;
  434. onlyV.prototype = $.extend({}, onlyH.prototype, {
  435. hscreen: function(){
  436. onlyH.prototype.vscreen.call(this);
  437. }
  438. ,vscreen: function(){
  439. onlyH.prototype.hscreen.call(this);
  440. }
  441. ,getScreenOption: function(){
  442. return {
  443. id: "bt-v-scrren"
  444. ,html: "<table><tr><td><img class='bt-v-screen-img' src='img/bt-play-v-screen.png' /></td></tr></table>"
  445. ,time: 0
  446. ,lockId: 'bt-hide-lock'
  447. };
  448. }
  449. });
  450. bt.onlyHScreen = function(once, callback){
  451. return new onlyH(once, callback);
  452. };
  453. bt.onlyVScreen = function(once, callback){
  454. return new onlyV(once, callback);
  455. }
  456. }(btGame || (btGame = {}));
  457. // 豆豆游戏的品牌闪屏
  458. var btGame;
  459. ;~function(bt){
  460. // 闪屏应该处于最高层,9000
  461. var id = "bt-play-logo-adv";
  462. function ad(cb){
  463. /*
  464. var a = bt.advertisement({
  465. id: id
  466. ,html: "就是那个logo闪屏"
  467. ,time: 3000
  468. ,lockId: "bt-play-logo-adv-lock"
  469. });
  470. a.show();
  471. a.off(id + "_timeup");
  472. a.on(id + "_timeup", function(){
  473. cb && cb();
  474. a.remove();
  475. a = null;
  476. });
  477. */
  478. }
  479. bt.playLogoAdv = ad;
  480. }(btGame || (bgGame = {}));
  481. // 豆豆游戏特有的分享提醒
  482. var btGame;
  483. ;~function(bt){
  484. var id = "bt-play-share-tip";
  485. function tip(){
  486. var a = bt.advertisement({
  487. id: id
  488. ,html: "<img class='bt-play-share-tip-img' src='img/bt-play-share-tip.png' />"
  489. ,time: 0
  490. });
  491. a.show();
  492. setTimeout(function(){
  493. a.elem.on("click touchstart", function(){
  494. a.remove();
  495. a = null;
  496. });
  497. }, 500);
  498. // 点击分享
  499. bt.dc("share");
  500. }
  501. bt.playShareTip = tip;
  502. }(btGame || (btGame = {}));
  503. // 豆豆游戏的结束弹窗
  504. var btGame;
  505. ;~function(bt){
  506. /*
  507. var id = "bt-play-score-msg";
  508. function msg(html){
  509. var popup = bt.advertisement({
  510. id: id
  511. ,html:
  512. });
  513. }
  514. */
  515. // 产品同学暂时不怎么要求
  516. function msg(text){
  517. if(confirm(text)){
  518. bt.playShareTip();
  519. }
  520. }
  521. bt.playScoreMsg = msg;
  522. }(btGame || (btGame = {}));
  523. // 设置分享
  524. var btGame;
  525. ;~function(bt){
  526. var clickTime = 0;
  527. var dataForWeixin={
  528. // appId:bt.URL.appId,
  529. width:"66",
  530. src: "img/icon.png",
  531. //src: "http://game.9g.com/sjm/icon.png",
  532. url:"/",
  533. title:document.title,
  534. desc:document.title,
  535. // fakeid:"",
  536. callback:function(
  537. //这里是分享成功后的回调功能
  538. ){
  539. if(clickTime <= 0){
  540. bt.attentOurGame();
  541. }
  542. clickTime++;
  543. bt.dc("realshare"); // 真正点了分享
  544. }
  545. };
  546. window.dataForWeixin = dataForWeixin;
  547. var onBridgeReady = function(){
  548. //if(!isOurShare){return;} // 没有调用过setShare,就不需要管了
  549. //发送给朋友
  550. WeixinJSBridge.on('menu:share:appmessage', function(argv){
  551. WeixinJSBridge.invoke('sendAppMessage',{
  552. // "appid":dataForWeixin.appId,
  553. "img_url":dataForWeixin.src,
  554. "img_width":dataForWeixin.width,
  555. "img_height":dataForWeixin.width,
  556. "link":dataForWeixin.url,
  557. "desc":dataForWeixin.desc,
  558. "title":dataForWeixin.title
  559. }, function(res){(dataForWeixin.callback)();});
  560. });
  561. //发送到朋友圈
  562. WeixinJSBridge.on('menu:share:timeline', function(argv){
  563. WeixinJSBridge.invoke('shareTimeline',{
  564. "img_url":dataForWeixin.src,
  565. "img_width":dataForWeixin.width,
  566. "img_height":dataForWeixin.width,
  567. "link":dataForWeixin.url,
  568. "desc":dataForWeixin.desc,
  569. "title":dataForWeixin.title
  570. }, function(res){(dataForWeixin.callback)();});});
  571. //分享到微博
  572. WeixinJSBridge.on('menu:share:weibo', function(argv){
  573. WeixinJSBridge.invoke('shareWeibo',{
  574. "content":dataForWeixin.title,
  575. "url":dataForWeixin.url
  576. }, function(res){(dataForWeixin.callback)();});
  577. });
  578. //分享到facebook
  579. WeixinJSBridge.on('menu:share:facebook', function(argv){
  580. (dataForWeixin.callback)();
  581. WeixinJSBridge.invoke('shareFB',{
  582. "img_url":dataForWeixin.src,
  583. "img_width":dataForWeixin.width,
  584. "img_height":dataForWeixin.width,
  585. "link":dataForWeixin.url,
  586. "desc":dataForWeixin.desc,
  587. "title":dataForWeixin.title
  588. }, function(res){});
  589. });
  590. };
  591. if (typeof WeixinJSBridge == "undefined"){
  592. if(document.addEventListener){
  593. document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  594. }else if(document.attachEvent){
  595. document.attachEvent('WeixinJSBridgeReady' , onBridgeReady);
  596. document.attachEvent('onWeixinJSBridgeReady' , onBridgeReady);
  597. }
  598. }else{
  599. onBridgeReady();
  600. }
  601. //var isOurShare = false; // 只有调用过btShare的时候,需要干涉之~
  602. // 暂时只有微信的分享
  603. bt.setShare = function(option){
  604. $.extend(dataForWeixin, option || {});
  605. document.title = dataForWeixin.desc = dataForWeixin.title;
  606. //isOurShare = true;
  607. }
  608. }(btGame || (btGame = {}));