record_v3.0.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. $(function() {
  2. var recordDetailTpl = __inline("../tpl/recordDetail.tmpl");
  3. var recordDataLayerTpl = __inline("../tpl/recordDataLayer.tmpl");
  4. var boxContentTpl = __inline("../tpl/boxContent.tmpl");
  5. var Record = function() {
  6. this.baseUrl = "http://api.lolbox.duowan.com/api/v2/player/";
  7. this.boxServerName = encodeURIComponent($.cookie("boxServerName"));
  8. this.boxPlayerName = encodeURIComponent($.cookie("boxPlayerName"));
  9. this.serverName = encodeURIComponent(getURLParam("serverName"));
  10. this.playerName = encodeURIComponent(getURLParam("playerName"));
  11. this.historyId = getURLParam("historyId");
  12. this.gameZone = getURLParam("gameZone");
  13. this.userId = getURLParam("userId");
  14. this.boxUserId = $.cookie("boxUserId");
  15. this.championId = getURLParam("championId");
  16. this.hero = getURLParam("hero");
  17. this.favorate = 0;
  18. this.suffix = "&serverName=" + this.serverName + "&playerName=" + this.playerName;
  19. if(this.hero) this.suffix += "&hero=" + this.hero;
  20. this.p = 1;//页码
  21. this.listData = null;//缓存list列表数据到本地
  22. this.$loading = $("#loading");
  23. this.$boxContent = $(".recent-record__list");
  24. this.$recordDetail = $("#recordDetail");
  25. this.$recordDataLayer = $("#recordDataLayer");
  26. this.$collect = $("#collect");
  27. this.$recordTarget = $("#recordTarget");
  28. this.$body = $("body");
  29. this.$wanScriptName = $("#wanScriptName");
  30. this.navStatus = 1; // 默认个人成就
  31. //饼图默认设置
  32. this.highchartsOpts = {
  33. chart: {
  34. type: 'pie'
  35. },
  36. plotOptions: {
  37. series: {
  38. dataLabels: {
  39. enabled: false
  40. }
  41. }
  42. },
  43. tooltip: {
  44. enabled:false
  45. },
  46. drilldown: {
  47. series: null
  48. }
  49. };
  50. this.init();
  51. };
  52. Record.prototype = {
  53. constructor: Record,
  54. init: function() {
  55. var self = this;
  56. self.bindEvents();
  57. self._getListData();
  58. $('#modeTabs>li').each(function(item) {
  59. var pageName = $(this).data('name');
  60. $(this).find('a').attr('href', pageName + '.html?serverName=' + self.serverName + '&playerName=' + self.playerName + '&userId=' + self.userId + '&gameZone=' + self.gameZone);
  61. });
  62. // 非本人才显示返回按钮
  63. if ( this.boxUserId != this.userId ) {
  64. $('.common-page__goBack').css('visibility', 'visible');
  65. }
  66. },
  67. // 事件绑定
  68. bindEvents: function() {
  69. var self = this;
  70. //我的收藏/TA的收藏r
  71. this.$collect.on('click', function(e) {
  72. e.preventDefault();
  73. var filterData = [];
  74. for(var i=0,len=self.listData.length;i<len;i++) {
  75. var item = self.listData[i];
  76. if(item.collect) {
  77. filterData.push(item);
  78. }
  79. }
  80. self.listData = filterData;
  81. var renderArr = filterData.slice(0, 8);
  82. self.$recordDetail.html('');
  83. self._renderList(renderArr);
  84. });
  85. //分页上一页、下一页点击
  86. this.$body.delegate(".zj-page .zj-s", "click", function(e) {
  87. e.preventDefault();
  88. var page = $(this).attr("data-page");
  89. if(page == 0 || page == (self.totalPage + 1)) return;
  90. self.p = page;
  91. self._renderList(self.listData.slice((page - 1) * 7, page * 7));
  92. });
  93. //分页点击去到哪一页
  94. this.$body.delegate(".zj-page .go-page", "click", function(e) {
  95. e.preventDefault();
  96. var page = $(".zj-cur").val();
  97. if(page <= 0 || page >= (self.totalPage + 1)) return;
  98. self.p = page;
  99. self._renderList(self.listData.slice((page - 1) * 7, page * 7));
  100. });
  101. //enter
  102. this.$body.delegate(".zj-page .zj-cur", "keyup", function(e) {
  103. if(event.keyCode==13) {
  104. var page = $(this).val();
  105. if(page <= 0 || page >= (self.totalPage + 1)) return;
  106. self.p = page;
  107. self._renderList(self.listData.slice((page - 1) * 8, page * 8));
  108. }
  109. });
  110. //点击左边栏li item时拿到其matchId并获取其对应detail数据
  111. this.$body.delegate(".l-box .recent-list li", "click", function(e) {
  112. e.preventDefault();
  113. var id = $(this).attr("data-id"),
  114. name = $(this).attr("data-name"),
  115. enname = $(this).attr("data-enname");
  116. $(this).addClass("hover").siblings().removeClass("hover");
  117. self.matchId = id;
  118. self._getDetailDataDone(id, name, enname);
  119. });
  120. //收藏/取消收藏
  121. this.$body.delegate(".fav-btn", "click", function(e) {
  122. e.preventDefault();
  123. e.stopPropagation();
  124. var _this = this;
  125. var isAdd = $(this).hasClass("add-fav");
  126. var action = isAdd ? 'collect' : 'delete';
  127. var id = $(this).attr("data-id");
  128. var url = "http://api.lolbox.duowan.com/api/v2/player/" + self.gameZone + "/" + self.userId + "/game/" + action + "/?game_id=" + id;
  129. $.ajax({
  130. url: url,
  131. type: 'get',
  132. dataType: 'jsonp',
  133. success: function(result) {
  134. if ( result ) {
  135. if( isAdd ) {
  136. $( _this ).removeClass("add-fav").addClass("fav").attr("title", "点击删除此收藏");
  137. } else {
  138. if( confirm('确认删除此收藏吗?') ) {
  139. $( _this ).removeClass("fav").addClass("add-fav").attr("title", "点击收藏比赛");
  140. }
  141. }
  142. }
  143. }
  144. });
  145. });
  146. this.$body.delegate(".zj-table tr a", "click", function(e) {
  147. var userid = $(this).attr("user-id");
  148. var sn = $(this).attr("game-zone");
  149. var pn = $(this).attr("data-playerName");
  150. if(!userid || userid == 0) {
  151. location.href = 'http://lolbox.duowan.com/staticPub/v3.1/playerList.html?sn=' + sn + '&pn=' + encodeURIComponent(pn);
  152. return false;
  153. }
  154. })
  155. // 详细战绩&个人成就导航
  156. this.$body.delegate(".record-nav li", "click", function(e) {
  157. e.preventDefault();
  158. e.stopPropagation();
  159. self.navStatus = $(this).index(); // 记录导航状态
  160. var tab = $(this).data('tab');
  161. $(this).addClass('active').siblings().removeClass('active');
  162. $('.tab-content').hide();
  163. $('#' + tab).show();
  164. // 导航点击统计
  165. var curIndex = $(this).index();
  166. var StatId = [
  167. {'id': 'click/grzj', 'desc': '点击/个人战绩'},
  168. {'id': 'click/xxzj', 'desc': '点击/详细战绩'}
  169. ];
  170. try{
  171. window.external.data_report( StatId[curIndex].id, StatId[curIndex].desc );
  172. }catch(e){}
  173. });
  174. // 返回上一页
  175. $('.common-page__goBack').on('click', function() {
  176. window.history.back();
  177. return false;
  178. });
  179. // 刷新按钮
  180. $('.common-page__reflash').on('click', function() {
  181. location.reload();
  182. });
  183. /*鼠标悬浮在用户头像的弹窗*/
  184. $(document).on('mouseenter', '.history-recordTab__userIcon', function() {
  185. self.userRecordDetail();
  186. }).on('mouseleave', '.history-recordTab__userIcon', function() {
  187. setTimeout(function() {
  188. var isHover = $('.userRecordDetail').hasClass('hover');
  189. if ( !isHover ) {
  190. $('.userRecordDetail').hide();
  191. }
  192. }, 0.5*1000)
  193. });
  194. $(document).on('mouseenter', '.userRecordDetail', function() {
  195. $(this).addClass('hover');
  196. }).on('mouseleave', '.userRecordDetail', function() {
  197. $(this).removeClass('hover').hide();
  198. });
  199. // 近期战绩下拉筛选列表 - 导航
  200. $(document).on('click', '.recent-nav__input', function() {
  201. $(this).toggleClass('on');
  202. $('.recent-nav__list').toggle();
  203. })
  204. // 近期战绩下拉筛选列表 - 选项
  205. $(document).on('click', '.recent-nav__list li', function(e) {
  206. e.preventDefault();
  207. var key = $(this).find('a').html();
  208. $('.recent-nav__input')
  209. .removeClass('on')
  210. .find('.key').html(key);
  211. $('.recent-nav__list').hide();
  212. self._getListData(key);
  213. })
  214. },
  215. // 获取左侧比赛列表
  216. _getListData: function(type) {
  217. var self = this;
  218. var game_type = {'匹配赛': 3, '排位赛': 4, '大乱斗': 6};
  219. var url = this.baseUrl + this.gameZone + '/'+this.userId+'/game_recent/';
  220. var args = {};
  221. self.championId && ( args.champion_id = self.championId );
  222. game_type[type] && ( args.game_type = game_type[type] );
  223. $.ajax({
  224. url: url,
  225. data: args,
  226. type: 'GET',
  227. dataType: 'jsonp',
  228. success: function(data, status, xhr) {
  229. if (xhr && xhr.status && xhr.status == 200) {
  230. var list = data.game_list;
  231. var renderArr = list.slice(0, 7);
  232. self.isSelf = data.is_myself;
  233. self.listData = list;
  234. self.totalPage = Math.ceil(list.length / 7);
  235. self._renderList(renderArr);
  236. //从数组第一个元素中获取到serverName的中文名
  237. var sn_cn = list[0] ? list[0].game_zone.alias : null;
  238. var crumbs = sn_cn ? decodeURIComponent(self.playerName) + "(" + sn_cn + ")" : decodeURIComponent(self.playerName);
  239. self.$recordTarget.attr("href", "playerDetail.html?serverName=" + self.serverName + "&playerName=" + self.playerName + "&userId=" + self.userId + "&gameZone=" + self.gameZone)
  240. .html(crumbs);
  241. }
  242. }
  243. });
  244. },
  245. // 获取玩家比赛详情
  246. _getDetailData: function(id) {
  247. var url = this.baseUrl + this.gameZone + "/"+this.userId+"/game/"+ id + '/';
  248. return $.ajax({
  249. url: url,
  250. type: 'GET',
  251. dataType: 'jsonp'
  252. });
  253. },
  254. _getDetailDataDone: function(id, name, enname) {
  255. var self = this;
  256. this.$loading.show();
  257. self.$recordDetail.hide();
  258. this._getDetailData(id).done(function(data) {
  259. self._renderDetail(data, name, enname);
  260. self.$loading.hide();
  261. self.$recordDetail.show();
  262. }).fail(function() {
  263. self.$loading.hide();
  264. self.$recordDetail.show();
  265. });
  266. },
  267. /*渲染左侧的战绩列表*/
  268. _renderList: function(data) {
  269. var self = this;
  270. // 左侧列表渲染
  271. data.totalPage = self.totalPage;
  272. data.p = self.p;
  273. data.isSelf = self.isSelf;
  274. self.$boxContent.html(boxContentTpl({data: data}));
  275. // 历史战绩对应详情初始化
  276. if(data && data.length > 0) {
  277. if ( self.historyId ) {
  278. getHistoryData(self.historyId);
  279. }else{
  280. getHistoryData(0);
  281. }
  282. }
  283. // 获取左侧第N个li历史战绩数据
  284. function getHistoryData(index) {
  285. var champion = data[index].champion,
  286. enname = champion.name,
  287. name = champion.display_name.concat(champion.title);
  288. self.matchId = data[index].game_id;
  289. self._getDetailDataDone(self.matchId, name, enname);
  290. $('.recent-list li').eq(index).addClass('hover');
  291. }
  292. },
  293. /*渲染历史战绩 - 个人成就*/
  294. _renderDetail: function(data, name, enname) {
  295. var template, self = this;
  296. if(data.player_game_list) {
  297. // 模板渲染
  298. this.$recordDetail.html(recordDetailTpl({data: data}));
  299. this.$recordDataLayer.html(recordDataLayerTpl({data: data}));
  300. // 显示显示默认或记录的导航位置
  301. $('.record-nav li').eq(self.navStatus).click();
  302. // 获取推荐视频
  303. self.positionRender(data.player_game_list[0]);
  304. } else {
  305. template = "<div class='tip-no-data'><img src='img/clothes-commit-fail.png'><p>由于未知原因,此场比赛消失在遥远的二次元空间中。</p></div>";
  306. this.$recordDetail.html(template);
  307. }
  308. },
  309. // 位置数据渲染
  310. positionRender: function(data) {
  311. // 位置计算
  312. var position_map = {'is_support': '辅助', 'is_adc': '输出', 'is_jungler': '打野'};
  313. var loseData = data.team_lose.player_champions;
  314. var allData = data.team_win.player_champions.concat(loseData); //输的和赢的数据合并
  315. var userId = data.player_champion.player.user_id;
  316. var key = getPositionKey(allData);
  317. $('.historyRecord-userInfo__position').html(position_map[key]);
  318. // 遍历战绩
  319. function getPositionKey(o) {
  320. for (var i = 0; i < o.length; i++) {
  321. var itemUserId = o[i].player.user_id;
  322. if ( userId == itemUserId ) {
  323. if ( o[i].is_jungler ) {
  324. return 'is_jungler';
  325. break;
  326. }
  327. if ( o[i].is_support ) {
  328. return 'is_support';
  329. break;
  330. }
  331. if ( o[i].is_adc ) {
  332. return 'is_adc';
  333. break;
  334. }
  335. }
  336. }
  337. }
  338. return position_map[key];
  339. },
  340. /*
  341. * @ 历史战绩-个人战绩TAB详细战绩弹窗
  342. * @ 注意!!!这里有坑,数据是来自详细战绩弹窗
  343. */
  344. userRecordDetail: function() {
  345. var self = this;
  346. var userName = decodeURIComponent(self.playerName).replace(/(^\s+)|(\s+$)/g,"");
  347. var recordList = $('.data-layer .layer');
  348. /*从历史战绩-个人战绩TAB #data-layer列表中拿到弹窗数据*/
  349. for (var i = 0; i < recordList.length; i++) {
  350. var listName = $(recordList[i]).find('.tip-user-name').text().replace(/(^\s+)|(\s+$)/g,"");
  351. if ( userName == listName.replace(/^\s+/,"") ) {
  352. var dom = $(recordList[i]).html();
  353. $('.userRecordDetail-con').html(dom);
  354. getUserLevel();
  355. break;
  356. }
  357. }
  358. /*获取用户段位*/
  359. function getUserLevel() {
  360. $.ajax({
  361. url: 'http://api.lolbox.duowan.com/api/v2/player/'+ self.gameZone+'/'+ self.userId+'/',
  362. dataType: 'jsonp',
  363. success: function(o) {
  364. var data = o && o.player_list && o.player_list[0];
  365. var tierRank = data.tier_rank, tierName;
  366. if (tierRank && tierRank['tier'] && tierRank['tier']['const'] > 0) {
  367. tierName = tierRank['tier']['name_cn'] + ' ' + tierRank['rank']['name'];
  368. } else {
  369. tierName = '-';
  370. }
  371. var dom = '<div class="tip-level"></div><div class="tip-topright"><p><em class="zj-zdl zj-s"></em>' + data.box_score + '</p><p class="zj-blue">' + tierName + '</p></div>';
  372. $('.userRecordDetail .tip-topright-wrap').html(dom);
  373. $('.userRecordDetail').show();
  374. }
  375. });
  376. }
  377. }
  378. };
  379. // URL解析
  380. function getURLParam(name) {
  381. var value = location.search.match(new RegExp("[?&]" + name + "=([^&]*)(&?)", "i"));
  382. return value ? decodeURIComponent(value[1]) : value;
  383. }
  384. // 删除字符串空格
  385. function Trim(str,is_global){
  386. var result;
  387. result = str.replace(/(^\s+)|(\s+$)/g,"");
  388. if(is_global.toLowerCase()=="g")
  389. result = result.replace(/\s/g,"");
  390. return result;
  391. }
  392. new Record();
  393. });