ok-admin/lib/layui_plugins/okUtils/okUtils.js

104 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-05-14 07:51:57 +00:00
"use strict";
2019-05-12 11:09:10 +00:00
layui.define("layer", function (exports) {
var $ = layui.jquery;
var okUtils = {
2019-05-13 15:41:06 +00:00
isFrontendBackendSeparate: true,
2019-05-12 11:09:10 +00:00
baseUrl: "http://localhost:8080",
2019-05-13 15:41:06 +00:00
/**
* ajax()函数二次封装
* @param url
* @param type
* @param param
* @returns {*|*|*}
*/
2019-05-12 11:09:10 +00:00
ajax: function (url, type, param) {
var deferred = $.Deferred();
var loadIndex;
$.ajax({
2019-05-13 00:45:45 +00:00
url: okUtils.isFrontendBackendSeparate ? okUtils.baseUrl + url : url,
2019-05-12 11:09:10 +00:00
type: type || "get",
data: param || {},
dataType: "json",
beforeSend: function () {
loadIndex = layer.load(0, {shade: false});
},
success: function (data) {
if (data.status == 1000) {
// 业务正常
deferred.resolve(data.data)
} else {
2019-05-13 15:41:06 +00:00
// 业务异常
2019-05-12 11:09:10 +00:00
layer.msg(data.msg, {icon: 7, time: 2000});
deferred.reject("okUtils.ajax warn: " + data.msg);
}
},
complete: function () {
layer.close(loadIndex);
},
error: function () {
layer.close(loadIndex);
layer.msg("服务器错误", {icon: 2, time: 2000});
deferred.reject("okUtils.ajax error: 服务器错误");
}
});
return deferred.promise();
2019-05-14 00:59:49 +00:00
},
table: {
/**
* 主要用于针对表格批量操作操作之前的检查
* @param table
* @returns {string}
*/
batchCheck: function (table) {
var checkStatus = table.checkStatus("tableId");
var rows = checkStatus.data.length;
if (rows > 0) {
var idsStr = "";
for (var i = 0; i < checkStatus.data.length; i++) {
idsStr += checkStatus.data[i].id + ",";
}
return idsStr;
} else {
layer.msg("未选择有效数据", {offset: "t", anim: 6});
}
},
/**
* 在表格页面操作成功后弹窗提示
* @param content
*/
2019-05-15 01:09:43 +00:00
successMsg: function (content) {
2019-05-14 00:59:49 +00:00
layer.msg(content, {icon: 1, time: 1000}, function () {
2019-05-15 01:09:43 +00:00
// 刷新当前页table数据
2019-05-14 00:59:49 +00:00
$(".layui-laypage-btn")[0].click();
});
2019-05-15 11:45:12 +00:00
}
},
/**
* 判断是否为一个正常的数字
* @param num
*/
isNum: function (num) {
if (num && !isNaN(num)) {
return true;
}
return false;
},
/**
* 判断一个数字是否包括在某个范围
* @param num
* @param begin
* @param end
*/
isNumWith: function (num, begin, end) {
if (this.isNum(num)) {
if (num >= begin && num <= end) {
return true;
}
return false;
}
2019-05-14 00:59:49 +00:00
}
2019-05-12 11:09:10 +00:00
}
exports("okUtils", okUtils);
});