$(function () { refreshtime(); var letterid = $("#letterid").val(); var modulename = $("#modulename").val(); var usedpageundefind = replyusedpage = $(".commentlist #usedpage").data(); var treeusedpageundefind = $(".treecomment #usedpage").data(); var replyusedpage = false; var treereplyusedpage = false; if (usedpageundefind != undefined) { replyusedpage = $(".commentlist #usedpage").data().usedpage === "False" ? false : true; } if (treeusedpageundefind != undefined) { treereplyusedpage = $(".treecomment #usedpage").data().usedpage === "False" ? false : true; } //var replyusedpage = $(".commentlist #usedpage").data().usedpage === "False" ? false : true; //var treereplyusedpage = $(".treecomment #usedpage").data().usedpage === "False" ? false : true; var replycount = parseInt($(".commentlist #replycount").val()); var treereplycount = parseInt($(".treecomment #replycount").val()); var urls = $("#urls").data(); function GetTime(datetime) { if (datetime == null) { return ''; } datetime = datetime.split(".")[0]; var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var month = day * 30; var now = new Date().getTime(); var diffDate = now - new Date(datetime.replace(/[-T]/g, '/')).getTime(); if (diffDate < 0) { return ''; } var monthC = diffDate / month; var weekC = diffDate / (7 * day); var dayC = diffDate / day; var hourC = diffDate / hour; var minuteC = diffDate / minute; var result; if (monthC >= 1) { result = parseInt(monthC) + '月前'; } else if (weekC >= 1) { result = parseInt(weekC) + '周前'; } else if (dayC >= 1) { result = parseInt(dayC) + '天前'; } else if (hourC >= 1) { result = parseInt(hourC) + '小时前'; } else if (minuteC >= 1) { result = parseInt(minuteC) + '分钟前'; } else { result = '刚刚'; } return result; }; function refreshtime() { $(".commenttime").each(function () { var value = $(this).data("value"); $(this).html(GetTime(value)); }); } //查看回复 var lookcomment; $(".comments").on("click", ".lookcomment", function () { var replyid = $(this).data().value; lookcomment = $(this); var usedpage = $(this).data().usedpage === "False" ? false : true; ajaxcomment(replycount, letterid, replyid, "commentlist", "评论回复列表", 1, usedpage); $(this).css("display", "none"); $(this).siblings(".dropcomment_" + replyid).css("display", "block"); }); //收起回复 $(".comments").on("click", ".dropcomment", function () { var replyId = $(this).data().value; $(this).parent("li").next("li").next("li").html(""); $(this).css("display", "none"); $(this).siblings(".commentid_" + replyId).css("display", "block"); }); //删除评论 $(".comments").on("click", ".deletecomment", function () { var replyid = $(this).data().value; $.ajaxPreventCSRF({ url: urls.deletecomment, type: 'post', data: { id: replyid }, success: function (response) { if (response.length > 1) { replyid = parseInt(response.substr(response .lastIndexOf('/', response.lastIndexOf('/') - 1) + 1)); } else { replyid = 0; } ajaxcomment(replycount, letterid, replyid, "commentlist", "评论回复列表", 1, replyusedpage); //ajaxcomment(treereplycount, letterid, replyid, "treecomment", "评论回复列表", 1, treereplyusedpage); } }); }); //用户登录 $("[data-userlogindialog=userlogincomment]").on("click", ".login", function () { var url = $("#loginUrl").val(); var validatecode = $(this).parent().prev().find("input[name=ValidateCode]").first().val(); var userName = $(this).parent().prev().find("input[name=UserName]").first().val(); var password = $(this).parent().prev().find("input[name=Password]").first().val(); //获取编码后的用户名密码和验证码。 var getSessionUrl = $(".getSession").val(); var test = {}; $.postPreventCSRF(getSessionUrl, {}, function (data) { test.username = data.username; test.password = data.password; test.validatecode = data.validatecode; loginActual(test, userName, password, validatecode); }, "json"); }); // 登录具体方法。 function loginActual(dataArry, userName, password, validateCode) { var url = $("#loginUrl").val(); GetRsaEncrypt(); userName = rsaEncrypt(userName); password = rsaEncrypt(password); validateCode = rsaEncrypt(validateCode); var data = {}; data[dataArry.username] = userName; data[dataArry.password] = password; data[dataArry.validatecode] = validateCode; $.postPreventCSRF(url, data, function (data) { if (data.status == 0) { var commentId = $("[data-logincommentid = logincommentid]").val(); $(".userlogin_userlogincomment").css("display", "none"); $("[data-userlogindialog=userlogincomment]").css("display", "none"); $(".notlogged").css("display", "none"); $(".loggedin").css("display", "block"); $(".nickname").html(data.NickName); $(".loggedin .exitlogincss").removeClass("exitlogincss"); ajaxCommentLogin(commentId, true); if (commentId != 0) { ajaxCommentLogin(0, false); } refreshcsrf(); if (!data.isstrongpassword) { window.location.href = "/user"; } } if (data.status == 1 || data.status == -1) { $(".errormessage").text(data.message); } if (!data.valcodevisible) { $(".enabledvalidcode").css("display", "none"); } else { $(".enabledvalidcode").css("display", "block"); refreshValidateCode(); } }, "json"); } //登录刷新评论列表 function ajaxCommentLogin(commentId, isRefreshList) { var commentcontent = $(".commentcontent_" + commentId).val(); $.ajax({ url: '/Ajax/AjaxPartial', type: 'post', data: { moduleName: "LetterBox", partialViewName: "发表评论", parameters: "{ commentId:" + commentId + "}" }, success: function (response) { $("#replyBox_" + commentId).replaceWith(response.html); refreshValidateCode(); $(".commentcontent_" + commentId).val(commentcontent); $(".errormessage_" + commentId).text(""); $(".input-validation-error").val(""); if (isRefreshList) { var commentreplylength = $(".dynamicmetadata-tree-comment .commentreply_" + commentId); ajaxcomment(replycount, letterid, 0, "commentlist", "评论回复列表", 1, replyusedpage); //ajaxcomment(treereplycount, letterid, 0, "treecomment", "评论回复列表", 1, treereplyusedpage); if (commentId > 0 && commentreplylength.length > 0) { var replyids = commentreplylength.data().replyids; replyids = replyids.split("/"); $.each(replyids, function (i, value) { if (value != "") { ajaxcomment(replycount, letterid, value, "commentlist", "评论回复列表", 1, replyusedpage); } }); } } } }); } function refreshcsrf() { $.ajax({ url: '/Ajax/AjaxPartial', type: 'post', async: false, data: { partialViewName: "表单防伪标记", }, success: function (response) { $("#ajaxcsrf").html(response.html); var csrfprvvalue = $('#ajaxcsrf input[name="__RequestVerificationToken"]').val(); $('input[name="__RequestVerificationToken"]') .each(function () { var $this = $(this); $this.val(csrfprvvalue); }); } }); } var isshowcomment; //回复评论 $(".comments").on("click", ".addComment", function () { var commentId = $(this).data().value; $(".errormessage").text(""); var currentusernameurl = $('[data-currentusernameurl ="currentusernameurl"]').val(); $.postPreventCSRF(currentusernameurl, {}, function (data) { if (data.nickname === "") { $("[data-logincommentid = logincommentid]").val(commentId); $(".userlogin_userlogincomment").css("display", "block"); $("[data-userlogindialog=userlogincomment]").css("display", "block"); $("input[name=UserName]").val(""); $("input[name=Password]").val(""); $("input[name=ValidateCode]").val(""); $(".errormessage").html(""); refreshValidateCode(); return false; } if (!data.valcodevisible) { $(".enabledvalidcode").css("display", "none"); } else { $(".enabledvalidcode").css("display", "block"); } }); var replycontent = $(".commentcontent_" + commentId).val(); if (replycontent.trim() == "") { $(".commentcontent_" + commentId).focus(); $(".errormessage_" + commentId).text("评论不能为空!"); return false; } $.ajaxPreventCSRF({ url: urls.addcomment + "?ValidateCode=" + $("#ValidateCode_" + commentId).val(), type: 'post', data: { Content: replycontent, replyId: commentId, SourceId: letterid }, success: function (data) { if (data.status == 0) { $("#ValidateCode_" + commentId).val(""); ajaxcomment(replycount, letterid, commentId, "commentlist", "评论回复列表", 1, replyusedpage); //ajaxcomment(treereplycount, letterid, commentId, "treecomment", "评论回复列表", 1, treereplyusedpage); $(".input-validation-error").val(""); $("#replyBox_" + commentId).css("display", "none"); $("#replyBox_0").css("display", "block"); refreshValidateCode(); if (commentId == 0) { ajaxcomment(0, 0, 0, "successnotice_" + commentId, "评论成功提示框"); } else { ajaxcomment(0, 0, commentId, "", "评论成功提示框"); } setTimeout(function () { if ($(".commentSuccess").length > 0) { if (commentId == 0) { $(".successnotice_" + commentId).html(''); } else { $(".dynamicmetadata-tree-comment>#comment_" + commentId).html(''); $(".newtreecomment>#comment_" + commentId).html(''); isshowcomment.siblings(".commentid_" + commentId).css("display", "block"); isshowcomment.siblings(".dropcomment_" + commentId).css("display", "none"); } } }, 5000); $(".commentcontent_" + commentId).val(""); $(".errormessage_" + commentId).text(""); } if (data.status == -1) { $(".errormessage_" + commentId).text(data.message); } } }); }); //关闭登录窗口 $(".closecomment").on("click", function () { $(".userlogin_userlogincomment").css("display", "none"); $("[data-userlogindialog=userlogincomment]").css("display", "none"); }); //显示回复文本框 $(".comments").on("click", ".commentreply", function () { isshowcomment = $(this); var $this = $(this); var commentId = $this.data().value; var currentusername = $(".currentusername").val(); $.postPreventCSRF(currentusername, {}, function (data) { if (data.nickname === "") { $("[data-logincommentid = logincommentid]").val(commentId); $(".userluserlogin_userlogincommentogin").css("display", "block"); $("[data-userlogindialog=userlogincomment]").css("display", "block"); refreshValidateCode(); return false; } if (!data.valcodevisible) { $(".enabledvalidcode").css("display", "none"); } else { $(".enabledvalidcode").css("display", "block"); refreshValidateCode(); } }); var replylogin = $this.parent("li").next("li"); if (replylogin.css("display") == "none") { $(".showlogin").css("display", "none"); $(".showlogin").html(""); replylogin.css("display", "block"); $.ajax({ url: '/Ajax/AjaxPartial', type: 'post', data: { moduleName: "LetterBox", partialViewName: "发表评论", parameters: "{ commentId:" + commentId + "}" }, success: function (response) { replylogin.html(response.html); refreshValidateCode(); } }); } else { $(".showlogin_" + commentId).css("display", "none"); } }); //刷新验证码 function refreshValidateCode() { var $captchaImage = $(''), self = $(".validationcode"); setting = self.data(); $captchaImage.attr('title', '看不清?换一张'); $captchaImage.attr('src', '/captcha/generatecaptcha' + '?code=' + Math.random()); $captchaImage.click(function () { $(".validationcode img").attr('src', '/captcha/generatecaptcha' + '?code=' + Math.random()); }); self.empty(); self.append($captchaImage); } //刷新评论列表 function ajaxcomment(outputcount, letterid, replyid, commentcss, partialViewName, pageid, usedpage) { pageid = pageid > 1 ? pageid : 1; replyid = parseInt(replyid); $.ajax({ url: '/Ajax/AjaxPartial', type: 'post', async: false, data: { moduleName: modulename, partialViewName: partialViewName, parameters: "{ outputCount:" + outputcount + ",pageid:" + pageid + ",sourceId:" + letterid + ",replyId:" + replyid + ",usedpage:" + usedpage + "}" }, success: function (response) { if (pageid > 1) { $(".pagecommont_" + commentcss + "_" + replyid + "_" + pageid).html(response.html); } else { if (replyid === 0) { $("." + commentcss).html(response.html); } else { if (lookcomment) { lookcomment.parent().siblings("#comment_" + replyid).html(response.html); lookcomment = null; } else { $("#comment_" + replyid).html(response.html); } } } refreshtime(); } }); } $(".comments").on("click", ".loadmorecommont", function () { var data = $(this).data(); var usedpage = data.usedpage === "False" ? false : true; ajaxcomment(data.outputcount, letterid, data.replyid, data.type, data.view, data.page, usedpage); }); $(".comments").on("click", ".promptclose", function () { var commentid = $(this).data().commentid; if (commentid > 0) { $('#comment_' + commentid).html(''); isshowcomment.siblings(".commentid_" + commentid).css("display", "block"); isshowcomment.siblings(".dropcomment_" + commentid).css("display", "none"); } else { $('.successnotice_' + commentid).html(''); } }); });