vr-shopxo-source/public/static/common/js/common.js

7430 lines
296 KiB
JavaScript
Executable File
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 公共提示
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-10T14:32:39+0800
* @param {[string]} msg [提示信息]
* @param {[string]} type [类型(失败:error, 警告:warning, 成功:success]
* @param {[int]} time [自动关闭时间(秒), 默认3秒]
*/
function Prompt (msg, type, time) {
if (msg != undefined && msg != '') {
// 存在的提示信息则不继续
var status = true;
$('.common-prompt').each(function (k, v) {
if (status && $(this).find('.prompt-msg').text() == msg) {
status = false;
}
});
if (status) {
// 是否已存在提示条
var height = 55;
var length = $('.common-prompt').length;
// 提示信息添加
if ((type || null) == null) {
type = 'danger';
}
// icon图标, 默认错误
var icon = 'am-icon-times-circle';
switch (type) {
// 成功
case 'success':
icon = 'am-icon-check-circle';
break;
// 警告
case 'warning':
icon = 'am-icon-exclamation-circle';
break;
}
var index = parseInt(Math.random() * 1000001);
var html = '<div class="common-prompt common-prompt-' + index + ' am-alert am-alert-' + type + ' am-animation-slide-top" data-index="' + index + '" style="top:' + ((height * length) + 20) + 'px;" data-am-alert><button type="button" class="am-close">&times;</button><div class="prompt-content"><i class="' + icon + ' am-icon-sm am-margin-right-sm"></i><p class="prompt-msg">' + msg + '</p></div></div>';
$('body').append(html);
// 自动关闭提示
setTimeout(function () {
$('.common-prompt-' + index).slideToggle(300, function () {
$('.common-prompt-' + index).remove();
$('.common-prompt').each(function (k, v) {
$(this).animate({ 'top': (k * height + 20) + 'px' });
});
});
}, (time || 3) * 1000);
return true;
}
}
return false;
}
/**
* js数组转json
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-10T14:32:04+0800
* @param {[array]} all [需要被转的数组]
* @param {[object]} object [需要压进去的json对象]
* @return {[object]} [josn对象]
*/
function ArrayTurnJson (all, object) {
for (var name in all) {
object.append(name, all[name]);
}
return object;
}
/**
* 获取form表单的数据
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-10T14:31:19+0800
* @param {[string]} element [元素的class或id]
* @param {[boolean]} is_json [是否返回json对象默认否]
* @return {[object]} [josn对象]
*/
function GetFormVal (element, is_json) {
var $form = (typeof (element) == 'object') ? $(element) : $(element);
var object = new FormData();
// input 常用类型
$form.find('input[type="hidden"], input[type="text"], input[type="password"], input[type="email"], input[type="number"], input[type="date"], input[type="url"], input[type="radio"]:checked, textarea, input[type="file"]').each(function (key, tmp) {
if (tmp.type == 'file') {
object.append(tmp.name, ($(this).get(0).files[0] == undefined) ? '' : $(this).get(0).files[0]);
} else {
object.append(tmp.name, tmp.value.replace(/^\s+|\s+$/g, ""));
}
});
// select 单选择和多选择
var tmp_all = [];
var i = 0;
$form.find('select').find('option').each(function (key, tmp) {
var name = $(this).parents('select').attr('name');
if (name != undefined && name != '') {
if ($(this).is(':selected')) {
var value = (tmp.value == undefined) ? '' : tmp.value;
if ($(this).parents('select').attr('multiple') != undefined) {
// 多选择
if (tmp_all[name] == undefined) {
tmp_all[name] = [];
i = 0;
}
tmp_all[name][i] = value;
i++;
} else {
// 单选择
if (object[name] == undefined) {
object.append(name, value);
}
}
}
}
});
object = ArrayTurnJson(tmp_all, object);
// input 复选框checkbox
tmp_all = [];
i = 0;
$form.find('input[type="checkbox"]').each(function (key, tmp) {
if (tmp.name != undefined && tmp.name != '') {
if ($(this).is(':checked')) {
if (tmp_all[tmp.name] == undefined) {
tmp_all[tmp.name] = [];
i = 0;
}
tmp_all[tmp.name][i] = tmp.value;
i++;
} else {
// 滑动开关、未选中则0
if (typeof ($(this).attr('data-am-switch')) != 'undefined') {
tmp_all[tmp.name] = 0;
}
}
}
});
object = ArrayTurnJson(tmp_all, object);
// 是否需要返回json对象
if (is_json === true) {
var json = {};
object.forEach(function (value, key) {
if ((key || null) != null) {
json[key] = value
}
});
return json;
}
return object;
}
/**
* 方法是否已定义
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-10T14:30:37+0800
* @param {[string]} fun_name [方法名]
* @return {[boolean]} [已定义true, 则false]
*/
function IsExitsFunction (fun_name) {
try {
if (typeof (eval(fun_name)) == "function") return true;
} catch (e) { }
return false;
}
/**
* 根据tag对象获取值
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2017-10-07T20:53:40+0800
* @param {[object]} tag_obj [tag对象]
*/
function GetTagValue (tag_obj) {
// 默认值
var v = null;
// 标签名称
var tag_name = tag_obj.prop("tagName");
// input
var type = tag_obj.attr('type');
switch (type) {
// 单选框
case 'checkbox':
v = tag_obj.is(':checked') ? tag_obj.val() : null;
break;
// 单选框
case 'radio':
v = tag_obj.is(':checked') ? tag_obj.val() : null;
break;
// 其它选择
default:
v = tag_obj.val() || null;
}
return v;
}
/**
* 公共表单校验, 添加class form-validation 类的表单自动校验
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-10T14:22:39+0800
* @param {[string] [form_name] [标题class或id]}
* @param {[string] [action] [请求地址]}
* @param {[string] [method] [请求类型 POST, GET]}
* @param {[string] [request-type] [回调类型 ajax-url, ajax-fun, ajax-reload]}
* @param {[string] [request-value] [回调值 ajax-url地址 或 ajax-fun方法]}
*/
function FromInit (form_name) {
if (form_name == undefined) {
form_name = 'form.form-validation';
}
var editor_tag_name = 'editor-tag';
var $form = $(form_name);
if ($form.length <= 0) {
return false;
}
var $editor_tag = $form.find('[id=' + editor_tag_name + ']');
var editor_count = $editor_tag.length;
if (editor_count > 0) {
// 编辑器初始化
var editor = UE.getEditor(editor_tag_name);
// 编辑器内容变化时同步到 textarea
editor.addListener('contentChange', function () {
editor.sync();
// 触发验证
$editor_tag.trigger('change');
});
}
$form.validator(
{
// 自定义校验规则
validate: function (validity) {
// 二选一校验
if ($(validity.field).is('.js-choice-one')) {
var tag = $(validity.field).attr('data-choice-one-to');
if (typeof ($(validity.field).attr('required')) == 'undefined' && typeof ($(tag).attr('required')) == 'undefined') {
validity.valid = true;
} else {
var v1 = GetTagValue($(validity.field));
var v2 = GetTagValue($(tag));
validity.valid = (v1 == null && v2 == null) ? false : true;
}
}
},
// 错误
onInValid: function (validity) {
var $this = this;
var $field = $(validity.field);
var tag_name = $field.prop('tagName');
if (tag_name == 'SELECT') {
setTimeout(function () {
// 错误信息
var $field = $(validity.field);
var value = $field.val();
var msg = $field.data('validationMessage') || $this.getValidationMessage(validity);
if ((value == '' || value == undefined) && $field.hasClass('am-field-error')) {
Prompt(msg);
}
}, 100);
} else {
var msg = $field.data('validationMessage') || $this.getValidationMessage(validity);
Prompt(msg);
}
},
// 提交
submit: function (e) {
if (editor_count > 0) {
// 同步编辑器数据
editor.sync();
// 表单验证未成功,而且未成功的第一个元素为 UEEditor 时focus 编辑器
if (!this.isFormValid() && $form.find('.' + this.options.inValidClass).eq(0).is($editor_tag)) {
// 编辑器获取焦点
editor.focus();
// 错误信息
var msg = $editor_tag.data('validationMessage') || $editor_tag.getValidationMessage(validity);
Prompt(msg);
}
}
// 通过验证
if (this.isFormValid()) {
// 多选插件校验
if ($form.find('select.chosen-select')) {
var is_success = true;
$form.find('select.chosen-select').each(function (k, v) {
var required = $(this).attr('required');
if (($(this).attr('required') || null) == 'required') {
var multiple = $(this).attr('multiple') || null;
var minchecked = parseInt($(this).attr('minchecked')) || 0;
var maxchecked = parseInt($(this).attr('maxchecked')) || 0;
var msg = $(this).attr('data-validation-message');
var value = $(this).val();
if ((value || null) == null && value != '0') {
is_success = false;
Prompt(msg || window['lang_select_not_chosen_tips'] || '请选择项');
$(this).trigger('blur');
return false;
} else {
if (multiple == 'multiple') {
var count = value.length;
if (minchecked > 0 && count < minchecked) {
is_success = false;
if ((msg || null) == null) {
var temp_msg = window['lang_select_chosen_min_tips'] || '至少选择{value}项';
msg = temp_msg.replace('{value}', minchecked);
}
}
if (maxchecked > 0 && count > maxchecked) {
is_success = false;
if ((msg || null) == null) {
var temp_msg = window['lang_select_chosen_max_tips'] || '最多选择{value}项';
msg = temp_msg.replace('{value}', maxchecked);
}
}
if (is_success === false) {
Prompt(msg);
$(this).trigger('blur');
$(this).parents('.am-form-group').removeClass('am-form-success').addClass('am-form-error');
return false;
}
}
}
}
});
if (is_success === false) {
return false;
}
}
// button加载
var $button = $form.find('button[type="submit"]');
$button.button('loading');
// 获取表单数据
var action = $form.attr('action') || null;
var method = $form.attr('method') || null;
var request_type = $form.attr('request-type') || null;
var request_value = $form.attr('request-value') || null;
var is_progress = ($form.attr('data-is-progress') == undefined || parseInt($form.attr('data-is-progress') || 0) == 1) ? 1 : 0;
var is_loading = parseInt($form.attr('data-is-loading') || 0);
var loading_msg = $form.attr('data-loading-msg') || window['lang_request_handle_loading_tips'] || '正在处理中、请稍候...';
// 以 ajax 开头的都会先请求再处理
// ajax-reload 请求完成后刷新页面
// ajax-close 请求完成后关闭弹窗
// ajax-url 请求完成后调整到指定的请求值
// ajax-fun 请求完成后调用指定方法
// ajax-view 请求完成后仅提示文本信息
// sync 不发起请求、直接同步调用指定的方法
// jump 不发起请求、拼接数据参数跳转到指定 url 地址
var request_handle = ['ajax-reload', 'ajax-close', 'ajax-url', 'ajax-fun', 'ajax-view', 'sync', 'jump', 'form'];
// 参数校验
if (request_handle.indexOf(request_type) == -1) {
$button.button('reset');
Prompt(window['lang_form_config_type_params_tips'] || '表单[类型]参数配置有误');
return false;
}
// 类型值必须配置校验
var request_type_value = ['ajax-url', 'ajax-fun', 'sync', 'jump']
if (request_type_value.indexOf(request_type) != -1 && request_value == null) {
$button.button('reset');
Prompt(window['lang_form_config_value_params_tips'] || '表单[类型值]参数配置有误');
return false;
}
// 请求类型
switch (request_type) {
// 是form表单直接通过
case 'form':
return true;
break;
// 同步调用方法
case 'sync':
$button.button('reset');
if (IsExitsFunction(request_value)) {
window[request_value](GetFormVal(form_name, true));
} else {
Prompt((window['lang_form_call_fun_not_exist_tips'] || '表单配置的方法未定义') + '[' + request_value + ']');
}
return false;
break;
// 拼接参数跳转
case 'jump':
var params = GetFormVal(form_name, true);
var pv = '';
for (var i in params) {
if (params[i] != undefined && params[i] != '') {
pv += i + '=' + encodeURIComponent(params[i]) + '&';
}
}
if (pv != '') {
var join = (request_value.indexOf('?') >= 0) ? '&' : '?';
request_value += join + pv.substr(0, pv.length - 1);
}
window.location.href = request_value;
return false;
break;
// 调用自定义回调方法
case 'ajax-fun':
if (!IsExitsFunction(request_value)) {
$button.button('reset');
Prompt((window['lang_form_call_fun_not_exist_tips'] || '表单配置的方法未定义') + '[' + request_value + ']');
return false;
}
break;
}
// 请求 url http类型
if (action == null || method == null) {
$button.button('reset');
Prompt(window['lang_form_config_main_tips'] || '表单[action或method]参数配置有误');
return false;
}
// 请求参数
var form_data_count = 0;
var form_data = GetFormVal(form_name);
var temp_form_data = form_data.appendData || form_data;
for (var i in temp_form_data) {
form_data_count += 1;
}
// 请求参数是否超过系统环境参数
if (typeof (__env_max_input_vars_count__) != 'undefined') {
var env_vars_count = parseInt(__env_max_input_vars_count__);
if (env_vars_count > 0 && form_data_count > env_vars_count) {
$button.button('reset');
Prompt((window['lang_max_input_vars_tips'] || '请求参数数量已超出php.ini限制') + '[max_input_vars](' + form_data_count + '>' + env_vars_count + ')');
return false;
}
}
// 弹层加载
if (is_loading == 1) {
AMUI.dialog.loading({ title: loading_msg });
}
// ajax请求
if(is_progress == 1) {
$.AMUI.progress.start();
}
$.ajax({
url: RequestUrlHandle(action),
type: method,
dataType: "json",
timeout: $form.attr('timeout') || 60000,
data: form_data,
processData: false,
contentType: false,
success: function (result) {
if (is_loading == 1) {
AMUI.dialog.loading('close');
}
if(is_progress == 1) {
$.AMUI.progress.done();
}
// 调用自定义回调方法
if (request_type == 'ajax-fun') {
if (IsExitsFunction(request_value)) {
window[request_value](result);
} else {
$button.button('reset');
Prompt((window['lang_form_call_fun_not_exist_tips'] || '表单配置的方法未定义') + '[' + request_value + ']');
}
} else {
// 统一处理
if (result.code == 0) {
switch (request_type) {
// url跳转
case 'ajax-url':
Prompt(result.msg, 'success');
setTimeout(function () {
window.location.href = request_value;
}, 1000);
break;
// 页面刷新
case 'ajax-reload':
Prompt(result.msg, 'success');
setTimeout(function () {
// 等于parent则刷新父窗口
if (request_value == 'parent') {
parent.location.reload();
} else {
window.location.reload();
}
}, 1000);
break;
// 页面关闭
case 'ajax-close':
Prompt(result.msg, 'success');
setTimeout(function () {
// 1. 已指定 data-am-modal-close 弹窗关闭属性
// 2. 为父级iframe载入的弹窗则调用父级定义的关闭方法、当前窗口则不用
if (request_value == 'parent' || ($form.find('button').is('[data-am-modal-close]') && $form.find('button').parents('.am-popup').length == 0 && $form.find('button').parents('.am-modal').length == 0)) {
parent.CommonPopupClose();
}
}, 1000);
break;
// 默认仅提示
default:
Prompt(result.msg, 'success');
// 等于parent则关闭父窗口
if (request_value == 'parent') {
setTimeout(function () {
$button.button('reset');
parent.CommonPopupClose();
}, 1000);
} else {
$button.button('reset');
}
}
} else {
Prompt(result.msg);
$button.button('reset');
}
}
},
error: function (xhr, type) {
if (is_loading == 1) {
AMUI.dialog.loading('close');
}
if(is_progress == 1) {
$.AMUI.progress.done();
}
$button.button('reset');
Prompt(HtmlToString(xhr.responseText) || (window['lang_error_text'] || '异常错误'), null, 30);
}
});
}
return false;
}
});
}
/**
* 表单数据填充
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-14T14:46:47+0800
* @param {[json]} json [json数据对象]
* @param {[string]} tag [tag标签]
*/
function FormDataFill (json, tag = null) {
if ((json || null) != null) {
if ((tag || null) == null) {
tag = 'form.form-validation';
}
$form = (typeof (tag) == 'object') ? tag : $(tag);
for (var i in json) {
$form.find('input[type="hidden"][name="' + i + '"], input[type="text"][name="' + i + '"], input[type="password"][name="' + i + '"], input[type="email"][name="' + i + '"], input[type="number"][name="' + i + '"], input[type="date"][name="' + i + '"], textarea[name="' + i + '"], select[name="' + i + '"], input[type="url"][name="' + i + '"]').val(json[i]);
// input radio
$form.find('input[type="radio"][name="' + i + '"]').each(function (k, v) {
this.checked = json[i] == $(this).val();
});
// input checkbox
$form.find('input[type="checkbox"][name="' + i + '"]').each(function (k, v) {
var temp_value = (typeof (json[i]) != 'object') ? json[i].toString().split(',') : json[i];
this.checked = temp_value.indexOf($(this).val()) != -1;
});
}
// 是否存在pid和当前id相同
if ($form.find('select[name="pid"]').length > 0) {
$form.find('select[name="pid"]').find('option').removeAttr('disabled');
if ((json['id'] || null) != null) {
$form.find('select[name="pid"]').find('option[value="' + json['id'] + '"]').attr('disabled', true);
}
}
// 状态切换开关
SwitchInit();
// 多选插件事件更新
SelectChosenInit();
}
}
/**
* 树方法头配置数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2024-01-07
* @desc description
*/
function TreeHeadConfig () {
var head_col = $('#tree').data('head') || null;
// 如果未定义头信息则默认id和name
if (head_col == null || head_col.length == 0) {
head_col = [{ "name": "ID", "field": "id" }, { "name": (window['lang_title_name'] || '名称'), "field": "name", "is_arrow": "1", "value_style": { "style": "color:#999", "value_style_key": "is_enable", "value_style_key_status": "0" } }, { "name": (window['lang_operate_name'] || '操作'), "type": "operate" }];
}
return head_col;
}
/**
* 树方法
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-01-13T10:30:23+0800
* @param {[int]} id [节点id]
* @param {[string]} url [请求url地址]
* @param {[int]} level [层级]
* @param {[int]} is_delete_all [是否所有开启删除按钮]
*/
function Tree (id, url, level = 0, is_delete_all = 0) {
$.ajax({
url: RequestUrlHandle(url),
type: 'POST',
dataType: 'json',
timeout: 60000,
data: { "id": id },
success: function (result) {
if (result.code == 0 && result.data.length > 0) {
html = '';
for (var i in result.data) {
html += TreeItemHtmlHandle(result.data[i], id, level, is_delete_all)
}
// 是否首次
if (id == 0) {
$('#tree').attr('data-is-delete-all', is_delete_all);
var table_html = '<table class="am-table am-table-striped am-table-hover">';
// 头信息配置
var head_col = TreeHeadConfig();
// 拼接头信息
table_html += '<thead>';
for (var i = 0; i < head_col.length; i++) {
// 类型class
var ent = (head_col[i]['type'] || null) == 'operate' ? 'type-operate ' : '';
// 字段class
ent += (head_col[i]['field'] || null) != null ? 'field-' + head_col[i].field + ' ' : '';
table_html += '<th class="' + ent + '">' + head_col[i].name + '</th>';
}
table_html += '</thead>';
table_html += html;
table_html += '</table>';
$('#tree').html(table_html);
} else {
$('.tree-pid-' + id).remove();
$('#data-list-' + id).after(html);
$('#data-list-' + id).find('.tree-submit').removeClass('icon-arrow-right');
$('#data-list-' + id).find('.tree-submit').addClass('icon-arrow-down');
}
// 图片预览初始化
$.AMUI.figure.init();
// 图片画廊初始化
$.AMUI.gallery.init();
// 折叠组件
$.AMUI.accordion.init();
// 切换开关初始化
SwitchInit();
} else {
$('#tree').find('p').text(result.msg);
$('#tree').find('img').attr('src', __attachment_host__ + '/static/common/images/no-data.png');
}
},
error: function (xhr, type) {
$('#tree').find('p').text(HtmlToString(xhr.responseText) || (window['lang_error_text'] || '异常错误'));
$('#tree').find('img').attr('src', __attachment_host__ + '/static/common/images/no-data.png');
}
});
}
/**
* tree列表数据处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-11-19
* @desc description
* @param {[boject]} item [数据]
* @param {[int]} pid [节点pid]
* @param {[int]} level [层级]
* @param {[int]} is_delete_all [是否所有开启删除按钮]