common.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // 排行榜通用接口
  2. CommonRank = function(gameid) {
  3. this.gameid = gameid;
  4. this.uid;
  5. this.myuid;
  6. this.is9Guser;
  7. this.order = "desc";
  8. this.init();
  9. }
  10. // 初始化
  11. CommonRank.prototype.init = function() {
  12. this.uid = this.getParameter("uid");
  13. this.myuid = this.getParameter("myuid");
  14. this.is9Guser = (this.getParameter("is9guser") === "true");
  15. }
  16. // 是否微信浏览器
  17. CommonRank.prototype.isWeixin = function() {
  18. // if (this.gameid == "sjm") return false;
  19. return false;
  20. var e = navigator.userAgent.toLowerCase();
  21. if(e.match(/MicroMessenger/i) == "micromessenger") {
  22. return true;
  23. } else {
  24. return false;
  25. }
  26. }
  27. // 验证身份
  28. CommonRank.prototype.check = function() {
  29. if (this.myuid === null) {
  30. var url = "http://wx.9g.com/oauth/check.jsp?fromurl=" + encodeURIComponent(window.location.href);
  31. if (this.uid != null) url += ("&uid=" + this.uid);
  32. window.location.href = url;
  33. return false;
  34. }
  35. else {
  36. return true;
  37. }
  38. }
  39. // 查看排行榜
  40. CommonRank.prototype.gotoRank = function(type) {
  41. var url = "http://wx.9g.com/rank/rank.jsp?gameid=" + this.gameid + "&order=" + this.order + "&type=" + type;
  42. window.location = url;
  43. }
  44. // 提交成绩
  45. CommonRank.prototype.submit = function(score, scoreName, callback) {
  46. if (!this.is9Guser) return;
  47. $.ajax({
  48. type: "GET",
  49. async: true,
  50. cache: false,
  51. url: "http://wx.9g.com/rank/submit.jsp?gameid=" + this.gameid + "&uid=" + this.myuid + "&score=" + score + "&scorename=" + encodeURIComponent(scoreName) + "&order=" + this.order,
  52. dataType: "jsonp",
  53. jsonp: "callback",
  54. jsonpCallback: "submitCompleteHandler",
  55. success: function(data){
  56. if (data.submit == "ok") {
  57. if (data.refreshRankScore) {
  58. alert("你的成绩已经成功提交到9G!\n刷新了上一次的最好成绩: " + data.lastRankScoreName);
  59. }
  60. else {
  61. alert("你的成绩已经成功提交到9G!");
  62. }
  63. }
  64. else {
  65. alert("提交成绩失败");
  66. }
  67. callback && callback.apply(null);
  68. },
  69. error: function(XMLHttpRequest, textStatus, errorThrown) {
  70. alert(textStatus + "\n" + errorThrown);
  71. }
  72. });
  73. }
  74. // 读取 QueryString
  75. CommonRank.prototype.getParameter = function(name) {
  76. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  77. var r = window.location.search.substr(1).match(reg);
  78. if (r != null) return r[2]; return null;
  79. }