(function () {
var oldAjax = jQuery.ajax;
var EMPTY = {};
var defaultOpts = {
contentType: 'application/json',
dataType: 'json',
processData: false,
beforeSend: function (jqXHR, options) {
if (options.contentType === 'application/json' && typeof options.data !== 'string') {
options.data = JSON.stringify(options.data);
}
}
};
jQuery.ajax = function (url, options) {
options = options || EMPTY;
if (typeof url === 'object') options = url;
else if (typeof url === 'string') options.url = url;
if (!options.url) throw new Error('bad args.');
return new Promise(function (resolve, reject) {
oldAjax(Object.assign({}, defaultOpts, options)).then(function (obj) {
if (obj.statusCode === 0) {
return resolve(obj.data);
}
obj.message = obj.messages.join('\n');
reject(obj);
}).catch(function (xhr) {
var obj = xhr.responseJSON;
if (!obj) {
obj = {
statusCode: xhr.statusCode,
messages: [xhr.statusText]
};
}
obj.message = obj.messages.join('\n');
reject(obj);
});
});
};
window.getValid = function (elm, string) {
var count = 0;
_getValid(count);
function _getValid() {
if (count >= 10) {
anertTip({content: '获取验证码失败, 请检查您的网络, 或联系管理员'});
}
$.get('/verification_code').then(function (data) {
elm.attr('src', data.url);
window[string] = data.id;
}).catch(function () {
_getValid(++count);
});
}
};
window.getBaseValue = function getBaseValue(seletor) {
var type = $(seletor).attr('type') ? $(seletor).attr('type').toLowerCase() : $(seletor)[0].tagName.toLowerCase();
if (type === 'radio') {
return $(seletor + ':checked').val();
} else if (type === 'text' || type === 'tel' || type === 'email' || type === 'hidden' || type === 'textarea') {
return $(seletor).val();
} else if (type === 'checkbox') {
return window.getCheckboxValues(seletor);
} else if (type === 'select') {
return $(seletor + ' option:selected').val();
}
};
window.getCheckboxValues = function getCheckboxValues(string) {
var CheckboxValues = [];
$(string).each(function (index, item) {
if (item.checked) {
CheckboxValues.push($(item).val());
}
});
return CheckboxValues;
};
window.getValues = function getValues(selectors) {
var map = {};
selectors.forEach(function (item) {
map[item.name] = window.getValue(item.selector);
});
return map;
};
window.getValue = function getValue(selector) {
var value, otherData;
value = window.getBaseValue(selector);
otherData = $(selector).data('other');
if (otherData && value == otherData) {
value = window.getBaseValue(selector.slice(0, -1) + 'Value]');
} else if (otherData && $.isArray(value) && $.inArray(otherData, value) !== -1) {
for (var i = 0; i < value.length; i++) {
if (value[i] === otherData) {
value[i] = window.getBaseValue(selector.slice(0, -1) + 'Value]');
break;
}
}
}
return value;
};
window.downloadFile = function downloadFile(url) {
window.open(url, '_blank');
};
window.waringNext = function waringNext(seletor) {
var type = $(seletor).attr('type') ? $(seletor).attr('type').toLowerCase() : $(seletor)[0].tagName.toLowerCase();
if (type === 'radio') {
var baseValue = window.getBaseValue(seletor),
otherData = $(seletor).data('other');
if (otherData && baseValue == otherData) {
var otherValue = window.getValue(seletor.slice(0, -1) + 'Value]'),
otherElm = $(seletor.slice(0, -1) + 'Value]');
if (otherValue) {
otherElm.css('border', '1px solid #d7dbe0');
} else {
otherElm.css('border', '1px solid #ff0000');
otherElm[0].focus();
}
}
if (!window.getValue(seletor)) {
$('.' + ($(seletor).attr('name') + 'Tip')).show();
$('.' + ($(seletor).attr('name') + 'Tip'))[0].scrollIntoView();
return false;
} else {
$('.' + ($(seletor).attr('name') + 'Tip')).hide();
return true;
}
} else if (type === 'text' || type === 'tel' || type === 'email') {
if ($(seletor).data('type') === 'first') {
var reg = new RegExp($(seletor).data('valid').slice(1, -1));
if (!reg.test($(seletor).val())) {
$('.' + ($(seletor).attr('name') + 'Tip')).show();
return false;
} else {
$('.' + ($(seletor).attr('name') + 'Tip')).hide();
return true;
}
} else if ($(seletor).data('type') === 'second' || $(seletor).data('type') === 'three') {
if ($(seletor).val() === '') {
$('.' + ($(seletor).attr('name') + 'Tip')).show();
$('.' + ($(seletor).attr('name') + 'Tip'))[0].scrollIntoView();
return false;
} else {
$('.' + ($(seletor).attr('name') + 'Tip')).hide();
return true;
}
}
} else if (type === 'checkbox') {
var checkboxValue = window.getCheckboxValues(seletor);
if ($.inArray('其他', checkboxValue) != -1 && !window.getValue(seletor.slice(0, -1) + 'Value]')) {
$(seletor.slice(0, -1) + 'Value]').css('border', '1px solid #ff0000');
$(seletor.slice(0, -1) + 'Value]')[0].focus();
$('.' + ($(seletor).attr('name') + 'Tip')).show();
$('.' + ($(seletor).attr('name') + 'Tip'))[0].scrollIntoView();
return false;
} else {
$('.' + ($(seletor).attr('name') + 'Tip')).hide();
$(seletor.slice(0, -1) + 'Value]').css('border', '1px solid #d7dbe0');
}
if (window.getCheckboxValues(seletor).length === 0) {
$('.' + ($(seletor).attr('name') + 'Tip')).show();
$('.' + ($(seletor).attr('name') + 'Tip'))[0].scrollIntoView();
return false;
} else {
$('.' + ($(seletor).attr('name') + 'Tip')).hide();
return true;
}
} else if (type === 'select') {
if ($(seletor + ' option:selected').val() === '请选择') {
$('.' + ($(seletor).attr('name') + 'Tip')).show();
$('.' + ($(seletor).attr('name') + 'Tip'))[0].scrollIntoView();
return false;
} else {
$('.' + ($(seletor).attr('name') + 'Tip')).hide();
return true;
}
}
};
window.is_weixin = function is_weixin() {
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf('micromessenger') != -1 && ua.indexOf('android') != -1) {
return true;
} else {
return false;
}
};
window.limitTextLength = function limitTextLength(targetParent, length) {
$(targetParent + '> .input').on('input', function () {
var value = $(this).val();
if (value.length > length) {
$(this).val(value.substring(0, length));
} else {
$(targetParent + '> span').text((length - value.length) + '/' + length);
}
});
};
window.nowrap = function noWrap(event) {
if (event.keyCode==13) event.returnValue = false;
};
})();
window.autoTextarea = function (elem, extra, maxHeight) {
extra = extra || 0;
var isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window,
isOpera = !!window.opera && !!window.opera.toString().indexOf('Opera'),
addEvent = function (type, callback) {
elem.addEventListener ?
elem.addEventListener(type, callback, false) :
elem.attachEvent('on' + type, callback);
},
getStyle = elem.currentStyle ? function (name) {
var val = elem.currentStyle[name];
if (name === 'height' && val.search(/px/i) !== 1) {
var rect = elem.getBoundingClientRect();
return rect.bottom - rect.top -
parseFloat(getStyle('paddingTop')) -
parseFloat(getStyle('paddingBottom')) + 'px';
}
return val;
} : function (name) {
return getComputedStyle(elem, null)[name];
},
minHeight = parseFloat(getStyle('height'));
elem.style.resize = 'none';
var change = function () {
var scrollTop, height,
padding = 0,
style = elem.style;
if (elem._length === elem.value.length) return;
elem._length = elem.value.length;
if (!isFirefox && !isOpera) {
padding = parseInt(getStyle('paddingTop')) + parseInt(getStyle('paddingBottom'));
}
scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
elem.style.height = minHeight + 'px';
if (elem.scrollHeight > minHeight) {
if (maxHeight && elem.scrollHeight > maxHeight) {
height = maxHeight - padding;
style.overflowY = 'auto';
} else {
height = elem.scrollHeight - padding;
style.overflowY = 'hidden';
}
style.height = height + extra + 'px';
scrollTop += parseInt(style.height) - elem.currHeight;
document.body.scrollTop = scrollTop;
document.documentElement.scrollTop = scrollTop;
elem.currHeight = parseInt(style.height);
}
};
addEvent('propertychange', change);
addEvent('input', change);
addEvent('focus', change);
change();
};