增加内置layui自定义模块
This commit is contained in:
parent
2d0a21e70e
commit
9b8708039a
@ -0,0 +1,282 @@
|
||||
layui.define(function (exports) {
|
||||
let $ = layui.$;
|
||||
let methods = {
|
||||
POST_METHOD: 'POST',
|
||||
DELETE_METHOD: 'DELETE',
|
||||
PUT_METHOD: 'PUT',
|
||||
GET_METHOD: 'GET'
|
||||
};
|
||||
|
||||
/**
|
||||
* 对象转form表单
|
||||
* @param obj
|
||||
* @returns {*}
|
||||
*/
|
||||
function objToForm(obj) {
|
||||
let formStr = '';
|
||||
for (let name in obj) {
|
||||
if (formStr == undefined || formStr == null || formStr == '') {
|
||||
formStr += name + '=' + obj[name];
|
||||
} else {
|
||||
formStr += '&' + name + '=' + obj[name];
|
||||
}
|
||||
}
|
||||
return formStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增
|
||||
* @param url
|
||||
* @param dataObj
|
||||
* @param args
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param beforeCallback
|
||||
* @param completeCallback
|
||||
*/
|
||||
function postJson(url, dataObj, args, successCallback, errorCallback, beforeCallback, completeCallback) {
|
||||
doAjax(url, methods.POST_METHOD, dataObj, args, successCallback, errorCallback, beforeCallback, completeCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param url
|
||||
* @param dataObj
|
||||
* @param args
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param beforeCallback
|
||||
* @param completeCallback
|
||||
*/
|
||||
function deleteForm(url, dataObj, args, successCallback, errorCallback, beforeCallback, completeCallback) {
|
||||
doAjax(url, methods.DELETE_METHOD, dataObj, args, successCallback, errorCallback, beforeCallback, completeCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
* @param url
|
||||
* @param dataObj
|
||||
* @param args
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param beforeCallback
|
||||
* @param completeCallback
|
||||
*/
|
||||
function putJson(url, dataObj, args, successCallback, errorCallback, beforeCallback, completeCallback) {
|
||||
doAjax(url, methods.PUT_METHOD, dataObj, args, successCallback, errorCallback, beforeCallback, completeCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询
|
||||
* @param url
|
||||
* @param dataObj
|
||||
* @param args
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param beforeCallback
|
||||
* @param completeCallback
|
||||
*/
|
||||
function getForm(url, dataObj, args, successCallback, errorCallback, beforeCallback, completeCallback) {
|
||||
doAjax(url, methods.GET_METHOD, dataObj, args, successCallback, errorCallback, beforeCallback, completeCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行上传
|
||||
* @param url
|
||||
* @param method
|
||||
* @param dataObj
|
||||
* @param args
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param beforeCallback
|
||||
* @param completeCallback
|
||||
*/
|
||||
function doAjax(url, method, dataObj, args, successCallback, errorCallback, beforeCallback, completeCallback) {
|
||||
let ajaxData = (dataObj == undefined || dataObj == null) ? {} : dataObj;
|
||||
if (methods.POST_METHOD == method || methods.PUT_METHOD == method) {
|
||||
ajaxData = JSON.stringify(ajaxData);
|
||||
} else {
|
||||
ajaxData = objToForm(ajaxData);
|
||||
dataObj.tm = new Date().getTime();
|
||||
}
|
||||
let headers = {};
|
||||
if (args != null && typeof (args.headers) != 'undefined' && args.headers != null) {
|
||||
headers = args.headers;
|
||||
}
|
||||
$.ajax({
|
||||
url: url,
|
||||
type: method,
|
||||
contentType: "application/json;charset=utf-8",
|
||||
headers: headers,
|
||||
data: ajaxData,
|
||||
success: function (data, status, XMLHttpRequest) {
|
||||
let responseCode = XMLHttpRequest.status;
|
||||
successCallback(responseCode, data, args);
|
||||
},
|
||||
error: function (XMLHttpRequest) {
|
||||
let responseCode = XMLHttpRequest.status;
|
||||
let responseData = JSON.parse(XMLHttpRequest.responseText);
|
||||
if (errorCallback != undefined && errorCallback != null && typeof (errorCallback) == 'function') {
|
||||
errorCallback(responseCode, responseData);
|
||||
}
|
||||
},
|
||||
beforeSend: function (XMLHttpRequest) {
|
||||
if (beforeCallback != undefined && beforeCallback != null && typeof (beforeCallback) == 'function') {
|
||||
beforeCallback(XMLHttpRequest);
|
||||
}
|
||||
},
|
||||
complete: function (XMLHttpRequest, status) {
|
||||
if (completeCallback != undefined && completeCallback != null && typeof (completeCallback) == 'function') {
|
||||
completeCallback(XMLHttpRequest, status);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测是路径参数有重复值
|
||||
* @param pathArgArray
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function pathArgsHasSameValue(pathArgArray) {
|
||||
let tempArgIndex = 0;
|
||||
let tempArgs = pathArgArray[tempArgIndex];
|
||||
for (let i = (tempArgIndex + 1), item; item = pathArgArray[i]; i++) {
|
||||
if (tempArgs == item) {
|
||||
throw new Error('参数' + item + '有重复值!!!');
|
||||
}
|
||||
if (i == pathArgArray.length - 1) {
|
||||
tempArgs = pathArgArray[++tempArgIndex];
|
||||
i = tempArgIndex;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取页面间传递的参数
|
||||
* @param url
|
||||
*/
|
||||
function getParamsArg(url) {
|
||||
let params = url.split('?')[1];
|
||||
let paramsObj = {};
|
||||
if (typeof (params) == 'undefined' || params == null) {
|
||||
return paramsObj;
|
||||
}
|
||||
let paramsKVs = params.split('&');
|
||||
for (let i = 0, item = null; item = paramsKVs[i++];) {
|
||||
let kvs = item.split('=');
|
||||
if (kvs.length == 1) {
|
||||
paramsObj[kvs[0]] = null;
|
||||
continue;
|
||||
}
|
||||
paramsObj[kvs[0]] = decodeURI(item.replace(kvs[0] + '=', ''));
|
||||
}
|
||||
return paramsObj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建路径
|
||||
* @param basePath 请求路径,{参数},
|
||||
* @param pathArgs 替换的路径参数,不能重复
|
||||
* @returns {*}
|
||||
*/
|
||||
function buildPath(basePath, pathArgs) {
|
||||
let path = basePath;
|
||||
if (!basePath || !(typeof (basePath) == 'string')) {
|
||||
throw new Error('basePath必须为字符串!!!');
|
||||
}
|
||||
if (!pathArgs || !Array.isArray(pathArgs)) {
|
||||
throw new Error('pathArgs必须为数组!!!');
|
||||
}
|
||||
let pathArgArray = basePath.match(/\{\w+\}/g);
|
||||
if (!pathArgArray) {
|
||||
return path;
|
||||
}
|
||||
pathArgsHasSameValue(pathArgArray);
|
||||
for (let i = 0, item; item = pathArgArray[i]; i++) {
|
||||
path = path.replace(item, pathArgs[i]);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过form对象上传文件
|
||||
* @param url
|
||||
* @param formData
|
||||
* @param args
|
||||
* @param successCallback
|
||||
* @param errorCallback
|
||||
* @param beforeCallback
|
||||
* @param completeCallback
|
||||
*/
|
||||
function postFile(url, formData, args, successCallback, errorCallback, beforeCallback, completeCallback) {
|
||||
$.ajax({
|
||||
url: url,
|
||||
type: 'POST',
|
||||
data: formData,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
cache: false,
|
||||
success: function (data, status, XMLHttpRequest) {
|
||||
let responseCode = XMLHttpRequest.status;
|
||||
successCallback(responseCode, data, args);
|
||||
},
|
||||
error: function (XMLHttpRequest) {
|
||||
let responseCode = XMLHttpRequest.status;
|
||||
let responseData = JSON.parse(XMLHttpRequest.responseText);
|
||||
if (errorCallback != undefined && errorCallback != null && typeof (errorCallback) == 'function') {
|
||||
errorCallback(responseCode, responseData);
|
||||
}
|
||||
},
|
||||
beforeSend: function (XMLHttpRequest) {
|
||||
if (beforeCallback != undefined && beforeCallback != null && typeof (beforeCallback) == 'function') {
|
||||
beforeCallback(XMLHttpRequest);
|
||||
}
|
||||
},
|
||||
complete: function (XMLHttpRequest, status) {
|
||||
if (completeCallback != undefined && completeCallback != null && typeof (completeCallback) == 'function') {
|
||||
completeCallback(XMLHttpRequest, status);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* xss 转义
|
||||
* @param html
|
||||
* @returns {string}
|
||||
*/
|
||||
function escape(html) {
|
||||
return String(html || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&')
|
||||
.replace(/</g, '<').replace(/>/g, '>')
|
||||
.replace(/'/g, ''').replace(/"/g, '"');
|
||||
}
|
||||
|
||||
function checkBoxToString(formObj, checkBoxKey) {
|
||||
let value = '';
|
||||
for (let key in formObj) {
|
||||
if (key.indexOf(checkBoxKey) != 0) {
|
||||
continue;
|
||||
}
|
||||
if (value !== '') {
|
||||
value += ',';
|
||||
}
|
||||
value += key.substring(checkBoxKey.length + 1, key.length - 1);
|
||||
delete formObj[key];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
exports('ajax', {
|
||||
post: postJson,
|
||||
postFile: postFile,
|
||||
delete: deleteForm,
|
||||
put: putJson,
|
||||
get: getForm,
|
||||
params: getParamsArg,
|
||||
path: buildPath,
|
||||
escape: escape,
|
||||
checkBoxToString: checkBoxToString
|
||||
});
|
||||
});
|
@ -0,0 +1,314 @@
|
||||
layui.define(function(exports) {
|
||||
var dialog = {
|
||||
dialogArray: [],
|
||||
dialogData: {},
|
||||
dialogTreeData: {
|
||||
apiUri: null,
|
||||
method: null,
|
||||
primaryKey: null,
|
||||
autoParams: null,
|
||||
otherParams: null,
|
||||
defaultParams: null,
|
||||
resultType: null,
|
||||
dataFilter: null,
|
||||
rootNode: null,
|
||||
data: null,
|
||||
check: null,
|
||||
selectedNodes: []
|
||||
},
|
||||
maxFileCount: null,
|
||||
close: function (index) {
|
||||
layer.close(index);
|
||||
},
|
||||
msg: function (msg, args, callback) {
|
||||
return layer.msg(msg, args, callback);
|
||||
},
|
||||
confirm: function (msg, callback) {
|
||||
var self = this;
|
||||
return top.layer.msg(msg, {
|
||||
time: 0,
|
||||
btn: ['确定', '取消'],
|
||||
shade: 0.3,
|
||||
yes: function (index) {
|
||||
callback(index);
|
||||
},
|
||||
btn1: function () {
|
||||
self.closeBox();
|
||||
}
|
||||
});
|
||||
},
|
||||
choiceConfirm: function (msg, yesCallback, noCallback) {
|
||||
var self = this;
|
||||
return top.layer.msg(msg, {
|
||||
time: 0,
|
||||
btn: ['是', '否', '取消'],
|
||||
shade: 0.3,
|
||||
btn1: function (index) {
|
||||
yesCallback(index);
|
||||
},
|
||||
btn2: function (index) {
|
||||
noCallback(index);
|
||||
},
|
||||
btn3: function () {
|
||||
self.closeBox();
|
||||
}
|
||||
});
|
||||
},
|
||||
open: function (opt) {
|
||||
var self = this;
|
||||
var index = top.layer.open({
|
||||
type: 2,
|
||||
icon: 1,
|
||||
title: opt.title,
|
||||
shadeClose: false,
|
||||
maxmin: false,
|
||||
resize: false,
|
||||
shade: 0.3,
|
||||
area: [opt.width, opt.height],
|
||||
content: opt.url,
|
||||
cancel: function (index) {
|
||||
self.closeBox();
|
||||
},
|
||||
end: opt.onClose
|
||||
});
|
||||
this.dialogArray.push(index);
|
||||
return index;
|
||||
},
|
||||
user: function (opt) {
|
||||
var self = this;
|
||||
var single = true;
|
||||
if (typeof (opt.single) == 'undefined' || opt.single == null || (opt.single != true && opt.single != false)) {
|
||||
single = false;
|
||||
} else {
|
||||
single = opt.single;
|
||||
}
|
||||
var selectedUserIds = '';
|
||||
if (typeof (opt.selectedUserIds) != 'undefined' && opt.selectedUserIds != null) {
|
||||
selectedUserIds = opt.selectedUserIds;
|
||||
}
|
||||
var index = top.layer.open({
|
||||
type: 2,
|
||||
icon: 1,
|
||||
title: opt.title,
|
||||
shadeClose: false,
|
||||
maxmin: false,
|
||||
closeBtn: 0,
|
||||
shade: 0.3,
|
||||
area: [opt.width, opt.height],
|
||||
content: 'route/user/departmentuser?single=' + single + '&selectedUserIds=' + selectedUserIds,
|
||||
cancel: function (index) {
|
||||
self.closeBox();
|
||||
},
|
||||
end: opt.onClose
|
||||
});
|
||||
this.dialogArray.push(index);
|
||||
return index;
|
||||
},
|
||||
tree: function (opt) {
|
||||
var self = this;
|
||||
self.dialogTreeData.apiUri = opt.apiUri;
|
||||
if (typeof (opt.method) != 'undefined' && opt.method != null) {
|
||||
self.dialogTreeData.method = opt.method;
|
||||
} else {
|
||||
self.dialogTreeData.method = 'get';
|
||||
}
|
||||
if (typeof (opt.primaryKey) != 'undefined' && opt.primaryKey != null) {
|
||||
self.dialogTreeData.primaryKey = opt.primaryKey;
|
||||
} else {
|
||||
self.dialogTreeData.primaryKey = 'id';
|
||||
}
|
||||
if (typeof (opt.autoParams) != 'undefined' && opt.autoParams != null) {
|
||||
self.dialogTreeData.autoParams = opt.autoParams;
|
||||
} else {
|
||||
self.dialogTreeData.autoParams = ['id'];
|
||||
}
|
||||
if (typeof (opt.otherParams) != 'undefined' && opt.otherParams != null) {
|
||||
self.dialogTreeData.otherParams = opt.otherParams;
|
||||
} else {
|
||||
self.dialogTreeData.otherParams = {};
|
||||
}
|
||||
if (typeof (opt.resultType) != 'undefined' && opt.resultType != null) {
|
||||
self.dialogTreeData.resultType = opt.resultType;
|
||||
} else {
|
||||
self.dialogTreeData.resultType = 'justList';
|
||||
}
|
||||
if (typeof (opt.dataFilter) != 'undefined' && opt.dataFilter != null) {
|
||||
self.dialogTreeData.dataFilter = opt.dataFilter;
|
||||
} else {
|
||||
self.dialogTreeData.dataFilter = function (treeId, parentNode, childNodes) {
|
||||
return childNodes;
|
||||
};
|
||||
}
|
||||
if (typeof (opt.rootNode) != 'undefined' && opt.rootNode != null) {
|
||||
self.dialogTreeData.rootNode = opt.rootNode;
|
||||
} else {
|
||||
self.dialogTreeData.rootNode = null;
|
||||
}
|
||||
if (typeof (opt.data) != 'undefined' && opt.data != null) {
|
||||
self.dialogTreeData.data = {};
|
||||
if (typeof (opt.data.checked) != 'undefined' && opt.data.checked != null) {
|
||||
self.dialogTreeData.data.checked = opt.data.checked;
|
||||
} else {
|
||||
self.dialogTreeData.data.checked = 'checked';
|
||||
}
|
||||
if (typeof (opt.data.children) != 'undefined' && opt.data.children != null) {
|
||||
self.dialogTreeData.data.children = opt.data.children;
|
||||
} else {
|
||||
self.dialogTreeData.data.children = 'children'
|
||||
}
|
||||
if (typeof (opt.data.isParent) != 'undefined' && opt.data.isParent != null) {
|
||||
self.dialogTreeData.data.isParent = opt.data.isParent;
|
||||
} else {
|
||||
self.dialogTreeData.data.isParent = 'isParent';
|
||||
}
|
||||
if (typeof (opt.data.name) != 'undefined' && opt.data.name != null) {
|
||||
self.dialogTreeData.data.name = opt.data.name;
|
||||
} else {
|
||||
self.dialogTreeData.data.name = 'name';
|
||||
}
|
||||
if (typeof (opt.data.title) != 'undefined' && opt.data.title != null) {
|
||||
self.dialogTreeData.data.title = opt.data.title;
|
||||
} else {
|
||||
self.dialogTreeData.data.title = 'title';
|
||||
}
|
||||
} else {
|
||||
self.dialogTreeData.data = {
|
||||
checked: 'checked',
|
||||
children: 'children',
|
||||
isParent: 'isParent',
|
||||
name: 'name',
|
||||
title: 'title'
|
||||
}
|
||||
}
|
||||
if (typeof (opt.defaultParams) != 'undefined' && opt.defaultParams != null) {
|
||||
self.dialogTreeData.defaultParams = opt.defaultParams;
|
||||
} else {
|
||||
self.dialogTreeData.defaultParams = null;
|
||||
}
|
||||
if (typeof (opt.check) != 'undefined' && opt.check != null) {
|
||||
self.dialogTreeData.check = {};
|
||||
if (typeof (opt.check.enable) != 'undefined' && opt.check.enable != null) {
|
||||
self.dialogTreeData.check.enable = opt.check.enable;
|
||||
} else {
|
||||
self.dialogTreeData.check.enable = false;
|
||||
}
|
||||
if (typeof (opt.check.selectType) != 'undefined' && opt.check.selectType != null) {
|
||||
self.dialogTreeData.check.chkStyle = opt.check.selectType;
|
||||
} else {
|
||||
self.dialogTreeData.check.chkStyle = 'checkbox';
|
||||
}
|
||||
if (typeof (opt.check.checkboxType) != 'undefined' && opt.check.checkboxType != null) {
|
||||
self.dialogTreeData.check.chkboxType = opt.check.checkboxType;
|
||||
} else {
|
||||
self.dialogTreeData.check.chkboxType = {Y: 'ps', N: 'ps'};
|
||||
}
|
||||
if (typeof (opt.check.radioType) != 'undefined' && opt.check.radioType != null) {
|
||||
self.dialogTreeData.check.radioType = opt.check.radioType;
|
||||
} else {
|
||||
self.dialogTreeData.check.radioType = 'level';
|
||||
}
|
||||
} else {
|
||||
self.dialogTreeData.check = {
|
||||
enable: false,
|
||||
chkStyle: 'checkbox',
|
||||
chkboxType: {Y: 'ps', N: 'ps'},
|
||||
radioType: 'level',
|
||||
}
|
||||
}
|
||||
if (typeof (opt.selectedNodes) != 'undefined' && opt.selectedNodes != null) {
|
||||
self.dialogTreeData.selectedNodes = opt.selectedNodes;
|
||||
} else {
|
||||
self.dialogTreeData.selectedNodes = [];
|
||||
}
|
||||
var index = top.layer.open({
|
||||
type: 2,
|
||||
icon: 1,
|
||||
title: opt.title,
|
||||
shadeClose: false,
|
||||
maxmin: false,
|
||||
shade: 0.3,
|
||||
area: [opt.width, opt.height],
|
||||
content: top.restAjax.path('route/tree/tree/v2', []),
|
||||
cancel: function (index) {
|
||||
self.closeBox();
|
||||
},
|
||||
end: opt.onClose
|
||||
});
|
||||
this.dialogArray.push(index);
|
||||
return index;
|
||||
},
|
||||
// 文件上传
|
||||
file: function (opt) {
|
||||
var self = this;
|
||||
var fileType = opt.type;
|
||||
var customType = opt.customType;
|
||||
var url = 'route/file/uploadfile/';
|
||||
if (fileType == 'image') {
|
||||
url += '2';
|
||||
} else if (fileType == 'video') {
|
||||
url += '3';
|
||||
} else if (fileType == 'audio') {
|
||||
url += '4';
|
||||
} else {
|
||||
url += '1';
|
||||
}
|
||||
if(customType) {
|
||||
url += '?customType='+ customType;
|
||||
}
|
||||
if (typeof (opt.maxFileCount) != 'undefined' && opt.maxFileCount != '') {
|
||||
self.maxFileCount = opt.maxFileCount;
|
||||
} else {
|
||||
self.maxFileCount = null;
|
||||
}
|
||||
self.dialogData.uploadFileArray = [];
|
||||
var index = top.layer.open({
|
||||
type: 2,
|
||||
icon: 1,
|
||||
title: opt.title,
|
||||
shadeClose: false,
|
||||
maxmin: false,
|
||||
shade: 0.3,
|
||||
area: [opt.width, opt.height],
|
||||
content: top.restAjax.path(url, []),
|
||||
cancel: function (index) {
|
||||
self.closeBox();
|
||||
},
|
||||
end: opt.onClose
|
||||
});
|
||||
this.dialogArray.push(index);
|
||||
return index;
|
||||
},
|
||||
// 显示文件,图片和视频(MP4)
|
||||
showFile: function (opt) {
|
||||
var self = this;
|
||||
var showType = opt.type;
|
||||
var url = 'route/file/showfile/';
|
||||
if (showType == 'image') {
|
||||
url += 'image';
|
||||
} else if (showType == 'video') {
|
||||
url += 'video';
|
||||
}
|
||||
url += '/' + opt.fileId;
|
||||
var index = top.layer.open({
|
||||
type: 2,
|
||||
icon: 1,
|
||||
title: opt.title,
|
||||
shadeClose: false,
|
||||
maxmin: true,
|
||||
shade: 0.3,
|
||||
area: [opt.width, opt.height],
|
||||
content: top.restAjax.path(url, []),
|
||||
cancel: function (index) {
|
||||
self.closeBox();
|
||||
},
|
||||
});
|
||||
this.dialogArray.push(index);
|
||||
return index;
|
||||
},
|
||||
closeBox: function () {
|
||||
top.layer.close(this.dialogArray[this.dialogArray.length - 1]);
|
||||
this.dialogArray.pop();
|
||||
}
|
||||
};
|
||||
exports('dialog', dialog);
|
||||
});
|
Loading…
Reference in New Issue
Block a user