(function(){ //存储私有数据 const _acInfoId = $('#acInfoId').val(); const _acInfoName = $('#acInfoName').val(); const _branchIds = $('#branchIds').val(); const richTextEditorSelector = '.messageContent'; /** * 对layer.msg方法进行简单封装 * @param message 弹出信息内容 */ function popMessage(message){ layer.msg(`${message}`,{time:2000}); } /** * 初始化留言输入框/回复输入框 * @param $richTextBox 用来做富文本容器的对象 * @param options 配置,具体参考{@link trumbowygMod}方法的说明 */ function initMeesageInput($richTextBox,options){ $richTextBox.trumbowygMod(options); //获取富文本键盘事件 $richTextBox.on('keyup', function(){ let content = $richTextBox.trumbowyg().trumbowyg('html'); if(!content){ if($('.trumbowyg-box .placeholder').length) return; $('.messageContent').after('
'+ $richTextBox.attr('placeholder')+'
'); }else { if($('.trumbowyg-box .placeholder').length){ $('.trumbowyg-box .placeholder').remove(); } }; }); } /** * 销毁留言输入框/回复输入框 * @param $richTextBox 用来做富文本容器的对象 */ function destroyMessageInput($richTextBox){ $richTextBox.trumbowyg('destroy'); } /** * 把富文本留言数据转移到隐藏表单中 * @param jqRichText 富文本的JQ对象 * @param jqHiddenInput 隐藏输入框的JQ对象,因为富文本不能直接做表单提交,要把值传递到一个INPUT框中 */ function setRichTextInput(jqRichText,jqHiddenInput){ let content = jqRichText.trumbowyg('html'); jqHiddenInput.val(content); } /** * 清空富文本输入框 * @param jqRichText 富文本的JQ对象 * @param jqHiddenInput 隐藏输入框的JQ对象,因为富文本不能直接做表单提交,要把值传递到一个INPUT框中 */ function clearRichTextInput(jqRichText,jqHiddenInput){ jqRichText.trumbowyg('empty'); jqHiddenInput.val(''); } /** * 自定义表单校验方法, * 校验规则调整,可以不输入用户名,但如果不支持送礼,就要强制输入留言内容,如果支持送礼,则可以在留言或送礼中二选一 * @param arr 表达提交的数据,格式是数组 * @return true表示校验通过,false表示不通过 */ function validateMessageOrReplySubmit(arr){ //下面这段代码的逻辑还需要再调整一下 let themeType = $('#themeType').val(); let content; let presents = []; //默认是不通过的状态 let result = false; if(arr instanceof Array){ arr.forEach((ele)=>{ let fieldName = ele.name; if(fieldName=='creator'){ let val = ele.value.trim(); if(val.length === 0){ //如果没有填写用户名,则默认为"匿名用户" ele.value = '匿名用户'; } } if(fieldName=='content'){ content = ele.value; } if(fieldName=='presents[]'){ presents.push(ele.value); } }); } //获取到内容和送礼之后进行判断 //可以送礼时,只要有礼物,留言内容可以为空,有留言时,可以不送礼 if(themeType=='birthday' || themeType == 'mourn'){ if(content.length == 0 && presents.length == 0){ popMessage('请输入留言'); }else{ result = true; } //不能送礼时,留言一定不能为空 }else if(themeType == 'common') { if(content.length == 0) { popMessage('请输入留言'); }else{ result = true; } } return result; } /** * 初始化礼物选择框,要设置maxPresentSize * @param maxPresentSize 最大礼物勾选数量 */ function initCheckbox(maxPresentSize){ maxPresentSize = parseInt(maxPresentSize); $('.message_present_img').on('click',function(e){ //点击礼物的时候检查已选中数量 let count = $('.message_present_checkbox:checked').size(); if(count>=maxPresentSize){ //之后找到当前label对应的CHECKBOX的选中状态,如果是未选中的,说明即将要勾选上了,此时就禁止默认行为并弹出提示 let forId = $(this).attr('for'); let checkedFlag = $('#'+forId).prop('checked'); if(checkedFlag==false||checkedFlag==undefined){ popMessage('最多只能选择'+maxPresentSize+'个'); e.preventDefault(); } } }); } /** * 获取地理位置信息,是否使用待定 * @param ipAddress IP地址 */ function getGeoLocation(ipAddress){ $.ajax({ url:'http://whois.pconline.com.cn/ipJson.jsp', type:'get', data:{ ip:ipAddress }, dataType:'jsonp', jsonpCallback:'IPCallBack', success:function(data){ if(data instanceof Object){ console.log(data.pro+','+data.city); } } }); } /** * 校验验证码是正确 */ function validateCode(jqForm){ let result = false; let inputCode = jqForm.find('.input_code').val().trim(); let verifyCode = jqForm.find('.verify_code').val(); if(inputCode == verifyCode){ result = true; }else{ layer.msg('请输入正确的验证码!'); } return result; } /** * 初始化留言表单/回复表单,支持动态绑定 * @param $form 表单的jQuery对象 */ function initMessageForm($form){ $form.ajaxForm({ method:'POST', delegation:true, clearForm:true, resetForm:true, beforeSerialize:function(jqForm){ layer.msg('提交中,请勿重复点击'); jqForm.find(':submit').prop('disabled',true); setRichTextInput(jqForm.find(richTextEditorSelector),jqForm.find('.hiddenMessageContent')); }, beforeSubmit:function(arr,jqForm){ let result = validateMessageOrReplySubmit(arr); if(result){ result = validateCode(jqForm); } if(result==false){ jqForm.find(':submit').prop('disabled',false); } return result; }, success:function(data,textStatus,jqXHR,jqForm){ clearRichTextInput(jqForm.find(richTextEditorSelector),jqForm.find('.hiddenMessageContent')); popMessage('感谢您的留言,我们会在审核通过后发布'); //解除提交按钮的禁用 jqForm.find(':submit').prop('disabled',false); //刷新当前验证码 jqForm.find('.verify_code').val(currentCode()); }, error:function(jqXHR,textStatus,error,jqForm){ let data = jqXHR.responseJSON; if(data instanceof Object && data.message != undefined){ popMessage(data.message); } //解除提交按钮的禁用 jqForm.find(':submit').prop('disabled',false); } }); } //初始留言化验证码 $('.birthday_msg_left').find('.verify_code').val(currentCode()); //绑定验证码刷新事件 $('.birthday_msg_box').on('click','.verify_code',function () { $(this).val(currentCode()); }); /** * 随机验证码 */ function currentCode(){ return Math.floor(1000 + Math.random() * 9000); } /** * 初始化快捷短语功能 */ function initMessageTemplate(){ $('.message_template > li').off('click'); $('.message_template > li').on('click', event => { const template = event.currentTarget.innerHTML; const parentForm = $(event.currentTarget).parents('.message_submit_form').eq(0); const editor = parentForm.find(richTextEditorSelector); //获取富文本焦点事件 $('#editor').trumbowyg().on('blur', function(){ var editor = $(this).trumbowyg('getHTML'); if (editor.trim() === '') { $(this).trumbowyg('setHTML', '请输入文本...'); } }); editor.trumbowyg('html',`

${template}

`); }); } function showLoading(){ $('.message_list').hide(); $('.message_loading').show(); $('.message_noResult').hide(); $('.message_pageBar').hide(); // $('.msg_collapse').hide(); } function showResult(){ $('.message_list').show(); $('.message_loading').hide(); $('.message_noResult').hide(); $('.message_pageBar').show(); } function showNoResult(){ $('.message_list').hide(); $('.message_loading').hide(); $('.message_noResult').show(); $('.message_pageBar').hide(); // $('.msg_collapse').hide(); } /** * 初始化留言列表,支持分页 * @param pageSize 每页展示数量,最少2个 */ function initMessageList(pageSize){ if(pageSize<=2){ pageSize = 2; } //默认展示留言加载中 showLoading(); $('.message_pageBar').paginationMod({ dataSource:'/ysgSpecialDay/page', locator:'htmlArray', totalNumberLocator:function(resp){ return resp.totalCount; }, ajax:{data:{acInfoId: _acInfoId}}, pageNumber:1, pageSize:pageSize, showGoInput:false, callback:function(data){ appendData(data); }, afterPreviousOnClick:function(){ showLoading(); }, afterNextOnClick:function(){ showLoading(); }, afterPageOnClick:function(){ showLoading(); } }); } /** * 无分页获取数据 * 展示所有数据则改为 function noPageBar(pagesize=99999){ */ function noPageBar(pagesize=5){ // /ysgSpecialDay/page //默认展示留言加载中 showLoading(); fetch('/ysgSpecialDay/page?acInfoId='+_acInfoId+'&pageSize='+pagesize+'&pageNumber=1', {method:'GET'}).then((response) => { return response.json(); }).then((data) => { appendData(data); }); } /** * 拼接分页数据到DOM中,并切换展示状态 * @param data 分页数据,必须要是数组格式 */ function appendData(data){ if(data.htmlArray instanceof Array){ if(data.htmlArray.length > 0){ $('.message_list').empty(); data.htmlArray.forEach((ele)=>{ $('.message_list').append(ele); }); // if(data.totalCount < 5){ // $('.msg_collapse .check-in').hide();// 隐藏更多按钮 // // $('.msg_collapse .check-out').hide();// 隐藏收起按钮 // } showResult(); //这里发送AJAX去获取所有的点赞数据 appendThumbUpCount(); }else{ showNoResult(); } //拼接完成后替换所有的院士ID和院士姓名的隐藏表单 $('[name="acInfoId"]').val(_acInfoId); $('[name="acInfoName"]').val(_acInfoName); $('[name="branchIds"]').val(_branchIds); } } /** * 发送AJAX获取所有的点赞数据并拼接到页面上 */ function appendThumbUpCount(){ let dataIdArray = []; $('.message_thumbupBtn').each((index,ele)=>{ let dataId = $(ele).attr('data-id'); dataIdArray.push(dataId); }); dataIdArray = dataIdArray.join(','); $.ajax({ url:'/ysgSpecialDay/thumbUpCount', type:'POST', data:{dataIdArray:dataIdArray}, dataType:'json', success:function(data){ if(data instanceof Array){ //把返回的对象数据设置到页面的点赞数中 data.forEach((ele)=>{ let dataId = ele.dataId; let $thumbUpIcon = $(`[data-id='${dataId}']`); if($thumbUpIcon.hasClass('message_thumbupBtn')){ let $thumbUpCount = $thumbUpIcon.next(); $thumbUpCount.text(ele.count); } }); } } }); } /** * 初始化回复框,绑定回复按钮事件 */ function initReplyForm(){ $('.message_list').on('click','.message_replyBtn',function(){ //这里对回复框和回复按钮的结构有要求 let replyBox = $(this).parents('.message_list_socialBar:eq(0)').next(); let messageContent = replyBox.find(richTextEditorSelector).eq(0); if(replyBox.is(':hidden')){ //先初始化富文本输入框,再展开,但是禁止回复框上传图片 initMeesageInput(messageContent,{allowImage:false}); //初始化验证码 replyBox.find('.verify_code').val(currentCode); replyBox.slideDown(300); }else{ //先收起,再销毁富文本输入框 replyBox.slideUp(300,()=>{ destroyMessageInput(messageContent); }); } }); } /** * 绑定点赞事件 */ function initThumbUpAction(){ $('body').on('click','.message_thumbupBtn',function(){ $(this).doThumbUp({duration:400},function($ele){ //页面的点赞数+1 let $thumbUpCount = $ele.next(); let count = $thumbUpCount.text(); if(count==''){ count == 0; } count++; //先让页面上的点赞数发送变化,再调用点赞接口更新数据库点赞数 $thumbUpCount.text(count); $.ajax({ type:'GET', url:'/ysgSpecialDay/doThumbUp', data:{ dataId:$ele.attr('data-id'), }, success:function(){ //是否成功其实都不影响的 } }); }); }); } //无需等待页面加载完成就可以执行此代码 initCheckbox($('.presentMaxSelectNumber').val()); initMeesageInput($(richTextEditorSelector),{allowImage:true}); initMessageTemplate(); initReplyForm(); initMessageForm($('.message_submit_form')); noPageBar(); // 点击更多按钮,展示全部数据,并将按钮文本改为收起 // $('.msg_collapse .check-in').click(function() { // noPageBar(99999); // $('.msg_collapse .check-out').show();// 显示收起按钮 // $('.msg_collapse .check-in').hide();// 隐藏更多按钮 // }); // 点击收起按钮,隐藏超出的数据,并显示为默认的5条数据,并将按钮文本改为更多 // $(' .msg_collapse .check-out').click(function() { // noPageBar(); // $('.msg_collapse .check-out').hide();// 隐藏收起按钮 // $('.msg_collapse .check-in').show();// 显示更多按钮 // }); initThumbUpAction(); })();