achievement.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. @ 个人成就页面拆分逻辑 by zengfukun@foxmail.com
  3. @ 2016/7/29
  4. */
  5. var App = {
  6. init: function() {
  7. var self = this;
  8. // 初始化数据
  9. self.yyuid = $.cookie('yyuid');
  10. self.userId = self.Kit.getArgs('userId');
  11. self.zone = self.Kit.getArgs('gameZone');
  12. self.Services = 'http://api.lolbox.duowan.com/api/v2/player/'+ self.zone +'/'+ self.userId +'/achievements/';
  13. // 业务初始化
  14. self.getUserAchievement();
  15. self.bindEvent();
  16. },
  17. // 事件绑定
  18. bindEvent: function() {
  19. var self = this;
  20. // 设置称号
  21. $(document).on('click', '.lock-wrap li', function() {
  22. var id = $(this).index();
  23. var isFinish = $(this).hasClass('finish');
  24. var isActive = $(this).hasClass('active');
  25. if ( isFinish && !isActive ) {
  26. // 防刷过滤
  27. if ( self.waiting ) return false;
  28. self.setUserTag(id);
  29. }
  30. })
  31. },
  32. // 获取用户个人成就
  33. getUserAchievement: function() {
  34. var self = this;
  35. $.ajax({
  36. url: self.Services,
  37. dataType: 'jsonp',
  38. data: {
  39. 'action': 'achievement'
  40. },
  41. success: function(o,status) {
  42. if ( status == 'success' ) {
  43. var result = o.personal_achievement;
  44. // 模板渲染
  45. var template = _.template($("#achievement-tpl").html());
  46. $(".mod-tabs-bd").html(template({
  47. data: result
  48. }));
  49. // 饼图初始化
  50. self.renderEchart(result);
  51. }
  52. }
  53. });
  54. },
  55. // 设置称号
  56. setUserTag: function(id) {
  57. var self = this;
  58. // 防刷加锁
  59. self.waiting = true;
  60. $.ajax({
  61. url: self.Services,
  62. dataType: 'jsonp',
  63. data: {
  64. 'action' : 'display',
  65. 'display_title' : id + 1 // 后端默认0位空
  66. },
  67. success: function(o, status) {
  68. if ( status == 'success' ) {
  69. // 勋章显示
  70. var medalSrc = $('.lock-wrap li').eq(id).find('img').attr('src');
  71. $('.userMedal').attr('src', medalSrc);
  72. $('.lock-wrap li')
  73. .removeClass('active')
  74. .eq(id).addClass('active');
  75. alert('称号设置成功');
  76. // 防刷解锁
  77. setTimeout(function() {
  78. self.waiting = false;
  79. }, 2*1000);
  80. }else{
  81. alert('称号设置失败');
  82. }
  83. }
  84. });
  85. },
  86. // 渲染饼图
  87. renderEchart: function(data) {
  88. var self = this;
  89. // 三杀
  90. var dp1 = calcRate(data.triple_kill, data.display_num);
  91. $('#threeKillContainer').highcharts($.extend({}, self.highchartsOpts, {
  92. colors: self.defaultColors,
  93. series: [{
  94. colorByPoint: true,
  95. data: [{
  96. y: dp1.all
  97. }, {
  98. y: dp1.num
  99. }]
  100. }]
  101. }));
  102. // 五杀
  103. var dp2 = calcRate(data.penta_kill, data.display_num);
  104. $('#fiveKillContainer').highcharts($.extend({}, self.highchartsOpts, {
  105. colors: self.defaultColors,
  106. series: [{
  107. colorByPoint: true,
  108. data: [{
  109. y: dp2.all
  110. }, {
  111. y: dp2.num
  112. }]
  113. }]
  114. }));
  115. // 超神
  116. var dp3 = calcRate(data.god_like, data.display_num);
  117. $('#superGodContainer').highcharts($.extend({}, self.highchartsOpts, {
  118. colors: self.defaultColors,
  119. series: [{
  120. colorByPoint: true,
  121. data: [{
  122. y: dp3.all
  123. }, {
  124. y: dp3.num
  125. }]
  126. }]
  127. }));
  128. // CARRY
  129. var dp4 = calcRate(data.carry_num, data.display_num);
  130. $('#carryContainer').highcharts($.extend({}, self.highchartsOpts, {
  131. colors: self.defaultColors,
  132. series: [{
  133. colorByPoint: true,
  134. data: [{
  135. y: dp4.all
  136. }, {
  137. y: dp4.num
  138. }]
  139. }]
  140. }));
  141. // MVP
  142. var dp5 = calcRate(data.mvp_num, data.display_num);
  143. $('#mvpContainer').highcharts($.extend({}, self.highchartsOpts, {
  144. colors: self.defaultColors,
  145. series: [{
  146. colorByPoint: true,
  147. data: [{
  148. y: dp5.all
  149. }, {
  150. y: dp5.num
  151. }]
  152. }]
  153. }));
  154. // 比例换算 - 包装返回数据
  155. function calcRate(num, all) {
  156. return {
  157. num: (num / all)*100 > 100 ? 100 : (num / all)*100,
  158. all: (num / all)*100 > 100 ? 0 : ( 100 - (num / all)*100 )
  159. }
  160. }
  161. }
  162. }
  163. // 工具箱
  164. App.Kit = {
  165. getArgs: function(key) {
  166. var value = location.search.match(new RegExp("[?&]" + key + "=([^&]*)(&?)", "i"));
  167. return value ? decodeURIComponent(value[1]) : value;
  168. }
  169. }
  170. App.init();