api.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // 排行榜通用接口
  2. Auth9G = function(gameid) {
  3. this.gameid = gameid;
  4. this.title = document.title;
  5. this.uid = null;
  6. this.myuid = null;
  7. this.accessToken = null;
  8. this.user = null;
  9. this.order = "desc";
  10. this.identify = function(){};
  11. this.ready = function(){};
  12. this.readyDone = false;
  13. this.baseUrl = "http://wx.9g.com";
  14. this.gameUrl = "http://game.9g.com";
  15. this.init();
  16. }
  17. // 初始化
  18. Auth9G.prototype.init = function() {
  19. this.uid = this.getParameter("uid");
  20. console.log("uid=" + this.uid);
  21. }
  22. // 是否微信浏览器
  23. Auth9G.prototype.isWeixin = function() {
  24. var e = navigator.userAgent.toLowerCase();
  25. if(e.match(/MicroMessenger/i) == "micromessenger") {
  26. return true;
  27. } else {
  28. return false;
  29. }
  30. }
  31. // 身份认证系统 - 连接测试
  32. Auth9G.prototype.connect = function(success, fail) {
  33. var isConnect;
  34. setTimeout(function(){
  35. if (isConnect == undefined) {
  36. isConnect = false;
  37. fail && fail.call(null);
  38. console.log("连接超过2秒");
  39. }
  40. }, 2000);
  41. jQuery.ajax({
  42. type: "GET",
  43. async: true,
  44. cache: false,
  45. timeout: 5000,
  46. url: this.baseUrl + "/auth/connect",
  47. dataType: "jsonp",
  48. jsonp: "callback",
  49. jsonpCallback: "authConnectHandler",
  50. success: function(data){
  51. console.log(data);
  52. if (data.success == "ok" && isConnect == undefined) {
  53. isConnect = true;
  54. success && success.call(null);
  55. console.log("连接测试成功!");
  56. }
  57. }
  58. });
  59. }
  60. // 验证身份
  61. Auth9G.prototype.check = function() {
  62. // TEMP
  63. document.title = "9G游戏";
  64. document.body.style.display = "none";
  65. // 禁用 API
  66. // this.doReady();
  67. // return;
  68. // 是否微信浏览器
  69. if (!this.isWeixin()) {
  70. console.log("非微信浏览器");
  71. this.doReady();
  72. return;
  73. }
  74. // 不支持 localStorage
  75. console.log(window.localStorage);
  76. if (!window.localStorage) {
  77. console.log("不支持 localStorage");
  78. this.doReady();
  79. return;
  80. }
  81. // 本地已保存 Access Token
  82. if (localStorage.accessToken) {
  83. this.accessToken = localStorage.accessToken;
  84. // 此步骤更合理应该是在验证 Access Token 有效性之后,但那个时机将无法再执行 document.write 之类的语句
  85. this.doReady();
  86. }
  87. // 微信 API 返回 errcode
  88. if (sessionStorage.errcode != undefined) {
  89. console.log("errcode=" + sessionStorage.errcode + ", errmsg=" + sessionStorage.errmsg);
  90. sessionStorage.removeItem("errcode");
  91. sessionStorage.removeItem("errmsg");
  92. // 继续静态流程
  93. this.doReady();
  94. return;
  95. }
  96. // 连接测试
  97. var _this = this;
  98. this.connect(
  99. function(){
  100. // 连接成功
  101. if (_this.accessToken) {
  102. // 通过 Access Token 调用 AJAX 获取 9G 用户信息
  103. _this.get9gUser(_this.accessToken);
  104. }
  105. else {
  106. // 开始身份验证
  107. _this.check9gAuth();
  108. }
  109. },
  110. function(){
  111. // 连接失败,继续静态流程
  112. // _this.doReady();
  113. // TEMP:此时执行 document.write 会有问题
  114. document.title = _this.title;
  115. document.body.style.display = "";
  116. setTimeout(function(){
  117. try {
  118. WeixinJSBridge.call("showOptionMenu");
  119. }
  120. catch (e) {}
  121. }, 2000);
  122. }
  123. );
  124. }
  125. // 执行 ready
  126. Auth9G.prototype.doReady = function() {
  127. if (this.readyDone) return;
  128. // TEMP
  129. document.title = this.title;
  130. document.body.style.display = "";
  131. // 执行 ready 方法
  132. this.ready && this.ready.call(null);
  133. this.readyDone = true;
  134. console.log("ready");
  135. // 附加:显示右上角转发按钮
  136. setTimeout(function(){
  137. try {
  138. WeixinJSBridge.call("showOptionMenu");
  139. }
  140. catch (e) {}
  141. }, 2000);
  142. }
  143. // 开始身份验证
  144. Auth9G.prototype.check9gAuth = function() {
  145. var origin = removeParameter(window.location.href, "uid");
  146. var trans = this.gameUrl + "/auth/trans.html?gameid=" + this.gameid + "&origin=" + encodeURIComponent(origin);
  147. var url = this.baseUrl + "/auth/check?fromurl=" + encodeURIComponent(trans);
  148. if (this.uid != null) url += ("&uid=" + this.uid);
  149. window.location = url;
  150. }
  151. // 获取 9G 用户信息
  152. Auth9G.prototype.get9gUser = function(accessToken) {
  153. var _this = this;
  154. jQuery.ajax({
  155. type: "GET",
  156. async: true,
  157. cache: false,
  158. url: this.baseUrl + "/auth/get9guser?access_token=" + accessToken,
  159. dataType: "jsonp",
  160. jsonp: "callback",
  161. jsonpCallback: "get9gUserHandler",
  162. success: function(data){
  163. if (data.errcode) {
  164. // access token 过期,重新验证
  165. localStorage.removeItem("accessToken");
  166. _this.accessToken = null;
  167. _this.check9gAuth();
  168. }
  169. else {
  170. // 获取成功
  171. _this.myuid = data.uid;
  172. _this.user = data.user;
  173. _this.identify && _this.identify.call(null);
  174. console.log(data);
  175. }
  176. },
  177. error: function(XMLHttpRequest, textStatus, errorThrown) {
  178. alert(textStatus + "\n" + errorThrown);
  179. }
  180. });
  181. }
  182. // 查看排行榜
  183. Auth9G.prototype.gotoRank = function(type) {
  184. var url = this.baseUrl + "/rank/rank.jsp?gameid=" + this.gameid + "&order=" + this.order + "&type=" + type;
  185. window.location = url;
  186. }
  187. // 提交成绩
  188. Auth9G.prototype.submit = function(score, scoreName, callback) {
  189. if (!this.user) return;
  190. jQuery.ajax({
  191. type: "GET",
  192. async: true,
  193. cache: false,
  194. url: this.baseUrl + "/rank/submit.jsp?gameid=" + this.gameid + "&access_token=" + this.accessToken + "&score=" + score + "&scorename=" + encodeURIComponent(scoreName) + "&order=" + this.order,
  195. dataType: "jsonp",
  196. jsonp: "callback",
  197. jsonpCallback: "submitCompleteHandler",
  198. success: function(data){
  199. if (data.submit == "ok") {
  200. if (data.refreshRankScore) {
  201. alert("你的成绩已经成功提交到9G!\n刷新了上一次的最好成绩: " + data.lastRankScoreName);
  202. }
  203. else {
  204. alert("你的成绩已经成功提交到9G!");
  205. }
  206. }
  207. else {
  208. alert("提交成绩失败");
  209. }
  210. callback && callback.apply(null);
  211. },
  212. error: function(XMLHttpRequest, textStatus, errorThrown) {
  213. alert(textStatus + "\n" + errorThrown);
  214. }
  215. });
  216. }
  217. // 读取 QueryString 参数
  218. Auth9G.prototype.getParameter = function(name) {
  219. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  220. var r = window.location.search.substr(1).match(reg);
  221. if (r != null) return r[2]; return null;
  222. }
  223. // 从 QueryString 中删除一项参数
  224. function removeParameter(url, name) {
  225. if (url.indexOf("?") == -1) return url;
  226. var origin = url.split("?")[0];
  227. var search = url.split("?")[1];
  228. var isRd = false;
  229. if (search.substr(search.length - 3) == "#rd") {
  230. search = search.substr(0, search.length - 3);
  231. isRd = true;
  232. }
  233. var a = search.split("&");
  234. for (var i=a.length-1; i>=0; i--) {
  235. var p = a[i].substr(0, a[i].indexOf("="));
  236. if (p == name) a.splice(i, 1);
  237. }
  238. var result;
  239. if (a.length == 0) {
  240. result = origin;
  241. }
  242. else {
  243. result = origin + "?" + a.join("&");
  244. }
  245. if (isRd) result += "#rd";
  246. return result;
  247. }