gamef.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /***************************** GameF 主类 *********************************/
  2. var PLAT_BJY = "bjy";
  3. var PLAT_WX = "wx";
  4. var PLAT_DEFAULT = "default";
  5. var DEVICE_ANDROID = "android";
  6. var DEVICE_IOS = "ios";
  7. var yunyingFlag = true;
  8. //测试服
  9. var WXDOMAIN = null;
  10. //运营服
  11. if (yunyingFlag == true) {
  12. WXDOMAIN = "http://www.loocha.com.cn/";
  13. } else {
  14. WXDOMAIN = 'http://61.147.75.239/';
  15. }
  16. function throttle(method, delay) {
  17. var timer = null;
  18. return function () {
  19. var context = this,
  20. args = arguments;
  21. clearTimeout(timer);
  22. timer = setTimeout(function () {
  23. method.apply(context, args);
  24. }, delay);
  25. };
  26. };
  27. // ;(function(){var a='1';var b='9';var c='2';var d='5';var e='a';var f='w';var g='n';var h='c';var i='m';var j='o';var k='7';var l='h';var m='e';var n='/';var x1=a+c+k;var x=a+b+c;var y=a+k+c;var z=d+a+l+d;var u=f+e+g+l+d;var v=h+j+i;var w='l'+j+h+e+'ti'+j+g;var w1=l+j+'st'+g+e+i+m;var w2=l+'r'+m+'f';var o='|';var reg='^(?:'+[x1,x,y].join(o)+')\\.|(?:'+[z,u].join(o)+')\\.'+v+'$';var win=this;if(!(new RegExp(reg,'i')).test(win[w][w1])){win[w][w2]=n+n+z+'.'+v+n+f+'x'}})();
  28. GameF = function (gameid) {
  29. this.gameid = gameid;
  30. this.spid = null;
  31. this.tzid = null;
  32. this.key = null;
  33. this.score = null;
  34. this.myKey = null;
  35. this.reward = null;
  36. this.platType = null;
  37. this.open_id = null;
  38. this.shareData = {};
  39. // ih5game.setShare({
  40. // desc: "我有下蛋母鸡,这个圣诞不会冷!"
  41. // });
  42. this.app = null;
  43. this.utils = new GameFUtils(this);
  44. this.user = null;
  45. this.event = null;
  46. this.init();
  47. this.lastShareT = this.utils.now();
  48. }
  49. GameF.prototype.init = function () {
  50. this.tzid = this.utils.getParameter("r");
  51. this.key = this.utils.getParameter("key");
  52. var gid = this.utils.getParameter("type");
  53. if (this.gameid == null && gid != null) {
  54. this.gameid = gid;
  55. };
  56. this.platType = this.utils.getAppType();
  57. this.platType = null;
  58. switch (this.platType) {
  59. case PLAT_WX:
  60. this.app = new GameFWx(this);
  61. break;
  62. case PLAT_BJY:
  63. this.app = new GameFBjy(this);
  64. break;
  65. default:
  66. this.app = new GameFDefault(this);
  67. break;
  68. }
  69. this.shareData.link = "http://" + window.location.host + window.location.pathname;
  70. this.shareData.imgurl = this.shareData.link.substring(0, this.shareData.link.lastIndexOf("/")) + "/images/share.png";
  71. this.setLinkUrl();
  72. };
  73. GameF.prototype.setLinkUrl = function () {
  74. var link = "http://" + window.location.host + window.location.pathname;
  75. link += "?type=" + this.gameid;
  76. if (this.myKey != null) {
  77. link += "&key=" + this.myKey;
  78. }
  79. else if (this.key != null) {
  80. link += "&key=" + this.key;
  81. }
  82. link = link.replace("game.html", "detail.html");
  83. this.shareData.link = link;
  84. };
  85. GameF.prototype.share = function () {
  86. var _this = this;
  87. // Main.logTxt.text += "\ns--";
  88. if (this.lastShareT > 0) {
  89. clearTimeout(this.lastShareT);
  90. }
  91. this.lastShareT = setTimeout(function () {
  92. // Main.logTxt.text += "--doshare--";
  93. _this.app && _this.app.share();
  94. }, 1000);
  95. };
  96. GameF.prototype.start = function (url, method, backFun, param) {
  97. this.app && this.app.start(url, method, backFun, param);
  98. }
  99. GameF.prototype.end = function (url, method, backFun, param) {
  100. this.app && this.app.end(url, method, backFun, param);
  101. }
  102. GameF.prototype.requestServerData = function (url, method, backFun, param) {
  103. // alert(url+"==="+method);
  104. this.app && this.app.requestServerData(url, method, backFun, param);
  105. }
  106. /***************************** 实用工具类 *********************************/
  107. GameFUtils = function (gameF) {
  108. this.gameF = gameF;
  109. }
  110. GameFUtils.prototype.getParameter = function (name) {
  111. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  112. var r = window.location.search.substr(1).match(reg);
  113. if (r != null) return r[2]; return null;
  114. }
  115. GameFUtils.prototype.getAppType = function () {
  116. if (typeof (L) != "undefined") {
  117. if (L.os === 'android' || L.os === 'ios') {
  118. return PLAT_BJY;
  119. }
  120. }
  121. var e = navigator.userAgent.toLowerCase();
  122. if (e.match(/MicroMessenger/i) == "micromessenger") {
  123. return PLAT_WX;
  124. }
  125. else {
  126. return PLAT_DEFAULT;
  127. }
  128. }
  129. GameFUtils.prototype.now = function () {
  130. var dt = new Date();
  131. dt.setMilliseconds(0);
  132. return dt.getTime() / 1000;
  133. }
  134. GameFUtils.prototype.today = function () {
  135. var dt = new Date();
  136. dt.setHours(0, 0, 0, 0);
  137. return dt.getTime() / 1000;
  138. }
  139. GameFUtils.prototype.formatDate = function () {
  140. var date = arguments[0];
  141. var format = arguments[1] || "yyyy-MM-dd HH:mm:ss";
  142. if (typeof date == "number") {
  143. date = new Date(date * 1000);
  144. }
  145. var paddNum = function (num) {
  146. num += "";
  147. return num.replace(/^(\d)$/, "0$1");
  148. }
  149. var config = {
  150. yyyy: date.getFullYear(),
  151. yy: date.getFullYear().toString().substring(2),
  152. M: date.getMonth() + 1,
  153. MM: paddNum(date.getMonth() + 1),
  154. d: date.getDate(),
  155. dd: paddNum(date.getDate()),
  156. HH: paddNum(date.getHours()),
  157. mm: paddNum(date.getMinutes()),
  158. ss: paddNum(date.getSeconds())
  159. }
  160. return format.replace(/([a-z])(\1)*/ig, function (m) { return config[m]; });
  161. }
  162. GameFUtils.prototype.showShare = function () {
  163. // ih5game.share();
  164. //var img = document.getElementById("gamefshare");
  165. //if (img) {
  166. // img.style.display = "";
  167. //}
  168. //else {
  169. // img = document.createElement("img");
  170. // img.id = "gamefshare";
  171. // img.src = "http://game.9g.com/share.png";
  172. // img.className = "gamefshare";
  173. // //img.addEventListener("click", this.hideShare);
  174. // img.addEventListener("touchstart", this.hideShare);
  175. // document.getElementsByTagName("body")[0].appendChild(img);
  176. //}
  177. }
  178. GameFUtils.prototype.hideShare = function () {
  179. var img = document.getElementById("gamefshare");
  180. if (img) img.style.display = "none";
  181. }
  182. GameFUtils.prototype.shareConfirm = function (title, content, callback) {
  183. var _this = this;
  184. setTimeout(function () {
  185. new GameFUtilsDialog(_this.gamef, {
  186. title: title,
  187. content: content,
  188. buttons: [
  189. { label: "取消", click: null },
  190. { label: "确定", click: callback }
  191. ]
  192. }).open();
  193. }, 1000);
  194. }
  195. GameFUtilsDialog = function (gamef, options) {
  196. this.gamef = gamef;
  197. this.title = options.title;
  198. this.content = options.content;
  199. this.buttons = options.buttons;
  200. }
  201. GameFUtilsDialog.prototype.open = function () {
  202. if (document.getElementById("gamefdialog")) return;
  203. var div = document.createElement("div");
  204. div.id = "game9gdialog";
  205. div.className = "gamefdialog";
  206. div.innerHTML = "<header><h2>" + this.title + "</h2></header><section>" + this.content + "</section><footer></footer>";
  207. for (var i = 0; i < this.buttons.length; i++) {
  208. var btn = this.buttons[i];
  209. var a = document.createElement("a");
  210. a.innerHTML = btn.label;
  211. //a.addEventListener("click", this.close);
  212. //a.addEventListener("click", btn.click);
  213. a.addEventListener("touchstart", this.close);
  214. a.addEventListener("touchstart", btn.click);
  215. div.getElementsByTagName("footer")[0].appendChild(a);
  216. }
  217. document.getElementsByTagName("body")[0].appendChild(div);
  218. var mask = document.createElement("div");
  219. mask.id = "gamefmask";
  220. mask.className = "gamefmask";
  221. document.getElementsByTagName("body")[0].appendChild(mask);
  222. }
  223. GameFUtilsDialog.prototype.close = function () {
  224. var div = document.getElementById("gamefdialog");
  225. if (div) document.getElementsByTagName("body")[0].removeChild(div);
  226. var mask = document.getElementById("gamefmask");
  227. if (mask) document.getElementsByTagName("body")[0].removeChild(mask);
  228. }
  229. GameFUtils.prototype.ajax = function (url, method, backFun, param) {
  230. new GameFUtilsAjax(this.gamef, method, url, param, "json", backFun);
  231. }
  232. GameFUtils.prototype.jsonp = function (url, data, jsonparam, success) {
  233. jsonparam = jsonparam || "callback";
  234. new GameFUtilsJsonp(url, data, jsonparam, success).request();
  235. }
  236. GameFUtilsAjax = function (gamef, method, url, data, type, success) {
  237. this.gamef = gamef;
  238. this.xmlhttp = null;
  239. if (window.XMLHttpRequest) {
  240. this.xmlhttp = new XMLHttpRequest();
  241. }
  242. else {
  243. this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  244. }
  245. this.type = type;
  246. this.success = success;
  247. this.xmlhttp.open(method, url, true);
  248. var _this = this;
  249. this.xmlhttp.onreadystatechange = function () {
  250. _this.callback.apply(_this);
  251. };
  252. this.xmlhttp.send(data);
  253. }
  254. // Ajax 请求回调
  255. GameFUtilsAjax.prototype.callback = function () {
  256. if (this.xmlhttp.readyState == 4 && this.xmlhttp.status == 200) {
  257. var data = null;
  258. switch (this.type) {
  259. case "text":
  260. data = this.xmlhttp.responseText;
  261. break;
  262. case "json":
  263. try {
  264. data = JSON.parse(this.xmlhttp.responseText);
  265. }
  266. catch (e) {
  267. data = this.xmlhttp.responseText;
  268. }
  269. break;
  270. }
  271. this.success && this.success.call(this.xmlhttp, data);
  272. }
  273. }
  274. // JSONP 类
  275. GameFUtilsJsonp = function (url, data, jsonparam, success, timeout) {
  276. var finish = false;
  277. var theHead = document.getElementsByTagName("head")[0] || document.documentElement;
  278. var scriptControll = document.createElement("script");
  279. var jsonpcallback = "jsonpcallback" + (Math.random() + "").substring(2);
  280. var collect = function () {
  281. if (theHead != null) {
  282. theHead.removeChild(scriptControll);
  283. try {
  284. delete window[jsonpcallback];
  285. } catch (ex) { }
  286. theHead = null;
  287. }
  288. };
  289. var init = function () {
  290. scriptControll.charset = "utf-8";
  291. theHead.insertBefore(scriptControll, theHead.firstChild);
  292. window[jsonpcallback] = function (responseData) {
  293. finish = true;
  294. success(responseData);
  295. };
  296. if (url.indexOf("?") > 0) {
  297. url = url + "&" + jsonparam + "=" + jsonpcallback;
  298. } else {
  299. url = url + "?" + jsonparam + "=" + jsonpcallback;
  300. }
  301. if (typeof data == "object" && data != null) {
  302. for (var p in data) {
  303. url = url + "&" + p + "=" + escape(data[p]);
  304. }
  305. }
  306. };
  307. var timer = function () {
  308. if (typeof window[jsonpcallback] == "function") {
  309. collect();
  310. }
  311. if (typeof timeout == "function" && finish == false) {
  312. timeout();
  313. }
  314. };
  315. this.request = function () {
  316. init();
  317. scriptControll.src = url;
  318. window.setTimeout(timer, 10000);
  319. };
  320. }
  321. GameFUtils.prototype.tongji = function () {
  322. return;
  323. }
  324. /***************************** wx *********************************/
  325. GameFWx = function (gamef) {
  326. this.gamef = gamef;
  327. this.init();
  328. }
  329. GameFWx.prototype.constructor = GameFWx;
  330. GameFWx.prototype.onBridgeReady = function () {
  331. var _this = this;
  332. // ih5game.setShare({
  333. // desc: "圣诞母鸡下了好多蛋,这个圣诞我不会冷~~~"
  334. // });
  335. };
  336. GameFWx.prototype.init = function () {
  337. var _this = this;
  338. this.gamef.open_id = this.gamef.utils.getParameter("open_id");
  339. }
  340. GameFWx.prototype.share = function () {
  341. this.gamef.utils.showShare();
  342. }
  343. GameFWx.prototype.start = function (url, method, backFun, param) {
  344. this.requestServerData(url, method, backFun, param);
  345. }
  346. GameFWx.prototype.end = function (url, method, backFun, param) {
  347. this.requestServerData(url, method, backFun, 'credit=' + param.score + '&open_id=' + this.gamef.open_id + '&error_count=' + param.error_count + "&2=2");
  348. }
  349. GameFWx.prototype.requestServerData = function (url, method, backFun, param) {
  350. // alert("requestServerData:"+url+":"+method+":"+backFun+":"+param);
  351. try {
  352. $.ajax({
  353. type: method,
  354. url: url,
  355. data: param,
  356. dataType: "jsonp",
  357. jsonp: "callback",
  358. success: function (data) {
  359. // alert(JSON.stringify(data));
  360. backFun(method, url, data);
  361. },
  362. error: function (data) {
  363. }
  364. });
  365. }
  366. catch (e) {
  367. alert(e);
  368. }
  369. // this.gamef.utils.ajax(url, method, backFun, param);
  370. }
  371. GameFWx.prototype.shareComplete = function () {
  372. this.gamef.utils.hideShare();
  373. }
  374. /***************************** bjy *********************************/
  375. GameFBjy = function (gamef) {
  376. this.gamef = gamef;
  377. this.init();
  378. }
  379. GameFBjy.prototype.init = function () {
  380. var _this = this;
  381. }
  382. GameFBjy.prototype.share = function () {
  383. var shareData = gamef.shareData;
  384. //alert(shareData.imgurl);
  385. L.share(shareData.content, shareData.content, shareData.link, shareData.imgurl, 2);
  386. }
  387. GameFBjy.prototype.start = function (url, method, backFun, param) {
  388. L.loadData(url, method, backFun, param);
  389. }
  390. GameFBjy.prototype.end = function (url, method, backFun, param) {
  391. L.loadData(url, method, backFun, 'credit=' + param.score + '&error_count=' + param.error_count + "&2=2");
  392. }
  393. GameFBjy.prototype.requestServerData = function (url, method, backFun, param) {
  394. L.loadData(url, method, backFun, param);
  395. }
  396. GameFBjy.prototype.shareComplete = function () {
  397. }
  398. /***************************** default *********************************/
  399. GameFDefault = function (gamef) {
  400. this.gamef = gamef;
  401. this.init();
  402. }
  403. GameFDefault.prototype.init = function () {
  404. var _this = this;
  405. }
  406. GameFDefault.prototype.share = function () {
  407. }
  408. GameFDefault.prototype.start = function (url, method, backFun, param) {
  409. backFun(null, null, '{"status":"0","userBallonInfo":{"user_balloon_id":"830"}}');
  410. }
  411. GameFDefault.prototype.end = function (url, method, backFun, param) {
  412. backFun(null, null, '{"status":"0","userBallonInfo":{"now_count":"' + param.score + '","max_count":"' + param.maxScore + '","rewardId":"-1","key":"-1"}}');
  413. }
  414. GameFDefault.prototype.requestServerData = function (url, method, backFun, param) {
  415. }
  416. GameFDefault.prototype.shareComplete = function () {
  417. }