1 |
- "use strict";!function(){function e(e){this.tabType=1,this.username=e.username,this.nickname=e.nickname,this.avatar=e.avatar,this.alreadyHaveCount=99,this.haveListReady=this.allListReady=!1,this.options=e,this.allMedalList=[],this.haveMedalList=[],this.spmMap=[{type:"创作",name:["持之以恒","1024勋章","1024超级勋章","勤写标兵Lv1","勤写标兵Lv2","博客之星","原力探索","原力突破","原力王者","学习力","原力新人","原力探索","原力突破"],spm:"3001.4299"},{type:"贡献",name:["分享学徒","分享小兵","分享达人","分享精英","分享宗师","分享王者","至尊王者"],spm:"3001.4300"}],this.getData()}var a="http"===location.protocol.substr(0,4)?"":"https:",t=a+"//g.csdnimg.cn/user-medal/1.0.6/user-medal.css",n="https://g-api.csdn.net";!function(e){var a=document.createElement("link");a.rel="stylesheet",a.type="text/css",a.href=e,document.getElementsByTagName("head")[0].appendChild(a)}(t),e.prototype={constructor:e,init:function(){this.haveListReady&&this.allListReady&&(this.setReportPoint(),this.renderMain(),this.renderTab(),this.renderNav())},close:function(){this.$box&&this.$box.remove()},setReportPoint:function(){var e=this;try{this.spmMap=this.spmMap.reduce(function(e,a){return a.name.forEach(function(t){e[t]={name:t,spm:a.spm,type:a.type}}),e},{}),this.allMedalList=this.allMedalList.map(function(a,t){return a.medals=a.medals.map(function(n){var s=e.spmMap[n.name]||{};return n.mod="1595992524_00"+(t+1),s.spm&&(n.spm=s.spm),e.spmMap[n.name]=Object.assign({},s,{mod:n.mod,type:a.name,name:n.name}),n}),a}),this.haveMedalList=this.haveMedalList.map(function(a){var t=e.spmMap[a.name]||{};return Object.assign({},a,t)})}catch(e){void 0}},renderMain:function(){var e=this,a=$('<div id="user-medal">\n <div class="user-medal-container">\n <div class="user-medal-head"> \n <img src="'+e.avatar+'">\n <span class="user-medal-close"></span>\n </div>\n <div class="user-medal-body">\n <div class="user-medal-content medal-clearfix">\n <div class="medal-content-body"></div>\n </div>\n </div>\n </div>\n </div>');e.$box=a,a.find(".user-medal-close").click(function(){e.close()}),a.on("click",function(a){$(a.target).find(".user-medal-container").length&&e.close()}),$("body").find("#user-medal").remove(),$("body").append(a)},renderTab:function(){var e=this,a=$('<div class="user-medal-tab medal-clearfix">\n <p class="medal-fl medal-user-info">\n <span class="medal-name" title="'+e.nickname+'">'+e.nickname+'</span>\n <span class="medal-count">已经获得'+e.alreadyHaveCount+'枚勋章啦</span>\n </p>\n <p class="medal-fr medal-user-btns">\n <span class="medal-btn medal-btn-tab" data-type=1>已拥有</span>\n <span class="medal-btn medal-btn-tab" data-type=2>成就勋章</span>\n </p>\n </div>');a.find(".medal-user-btns .medal-btn").click(function(a){$(this).hasClass("medal-btn-red")||(e.resetScrollTop(),e.tabType=$(this).data("type"),e.toggleNav(),1===e.tabType?e.renderMedal(e.haveMedalList):e.$nav.find("li").first().click(),$(this).addClass("medal-btn-red").siblings().removeClass("medal-btn-red"))}),this.$box.find(".user-medal-body").prepend(a),this.$box.find(".medal-btn-tab[data-type="+this.tabType+"]").click()},toggleNav:function(){this.$nav&&this.$nav.toggle()},renderNav:function(){var e=this,a=e.allMedalList;if(a){var t=$('<ul class="medal-content-nav medal-fl"></ul>');a.forEach(function(e,a){t.append('<li class="medal-nav-item" data-list-index='+a+">"+e.name+"<span>("+e.haveCount+"/"+e.totalCount+")</span></li>")}),t.on("click","li",function(a){if(!$(this).hasClass("active")){var t=$(this),n=t.data("list-index");t.addClass("active").siblings().removeClass("active"),e.resetScrollTop(),e.renderMedal(e.allMedalList[n].medals)}}),e.$nav=t,e.$box.find(".user-medal-content").prepend(t),e.toggleNav()}},renderMedal:function(e,a){var t="",n=this.isSelf();e="string"==typeof e?JSON.parse(e):e,e.forEach(function(e){var a=e.mod?{mod:e.mod,extend1:e.name}:{};e.spm&&(a.spm=e.spm),t+='<div class="medal-detail-item">\n <img src="'+e.imageUrl+'">\n <p class="medal-detail-item-title">'+e.name+'</p>\n <p class="medal-detail-item-desc">'+e.description+"</p>\n "+(e.sourceUrl?'<a href="'+e.sourceUrl+'" '+(e.mod?"data-report-click="+JSON.stringify(a):"")+" "+(e.spm?'data-report-query="spm='+e.spm+'"':"")+' target="_blank" class="medal-detail-item-btn">'+(n?"去获得":"我也要")+"</a>":'<a class="medal-detail-item-btn" style="visibility:hidden">隐藏<a/>')+"\n </div>"}),0===e.length&&(t='<div class="medal-detail-item-empty"></div>'),this.$box.find(".medal-content-body").html(t)},getCookie:function(e){var a={};return document.cookie.replace(/([^=;\s]+)=([^=;\s]+)/g,function(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];var s=t[1],i=t[2];a[s]=i}),e?a[e]:a},isSelf:function(){return this.username===this.getCookie("username")},getHaveMedalList:function(){var e=this;$.ajax({url:n+"/user-medal/v1/medal/get/alreadyHave",type:"get",data:{username:e.username},contentType:"application/json",dataType:"json",success:function(a){a.data&&(e.haveMedalList=a.data,e.haveListReady=!0,e.init())},error:function(e){}})},getAllMedalList:function(){var e=this;$.ajax({url:n+"/user-medal/v1/medal/get/all",type:"get",data:{username:e.username},contentType:"application/json",dataType:"json",success:function(a){a.data&&(e.allMedalList=a.data.medalList,e.alreadyHaveCount=a.data.alreadyHaveCount,e.allListReady=!0,e.init())},error:function(e){}})},getData:function(){this.getHaveMedalList(),this.getAllMedalList()},resetScrollTop:function(){this.$box.find(".medal-content-body").scrollTop(0)}},window.csdn=window.csdn||{},window.csdn.userMedal={show:function(a){return new e(a)}},$(document).on("click","[csdn-user-medal-btn=true], .csdn-user-medal-btn",function(e){var a=$(this),t=a.data("username"),n=a.data("nickname"),s=a.data("avatar");n&&t&&s&&window.csdn.userMedal.show({username:t,avatar:s,nickname:n})})}();
|