EgretShare.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * Created by saco on 14-9-26
  3. */
  4. var EgretShare = {
  5. ShareInfo:{
  6. "appId":"",
  7. "title":"",
  8. "desc":"",
  9. "imgUrl":"",
  10. "link":""
  11. },
  12. setShareData:function(title, content, link, ico){
  13. EgretShare.ShareInfo.title = title;
  14. EgretShare.ShareInfo.desc = content;
  15. EgretShare.ShareInfo.link = link;
  16. EgretShare.ShareInfo.imgUrl = ico;
  17. EgretShare.weixinShareGetReady();
  18. },
  19. /*
  20. * 单独设置分享数据
  21. * */
  22. setShareTitle:function(title){
  23. EgretShare.ShareInfo.title = title;
  24. EgretShare.weixinShareGetReady();
  25. },
  26. setShareContent:function (content){
  27. EgretShare.ShareInfo.desc = content;
  28. EgretShare.weixinShareGetReady();
  29. },
  30. setShareLink:function (link){
  31. EgretShare.ShareInfo.link = link;
  32. EgretShare.weixinShareGetReady();
  33. },
  34. setShareIco:function (ico){
  35. EgretShare.ShareInfo.imgUrl = ico;
  36. EgretShare.weixinShareGetReady();
  37. },
  38. weixinShareGetReady:function (){
  39. if (window.hasOwnProperty("WeixinApi")) {
  40. WeixinApi.ready(function (api) {
  41. api.shareToFriend(EgretShare.ShareInfo);
  42. api.shareToTimeline(EgretShare.ShareInfo);
  43. });
  44. }
  45. },
  46. moreGame:function () {
  47. var ua = window.navigator.userAgent;
  48. if (ua.indexOf("EgretRuntime") != -1 && ua.indexOf("yoyo") != -1) {
  49. location.href = "u9time://gamelist";
  50. }else if(EgretShare.isInHaima()){
  51. location.href = "haima://GoFrontPage";
  52. }else {
  53. var appId = EgretShare.findLocationProperty("app_id");
  54. window.open("http://games.egret-labs.org/more.php?app_id=" + appId, "_self");
  55. }
  56. },
  57. share:function (){
  58. if(EgretShare.isInU9())
  59. EgretShare.shareToU9();
  60. else if(EgretShare.isInWeChat())
  61. EgretShare.showShareImg();
  62. else if(EgretShare.isInHaima())
  63. EgretShare.shareToHaima();
  64. else if(EgretShare.isInU9Web())
  65. EgretShare.shareToU9Web();
  66. else
  67. EgretShare.showShareImg();
  68. },
  69. showShareImg:function(){
  70. if(document.getElementById("shareImg")){
  71. document.getElementById("shareImg").style.display = "";
  72. }else
  73. {
  74. var shareDiv = document.createElement("div");
  75. shareDiv.id = "shareImg";
  76. shareDiv.style.display = "";
  77. shareDiv.style.zIndex = 9999;
  78. var s = "<img width='100%' height='100%' src='http://static.egret-labs.org/h5game/shareUtils/share.png' style='position: fixed; z-index: 9999; top: 0; left: 0;'";
  79. s += "onClick=\"document.getElementById('shareImg').style.display='none'\" />";
  80. shareDiv.innerHTML = s;
  81. document.body.appendChild(shareDiv);
  82. }
  83. },
  84. showShareDiv:function(){
  85. if(document.getElementById("shareDiv")){
  86. document.getElementById("shareDiv").style.display = "";
  87. }else
  88. {
  89. var shareDiv = document.createElement("div");
  90. shareDiv.id = "shareDiv";
  91. shareDiv.style.display = "";
  92. shareDiv.style.zIndex = 9999;
  93. // var s = "<img width='100%' height='100%' src='http://static.egret-labs.org/h5game/shareUtils/share.png' style='position: fixed; z-index: 9999; top: 0; left: 0;'";
  94. // s += "onClick=\"document.getElementById('shareDiv').style.display='none'\" />";
  95. // shareDiv.innerHTML = s;
  96. document.body.appendChild(shareDiv);
  97. }
  98. },
  99. shareToU9:function () {
  100. var url = location.href;
  101. if (location.search == "") {
  102. url += "?channel=weixin";
  103. } else {
  104. url += "&channel=weixin";
  105. }
  106. url = encodeURIComponent(url);
  107. var a = "123";
  108. var msg = encodeURIComponent(EgretShare.ShareInfo.desc);
  109. var uid = EgretShare.getUid();
  110. var link = "u9time://share?" + "uid=" + uid + "&game_url=" + url + "&a=" + a + "&msg=" + msg;
  111. if (!uid) {
  112. link = "u9time://share?" + "&game_url=" + url + "&a=" + a + "&msg=" + msg;
  113. }
  114. location.href = link;
  115. },
  116. shareToHaima:function () {
  117. var url = location.href;
  118. url = encodeURIComponent(url);
  119. var msg = encodeURIComponent(EgretShare.ShareInfo.desc);
  120. var link = "haima://sharecustom?imgurl=" + EgretShare.ShareInfo.imgUrl+ "&sharetitle=" + EgretShare.ShareInfo.title + "&shareurl=" + url + "&sharetext=" + msg;
  121. location.href = link;
  122. },
  123. shareToU9Web:function () {
  124. EgretShare.showShareDiv();
  125. var share_data={
  126. "title":EgretShare.ShareInfo.title,
  127. "content":EgretShare.ShareInfo.desc,
  128. "link":location.href,
  129. "imgUrl":EgretShare.ShareInfo.imgUrl,
  130. };
  131. GS.share("shareDiv",share_data);
  132. },
  133. isInHaima:function () {
  134. if (EgretShare.findLocationProperty("app_id") == 1009) {
  135. return true;
  136. }
  137. return false;
  138. },
  139. isInWeChat:function () {
  140. if (window.hasOwnProperty("egret_native")) {
  141. return false;
  142. } else {
  143. var ua = window.navigator.userAgent;
  144. return ua.indexOf("MicroMessenger") != -1;
  145. }
  146. },
  147. isInU9:function () {
  148. if (window.hasOwnProperty("egret_native")) {
  149. return true;
  150. } else {
  151. var ua = window.navigator.userAgent;
  152. return ua.indexOf("EgretRuntime") != -1 && ua.indexOf("yoyo") != -1;
  153. }
  154. },
  155. isInU9Web:function(){
  156. if (EgretShare.findLocationProperty("app_id") == 1010) {
  157. return true;
  158. }
  159. return false;
  160. },
  161. findLocationProperty:function (key) {
  162. if (window.hasOwnProperty("location")) {
  163. var search = location.search;
  164. if (search == "") {
  165. return null;
  166. }
  167. search = search.slice(1);
  168. var searchArr = search.split("&");
  169. var length = searchArr.length;
  170. for (var i = 0; i < length; i++) {
  171. var str = searchArr[i];
  172. var arr = str.split("=");
  173. if (arr[0] == key) {
  174. return arr[1];
  175. }
  176. }
  177. }
  178. return null;
  179. },
  180. getUid:function () {
  181. return EgretShare.findLocationProperty("uid");
  182. },
  183. loaded:function(){
  184. var evt = document.createEvent("HTMLEvents");
  185. evt.initEvent("EgretShareLoaded", true, true);
  186. document.dispatchEvent(evt);
  187. }
  188. };
  189. EgretShare.loaded();