gameshare.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*15175游戏基础代码 */
  2. (function(window){
  3. if(!window.LBGAME) window.LBGAME={};
  4. LBGAME.Ua=function(){
  5. var ua=window.navigator.userAgent,type="pc";
  6. if(/MicroMessenger/.test(ua)) type="weixin";
  7. if(/CM/.test(ua)) type="cm";
  8. if(/(Gkuwan)/gi.test(ua)) type="kuwan";
  9. return type;
  10. };
  11. LBGAME.Share={
  12. shareData:{},
  13. updateData:function(obj){
  14. this.shareData=obj;
  15. switch(LBGAME.Ua()){
  16. case "weixin":
  17. if(!obj.call) this.weixinFun(obj);
  18. break;
  19. case "cm":
  20. if(!window.android) return
  21. shareFun(window.android);
  22. break;
  23. case "kuwan":
  24. if(!window.updateclient) return
  25. shareFun(window.updateclient);
  26. break;
  27. default :
  28. }
  29. //if(obj.call) LBGAME.Share.show();
  30. if(obj.fun) obj.fun();
  31. function shareFun(d){
  32. //更新分享数据
  33. d.updatesharedata(obj.title,obj.imgUrl, obj.cont, obj.url);
  34. //调起客户端分享
  35. if(obj.call) d.sharescore();
  36. }
  37. },
  38. getJson : function(a) {
  39. var b = new XMLHttpRequest();
  40. b.onreadystatechange = function() {
  41. if (b.readyState == 4 && b.status == 200) {
  42. a.callback(b.responseText)
  43. }
  44. };
  45. b.open("get", a.url);
  46. b.send(null)
  47. },
  48. setShareUrl : function(setfun){
  49. this.getJson({url: "./jumpCloud.json?t=" + new Date().getTime(),
  50. callback: function(b) {
  51. var a = JSON.parse(b);
  52. /*check weixin jump*/
  53. var host = window.location.href;
  54. try{
  55. var gameType = host.match(/liebao\.cn\/game\/(.*)\//)[1];
  56. }catch(e){
  57. return
  58. }
  59. if( /liebao\.cn\//.test(host) && LBGAME.Ua() == 'weixin'){
  60. window.location.href = "http://q" + (Math.floor(Math.random() * 5000)) + "." + a.jumphost + "/game/"+gameType+"/?t=" + (new Date()).valueOf();
  61. }
  62. /*check weixin jump*/
  63. var link = "http://q" + (Math.floor(Math.random() * 5000)) + "." + a.jumphost + "/game/"+gameType+"/?t=" + (new Date()).valueOf();
  64. setfun(link);
  65. }
  66. });
  67. },
  68. weixinFun:function(){
  69. document.addEventListener("WeixinJSBridgeReady", function a() {
  70. WeixinJSBridge.on("menu:share:appmessage", function(c) {
  71. WeixinJSBridge.invoke("sendAppMessage", {
  72. img_url: LBGAME.Share.shareData.imgUrl,
  73. link:LBGAME.Share.shareData.url,
  74. desc:LBGAME.Share.shareData.cont,
  75. title:LBGAME.Share.shareData.title
  76. }, function(d) {
  77. // if(_hmt){
  78. // _hmt.push(["_trackEvent", "WXShareButton", "gameShareWXFriend"]);
  79. // }
  80. if(LBGAME.Share.shareData.doneJump == "" ) return
  81. document.location.href = LBGAME.Share.shareData.doneJump
  82. })
  83. });
  84. WeixinJSBridge.on("menu:share:timeline", function(c) {
  85. WeixinJSBridge.invoke("shareTimeline", {
  86. img_url: LBGAME.Share.shareData.imgUrl,
  87. img_width: "300",
  88. img_height: "300",
  89. link:LBGAME.Share.shareData.url,
  90. desc:LBGAME.Share.shareData.cont,
  91. title:LBGAME.Share.shareData.title
  92. }, function(d) {
  93. // if(_hmt){
  94. // _hmt.push(["_trackEvent", "WXShareButton", "gameShareWX"]);
  95. // }
  96. if(LBGAME.Share.shareData.doneJump == "" ) return
  97. document.location.href = LBGAME.Share.shareData.doneJump
  98. })
  99. });
  100. WeixinJSBridge.on("menu:share:weibo", function(c) {
  101. WeixinJSBridge.invoke("shareWeibo", {
  102. content: LBGAME.Share.shareData.cont,
  103. url: LBGAME.Share.shareData.url
  104. }, function(d) {
  105. // if(_hmt){
  106. // _hmt.push(["_trackEvent", "WXShareButton", "gameShareWXweibo"]);
  107. // }
  108. if(LBGAME.Share.shareData.doneJump == "" ) return
  109. document.location.href = LBGAME.Share.shareData.doneJump
  110. })
  111. })
  112. }, false);
  113. },
  114. createTip:function(){
  115. var tip=document.createElement("div");
  116. tip.id="LBGAME_share";
  117. tip.setAttribute("style","position: fixed;top: 0px;left: 0px;width: 100%;height: 100%;z-index: 100;display: block;");
  118. if(LBGAME.Ua=="pc"){
  119. tip.style.background="url(/game/iq_test/images/share_tip_2.png?v=1) center bottom rgba(0,0,0,.6) no-repeat";
  120. }else{
  121. tip.style.background="url('/game/iq_test/images/share_tip.png?v=1') 50% 0 rgba(0,0,0,.6) no-repeat";
  122. }
  123. tip.addEventListener("click",function(){
  124. tip.style.display="none";
  125. },false);
  126. document.getElementsByTagName("body")[0].appendChild(tip);
  127. },
  128. show:function(){
  129. var s=document.getElementById("LBGAME_share");
  130. if(!s) LBGAME.Share.createTip();
  131. else{s.style.display="block"}
  132. },
  133. hide:function(){
  134. var s=document.getElementById("LBGAME_share");
  135. if(s) s.style.display="none";
  136. }
  137. };
  138. })(window);
  139. /*
  140. LBGAME.Share.updateData({
  141. call:false,// false只更新数据||true更新数据+调启用户端分享
  142. title:"title",
  143. imgUrl:"imgUrl",
  144. cont:"cont",
  145. url:"url",
  146. doneJump:"doneJump",
  147. fun:"" // 分享时回调,不 fun:function(){}
  148. });
  149. */