personal.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. $(function() {
  2. //获取url里面参数
  3. var urlServerName = getURLParam("serverName");
  4. var urlPlayerName = getURLParam("playerName");
  5. var gameZone = getURLParam("gameZone");
  6. var userId = getURLParam("userId");
  7. var serverNameParam = encodeURIComponent(urlServerName);
  8. var playerNameParam = encodeURIComponent(urlPlayerName);
  9. var Personal = function() {
  10. this.baseUrl = "http://api.lolbox.duowan.com/api/v2/player/"+gameZone+"/" + userId + '/';
  11. this.serverName = serverNameParam;
  12. this.playerName = playerNameParam;
  13. this.boxUserId = $.cookie("boxUserId");
  14. this.boxPlayerName = encodeURIComponent($.cookie("boxPlayerName"));
  15. this.boxPlayerName = encodeURIComponent($.cookie("boxPlayerName"));
  16. //由于常用英雄浮窗的数据由hover调接口,改为直接从主接口中获取,所以做一下缓存.
  17. this.commonHeroHoverData = [];
  18. };
  19. function getCookie(cookieName) {
  20. var cookieString = document.cookie;
  21. var start = cookieString.indexOf(cookieName + '=');
  22. // 加上等号的原因是避免在某些 Cookie 的值里有
  23. // 与 cookieName 一样的字符串。
  24. if (start == -1) // 找不到
  25. return null;
  26. start += cookieName.length + 1;
  27. var end = cookieString.indexOf(';', start);
  28. if (end == -1)
  29. return cookieString.substring(start);
  30. return cookieString.substring(start, end);
  31. }
  32. function beenBlockedTimes(action) {
  33. var boxPlayerName = decodeURI(getCookie('boxPlayerName'));
  34. var boxServerName = decodeURI(getCookie('boxServerName'));
  35. $.ajax({
  36. url: "http://lolbox.duowan.com/webApi.php",
  37. data: {serverName: boxServerName, playerName: boxPlayerName, action: action},
  38. cache: false,
  39. success: function (msg) {
  40. if (msg == 1) {
  41. location.reload();
  42. } else if (msg == -1) {
  43. alert('非法操作,请至少登陆一次游戏!');
  44. }
  45. },
  46. error: function () {
  47. }
  48. });
  49. }
  50. Personal.prototype = {
  51. constructor: Personal,
  52. init: function() {
  53. var that = this;
  54. this._moreGameResult();
  55. this._moreCommonHero();
  56. //获取除开KDA那行的其他数据
  57. this._getData().done(function(result, status, xhr) {
  58. var isMyself = result.is_myself;
  59. var followed = result.followed;
  60. var data = result.player_list[0];
  61. data.isSelf = isMyself;
  62. data.followed = followed;
  63. that._render(data);
  64. }).fail(function() {});
  65. $("body").delegate(".handles .copy", "click", function(e) {
  66. if (window.clipboardData && window.clipboardData.setData !== undefined) {
  67. var achievement = $(this).parent().find(".copy-text").text();
  68. window.clipboardData.setData('Text', achievement);
  69. alert('复制成功');
  70. }
  71. event.preventDefault();
  72. });
  73. $("body").delegate( '.black-eye', 'mouseenter', function(e) {
  74. e.preventDefault();
  75. $(".black-label").show();
  76. });
  77. $("body").delegate( '.black-eye', 'mouseleave', function(e) {
  78. e.preventDefault();
  79. $(".black-label").hide();
  80. });
  81. $("body").delegate( '.black-eye', 'click', function(e) {
  82. e.preventDefault();
  83. // $(".blacklist-content1").hide();
  84. // $(".blacklist-content2").show();
  85. beenBlockedTimes('showBeenBlockedTimes');
  86. });
  87. $("body").delegate( '.blacklist-content2>a', 'click', function(e) {
  88. e.preventDefault();
  89. // $(".blacklist-content1").show();
  90. // $(".blacklist-content2").hide();
  91. beenBlockedTimes('hiddenBeenBlockedTimes');
  92. });
  93. $("body").delegate('.nav-top-bottom', 'click', function(e) {
  94. $(this).toggleClass('active');
  95. $('.hide-default').toggle();
  96. });
  97. },
  98. _getData: function() {
  99. var url = this.baseUrl;
  100. return $.ajax({
  101. url: url,
  102. type: 'GET',
  103. dataType: 'jsonp'
  104. });
  105. },
  106. _getKDAData: function() {
  107. var url = this.baseUrl + "?_do=personal/positionhonorkda&serverName=" + this.serverName + "&playerName=" + this.playerName;
  108. return $.ajax({
  109. url: url,
  110. type: 'GET',
  111. dataType: 'jsonp'
  112. });
  113. },
  114. _render: function(data) {
  115. // var status = data.status;
  116. var that = this;
  117. this.playerId = data.user_id;
  118. // if(status) {
  119. // var tip;
  120. // if(status == 10402) {
  121. // tip = "盒子未保存过该用户的战斗日志";
  122. // } else if(status == 10407) {
  123. // tip = "请重新打一场匹配恢复战绩";
  124. // } else if(status == 10401){
  125. // tip = "参数不全";
  126. // }
  127. // $(".no-data-tip").show().find("p").html(tip);
  128. // $(".box-bd").hide();
  129. // return;
  130. // }
  131. $(".box-bd").show();
  132. //获取KDA行数据--放在box-bd show 之后,保证Morris正常渲染.
  133. // this._getKDAData().done(function(data) {
  134. // that._renderKDA(data);
  135. // }).fail(function() {});
  136. this._renderPersonalInfo(data);
  137. this._renderHistory(data);
  138. this._renderHero(data);
  139. this._renderGameCount(data);
  140. this._renderKDA(data);
  141. },
  142. _renderPersonalInfo: function(data) {
  143. var self = this;
  144. var href = "recordlist.html?serverName=" + this.serverName + "&playerName=" + this.playerName + "&userId=" + userId + "&gameZone=" + gameZone;
  145. var attentionUrl = "http://lolbox.duowan.com/webApi.php?serverName=" + this.serverName + "&playerName=" + this.playerName + "&targetServerName=" + this.boxServerName + "&targetPlayerName=" + this.boxPlayerName + "&action=";
  146. // var attentionUrl = "http://lolbox.duowan.com/webApi.php?serverName=" + this.serverName + "&playerName=" + this.playerName + "&targetServerName=%E7%94%B5%E4%BF%A1%E4%B8%80&targetPlayerName=%E6%96%8C%E7%A6%BE%E4%B8%96%E7%BA%AA&action=";
  147. //储存个人信息
  148. data.global = {};
  149. //二维码url
  150. data.global.qrcodeUrl = "http://lolbox.duowan.com/qrcode.php?openmode=barcode&serverName=" + this.serverName + "&playerName=" + this.playerName;
  151. //头像url,点击去到战绩页面
  152. data.global.href = href;
  153. //关注/取消关注接口baseUrl
  154. data.global.aUrl = attentionUrl;
  155. $("#currentNav").html(data.pn+"("+data.game_zone.alias+")").attr("href", location.href);
  156. var template = _.template($("#personalInfoTemp").html());
  157. $("#personalInfo").html(template({
  158. data: data
  159. }));
  160. $("#screenshot").on("click", function(e) {
  161. $('.share').removeClass('hover');
  162. setTimeout(function () {
  163. window.location.href = 'lolboxphoto://'+data.game_zone.server_name+'_'+data.pn+'_玩家详情';
  164. }, 200);
  165. });
  166. //关注、取消关注
  167. $("body").delegate(".attention", "click", function(e) {
  168. e.preventDefault();
  169. if(!self.boxUserId) {
  170. alert('非法操作,请至少登陆一次游戏!');
  171. return;
  172. }
  173. var type = $(this).attr("data-action"), that = this;
  174. var action = type == 'addFocus' ? 'follow' : 'unfollow';
  175. var url = "http://api.lolbox.duowan.com/api/v2/player/"+gameZone+"/" + self.boxUserId + '/'+action+'/?user_id='+userId;
  176. $.ajax({
  177. url: url,
  178. type: 'get',
  179. dataType: 'jsonp',
  180. success: function(result) {
  181. if (result) {
  182. if(type == "addFocus") {
  183. $(that).addClass("already").attr("data-action", "removeFocus")
  184. .find("span").html("已关注");
  185. } else {
  186. $(that).removeClass("already").attr("data-action", "addFocus")
  187. .find("span").html("关注");
  188. }
  189. }
  190. }
  191. });
  192. });
  193. //分享
  194. jiathis_config = {
  195. title: "#无盒子 不开撸# 我是" + data.pn +",我在使用LOL盒子。来看看我的战斗力有多少吧!",
  196. appkey: {
  197. "tsina": "3629014272",
  198. "tqq": "801220649",
  199. "qzone": "100302822"
  200. },
  201. summary: " ",
  202. ralateuid: {
  203. "tsina": "2909042820"
  204. }
  205. };
  206. if (playerMostUsedHero)
  207. jiathis_config.pic = 'http://lolbox.duowan.com/images/champions/' + playerMostUsedHero + '_120x120.png';
  208. if (SnsShare) SnsShare.init();
  209. },
  210. _renderKDA: function(data) {
  211. this._renderDaYe(data);
  212. this._renderKDADetail(data);
  213. },
  214. _renderDaYe: function(data) {
  215. var colorsArr = ['#fbb28f','#cea9d7','#fa9f9f','#80c989','#9dc7eb'], setColors = [], setData = [], i=0;
  216. var statPosition = data.stat_position;//英雄位置统计
  217. if(statPosition) {
  218. for(var key in statPosition) {
  219. var value = statPosition[key];
  220. if(key != 'total_game' && value != 0) {
  221. var keyTxt = '';
  222. switch(key) {
  223. case 'adc': keyTxt = 'ADC';break;
  224. case 'jungler': keyTxt = '打野';break;
  225. case 'mid': keyTxt = '中单';break;
  226. case 'support': keyTxt = '辅助';break;
  227. case 'top': keyTxt = '上单';break;
  228. };
  229. setData.push({
  230. value: value,
  231. label: keyTxt
  232. });
  233. setColors.push(colorsArr[i++]);
  234. }
  235. }
  236. if(setData.length > 0) {
  237. //初始化打野
  238. Morris.Donut({
  239. element: 'daYe',
  240. data: setData,
  241. colors: setColors,
  242. gridTextSize: 30
  243. });
  244. }
  245. }
  246. $("body").delegate("#daYe, .kda-li, .kill-li", "mouseenter", function(e) {
  247. e.preventDefault();
  248. if($(this).hasClass("da-ye")) {
  249. $(".daye-hover").show();
  250. } else if($(this).hasClass("kda-li")) {
  251. $(".kda-hover").show();
  252. } else {
  253. $(".kill-hover").show();
  254. }
  255. });
  256. $("body").delegate("#daYe, .kda-li, .kill-li", "mouseleave", function(e) {
  257. e.preventDefault();
  258. if($(this).hasClass("da-ye")) {
  259. $(".daye-hover").hide();
  260. } else if($(this).hasClass("kda-li")) {
  261. $(".kda-hover").hide();
  262. } else {
  263. $(".kill-hover").hide();
  264. }
  265. });
  266. //近7天英雄使用次数
  267. $("#heroTime").html(statPosition.total_game);
  268. },
  269. _renderKDADetail: function(data) {
  270. var template = _.template($("#kdaTemp").html());
  271. $("#kda").html(template({
  272. data: data
  273. }));
  274. },
  275. _renderHistory: function(data) {
  276. // var continueUsed = data.continuousWinOrLose;
  277. var nextFirstWinTime = data.timestamp_until_next_first_win_bonus;
  278. //如果 是未来某个一个时刻的 Unix 时间戳,展示灰色图标,格式化为倒计时,tips显示 “首胜:x小时x分”;
  279. //如果 是过去某个时刻时间戳,表示首胜可用,展示金色图标,tips显示 “首胜可用”;
  280. //如果是 null ,表示“数据缺失”,不展示图标和tips。
  281. if (nextFirstWinTime) {
  282. var firstWinSeconds = nextFirstWinTime * 1000;
  283. var now = new Date().getTime();
  284. var tipMsg = '', $target = $("#firstMsg");
  285. if(firstWinSeconds > now) {
  286. var gap = firstWinSeconds - now;
  287. //计算出相差天数
  288. var days=Math.floor(gap/(24*3600*1000))
  289. //计算出小时数
  290. var leave1=gap % (24*3600*1000) //计算天数后剩余的毫秒数
  291. var hours = Math.floor(leave1 / (3600*1000))
  292. //计算相差分钟数
  293. var leave2 = leave1 % (3600*1000) //计算小时数后剩余的毫秒数
  294. var minutes = Math.floor(leave2 / (60*1000))
  295. tipMsg = hours + "小时" + minutes + "分钟";
  296. } else {
  297. tipMsg = '首胜可用';
  298. $target.addClass('active-status');
  299. }
  300. $(".f-time").html(tipMsg);
  301. $target.on('mouseenter', function(e) {
  302. e.preventDefault();
  303. $('.first-win-hover').show();
  304. }).on('mouseleave', function(e) {
  305. e.preventDefault();
  306. $('.first-win-hover').hide();
  307. });
  308. }
  309. //连胜、连败
  310. // if(continueUsed.statCode != -1) {
  311. // var template1 = _.template($("#flagTemp").html());
  312. // $("#flag").html(template1({
  313. // data: continueUsed
  314. // }));
  315. // }
  316. //最近战绩
  317. var template = _.template($("#historyTemp").html());
  318. var data = data.game_recent_list;
  319. setTimeout(function() {
  320. $("#history").html(template({
  321. data: data
  322. })).niceScroll({
  323. cursorcolor: "#D9D9D9",
  324. cursorwidth: "8",
  325. cursoropacitymin: 1,
  326. cursorborder: 'none'
  327. });
  328. });
  329. },
  330. _renderHero: function(data) {
  331. if(data) {
  332. commonHeroHoverData = data.champion_performance_list;
  333. var self = this;
  334. //英雄浮窗数据获取url
  335. var baseUrl = "http://lolbox.duowan.com/new/api/index.php?_do=personal/mostusedchampion&serverName=" + this.serverName + "&playerName=" + this.playerName;
  336. data.heroWindowUrl = baseUrl;
  337. var template = _.template($("#commonHeroTemp").html());
  338. $("#commonHeroContent").html(template({
  339. data: data
  340. }));
  341. var enterTimeout, leaveTimeout, canBeClosed;
  342. //常用英雄浮窗
  343. $("body").delegate('#commonHero>li', 'mouseenter', function(e) {
  344. e.preventDefault();
  345. if($(this).hasClass('default')) return;
  346. var tag= $(e.target)[0].tagName;
  347. var $el = /LI/g.test(tag) ? $(e.target) : $(e.target).parents('li');
  348. var pos_left = $el.offset().left + $el.outerWidth() + 6;
  349. var pos_top = $el.offset().top + ($el.outerHeight() / 2) - 80;
  350. var name = $el.attr('data-name');
  351. var index = $el.attr('data-index');
  352. //从commonHeroHoverData缓存中获取对应的英雄数据
  353. var tdata = commonHeroHoverData[index];
  354. clearTimeout(enterTimeout);
  355. clearTimeout(leaveTimeout);
  356. canBeClosed = true;
  357. if($(".hover-content")) $(".hover-content").remove();
  358. var $hContent = $("<div class='hover-content personal-hover'></div>");
  359. if(tdata.length == 0) {
  360. tdata.noData = true;
  361. tdata.tipMsg = "因长时间没有游戏,数据无法采集";
  362. $hContent.addClass("blank-content");
  363. }
  364. //截图url
  365. var shotUrl = "http://lolbox.duowan.com/playerDetail/championsStatShareShot.php?serverName=" + self.serverName + "&playerName=" + self.playerName + "&ch=" + name + "&openmode=default";
  366. tdata.shotUrl = shotUrl;
  367. var template = _.template($("#heroHoverTemp").html());
  368. $hContent.html(template({
  369. data: tdata
  370. })).css({
  371. left: pos_left,
  372. top: pos_top
  373. });
  374. enterTimeout = setTimeout(function(){
  375. $('body').append($hContent);
  376. if (SnsShare) SnsShare.init();
  377. }, 100);
  378. });
  379. $("body").delegate('#commonHero>li', 'mouseleave', function(e) {
  380. e.preventDefault();
  381. clearTimeout(enterTimeout);
  382. clearTimeout(leaveTimeout);
  383. leaveTimeout = setTimeout(function(){
  384. if(canBeClosed) $(".hover-content").remove();
  385. }, 100);
  386. $(".hover-content").on('mouseenter', function(e) {
  387. canBeClosed = false;
  388. }).on('mouseleave',function(e) {
  389. clearTimeout(leaveTimeout);
  390. leaveTimeout = setTimeout(function(){
  391. $(".hover-content").remove();
  392. }, 100);
  393. });
  394. });
  395. }
  396. },
  397. _renderHeroHover: function(name) {
  398. //英雄浮窗数据获取url
  399. var baseUrl = "http://lolbox.duowan.com/new/api/index.php?_do=personal/mostusedchampion&serverName=" + this.serverName + "&playerName=" + this.playerName;
  400. var url = baseUrl + "&zhName=" + name + "&playerId=" + this.playerId;
  401. return $.ajax({
  402. url: url,
  403. type: 'GET',
  404. dataType: 'jsonp'
  405. });
  406. },
  407. _renderGameCount: function(data) {
  408. // 经典模式
  409. var template = _.template($("#gameCountTemp").html());
  410. $("#gameCount").html(template({
  411. data: data
  412. }));
  413. //2015赛季
  414. var template1 = _.template($("#gameCount2015Temp").html());
  415. $("#gameCount2015").html(template1({
  416. data: data
  417. }));
  418. },
  419. _moreGameResult: function() {
  420. var href = "recordlist.html?serverName=" + urlServerName + "&playerName=" + urlPlayerName+'&userId='+userId+'&gameZone='+gameZone;
  421. $("#moreGameResult").attr("href", href);
  422. },
  423. _moreCommonHero: function() {
  424. var href = "championsStat.html?serverName=" + this.serverName + "&playerName=" + this.playerName;
  425. $("#moreCommonHero").attr("href", href);
  426. }
  427. };
  428. function getURLParam(name) {
  429. var value = location.search.match(new RegExp("[?&]" + name + "=([^&]*)(&?)", "i"));
  430. return value ? decodeURIComponent(value[1]) : value;
  431. }
  432. var personal = new Personal();
  433. personal.init();
  434. });