',
'
',
'请确保您填写的资料正确无误,以便我们与您联系。',
'
',
'
',
'',
'
',
];
dialog.html(html.join(''));
dialog.show().css({
left: (window.innerWidth - dialog.width()) / 2 + 'px',
top: (window.innerHeight - dialog.height()) / 2 - 100 + 'px',
});
dialog.find('.f360-guestbook-button').click(function () {
dialog.hide();
});
}
function showErrorDialog(title, message) {
html = [
'
',
title,
'
',
'
',
message,
'
',
'
',
'',
'
',
];
dialog.html(html.join(''));
dialog.show().css({
left: (window.innerWidth - dialog.width()) / 2 + 'px',
top: (window.innerHeight - dialog.height()) / 2 - 100 + 'px',
});
dialog.find('.f360-guestbook-button').click(function () {
dialog.hide();
});
}
function createField(options) {
var dataRequired = options.required ? ' data-required="' + options.required + '"' : '';
var label = options.required ? '
* ' + options.label : options.label;
var dataControl = ' data-control="' + options.id + '"';
var input = '
';
if (options.controlType === 'textarea') {
input = '
';
}
var html = [
'
',
];
return fieldsWrap.append($(html.join('')));
}
function addFields() {
var formOptions = [];
var maxLabelLength = 0;
if (gbConfig.fieldNames.indexOf('online') !== -1) {
formOptions = $.grep(defaultOptions, function (opt) {
if (opt.id === 'Name' || opt.id === 'Content') return true;
return gbConfig.fieldNames.indexOf(opt.id) !== -1;
});
} else if (gbConfig.optionsJSON.length > 0) {
try {
var data = JSON.parse(gbConfig.optionsJSON);
gbConfig.enableDisplayList = data.EnableDisplayMessage;
for (var i = 0; i < data.FormOptions.length; i++) {
var item = data.FormOptions[i];
if (item.enable) {
var labelLength = item.label.replace(/[\u0391-\uFFE5]/g, 'aa').length;
if (maxLabelLength < labelLength) {
maxLabelLength = labelLength;
}
for (var j = 0; j < defaultOptions.length; j++) {
var item2 = defaultOptions[j];
if (item.field === item2.id) {
formOptions.push({
id: item2.id,
label: item.label,
required: item.required,
controlType: item2.controlType
});
}
}
}
}
} catch (err) { }
}
$.each(formOptions, function (i, opt) {
createField({
id: opt.id.toLowerCase(),
label: opt.label,
required: opt.required ? '请填写' + opt.label : false,
controlType: opt.controlType
});
});
if (maxLabelLength > 0) {
$('.f360-guestbook-form-item-label').css({ minWidth: (maxLabelLength * 10) + 'px' });
}
}
function postMessage() {
var data = {
sid: gbConfig.sid,
lang: gbConfig.lang,
m: gbConfig.m
};
$('.f360-guestbook-form-item-field [data-control]').each(function () {
var id = $(this).data('control');
if (id && id.length) {
data[id] = $(this).val();
}
});
$.ajax('https://cmsapi.fuhai360.com/async.ashx?act=post-message', {
type: 'GET',
dataType: 'JSON',
data: data,
xhrFields: {
withCredentials: true
},
success: function (data) {
submitButton.removeAttr('disabled');
submitButton.removeClass('f360-guestbook-button-disabled');
submitButton.html('提交');
if (data.success) {
$('.f360-guestbook-form-item-field [data-control]').val('');
changeCode();
showSuccessDialog();
} else {
showErrorDialog('提交失败!', data.msg);
}
},
error: function (jqXHR, textStatus, errorThrown) {
submitButton.removeAttr('disabled');
submitButton.removeClass('f360-guestbook-button-disabled');
submitButton.html('提交');
showErrorDialog('提交失败!', errorThrown);
}
});
}
function rednerMessages(list) {
listItemsWrap.empty();
if (list.length === 0) {
listItemsWrap.append('
暂无留言
');
return;
}
$.each(list, function (i, item) {
var html = [
'
',
'
',
'' + item.name + '',
'时间: ' + item.date + '',
'
',
'
',
item.content.replace(/\n/g, '
'),
'
',
'
'
];
listItemsWrap.append(html.join(''));
});
}
function loadMessage(pageNumber) {
listLoadingWrap.show();
$.ajax('http://cmsapi.fuhai360.com/async.ashx?act=list-message', {
type: 'GET',
dataType: 'JSONP',
data: {
sid: gbConfig.sid,
lang: gbConfig.lang,
m: gbConfig.m,
pn: pageNumber
},
success: function (data) {
listLoadingWrap.hide();
if (data.error) {
showErrorDialog('加载留言列表出现错误!', data.error);
return;
}
if (data.total > 15) {
$.getScript('https://cdn.bootcss.com/simplePagination.js/1.6/jquery.simplePagination.js', function () {
var pagingOptions = {
items: data.total,
itemsOnPage: 15,
currentPage: pageNumber,
onPageClick: function (pn) {
loadMessage(pn);
}
};
if (gbConfig.lang === 'cn') {
pagingOptions.prevText = '上一页';
pagingOptions.nextText = '下一页';
}
listPagingWrap.show().pagination(pagingOptions);
});
} else {
listPagingWrap.hide();
}
rednerMessages(data.list);
},
error: function (jqXHR, textStatus, errorThrown) {
listLoadingWrap.hide();
showErrorDialog('加载留言列表出现错误!', errorThrown);
}
});
}
submitButton.click(function () {
var errorCount = 0;
$('.f360-guestbook-form-item').each(function () {
var requiredMsg = $(this).attr('data-required');
var rule = $(this).attr('data-rule');
if (!requiredMsg && !rule) return;
var errorMsg = '';
var control = $(this).find('.f360-guestbook-form-item-field [data-control]');
var isValueControl = control.is('input') || control.is('textarea');
// input text
if (isValueControl) {
var value = $.trim(control.val());
if (requiredMsg && value.length === 0) {
errorMsg = requiredMsg;
} else if (rule && rule.length && !(new RegExp(rule)).test(value)) {
errorMsg = $(this).attr('data-rule-msg');
}
}
// error message
var errorContent = $(this).find('.f360-guestbook-form-item-error-content');
if (!errorContent.length) {
errorContent = $('
');
$(this).find('.f360-guestbook-form-item-field').append(errorContent);
}
errorContent.text(errorMsg);
if (errorMsg !== '') {
$(this).addClass('f360-guestbook-form-item-error');
errorCount++;
} else {
$(this).removeClass('f360-guestbook-form-item-error');
}
});
if (errorCount === 0) {
submitButton.attr('disabled', '');
submitButton.addClass('f360-guestbook-button-disabled');
submitButton.html('正在提交...');
postMessage();
}
});
$('#f360_btn_refresh').click(function () {
changeCode();
});
addFields();
changeCode();
if (gbConfig.enableDisplayList) {
$('#f360_gb_tabs').show();
tabItems.click(function (e) {
e.stopPropagation();
tabItems.removeClass('active');
$(this).addClass('active');
tabItems.each(function () {
$($(this).data('target')).hide();
});
var target = $(this).data('target');
$(target).show();
if (target === '#f360_gb_list') {
if (!hasFirstLoaded) {
loadMessage(1);
hasFirstLoaded = true;
}
}
return false;
});
}
});