game9g.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /***************************** Game 9G 主类 *********************************/
  2. Game9G = function(gameid) {
  3. this.gameid = gameid;
  4. this.spid = null;
  5. this.baseurl = "";
  6. this.homeurl = null;
  7. this.gzurl = null;
  8. this.score = null;
  9. this.scoreName = null;
  10. this.shareDomain = null;
  11. // this.shareDomains = ["ytins.cn","ytins.cn","ytins.cn", "impak.cn", "impak.cn", "frela.cn"];
  12. this.shareData = {
  13. imgurl: null,
  14. link: null,
  15. title: "游戏",
  16. content: "游戏"
  17. };
  18. this.app = null;
  19. this.utils = new Game9GUtils(this);
  20. this.init();
  21. }
  22. // 初始化
  23. Game9G.prototype.init = function() {
  24. this.spid = this.utils.getParameter("spid");
  25. this.homeurl = "http://mp.weixin.qq.com/s?__biz=MzI4MjA2MjE0MQ==&mid=246005295&idx=1&sn=490f8141976d607ba079d48f52a3fcd7#rd";
  26. this.gzurl = "http://game.ikongzhong.cn";
  27. // this.shareDomain = this.shareDomains[parseInt(Math.random() * this.shareDomains.length)];
  28. this.shareData.imgurl = "http://mmbiz.qpic.cn/mmbiz/2zpp2iaH4HWEgbChjnDCvrnNlGhflD2ia06fcvQtgvvU0wdDVdAHbxbGxK8SCMKFBIpo20ZbjLLHkrxzT7eyKiaiaQ/640";
  29. this.shareData.link = "http://game.ikongzhong.cn/games/dtsl/";
  30. switch (this.utils.getAppType()) {
  31. case "wx":
  32. this.app = new Game9GWx(this);
  33. break;
  34. case "uc":
  35. this.app = new Game9GUC(this);
  36. break;
  37. }
  38. };
  39. // 分享
  40. Game9G.prototype.share = function() {
  41. // 调用各自 App 的分享接口
  42. this.app && this.app.share();
  43. }
  44. // 提交成绩
  45. Game9G.prototype.submit = function(callback) {
  46. if (localStorage.myuid && this.score != null) {
  47. var _this = this;
  48. setTimeout(function(){
  49. if (confirm("?")) {
  50. window.location =_this.homeurl;
  51. }
  52. else {
  53. window.location = _this.homeurl;
  54. }
  55. callback && callback.apply(null);
  56. }, 500);
  57. }
  58. else {
  59. window.location = this.homeurl;
  60. callback && callback.apply(null);
  61. }
  62. }
  63. /***************************** 实用工具类 *********************************/
  64. Game9GUtils = function(game9g) {
  65. this.game9g = game9g;
  66. }
  67. // 判断当前 App [微信、UC浏览器、etc]
  68. Game9GUtils.prototype.getAppType = function() {
  69. var e = navigator.userAgent.toLowerCase();
  70. if (e.match(/MicroMessenger/i) == "micromessenger") {
  71. return "wx";
  72. }
  73. else if (e.match(/UCBrowser/i) == "ucbrowser") {
  74. return "uc";
  75. }
  76. else {
  77. return "other";
  78. }
  79. }
  80. // 获取 URL 参数
  81. Game9GUtils.prototype.getParameter = function(name) {
  82. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  83. var r = window.location.search.substr(1).match(reg);
  84. if (r != null) return r[2]; return null;
  85. }
  86. // 显示分享图片
  87. Game9GUtils.prototype.showShare = function() {
  88. var img = document.getElementById("game9gshare");
  89. if (img) {
  90. img.style.display = "";
  91. }
  92. else {
  93. img = document.createElement("img");
  94. img.id = "game9gshare";
  95. img.src = "share.png";
  96. img.className = "game9gshare";
  97. //img.addEventListener("click", this.hideShare);
  98. img.addEventListener("touchstart", this.hideShare);
  99. document.getElementsByTagName("body")[0].appendChild(img);
  100. }
  101. }
  102. // 隐藏分享图片
  103. Game9GUtils.prototype.hideShare = function() {
  104. var img = document.getElementById("game9gshare");
  105. if (img) img.style.display = "none";
  106. }
  107. // 显示分享对话框
  108. Game9GUtils.prototype.shareConfirm = function(content, callback) {
  109. var _this = this;
  110. setTimeout(function(){
  111. new Game9GUtilsDialog(_this.game9g, {
  112. title: "游戏提示",
  113. content: content,
  114. buttons: [
  115. { label: "取消", click: null },
  116. { label: "确定", click: callback }
  117. ]
  118. }).open();
  119. }, 1000);
  120. }
  121. // 对话框
  122. Game9GUtilsDialog = function(game9g, options) {
  123. this.game9g = game9g;
  124. this.title = options.title;
  125. this.content = options.content;
  126. this.buttons = options.buttons;
  127. }
  128. // 打开对话框
  129. Game9GUtilsDialog.prototype.open = function() {
  130. if (document.getElementById("game9gdialog")) return;
  131. var div = document.createElement("div");
  132. div.id = "game9gdialog";
  133. div.className = "game9gdialog";
  134. div.innerHTML = "<header><h2>" + this.title + "</h2></header><section>" + this.content + "</section><footer></footer>";
  135. for (var i=0; i<this.buttons.length; i++) {
  136. var btn = this.buttons[i];
  137. var a = document.createElement("a");
  138. a.innerHTML = btn.label;
  139. //a.addEventListener("click", btn.click);
  140. //a.addEventListener("click", this.close);
  141. a.addEventListener("touchstart", btn.click);
  142. a.addEventListener("touchstart", this.close);
  143. div.getElementsByTagName("footer")[0].appendChild(a);
  144. }
  145. document.getElementsByTagName("body")[0].appendChild(div);
  146. var mask = document.createElement("div");
  147. mask.id = "game9gmask";
  148. mask.className="game9gmask";
  149. document.getElementsByTagName("body")[0].appendChild(mask);
  150. }
  151. // 关闭对话框
  152. Game9GUtilsDialog.prototype.close = function() {
  153. var div = document.getElementById("game9gdialog");
  154. if (div) document.getElementsByTagName("body")[0].removeChild(div);
  155. var mask = document.getElementById("game9gmask");
  156. if (mask) document.getElementsByTagName("body")[0].removeChild(mask);
  157. }
  158. // Ajax 请求
  159. Game9GUtils.prototype.ajax = function(url, success) {
  160. new Game9GUtilsAjax(this.game9g, "GET", url, null, "json", success);
  161. }
  162. // JSONP 请求
  163. Game9GUtils.prototype.jsonp = function(url, data, param, success) {
  164. new Game9GUtilsJsonp(url, data, param, success).request();
  165. }
  166. // Ajax 类
  167. Game9GUtilsAjax = function(game9g, method, url, data, type, success) {
  168. this.game9g = game9g;
  169. this.xmlhttp = null;
  170. if (window.XMLHttpRequest) {
  171. this.xmlhttp = new XMLHttpRequest();
  172. }
  173. else {
  174. this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  175. }
  176. this.type = type;
  177. this.success = success;
  178. this.xmlhttp.open(method, url, true);
  179. var _this = this;
  180. this.xmlhttp.onreadystatechange = function() {
  181. _this.callback.apply(_this);
  182. };
  183. this.xmlhttp.send(data);
  184. }
  185. // Ajax 请求回调
  186. Game9GUtilsAjax.prototype.callback = function() {
  187. if (this.xmlhttp.readyState == 4 && this.xmlhttp.status == 200) {
  188. var data = null;
  189. switch (this.type) {
  190. case "text":
  191. data = this.xmlhttp.responseText;
  192. break;
  193. case "json":
  194. try {
  195. data = JSON.parse(this.xmlhttp.responseText);
  196. }
  197. catch (e) {
  198. data = this.xmlhttp.responseText;
  199. }
  200. break;
  201. }
  202. this.success && this.success.call(this.xmlhttp, data);
  203. }
  204. }
  205. // JSONP 类
  206. Game9GUtilsJsonp = function(url, data, jsonparam, success, timeout) {
  207. var finish = false;
  208. var theHead = document.getElementsByTagName("head")[0] || document.documentElement;
  209. var scriptControll = document.createElement("script");
  210. var jsonpcallback = "jsonpcallback" + (Math.random() + "").substring(2);
  211. var collect = function() {
  212. if (theHead != null) {
  213. theHead.removeChild(scriptControll);
  214. try {
  215. delete window[jsonpcallback];
  216. } catch (ex) { }
  217. theHead = null;
  218. }
  219. };
  220. var init = function() {
  221. scriptControll.charset = "utf-8";
  222. theHead.insertBefore(scriptControll, theHead.firstChild);
  223. window[jsonpcallback] = function(responseData) {
  224. finish = true;
  225. success(responseData);
  226. };
  227. if (url.indexOf("?") > 0) {
  228. url = url + "&" + jsonparam + "=" + jsonpcallback;
  229. } else {
  230. url = url + "?" + jsonparam + "=" + jsonpcallback;
  231. }
  232. if (typeof data == "object" && data != null) {
  233. for (var p in data) {
  234. url = url + "&" + p + "=" + escape(data[p]);
  235. }
  236. }
  237. };
  238. var timer = function() {
  239. if (typeof window[jsonpcallback] == "function") {
  240. collect();
  241. }
  242. if (typeof timeout == "function" && finish == false) {
  243. timeout();
  244. }
  245. };
  246. this.request = function() {
  247. init();
  248. scriptControll.src = url;
  249. window.setTimeout(timer, 10000);
  250. };
  251. }
  252. // 统计代码
  253. Game9GUtils.prototype.tongji = function() {
  254. // baidu
  255. var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
  256. document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F0ae524064813b8dc07ece5ce724a7b04' type='text/javascript'%3E%3C/script%3E"));
  257. // cnzz
  258. var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");
  259. document.write(unescape("%3Cspan id='cnzz_stat_icon_2947366'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s5.cnzz.com/stat.php%3Fid%3D2947366' type='text/javascript'%3E%3C/script%3E"));
  260. }
  261. /***************************** 微信工具类 *********************************/
  262. Game9GWx = function(game9g) {
  263. this.game9g = game9g;
  264. this.init();
  265. }
  266. // 初始化
  267. Game9GWx.prototype.init = function() {
  268. var _this = this;
  269. document.addEventListener("WeixinJSBridgeReady", function onBridgeReady() {
  270. WeixinJSBridge.on("menu:share:appmessage", function(argv) {
  271. WeixinJSBridge.invoke("sendAppMessage", {
  272. "img_url": _this.game9g.shareData.imgurl,
  273. "link": _this.game9g.shareData.link,
  274. "desc": _this.game9g.shareData.content,
  275. "title": _this.game9g.shareData.title
  276. }, function(res){
  277. _this.shareComplete();
  278. });
  279. });
  280. WeixinJSBridge.on("menu:share:timeline", function(argv) {
  281. WeixinJSBridge.invoke("shareTimeline", {
  282. "img_url": _this.game9g.shareData.imgurl,
  283. "img_width": "640",
  284. "img_height": "640",
  285. "link": _this.game9g.shareData.link,
  286. "desc": _this.game9g.shareData.content,
  287. "title": _this.game9g.shareData.title
  288. }, function(res){
  289. _this.shareComplete();
  290. });
  291. });
  292. }, false);
  293. }
  294. // 分享接口实现
  295. Game9GWx.prototype.share = function() {
  296. this.game9g.utils.showShare();
  297. }
  298. // 分享完成
  299. Game9GWx.prototype.shareComplete = function() {
  300. this.game9g.utils.hideShare();
  301. this.game9g.submit();
  302. }
  303. /***************************** UC 工具类 *********************************/
  304. Game9GUC = function(game9g) {
  305. this.game9g = game9g;
  306. window.uc_param_str = {};
  307. this.init();
  308. }
  309. // 初始化
  310. Game9GUC.prototype.init = function() {
  311. var url = "http://hao.uc.cn/getucparam.php";
  312. var data = { uc_param_str: "dnfrpfbivecpbtnt" };
  313. this.game9g.utils.jsonp(url, data, "callback", function(data) {
  314. window.uc_param_str = data;
  315. });
  316. }
  317. // 分享接口实现
  318. Game9GUC.prototype.share = function() {
  319. if (uc_param_str.fr === 'android' || uc_param_str.fr === 'iphone') {
  320. if (uc_param_str.fr === 'android') {
  321. try {
  322. ucweb.startRequest("shell.page_share", [
  323. this.game9g.shareData.title,
  324. this.game9g.shareData.content,
  325. this.game9g.shareData.link,
  326. ''
  327. ]);
  328. } catch (e) {
  329. console.error(e.message);
  330. }
  331. } else {
  332. // 如果是iphone平台,调用iso的分享接口
  333. location.href = "ext:web_share:";
  334. }
  335. }
  336. else {
  337. alert("其它分享接口");
  338. }
  339. }